-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogleMapsApi.py
71 lines (51 loc) · 2.72 KB
/
googleMapsApi.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
# this grabs new google maps images when needed
import cv2
import numpy as np
import requests
import keys
def grabNewGoogleMapsImage(pos, fileName, roadMaskPath, buildingMaskPath, zoom):
try:
satMapRequest = requests.get(
f'https://maps.googleapis.com/maps/api/staticmap?center={pos[0]},{pos[1]}&zoom={zoom}&size=640x640&maptype=satellite&key={keys.GOOGLE_API_KEY}', stream=True).raw
roadmapRequest = requests.get(
f'https://maps.googleapis.com/maps/api/staticmap?center={pos[0]},{pos[1]}&zoom={zoom}&size=640x640&maptype=roadmap&key={keys.GOOGLE_API_KEY}', stream=True).raw
if satMapRequest.status == 200 and roadmapRequest.status == 200:
# turn the responses into images
satMapImage = np.asarray(
bytearray(satMapRequest.read()), dtype="uint8")
satMapImage = cv2.imdecode(satMapImage, cv2.IMREAD_COLOR)
roadmapImage = np.asarray(
bytearray(roadmapRequest.read()), dtype="uint8")
roadmapImage = cv2.imdecode(roadmapImage, cv2.IMREAD_COLOR)
gray = cv2.cvtColor(roadmapImage, cv2.COLOR_BGR2GRAY)
getRoadMask(gray, roadMaskPath)
getBuildingMask(gray, buildingMaskPath)
cv2.imwrite(fileName, satMapImage)
print('Successfully updated maps reference image')
else:
print('\nERROR(s):', satMapRequest.data,
'\n', roadmapRequest.data, '\n')
exit()
except requests.exceptions.RequestException as e:
raise SystemExit(e)
def getRoadMask(mapImage, roadMaskPath):
_, thresh = cv2.threshold(mapImage, 253, 255, 0)
kernel = np.ones((25, 25), np.uint8)
mask = cv2.dilate(thresh, kernel, iterations=4)
cv2.imwrite(roadMaskPath, mask)
def getBuildingMask(mapImage, buildingMaskPath):
# apply thresholding to convert grayscale to binary image
_, thresh1 = cv2.threshold(mapImage, 240, 255, 0)
_, thresh2 = cv2.threshold(mapImage, 243, 255, 0)
e_kernel = np.ones((3, 3), np.uint8)
d_kernel = np.ones((9, 9), np.uint8)
img_erosion = cv2.erode(thresh1 - thresh2, e_kernel, iterations=1)
img_dilation = cv2.dilate(img_erosion, d_kernel, iterations=1)
cv2.imwrite(buildingMaskPath, 255-img_dilation)
# this checks to see if the google maps image needs to be updated
def googleMapsImageNeedsToUpdate(lastUpdatedPos, pos):
degrees_per_meter = 360 / (2 * np.pi * 6378137)
return np.sqrt(((lastUpdatedPos[0] - pos[0])/degrees_per_meter)**2 + ((lastUpdatedPos[1] - pos[1])/degrees_per_meter)**2) > 5
if __name__ == "__main__":
grabNewGoogleMapsImage([40.016517261816276, -83.00677793947504],
'currentLocation.png', 'junk.png', 'junk.png')