-
Notifications
You must be signed in to change notification settings - Fork 41
/
lat_long_metre_pixel.py
executable file
·80 lines (61 loc) · 2.46 KB
/
lat_long_metre_pixel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import math
zoom = 19
tileSize = 256
initialResolution = 2 * math.pi * 6378137 / tileSize
# 156543.03392804062 for tileSize 256 pixels
originShift = 2 * math.pi * 6378137 / 2.0
earthc = 6378137 * 2 * math.pi
factor = math.pow(2, zoom)
map_width = 256 * (2 ** zoom)
def LatLonToPixels(lat, lon, zoom):
"""Converts given lat/lon in WGS84 Datum to XY in Spherical Mercator EPSG:900913"""
mx = lon * originShift / 180.0
my = math.log(math.tan((90 + lat) * math.pi / 360.0)) / (math.pi / 180.0)
my = my * originShift / 180.0
res = initialResolution / (2 ** zoom)
px = (mx + originShift) / res
py = (my + originShift) / res
return px, py
def MetersToPixels(mx, my, zoom):
"Converts EPSG:900913 to pyramid pixel coordinates in given zoom level"
res = initialResolution / (2 ** zoom)
px = (mx + originShift) / res
py = (my + originShift) / res
return px, py
# Dont forget you have to convert your projection to EPSG:900913
# mx = -8237494.4864285 # -73.998672 28.737181, 77.063889
# my = 4970354.7325767 # 40.714728
# mx = 28.657555
# my = 77.174684
# mx = 28.652371
mx, my = 28.740963, 77.115281
# Meter per Pixel is dependent on latitude
MeterPerPixel = math.cos(mx * math.pi/180) * earthc / map_width
print (MeterPerPixel)
# MapWidthDistance = 480 * MeterPerPixel
# ActualMeterPerPixel = MapWidthDistance / imgWidthAfterResize
pixel_x, pixel_y = LatLonToPixels(mx, my, zoom)
print (pixel_x, pixel_y)
x = pixel_x + 140
y = pixel_y + 0
print (140 * MeterPerPixel)
def PixelsToLatlon(px, py, zoom):
"""Converts pixel coordinates in given zoom level of pyramid to EPSG:900913"""
res = initialResolution / (2 ** zoom)
mx = px * res - originShift
my = py * res - originShift
# """Converts XY point from Spherical Mercator EPSG:900913 to lat/lon in WGS84 Datum"""
lon = (mx / originShift) * 180.0
lat = (my / originShift) * 180.0
lat = 180 / math.pi * (2 * math.atan(math.exp(lat * math.pi / 180.0)) - math.pi / 2.0)
return lat, lon
def MetersToLatLon(mx, my):
"""Converts XY point from Spherical Mercator EPSG:900913 to lat/lon in WGS84 Datum"""
lon = (mx / originShift) * 180.0
lat = (my / originShift) * 180.0
lat = 180 / math.pi * (2 * math.atan(math.exp(lat * math.pi / 180.0)) - math.pi / 2.0)
return lat, lon
llx, lly = PixelsToLatlon(x, y, zoom)
m, n = MetersToLatLon(llx, lly)
print (llx, lly)
print (m, n)