-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
58 lines (46 loc) · 1.76 KB
/
utils.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
import os
import numpy as np
import pandas as pd
import urllib.request
import geocoder
import matplotlib.pyplot as plt
from PIL import Image
from gmplot import gmplot
HK_LOCATION = (22.396400, 114.10950)
def geomap(data, zoom=11, point_size=3, point_color='r', point_alpha=1, y=HK_LOCATION[0], x=HK_LOCATION[1]):
'''
plot point location on OSM map
default parameters: zoom into Hong Kong bounding box
'''
datadir='map/'
# corrections to match geo with static map
z = zoom
picsize=1000
wx = 1.0*360*(picsize/256)/(2**z)
wy = 0.76*360*(picsize/256)/(2**z)
x_min, x_max = x-wx/2, x+wx/2
y_min, y_max = y-wy/2, y+wy/2
static_map_filename = os.path.join(datadir, 'map_zoom{}.png'.format(z,picsize))
if os.path.isfile(static_map_filename)==False:
osm_staticmap_api(x,y,z,picsize,static_map_filename)
print('OSM map file location: ',static_map_filename)
img = Image.open(static_map_filename)
# add the static map
plt.imshow(img, zorder=0, extent=[x_min, x_max, y_min, y_max], interpolation='none', aspect='auto')
# add the scatter plot of events
plt.plot(
data['lon'],
data['lat'],
'.',
markerfacecolor=point_color,
markeredgewidth=0.0,
markersize=point_size,
alpha=point_alpha)
# limit the plot to the given bounding box
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
def osm_staticmap_api(x,y,z,size,filename) :
'''Use OMS staticmap API to get the base map'''
static_map = "http://staticmap.openstreetmap.de/staticmap.php?center={0},{1}&zoom={2}&size={3}x{3}&maptype=mapnik".format(y,x,z,size)
static_map_filename, headers = urllib.request.urlretrieve(static_map, filename)
return static_map_filename