@@ -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
142205def generate_color () -> Text :
@@ -146,6 +209,14 @@ def generate_color() -> Text:
146209 Returns
147210 -------
148211 Random HEX color
212+
213+ Examples
214+ --------
215+ >>> from pymove.utils.visual import generate_color
216+ >>> print(generate_color(), type(generate_color()))
217+ '#E0FFFF' <class 'str'>
218+ >>> print(generate_color(), type(generate_color()))
219+ '#808000' <class 'str'>
149220 """
150221 return COLORS [randint (0 , len (COLORS ))]
151222
@@ -167,9 +238,11 @@ def rgb(rgb_colors: Tuple[float, float, float]) -> Tuple[int, int, int]:
167238
168239 Examples
169240 --------
170- >>> from pymove.visualization.visualization import rgb
171- >>> rgb([0.6,0.2,0.2])
172- (51, 51, 153)
241+ >>> from pymove.utils.visual import rgb
242+ >>> print(rgb((0.1, 0.2, 0.7)), type(rgb((0.1, 0.2, 0.7))))
243+ (51, 178, 25) <class 'tuple'>
244+ >>> print(rgb((0.5, 0.4, 0.1)), type(rgb((0.5, 0.4, 0.1))))
245+ (102, 25, 127) <class 'tuple'>
173246 """
174247 blue = rgb_colors [0 ]
175248 red = rgb_colors [1 ]
@@ -194,9 +267,11 @@ def hex_rgb(rgb_colors: Tuple[float, float, float]) -> Text:
194267
195268 Examples
196269 --------
197- >>> from pymove.visualization.visualization import hex_rgb
198- >>> hex_rgb([0.6,0.2,0.2])
199- '#333399'
270+ >>> from pymove.utils.visual import hex_rgb
271+ >>> print(hex_rgb((0.1, 0.2, 0.7)), type(hex_rgb((0.1, 0.2, 0.7))))
272+ '#33B219' <class 'str'>
273+ >>> print(hex_rgb((0.5, 0.4, 0.1)), type(hex_rgb((0.5, 0.4, 0.1))))
274+ '#66197F' <class 'str'>
200275 """
201276 return '#%02X%02X%02X' % rgb (rgb_colors )
202277
@@ -216,6 +291,16 @@ def cmap_hex_color(cmap: ListedColormap, i: int) -> Text:
216291 -------
217292 str
218293 Represents corresponding hex str
294+
295+ Examples
296+ --------
297+ >>> from pymove.utils.visual import cmap_hex_color
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))
301+ '#000080'
302+ >>> print(cmap_hex_color(jet, 1))
303+ '#000084'
219304 """
220305 return rgb2hex (cmap (i ))
221306
@@ -233,6 +318,12 @@ def get_cmap(cmap: Text) -> Colormap:
233318 -------
234319 Colormap
235320 matplotlib colormap
321+
322+ Examples
323+ --------
324+ >>> from pymove.utils.visual import get_cmap
325+ >>> print(get_cmap('Greys')
326+ <matplotlib.colors.LinearSegmentedColormap object at 0x7f743fc04bb0>
236327 """
237328 return _get_cmap (cmap )
238329
@@ -252,8 +343,28 @@ def save_wkt(
252343 label_id : str
253344 Represents column name of trajectory id
254345
346+ Returns
347+ -------
348+ File: A file.wkt that contains geometric points that build a map visualization
349+
350+ Examples
351+ --------
352+ >>> from pymove.utils.visual import save_wkt
353+ >>> df.head()
354+ lat lon datetime id
355+ 0 39.984094 116.319236 2008-10-23 05:53:05 1
356+ 1 39.984198 116.319322 2008-10-23 05:53:06 1
357+ 2 39.984224 116.319402 2008-10-23 05:53:11 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
360+ >>> 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)'
255366 """
256- str_ = '%s;linestring\n ' % label_id
367+ wtk = '%s;linestring\n ' % label_id
257368 ids = move_data [label_id ].unique ()
258369 for id_ in ids :
259370 move_df = move_data [move_data [label_id ] == id_ ]
@@ -263,6 +374,6 @@ def save_wkt(
263374 for x in move_df [[LONGITUDE , LATITUDE ]].values
264375 )
265376 curr_str += ')\n '
266- str_ += curr_str
377+ wtk += curr_str
267378 with open (filename , 'w' ) as f :
268- f .write (str_ )
379+ f .write (wtk )
0 commit comments