|
2 | 2 | Test integration with geopandas. |
3 | 3 | """ |
4 | 4 | import numpy.testing as npt |
| 5 | +import pandas as pd |
5 | 6 | import pytest |
6 | | -from pygmt import Figure, info |
| 7 | +from pygmt import Figure, info, which, makecpt |
7 | 8 |
|
8 | 9 | gpd = pytest.importorskip("geopandas") |
9 | 10 | shapely = pytest.importorskip("shapely") |
@@ -131,3 +132,44 @@ def test_geopandas_plot3d_non_default_circle(): |
131 | 132 | style="c0.2c", |
132 | 133 | ) |
133 | 134 | 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