Created
September 28, 2011 17:48
-
-
Save raine/1248639 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| TILE_SIZE = 256 | |
| lat = 41.850033 | |
| lon = -87.65005229999997 | |
| zoom = 10 | |
| numTiles = 1 << zoom | |
| bound = (value, min, max) -> | |
| if min then return Math.max value, min | |
| if max then return Math.min value, max | |
| degreesToRadians = (deg) -> | |
| deg * (Math.PI / 180) | |
| class MercatorProjection | |
| constructor: -> | |
| this.pixelOriginX = this.pixelOriginY = TILE_SIZE / 2 | |
| this.pixelsPerLonDegree = TILE_SIZE / 360 | |
| this.pixelsPerLonRadian = TILE_SIZE / (2 * Math.PI) | |
| fromLatLngToPoint: (lat, lon) -> | |
| x = this.pixelOriginX + lon * this.pixelsPerLonDegree | |
| sinY = bound Math.sin(degreesToRadians(lat)), -0.9999, 0.9999 | |
| y = this.pixelOriginY + 0.5 * Math.log((1 + sinY) / (1 - sinY)) * -this.pixelsPerLonRadian | |
| [ x, y ] | |
| projection = new MercatorProjection | |
| worldCoordinate = projection.fromLatLngToPoint lat, lon | |
| pixelCoordinate = (c * numTiles for c in worldCoordinate) | |
| tileCoordinate = (Math.floor(c / TILE_SIZE) for c in pixelCoordinate) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ported some code from here to coffeescript, don't ask me how it works!