Skip to content

Commit c5f4b42

Browse files
committed
moved plot functions from utils/geoutils to visualization/matplotlib
1 parent 7facf60 commit c5f4b42

File tree

4 files changed

+158
-160
lines changed

4 files changed

+158
-160
lines changed

pymove/tests/test_utils_geoutils.py

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import os
2-
3-
import matplotlib.pyplot as plt
41
import numpy as np
5-
from matplotlib.testing.compare import compare_images
62
from numpy.testing import assert_array_equal, assert_equal
73
from pandas import DataFrame
84
from pandas.testing import assert_frame_equal
@@ -30,75 +26,6 @@ def test_v_color():
3026
assert_equal(expected_2, geoutils.v_color(line_2))
3127

3228

33-
def test_plot_coords(tmpdir):
34-
d = tmpdir.mkdir('preprocessing')
35-
36-
file_write_default = d.join('plot_coords.png')
37-
38-
filename_write_default = os.path.join(
39-
file_write_default.dirname, file_write_default.basename
40-
)
41-
42-
coords = LineString([(1, 1), (1, 2), (2, 2), (2, 3)])
43-
44-
fig, ax = plt.subplots(figsize=(21, 9))
45-
geoutils.plot_coords(ax, coords)
46-
plt.savefig(filename_write_default, dpi=100)
47-
48-
test_dir = os.path.abspath(os.path.dirname(__file__))
49-
data_dir = os.path.join(test_dir, 'baseline/plot_coords.png')
50-
51-
compare_images(
52-
data_dir, filename_write_default, 0.0001, in_decorator=False
53-
)
54-
55-
56-
def test_plot_bounds(tmpdir):
57-
d = tmpdir.mkdir('preprocessing')
58-
59-
file_write_default = d.join('plot_bounds.png')
60-
61-
filename_write_default = os.path.join(
62-
file_write_default.dirname, file_write_default.basename
63-
)
64-
65-
bounds = LineString([(1, 1), (1, 2), (2, 2), (2, 3)])
66-
67-
fig, ax = plt.subplots(figsize=(21, 9))
68-
geoutils.plot_bounds(ax, bounds)
69-
plt.savefig(filename_write_default, dpi=100)
70-
71-
test_dir = os.path.abspath(os.path.dirname(__file__))
72-
data_dir = os.path.join(test_dir, 'baseline/plot_bounds.png')
73-
74-
compare_images(
75-
data_dir, filename_write_default, 0.0001, in_decorator=False
76-
)
77-
78-
79-
def test_plot_line(tmpdir):
80-
d = tmpdir.mkdir('preprocessing')
81-
82-
file_write_default = d.join('plot_line.png')
83-
84-
filename_write_default = os.path.join(
85-
file_write_default.dirname, file_write_default.basename
86-
)
87-
88-
line = LineString([(1, 1), (1, 2), (2, 2), (2, 3)])
89-
90-
fig, ax = plt.subplots(figsize=(21, 9))
91-
geoutils.plot_line(ax, line)
92-
plt.savefig(filename_write_default, dpi=100)
93-
94-
test_dir = os.path.abspath(os.path.dirname(__file__))
95-
data_dir = os.path.join(test_dir, 'baseline/plot_line.png')
96-
97-
compare_images(
98-
data_dir, filename_write_default, 0.0001, in_decorator=False
99-
)
100-
101-
10229
def test_encode():
10330
lat1, lon1 = -3.777736, -38.547792
10431
lat2, lon2 = -3.793388, -38.517722

pymove/tests/test_visualization_matplotlib.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
22

3+
import matplotlib.pyplot as plt
34
from matplotlib.testing.compare import compare_images
5+
from shapely.geometry import LineString
46

