Created
July 31, 2015 13:01
-
-
Save wpf500/37d288c3991527a21017 to your computer and use it in GitHub Desktop.
Pre-projection for Leaflet
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
// Based on Leaflet's projection/transformation functions | |
var project = (function () { | |
var R = 6378137, d = Math.PI / 180, max = 1 - 1E-15; | |
var a = 0.5 / (Math.PI * R); | |
function _project(lat, lng) { | |
var sin = Math.max(Math.min(Math.sin(lat * d), max), -max); | |
return [R * lng * d, R * Math.log((1 + sin) / (1 - sin)) / 2]; | |
} | |
function _transform(x, y) { | |
return [a * x + 0.5, -a * y + 0.5]; | |
} | |
function _scale(zoom) { | |
return 256 * Math.pow(2, zoom); | |
} | |
return function(lat, lng) { | |
var p = _project(lat, lng); | |
return _transform(p[0], p[1]).map(function (n) { return n * _scale(0); }); | |
}; | |
})(); | |
function projectPolygon(rings) { | |
rings.forEach(function (ring) { | |
ring.forEach(function (point, i) { | |
ring[i] = project(point[1], point[0]); | |
}); | |
}); | |
} | |
geojson.features.forEach(function (feature) { | |
var coordinates = feature.geometry.coordinates; | |
switch (feature.geometry.type) { | |
case 'Polygon': | |
coordinates = [coordinates]; | |
case 'MultiPolygon': | |
coordinates.forEach(projectPolygon); | |
break; | |
// TODO: other features | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment