Skip to content

Commit 672c440

Browse files
committed
added add_map_legend example
1 parent a294f9c commit 672c440

File tree

1 file changed

+79
-12
lines changed

1 file changed

+79
-12
lines changed

pymove/utils/visual.py

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,69 @@ def add_map_legend(m: Map, title: Text, items: List[Tuple]):
4040
----------
4141
https://github.com/python-visualization/folium/issues/528#issuecomment-421445303
4242
43+
Examples
44+
--------
45+
>>> import folium
46+
>>> from pymove.utils.visual import add_map_legend
47+
>>> df
48+
lat lon datetime id
49+
0 39.984094 116.319236 2008-10-23 05:53:05 1
50+
1 39.984198 116.319322 2008-10-23 05:53:06 1
51+
2 39.984224 116.319402 2008-10-23 05:53:11 1
52+
3 39.984211 116.319389 2008-10-23 05:53:16 2
53+
4 39.984217 116.319422 2008-10-23 05:53:21 2
54+
>>> m = folium.Map(location=[df.lat.median(), df.lon.median()])
55+
>>> folium.PolyLine(mdf[['lat', 'lon']], color='red').add_to(m)
56+
>>> pm.visual.add_map_legend(m, 'Color by ID', [(1, 'red')])
57+
>>> m.get_root().to_dict()
58+
{
59+
"name": "Figure",
60+
"id": "1d32230cd6c54b19b35ceaa864e61168",
61+
"children": {
62+
"map_6f1abc8eacee41e8aa9d163e6bbb295f": {
63+
"name": "Map",
64+
"id": "6f1abc8eacee41e8aa9d163e6bbb295f",
65+
"children": {
66+
"openstreetmap": {
67+
"name": "TileLayer",
68+
"id": "f58c3659fea348cb828775f223e1e6a4",
69+
"children": {}
70+
},
71+
"poly_line_75023fd7df01475ea5e5606ddd7f4dd2": {
72+
"name": "PolyLine",
73+
"id": "75023fd7df01475ea5e5606ddd7f4dd2",
74+
"children": {}
75+
}
76+
}
77+
},
78+
"map_legend": { # legend element
79+
"name": "MacroElement",
80+
"id": "72911b4418a94358ba8790aab93573d1",
81+
"children": {}
82+
}
83+
},
84+
"header": {
85+
"name": "Element",
86+
"id": "e46930fc4152431090b112424b5beb6a",
87+
"children": {
88+
"meta_http": {
89+
"name": "Element",
90+
"id": "868e20baf5744e82baf8f13a06849ecc",
91+
"children": {}
92+
}
93+
}
94+
},
95+
"html": {
96+
"name": "Element",
97+
"id": "9c4da9e0aac349f594e2d23298bac171",
98+
"children": {}
99+
},
100+
"script": {
101+
"name": "Element",
102+
"id": "d092078607c04076bf58bd4593fa1684",
103+
"children": {}
104+
}
105+
}
43106
"""
44107
item = "<li><span style='background:%s;'></span>%s</li>"
45108
list_items = '\n'.join([item % (c, n) for (n, c) in items])
@@ -136,7 +199,7 @@ def add_map_legend(m: Map, title: Text, items: List[Tuple]):
136199
macro = MacroElement()
137200
macro._template = Template(template)
138201

139-
m.get_root().add_child(macro)
202+
m.get_root().add_child(macro, name='map_legend')
140203

141204

142205
def generate_color() -> Text:
@@ -232,11 +295,11 @@ def cmap_hex_color(cmap: ListedColormap, i: int) -> Text:
232295
Examples
233296
--------
234297
>>> from pymove.utils.visual import cmap_hex_color
235-
>>> # import matplotlib.pyplot as plt
236-
>>> # jet = plt.get_cmap('jet') // This comand generates a Linear Segmented Colormap
237-
>>> print(cmap_hex_color(jet,0))
298+
>>> import matplotlib.pyplot as plt
299+
>>> jet = plt.get_cmap('jet') # This comand generates a Linear Segmented Colormap
300+
>>> print(cmap_hex_color(jet, 0))
238301
'#000080'
239-
>>> print(cmap_hex_color(jet,1))
302+
>>> print(cmap_hex_color(jet, 1))
240303
'#000084'
241304
"""
242305
return rgb2hex(cmap(i))
@@ -259,9 +322,8 @@ def get_cmap(cmap: Text) -> Colormap:
259322
Examples
260323
--------
261324
>>> from pymove.utils.visual import get_cmap
262-
>>> print(get_cmap('Greys'), type(get_cmap('Greys')))
325+
>>> print(get_cmap('Greys')
263326
<matplotlib.colors.LinearSegmentedColormap object at 0x7f743fc04bb0>
264-
<class 'matplotlib.colors.LinearSegmentedColormap'>
265327
"""
266328
return _get_cmap(cmap)
267329

@@ -293,11 +355,16 @@ def save_wkt(
293355
0 39.984094 116.319236 2008-10-23 05:53:05 1
294356
1 39.984198 116.319322 2008-10-23 05:53:06 1
295357
2 39.984224 116.319402 2008-10-23 05:53:11 1
296-
3 39.984211 116.319389 2008-10-23 05:53:16 1
297-
4 39.984217 116.319422 2008-10-23 05:53:21 1
358+
3 39.984211 116.319389 2008-10-23 05:53:16 2
359+
4 39.984217 116.319422 2008-10-23 05:53:21 2
298360
>>> save_wkt(df, 'test.wkt', 'id')
361+
>>> with open('test.wtk') as f:
362+
>>> print(f.read())
363+
'id;linestring'
364+
'1;LINESTRING(116.319236 39.984094,116.319322 39.984198,116.319402 39.984224)'
365+
'2;LINESTRING(116.319389 39.984211,116.319422 39.984217)'
299366
"""
300-
str_ = '%s;linestring\n' % label_id
367+
wtk = '%s;linestring\n' % label_id
301368
ids = move_data[label_id].unique()
302369
for id_ in ids:
303370
move_df = move_data[move_data[label_id] == id_]
@@ -307,6 +374,6 @@ def save_wkt(
307374
for x in move_df[[LONGITUDE, LATITUDE]].values
308375
)
309376
curr_str += ')\n'
310-
str_ += curr_str
377+
wtk += curr_str
311378
with open(filename, 'w') as f:
312-
f.write(str_)
379+
f.write(wtk)

0 commit comments

Comments
 (0)