57
import pymove.visualization.matplotlib as mpl
68
from pymove import MoveDataFrame
@@ -156,3 +158,72 @@ def test_plot_trajectories(tmpdir):
156158
filename_write_default,
157159
0.0001,
158160
in_decorator=False)
161+
162+
163+
def test_plot_coords(tmpdir):
164+
d = tmpdir.mkdir('visualization')
165+
166+
file_write_default = d.join('plot_coords.png')
167+
168+
filename_write_default = os.path.join(
169+
file_write_default.dirname, file_write_default.basename
170+
)
171+
172+
coords = LineString([(1, 1), (1, 2), (2, 2), (2, 3)])
173+
174+
_, ax = plt.subplots(figsize=(21, 9))
175+
mpl.plot_coords(ax, coords)
176+
plt.savefig(filename_write_default, dpi=100)
177+
178+
test_dir = os.path.abspath(os.path.dirname(__file__))
179+
data_dir = os.path.join(test_dir, 'baseline/plot_coords.png')
180+
181+
compare_images(
182+
data_dir, filename_write_default, 0.0001, in_decorator=False
183+
)
184+
185+
186+
def test_plot_bounds(tmpdir):
187+
d = tmpdir.mkdir('visualization')
188+
189+
file_write_default = d.join('plot_bounds.png')
190+
191+
filename_write_default = os.path.join(
192+
file_write_default.dirname, file_write_default.basename
193+
)
194+
195+
bounds = LineString([(1, 1), (1, 2), (2, 2), (2, 3)])
196+
197+
_, ax = plt.subplots(figsize=(21, 9))
198+
mpl.plot_bounds(ax, bounds)
199+
plt.savefig(filename_write_default, dpi=100)
200+
201+
test_dir = os.path.abspath(os.path.dirname(__file__))
202+
data_dir = os.path.join(test_dir, 'baseline/plot_bounds.png')
203+
204+
compare_images(
205+
data_dir, filename_write_default, 0.0001, in_decorator=False
206+
)
207+
208+
209+
def test_plot_line(tmpdir):
210+
d = tmpdir.mkdir('visualization')
211+
212+
file_write_default = d.join('plot_line.png')
213+
214+
filename_write_default = os.path.join(
215+
file_write_default.dirname, file_write_default.basename
216+
)
217+
218+
line = LineString([(1, 1), (1, 2), (2, 2), (2, 3)])
219+
220+
_, ax = plt.subplots(figsize=(21, 9))
221+
mpl.plot_line(ax, line)
222+
plt.savefig(filename_write_default, dpi=100)
223+
224+
test_dir = os.path.abspath(os.path.dirname(__file__))
225+
data_dir = os.path.join(test_dir, 'baseline/plot_line.png')
226+
227+
compare_images(
228+
data_dir, filename_write_default, 0.0001, in_decorator=False
229+
)

pymove/utils/geoutils.py

Lines changed: 1 addition & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,18 @@
22
Geo operations.
33
44
v_color,
5-
plot_coords,
6-
plot_bounds,
7-
plot_line,
85
create_geohash_df,
96
create_bin_geohash_df,
107
decode_geohash_to_latlon,
118
129
"""
1310

14-
from typing import Optional, Text, Tuple, Union
11+
from typing import Optional, Text, Tuple
1512

1613
import geohash2 as gh
1714
import numpy as np
18-
from matplotlib.pyplot import axes
1915
from numpy import ndarray
2016
from pandas import DataFrame
21-
from shapely.geometry import LineString, MultiLineString
2217
from shapely.geometry.base import BaseGeometry
2318

2419
from pymove.utils.constants import (
@@ -74,86 +69,6 @@ def v_color(ob: BaseGeometry) -> Text:
7469
return COLORS[ob.is_simple + 33]
7570

7671

77-
def plot_coords(ax: axes, ob: BaseGeometry, color: Optional[Text] = 'r'):
78-
"""
79-
Plot the coordinates of each point of the object in a 2D chart.
80-
81-
Parameters
82-
----------
83-
ax : axes
84-
Single axes object
85-
ob : geometry object
86-
Any geometric object
87-
color : str, optional
88-
Sets the geometric object color, by default 'r'
89-
90-
Example
91-
-------
92-
"""
93-
x, y = ob.xy
94-
ax.plot(x, y, 'o', color=color, zorder=1)
95-
96-
97-
def plot_bounds(ax: axes, ob: Union[LineString, MultiLineString], color='b'):
98-
"""
99-
Plot the limites of geometric object.
100-
101-
Parameters
102-
----------
103-
ax : axes
104-
Single axes object
105-
ob : LineString or MultiLineString
106-
Geometric object formed by lines.
107-
color : str, optional
108-
Sets the geometric object color, by default 'b'
109-
110-
Example
111-
-------
112-
113-
"""
114-
x, y = zip(*list((p.x, p.y) for p in ob.boundary))
115-
ax.plot(x, y, '-', color=color, zorder=1)
116-
117-
118-
def plot_line(
119-
ax: axes,
120-
ob: LineString,
121-
color: Optional[Text] = 'r',
122-
alpha: Optional[float] = 0.7,
123-
linewidth: Optional[float] = 3,
124-
solid_capstyle: Optional[Text] = 'round',
125-
zorder: Optional[float] = 2
126-
):
127-
"""
128-
Plot a LineString.
129-
130-
Parameters
131-
----------
132-
ax : axes
133-
Single axes object
134-
ob : LineString
135-
Sequence of points.
136-
color : str, optional
137-
Sets the line color, by default 'r'
138-
alpha : float, optional
139-
Defines the opacity of the line, by default 0.7
140-
linewidth : float, optional
141-
Defines the line thickness, by default 3
142-
solid_capstyle : str, optional
143-
Defines the style of the ends of the line, by default 'round'
144-
zorder : float, optional
145-
Determines the default drawing order for the axes, by default 2
146-
147-
Example
148-
-------
149-
"""
150-
x, y = ob.xy
151-
ax.plot(
152-
x, y, color=color, alpha=alpha, linewidth=linewidth,
153-
solid_capstyle=solid_capstyle, zorder=zorder
154-
)
155-
156-
15772
def _encode(lat: float, lon: float, precision: Optional[float] = 15) -> Text:
15873
"""
15974
Encodes latitude/longitude to geohash.

pymove/visualization/matplotlib.py

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
plot_trajectories,
66
plot_traj_by_id,
77
plot_all_features
8+
plot_coords,
9+
plot_bounds,
10+
plot_line,
811
912
"""
1013

1114
from typing import TYPE_CHECKING, Any, Callable, List, Optional, Text, Tuple, Union
1215

1316
import matplotlib.pyplot as plt
14-
from matplotlib.pyplot import figure
17+
from matplotlib.pyplot import axes, figure
1518
from pandas.core.frame import DataFrame
19+
from shapely.geometry import LineString, MultiLineString
20+
from shapely.geometry.base import BaseGeometry
1621

1722
from pymove.utils.constants import (
1823
DATE,
@@ -319,3 +324,83 @@ def plot_all_features(
319324

320325
if return_fig:
321326
return fig
327+
328+
329+
def plot_coords(ax: axes, ob: BaseGeometry, color: Optional[Text] = 'r'):
330+
"""
331+
Plot the coordinates of each point of the object in a 2D chart.
332+
333+
Parameters
334+
----------
335+
ax : axes
336+
Single axes object
337+
ob : geometry object
338+
Any geometric object
339+
color : str, optional
340+
Sets the geometric object color, by default 'r'
341+
342+
Example
343+
-------
344+
"""
345+
x, y = ob.xy
346+
ax.plot(x, y, 'o', color=color, zorder=1)
347+
348+
349+
def plot_bounds(ax: axes, ob: Union[LineString, MultiLineString], color='b'):
350+
"""
351+
Plot the limites of geometric object.
352+
353+
Parameters
354+
----------
355+
ax : axes
356+
Single axes object
357+
ob : LineString or MultiLineString
358+
Geometric object formed by lines.
359+
color : str, optional
360+
Sets the geometric object color, by default 'b'
361+
362+
Example
363+
-------
364+
365+
"""
366+
x, y = zip(*list((p.x, p.y) for p in ob.boundary))
367+
ax.plot(x, y, '-', color=color, zorder=1)
368+
369+
370+
def plot_line(
371+
ax: axes,
372+
ob: LineString,
373+
color: Optional[Text] = 'r',
374+
alpha: Optional[float] = 0.7,
375+
linewidth: Optional[float] = 3,
376+
solid_capstyle: Optional[Text] = 'round',
377+
zorder: Optional[float] = 2
378+
):
379+
"""
380+
Plot a LineString.
381+
382+
Parameters
383+
----------
384+
ax : axes
385+
Single axes object
386+
ob : LineString
387+
Sequence of points.
388+
color : str, optional
389+
Sets the line color, by default 'r'
390+
alpha : float, optional
391+
Defines the opacity of the line, by default 0.7
392+
linewidth : float, optional
393+
Defines the line thickness, by default 3
394+
solid_capstyle : str, optional
395+
Defines the style of the ends of the line, by default 'round'
396+
zorder : float, optional
397+
Determines the default drawing order for the axes, by default 2
398+
399+
Example
400+
-------
401+
"""
402+
x, y = ob.xy
403+
ax.plot(
404+
x, y, color=color, alpha=alpha, linewidth=linewidth,
405+
solid_capstyle=solid_capstyle, zorder=zorder
406+
)

0 commit comments

Comments
 (0)