Skip to content

Commit cd822ca

Browse files
yohaimagenweiji14michaelgrundseisman
authored
Plot square or cube by default for geopandas Point/MultiPoint types (#1405)
Set default behavior for plotting geopandas dataframe with Point/Multipoint type geometry as points (-Sp0.2c) for `plot` and cubes (-Su0.2c) for `plot3d`, instead of GMT's default line style plot. * testing default behavior of plot and plot3d with geopandas Point and MultiPoint DataFrames * adding test for non default style * adding a comment explaining the if statment Co-authored-by: Wei Ji <[email protected]> Co-authored-by: Michael Grund <[email protected]> Co-authored-by: Dongdong Tian <[email protected]>
1 parent 71bb08d commit cd822ca

7 files changed

+96
-1
lines changed

pygmt/src/plot.py

+6
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ def plot(self, x=None, y=None, data=None, size=None, direction=None, **kwargs):
207207
extra_arrays = []
208208
if "S" in kwargs and kwargs["S"][0] in "vV" and direction is not None:
209209
extra_arrays.extend(direction)
210+
elif (
211+
"S" not in kwargs
212+
and kind == "geojson"
213+
and data.geom_type.isin(["Point", "MultiPoint"]).all()
214+
): # checking if the geometry of a geoDataFrame is Point or MultiPoint
215+
kwargs["S"] = "s0.2c"
210216
if "G" in kwargs and not isinstance(kwargs["G"], str):
211217
if kind != "vectors":
212218
raise GMTInvalidInput(

pygmt/src/plot3d.py

+6
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ def plot3d(
177177
extra_arrays = []
178178
if "S" in kwargs and kwargs["S"][0] in "vV" and direction is not None:
179179
extra_arrays.extend(direction)
180+
elif (
181+
"S" not in kwargs
182+
and kind == "geojson"
183+
and data.geom_type.isin(["Point", "MultiPoint"]).all()
184+
): # checking if the geometry of a geoDataFrame is Point or MultiPoint
185+
kwargs["S"] = "u0.2c"
180186
if "G" in kwargs and not isinstance(kwargs["G"], str):
181187
if kind != "vectors":
182188
raise GMTInvalidInput(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 48e8bd30372aa5ab9004c1fd53db7ab6
3+
size: 12132
4+
path: test_geopandas_plot3d_default_cube.png
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 54cdcd316554c9db4d71cc2bea2a690a
3+
size: 12028
4+
path: test_geopandas_plot3d_non_default_circle.png
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 6c395fb503f67d024925a2c86cae7ba8
3+
size: 4272
4+
path: test_geopandas_plot_default_square.png
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 4a946506f8a48be04792fdabffc6fa07
3+
size: 4451
4+
path: test_geopandas_plot_non_default_circle.png

pygmt/tests/test_geopandas.py

+68-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
import numpy.testing as npt
55
import pytest
6-
from pygmt import info
6+
from pygmt import Figure, info
77

88
gpd = pytest.importorskip("geopandas")
99
shapely = pytest.importorskip("shapely")
@@ -64,3 +64,70 @@ def test_geopandas_info_shapely(gdf, geomtype, desired):
6464
geom = gdf.loc[geomtype].geometry
6565
output = info(table=geom, per_column=True)
6666
npt.assert_allclose(actual=output, desired=desired)
67+
68+
69+
@pytest.mark.mpl_image_compare
70+
def test_geopandas_plot_default_square():
71+
"""
72+
Check the default behavior of plotting a geopandas DataFrame with Point
73+
geometry in 2d.
74+
"""
75+
point = shapely.geometry.Point(1, 2)
76+
gdf = gpd.GeoDataFrame(geometry=[point])
77+
fig = Figure()
78+
fig.plot(data=gdf, region=[0, 2, 1, 3], projection="X2c", frame=True)
79+
return fig
80+
81+
82+
@pytest.mark.mpl_image_compare
83+
def test_geopandas_plot3d_default_cube():
84+
"""
85+
Check the default behavior of plotting a geopandas DataFrame with
86+
MultiPoint geometry in 3d.
87+
"""
88+
multipoint = shapely.geometry.MultiPoint([(0.5, 0.5, 0.5), (1.5, 1.5, 1.5)])
89+
gdf = gpd.GeoDataFrame(geometry=[multipoint])
90+
fig = Figure()
91+
fig.plot3d(
92+
data=gdf,
93+
perspective=[315, 25],
94+
region=[0, 2, 0, 2, 0, 2],
95+
projection="X2c",
96+
frame=["WsNeZ1", "xag", "yag", "zag"],
97+
zscale=1.5,
98+
)
99+
return fig
100+
101+
102+
@pytest.mark.mpl_image_compare
103+
def test_geopandas_plot_non_default_circle():
104+
"""
105+
Check the default behavior of plotting geopandas DataFrame with Point
106+
geometry in 2d.
107+
"""
108+
point = shapely.geometry.Point(1, 2)
109+
gdf = gpd.GeoDataFrame(geometry=[point])
110+
fig = Figure()
111+
fig.plot(data=gdf, region=[0, 2, 1, 3], projection="X2c", frame=True, style="c0.2c")
112+
return fig
113+
114+
115+
@pytest.mark.mpl_image_compare
116+
def test_geopandas_plot3d_non_default_circle():
117+
"""
118+
Check the default behavior of plotting geopandas DataFrame with MultiPoint
119+
geometry in 3d.
120+
"""
121+
multipoint = shapely.geometry.MultiPoint([(0.5, 0.5, 0.5), (1.5, 1.5, 1.5)])
122+
gdf = gpd.GeoDataFrame(geometry=[multipoint])
123+
fig = Figure()
124+
fig.plot3d(
125+
data=gdf,
126+
perspective=[315, 25],
127+
region=[0, 2, 0, 2, 0, 2],
128+
projection="X2c",
129+
frame=["WsNeZ1", "xag", "yag", "zag"],
130+
zscale=1.5,
131+
style="c0.2c",
132+
)
133+
return fig

0 commit comments

Comments
 (0)