Created
November 7, 2016 05:01
-
-
Save nitrag/1ce13cad1df31d7969e1e535fa7f5942 to your computer and use it in GitHub Desktop.
Swift 3 Slippy Map Tiles Counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Swift 3 - Slippy Map Tile Counter | |
import UIKit | |
import CoreLocation | |
private func long2tile(lon: Double, zoom: Int) -> Int { | |
let z = Double(zoom) | |
return Int(floor((lon+180) / 360 * pow(2.0, z))) | |
} | |
private func lat2tile(lat: Double, zoom: Int) -> Int { | |
let z = Double(zoom) | |
return Int(floor((1 - log(tan(lat * M_PI/180) + 1 / cos(lat * M_PI/180)) / M_PI) / 2 * pow(2, z))); | |
} | |
//can't import Mapbox SDK so can't use MGLCoordinateBounds | |
//private func countTiles(minZoom: Int, maxZoom: Int, bbox: MGLCoordinateBounds) -> Int{ | |
private func countTiles(minZoom: Int, maxZoom: Int) -> Int{ | |
/** | |
* Count Tiles from BBOX (Swift 3) | |
* Original Python Version https://gist.github.com/ThomasG77/1677408 | |
* | |
**/ | |
//uncomment | |
//let latmin = bbox.sw.latitude | |
//let latmax = bbox.ne.latitude | |
//let lonmin = bbox.sw.longitude | |
//let lonmax = bbox.ne.longitude | |
//remove, hardcoded for testing | |
let latmin = CLLocationDegrees(exactly: 12.96524048)! | |
let latmax = CLLocationDegrees(exactly: 13.06549072)! | |
let lonmin = CLLocationDegrees(exactly: 47.77163739)! | |
let lonmax = CLLocationDegrees(exactly: 47.81684332)! | |
var tileCount: Int = 0; | |
for z in minZoom...maxZoom { | |
let txmin = long2tile(lon: lonmin, zoom: z); | |
let txmax = long2tile(lon: lonmax, zoom: z); | |
let tymin = lat2tile(lat: latmax, zoom: z); | |
let tymax = lat2tile(lat: latmin, zoom: z); | |
for _ in txmin...txmax { | |
for _ in tymin...tymax { | |
tileCount += 1; | |
} | |
} | |
} | |
return tileCount; | |
} | |
let count = countTiles(minZoom: 12, maxZoom: 18) | |
print("# of tiles", count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment