Skip to content

Commit bacf351

Browse files
committed
putting examples on geoutils module
1 parent 81c869c commit bacf351

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

pymove/utils/geoutils.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ def v_color(ob: BaseGeometry) -> Text:
5656
------
5757
str
5858
Geometric object color
59+
60+
Example
61+
-------
62+
>>> from pymove.utils.geoutils import v_color
63+
>>> from shapely.geometry import LineString
64+
>>> case_1 = LineString([(3,3),(4,4), (3,4)])
65+
>>> case_2 = LineString([(3,3),(4,4), (4,3)])
66+
>>> case_3 = LineString([(3,3),(4,4), (3,4), (4,3)])
67+
>>> print(v_color(case_1), type(v_color(case_1)))
68+
#6699cc <class 'str'>
69+
>>> print(v_color(case_2), type(v_color(case_2)))
70+
#6699cc <class 'str'>
71+
>>> print(v_color(case_3), type(v_color(case_3)))
72+
#ffcc33 <class 'str'>
5973
"""
6074
return COLORS[ob.is_simple + 33]
6175

@@ -72,6 +86,9 @@ def plot_coords(ax: axes, ob: BaseGeometry, color: Optional[Text] = 'r'):
7286
Any geometric object
7387
color : str, optional
7488
Sets the geometric object color, by default 'r'
89+
90+
Example
91+
-------
7592
"""
7693
x, y = ob.xy
7794
ax.plot(x, y, 'o', color=color, zorder=1)
@@ -89,6 +106,10 @@ def plot_bounds(ax: axes, ob: Union[LineString, MultiLineString], color='b'):
89106
Geometric object formed by lines.
90107
color : str, optional
91108
Sets the geometric object color, by default 'b'
109+
110+
Example
111+
-------
112+
92113
"""
93114
x, y = zip(*list((p.x, p.y) for p in ob.boundary))
94115
ax.plot(x, y, '-', color=color, zorder=1)
@@ -122,6 +143,9 @@ def plot_line(
122143
Defines the style of the ends of the line, by default 'round'
123144
zorder : float, optional
124145
Determines the default drawing order for the axes, by default 2
146+
147+
Example
148+
-------
125149
"""
126150
x, y = ob.xy
127151
ax.plot(
@@ -149,6 +173,16 @@ def _encode(lat: float, lon: float, precision: Optional[float] = 15) -> Text:
149173
------
150174
str
151175
Geohash of supplied latitude/longitude.
176+
177+
Example
178+
-------
179+
>>> from pymove.utils.geoutils import _encode
180+
>>> lat1, lon1 = -3.777736, -38.547792
181+
>>> lat2, lon2 = -3.793388, -38.517722
182+
>>> print(_encode(lat1,lon1), type(_encode(lat1,lon1)))
183+
7pkddb6356fyzxq <class 'str'>
184+
>>> print(_encode(lat2,lon2), type(_encode(lat2,lon2)))
185+
7pkd7t2mbj0z1v7 <class 'str'>
152186
"""
153187
return gh.encode(lat, lon, precision)
154188

@@ -168,6 +202,16 @@ def _decode(geohash: Text) -> Tuple[float, float]:
168202
------
169203
(lat : float, lon : float)
170204
Geohashed location.
205+
206+
Example
207+
-------
208+
>>> from pymove.utils.geoutils import _decode
209+
>>> geoHash_1 = '7pkddb6356fyzxq'
210+
>>> geoHash_2 = '7pkd7t2mbj0z1v7'
211+
>>> print(_decode(geoHash_1), type(_decode(geoHash_1)))
212+
('-3.777736', '-38.547792') <class 'tuple'>
213+
>>> print(_decode(geoHash_2), type(_decode(geoHash_2)))
214+
('-3.793388', '-38.517722') <class 'tuple'>
171215
"""
172216
return gh.decode(geohash)
173217

@@ -189,6 +233,20 @@ def _bin_geohash(lat: float, lon: float, precision: Optional[float] = 15) -> nda
189233
------
190234
array
191235
Returns a binary geohash array
236+
237+
Example
238+
-------
239+
>>> from pymove.utils.geoutils import _bin_geohash
240+
>>> lat1, lon1 = -3.777736, -38.547792
241+
>>> lat2, lon2 = -3.793388, -38.517722
242+
>>> print(_bin_geohash(lat1,lon1), type(_bin_geohash(lat1,lon1)))
243+
[0 0 1 1 1 1 1 0 0 1 1 0 1 0 0 0 1 1 0 1 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 0 0
244+
0 1 1 0 0 1 0 1 0 0 1 1 0 0 1 1 1 1 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 0 0 1 1
245+
1 0 1 0] <class 'numpy.ndarray'>
246+
>>> print(_bin_geohash(lat2,lon2), type(_bin_geohash(lat2,lon2)))
247+
[0 0 1 1 1 1 1 0 0 1 1 0 1 0 0 0 1 1 0 1 0 0 1 1 1 1 1 1 0 1 0 0 0 1 0 1 0
248+
1 1 0 0 1 0 1 1 1 0 0 1 1 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 0 0 1
249+
1 1] <class 'numpy.ndarray'>
192250
"""
193251
hashed = _encode(lat, lon, precision)
194252
return np.concatenate([BASE_32_TO_BIN[x] for x in hashed])
@@ -211,6 +269,17 @@ def _reset_and_create_arrays_none(
211269
------
212270
arrays
213271
Returns arrays of none values, of the size of the df.
272+
273+
Example
274+
-------
275+
>>> from pymove.utils.geoutils import _reset_and_create_arrays_none
276+
>>> print(type(_reset_and_create_arrays_none(geoLife_df)))
277+
>>> _reset_and_create_arrays_none(geoLife_df)
278+
<class 'tuple'>
279+
(array([nan, nan, nan, nan, nan]),
280+
array([nan, nan, nan, nan, nan]),
281+
array([None, None, None, None, None], dtype=object),
282+
array([None, None, None, None, None], dtype=object))
214283
"""
215284
if reset_index:
216285
data.reset_index(drop=True, inplace=True)
@@ -244,6 +313,31 @@ def create_geohash_df(data: DataFrame, precision: Optional[float] = 15):
244313
The input trajectories data
245314
precision : float, optional
246315
Number of characters in resulting geohash, by default 15
316+
317+
Return
318+
------
319+
A DataFrame with the additional column 'geohash'
320+
321+
Example
322+
-------
323+
>>> from pymove.utils.geoutils import create_geohash_df, _reset_and_create_arrays_none
324+
>>> geoLife_df
325+
lat lon
326+
0 39.984094 116.319236
327+
1 39.984198 116.319322
328+
2 39.984224 116.319402
329+
3 39.984211 116.319389
330+
4 39.984217 116.319422
331+
>>> print(type (create_geohash_df(geoLife_df)))
332+
>>> geoLife_df
333+
<class 'NoneType'>
334+
335+
lat lon geohash
336+
0 39.984094 116.319236 wx4eqyvh4xkg0xs
337+
1 39.984198 116.319322 wx4eqyvhudszsev
338+
2 39.984224 116.319402 wx4eqyvhyx8d9wc
339+
3 39.984211 116.319389 wx4eqyvhyjnv5m7
340+
4 39.984217 116.319422 wx4eqyvhyyr2yy8
247341
"""
248342
_, _, geohash, _ = _reset_and_create_arrays_none(data)
249343

@@ -265,6 +359,30 @@ def create_bin_geohash_df(data: DataFrame, precision: Optional[float] = 15):
265359
The input trajectories data
266360
precision : float, optional
267361
Number of characters in resulting geohash, by default 15
362+
363+
Return
364+
------
365+
A DataFrame with the additional column 'bin_geohash'
366+
367+
Example
368+
-------
369+
>>> from pymove.utils.geoutils import create_bin_geohash_df
370+
>>> geoLife_df
371+
lat lon
372+
0 39.984094 116.319236
373+
1 39.984198 116.319322
374+
2 39.984224 116.319402
375+
3 39.984211 116.319389
376+
4 39.984217 116.319422
377+
>>> print(type(create_bin_geohash_df(geoLife_df)))
378+
>>> geoLife_df
379+
<class 'NoneType'>
380+
lat lon bin_geohash
381+
0 39.984094 116.319236 [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, ...
382+
1 39.984198 116.319322 [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, ...
383+
2 39.984224 116.319402 [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, ...
384+
3 39.984211 116.319389 [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, ...
385+
4 39.984217 116.319422 [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, ...
268386
"""
269387
_, _, _, bin_geohash = _reset_and_create_arrays_none(data)
270388

@@ -292,6 +410,30 @@ def decode_geohash_to_latlon(
292410
The name of the feature with hashed trajectories, by default GEOHASH
293411
reset_index : boolean, optional
294412
Condition to reset the df index, by default True
413+
414+
Return
415+
------
416+
A DataFrame with the additional columns 'lat_decode' and 'lon_decode'
417+
418+
Example
419+
-------
420+
>>> from pymove.utils.geoutils import decode_geohash_to_latlon
421+
>>> geoLife_df
422+
lat lon geohash
423+
0 39.984094 116.319236 wx4eqyvh4xkg0xs
424+
1 39.984198 116.319322 wx4eqyvhudszsev
425+
2 39.984224 116.319402 wx4eqyvhyx8d9wc
426+
3 39.984211 116.319389 wx4eqyvhyjnv5m7
427+
4 39.984217 116.319422 wx4eqyvhyyr2yy8
428+
>>> print(type(decode_geohash_to_latlon(geoLife_df)))
429+
>>> geoLife_df
430+
<class 'NoneType'>
431+
lat lon geohash lat_decode lon_decode
432+
0 39.984094 116.319236 wx4eqyvh4xkg0xs 39.984094 116.319236
433+
1 39.984198 116.319322 wx4eqyvhudszsev 39.984198 116.319322
434+
2 39.984224 116.319402 wx4eqyvhyx8d9wc 39.984224 116.319402
435+
3 39.984211 116.319389 wx4eqyvhyjnv5m7 39.984211 116.319389
436+
4 39.984217 116.319422 wx4eqyvhyyr2yy8 39.984217 116.319422
295437
"""
296438
if label_geohash not in data:
297439
raise ValueError('feature {} not in df'.format(label_geohash))

0 commit comments

Comments
 (0)