wmts-exfiltrator/pkg/spatial.py

25 lines
641 B
Python

import math
def deg2num(lat_deg, lon_deg, zoom):
"""
Convert a lat/long coordinate to x and y numbers.
:param lat_deg:
:param lon_deg:
:param zoom:
:return:
"""
lat_rad = math.radians(lat_deg)
n = 2.0 ** zoom
xtile = int((lon_deg + 180.0) / 360.0 * n)
ytile = int((1.0 - math.asinh(math.tan(lat_rad)) / math.pi) / 2.0 * n)
return xtile, ytile
def lonlat_to_meters(lon, lat):
origin_shift = 2 * math.pi * 6378137 / 2.0
mx = lon * origin_shift / 180.0
my = math.log(math.tan((90 + lat) * math.pi / 360.0)) / (math.pi / 180.0)
my = my * origin_shift / 180.0
return mx, my