-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmap_downloader.py
24 lines (22 loc) · 1.09 KB
/
map_downloader.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
import requests
import math
import numpy
import cv2
def download_patch(lat_long,api_key, size, zoom=19,file_format='png'):
latitude, longitude = lat_long
extra = 100
x, y = size, str((int(size) + extra))
URL = ('https://dev.virtualearth.net/REST/v1/Imagery/Map/Aerial/{},{}/'
'{}?mapSize={},{}&format={}').format(latitude,longitude,zoom,x,y,file_format)
print("Downloading the map from {}&key={}".format(URL, api_key))
image = requests.get(("{}&key={}").format(URL, api_key))
meters_squared_per_pixel = (156543.04 * math.cos(math.radians(float(latitude))) / (2**zoom))**2
print("Meters squared per pixel: {} ".format(str(meters_squared_per_pixel)))
# metadata = requests.get(("{}&mapMetadata=1&key={}").format(URL, api_key))
# boundingbox = metadata.json()["resourceSets"][0]["resources"][0]["bbox"]
# print(metadata.text);
image_array = numpy.asarray(bytearray(image.content), dtype=numpy.uint8)
map_image = cv2.imdecode(image_array, -1)
# Crop
map_image = map_image[0:int(size), 0:int(size)]
return map_image, meters_squared_per_pixel