--I want to aggregate latitude and longitude information in units divided into meshes. ――Therefore, I wanted to place the latitude and longitude in the section xy with reduced resolution. -Google Maps API fromLatLngToPoint I wanted to do the same thing offline in Python
Based on the information in TRAIL NOTE's http://www.trail-note.net/tech/coordinate/, I tried to make it a function that converts latitude / longitude and world coordinates with python.
I just copied the contents of ↑.
from math import pi
from math import tanh
from math import sin
from math import asin
from numpy import arctanh
# refer from http://www.trail-note.net/tech/coordinate/
def tile2latlon(x, y, z):
L = 85.05112878
lon = ((x / 2.0**(z+7) )-1) * 180
lat = 180/pi * (asin(tanh(-pi/2**(z+7)*y + arctanh(sin(pi/180*L)))))
return [lat, lon]
def latlon2tile2(lat, lon, z):
L = 85.05112878
x = int((lon/180 + 1) * 2**(z+7))
y = int( (2**(z+7) / pi * ( -arctanh(sin(pi*lat/180)) + arctanh(sin(pi*L/180)) ) ))
return [x,y]
Enter a known latitude and longitude and check it on Map of Geographical Survey Institute.
#simbashi
lat = 35.666280
lon = 139.758375
# lat = 30.335927
# lon = 130.504283
pix = 256
# lat/lon to tile
for z in range(15,19):
a,b = latlon2tile2(lat,lon,z)
print [a,b]
print 'http://cyberjapandata.gsi.go.jp/xyz/std/{0}/{1:d}/{2:d}.png'.format(z,a/pix,b/pix)
a,b = tile2latlon(a,b,z)
print [a,b]
When executed, it will be output like this, so it matches the original lat / long, and the accuracy increases as the zoom level increases. I confirmed the location on the actual map. (Map of Geographical Survey Institute is up to zoom level 18)
[7450910, 3303678]
http://cyberjapandata.gsi.go.jp/xyz/std/15/29105/12904.png
[35.66629207402928, 139.75836753845215]
[14901820, 6607356]
http://cyberjapandata.gsi.go.jp/xyz/std/16/58210/25809.png
[35.66629207402928, 139.75836753845215]
[29803640, 13214713]
http://cyberjapandata.gsi.go.jp/xyz/std/17/116420/51619.png
[35.66628335763601, 139.75836753845215]
[59607281, 26429426]
http://cyberjapandata.gsi.go.jp/xyz/std/18/232840/103239.png
[35.66628335763601, 139.75837290287018]
--I don't really understand the geodetic system --There seems to be a library such as pyproj, so it may be better to use this. If you know how to use it, please let me know!