Skip to content

Commit c1b3b43

Browse files
committed
Add unit test for plotting GeoDataFrame with int32 and int64 columns
Using the @RidgeTest.shp dataset that has been buffered, and colouring the polygons with a different colormap.
1 parent 84f87a5 commit c1b3b43

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 6ce1b73d25ac10900a2ce4c7148d2953
3+
size: 43434
4+
path: test_geopandas_plot_int_dtypes.png

pygmt/tests/test_geopandas.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
Test integration with geopandas.
33
"""
44
import numpy.testing as npt
5+
import pandas as pd
56
import pytest
6-
from pygmt import Figure, info
7+
from pygmt import Figure, info, which, makecpt
78

89
gpd = pytest.importorskip("geopandas")
910
shapely = pytest.importorskip("shapely")
@@ -131,3 +132,44 @@ def test_geopandas_plot3d_non_default_circle():
131132
style="c0.2c",
132133
)
133134
return fig
135+
136+
137+
@pytest.mark.parametrize("dtype", ["int32", "int64", pd.Int32Dtype(), pd.Int64Dtype()])
138+
@pytest.mark.mpl_image_compare(filename="test_geopandas_plot_int_dtypes.png")
139+
def test_geopandas_plot_int_dtypes(dtype):
140+
"""
141+
Check that plotting a geopandas GeoDataFrame with integer columns works,
142+
including int32 and int64 (non-nullable), Int32 and Int64 (nullable).
143+
144+
This is a regression test for
145+
https://github.com/GenericMappingTools/pygmt/issues/2497
146+
"""
147+
# Read shapefile in geopandas.GeoDataFrame
148+
shapefile = which(
149+
fname="@RidgeTest.shp @RidgeTest.shx @RidgeTest.dbf @RidgeTest.prj",
150+
download="c",
151+
)
152+
gdf = gpd.read_file(shapefile[0])
153+
154+
# Reproject geometry and change dtype of NPOINTS column
155+
gdf["geometry"] = (
156+
gdf.to_crs(crs="EPSG:3857")
157+
.buffer(distance=100000)
158+
.to_crs(crs="OGC:CRS84") # convert to lon/lat to prevent @null in PROJ CRS
159+
)
160+
gdf["NPOINTS"] = gdf.NPOINTS.astype(dtype=dtype)
161+
162+
# Plot figure with three polygons colored based on NPOINTS value
163+
fig = Figure()
164+
makecpt(cmap="lajolla", series=[10, 60, 10], continuous=True)
165+
fig.plot(
166+
data=gdf,
167+
frame=True,
168+
pen="1p,black",
169+
close=True,
170+
fill="+z",
171+
cmap=True,
172+
aspatial="Z=NPOINTS",
173+
)
174+
fig.colorbar()
175+
return fig

0 commit comments

Comments
 (0)