Thursday, 27 June 2019

python - Transforming between epsg:3857 and Google Maps Tile coordinates


Hio, I used to use a simple python project (https://github.com/hrldcpr/mercator.py) to convert between wgs84 and google maps tile coordinates. This project doesn't have a license, and looks like it may be inaccurate (assuming a spherical earth?), so I decided to use pyproj.


I understand that google maps uses a projection called epsg:3857 to draw their tiles. When I convert from wgs84 to epsg:3857, I get numbers that I just don't understand:


>> import pyproj
>> epsg3857 = pyproj.Proj(init='epsg:3857')
>> wgs84 = pyproj.Proj(init='EPSG:4326')
>> latlng = (45.618, -73.604)

>> pyproj.transform(wgs84,epsg3857,latlng[0],latlng[1])
(5078172.5310075525, -12357511.560866801)

I know that the lat,lng coordinate I took is in the tile http://b.tile.openstreetmap.org/11/605/731.png, But I just can't figure out how to go from the returned numbers to the tile coordinates. Firstly, the numbers shouldn't be negative (tile coordinates are positive). Trying different scales based on the 11 zoom levels, I just can't figure this out. Searching the internet, I can't figure out what the epsg:3857 coordinates actually denote.



Answer



(answering own question, hoisted from comment to question)


It seems that I may have reversed lat,lng. Based on transforming (180,0), it seems that this epsg:3857 coordinate system is centered at the equator and scales by meters at the equator, using a circumference of the earth of 40075016.68557849m. So I guess that's how to scale the result.


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...