-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapEngine.py
More file actions
126 lines (82 loc) · 3.46 KB
/
mapEngine.py
File metadata and controls
126 lines (82 loc) · 3.46 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import math
import json
import requests
from mercatorProjection import fromPointToLatLong, fromLatLongToPoint
EARTH_RADIUS = 6378137
MIN_LAT = -85.05112878
MAX_LAT = 85.05112878
MIN_LNG = -180
MAX_LNG = 180
ASPECT_RATIO = float(4.0/3.0)
def maintainAspectRatio(width, height):
expectedHeight = width/ASPECT_RATIO
if expectedHeight > height:
return ( int(ASPECT_RATIO*height, height))
else:
return ( width, int(expectedHeight))
def latRad(latitude):
sin = math.sin( latitude * math.pi / 180 )
radx2 = math.log((1+sin)/(1-sin))/2
return max(min(radx2, math.pi), -math.pi)/2
def calcZoom(mapPx, worldPx, fraction):
return round(math.log(mapPx / worldPx / fraction) / math.log1p(1))
def latRad(lat):
sin = math.sin( lat * math.pi/180)
radx2 = math.log( (1 + sin)/ (1 - sin))/2
return max( min(radx2, math.pi), -math.pi)/2
def calcZoom(mapPx, worldPx, fraction):
print(mapPx, worldPx, fraction)
return math.floor(math.log( mapPx/worldPx/fraction)/ math.log1p(1))
def getBoundaries(bounds):
northEast = { 'lat' : max(bounds, key=lambda x:x['lat'])['lat'], 'lng' : max(bounds, key=lambda x:x['lng'])['lng']}
southWest = {'lat': min(bounds, key=lambda x: x['lat'])['lat'], 'lng': min(bounds, key=lambda x: x['lng'])['lng']}
return northEast, southWest
def calcMapSize(zoom):
return 256 << zoom
def clip(n, minValue, maxValue):
return min(max(n, minValue), maxValue)
def LatLongttoPixel(latLng, zoom):
lat = clip(latLng['lat'], MIN_LAT, MAX_LAT)
lng = clip(latLng['lng'], MIN_LNG, MAX_LNG)
x = (lng + 180)/360
siny = math.sin(lat * math.pi/180)
y = 0.5 - math.log( (1 + siny)/(1 - siny))/(4 * math.pi)
mapSize = calcMapSize(zoom)
pixelX = int()
def getBoundsZoomLevel(bounds, mapDim):
WORLD_DIM = { 'height': 256, 'width': 256}
ZOOM_MAX = 21
'''northEast = getNorthEast(bounds)
southWest = getSouthWest(bounds)'''
northEast, southWest = getBoundaries(bounds)
latFraction = (latRad(northEast['lat']) - latRad(southWest['lat']))/math.pi
lngDiff = northEast['lng'] - southWest['lng']
if lngDiff < 0:
lngFraction = (lngDiff + 360)/360
else:
lngFraction = lngDiff/360
latZoom = calcZoom(mapDim['height'], WORLD_DIM['height'], latFraction)
lngZoom = calcZoom(mapDim['width'], WORLD_DIM['width'], lngFraction)
return min(latZoom, lngZoom, ZOOM_MAX)
def calcBounds(point, zoom, width, height):
scale = math.pow(2.0, float(zoom))
centerPx = fromLatLongToPoint(point)
southWestPoint = { 'x': centerPx['x'] - (width/2)/scale, 'y': centerPx['y'] + (height/2)/scale }
southWestLatLng = fromPointToLatLong(southWestPoint)
northEastPoint = { 'x': centerPx['x'] + (width/2)/scale, 'y': centerPx['y'] - (height/2)/scale }
northEastLatLng = fromPointToLatLong(northEastPoint)
return northEastLatLng, southWestLatLng
def getLatLngtoPixel(latLng, zoom):
siny = math.sin(latLng['lat'] * math.pi / 180)
pixelX = ((latLng['lng'] + 180) / 360) * 256 * math.pow(2, zoom)
pixelY = (0.5 - math.log((1 + siny) / (1 - siny)) / (4 * math.pi)) * 256 * math.pow(2, zoom)
return {'x': pixelX, 'y': pixelY }
def getCentre(bounds):
centre = { 'lat': 0, 'lng': 0}
for marker in bounds:
centre['lat'] += marker['lat']
centre['lng'] += marker['lng']
centre['lat'] = centre['lat']/len(bounds)
centre['lng'] = centre['lng']/len(bounds)
print('centre is ', json.dumps(centre))
return centre