Saturday, 7 April 2018

How to access Leaflet EPSG:3857 coordinates?


I read everywhere that Leaflet is internally using a spherical web mercator (EPSG:3857), but whatever I try I get either Lat/Lon (EPSG:4326) or strange coordinates I dont understand:


map.getBounds().getNorthEast();          // LatLng(52.5262, 13.44907)
map.getPixelBounds().getTopRight(); // Point(2253845, 1375425) ???
map.getPixelOrigin(); // Point(2252165, 1375425) ???


Let's try to project the spherical mercator:


L.Projection.SphericalMercator.project(
map.getBounds().getNorthEast()
); // Point(0.23473, 1.08117) !?!

All right, there is a known bug in Leaflet 0.7.3, so let's add the earth radius:


L.point(
L.Projection.SphericalMercator.project(
map.getBounds().getNorthEast()
).x * 6378137.0,

L.Projection.SphericalMercator.project(
map.getBounds().getNorthEast()
).y * 6378137.0
); // Point(1497143.40914, 6895833.38915)

So, really, is it so hard to get the default projection coordinates of my map? Why do I have to call project() at all? What did I miss?


I am using leaflet 0.7.3 and did not change the CRS, using default EPSG:3857. Any ideas?


How to access Leaflet EPSG:3857 coordinates?



Answer



Finally got an answer on github:




Internally, Leaflet transforms EPSG:4326 coordinates to pixel coordinates. These pixel coordinates are the EPSG:3857 coordinates divided by a power of 2 (and then rounded).


It might be possible to fetch some of these coordinates (look at methods like L.Map.latLngToLayerPoint) and then multiply stuff given the zoom level, but beware of rounding errors.



So my assumption EPSG:3857 is the default was wrong.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...