Repository navigation
Closed
Description
Description of the problem
When using polygons (e.g. read in via geopandas), the aspatial parameter of Figure.plot does not accept integers for filling the corresponding polygons (the polygons remain empty). First they need to be converted to float.
Minimal Complete Verifiable Example
import pygmt
import geopandas as gpd
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
world = world[world["continent"] == "Europe"]
world["pop_est"] = world.pop_est * 1e-6
fig = pygmt.Figure()
pygmt.makecpt(cmap = "lajolla", series = [0, 150, 50], continuous = True)
fig.basemap(region = [-12, 35, 35, 60],
projection = "M12c",
frame = True)
fig.plot(data = world,
pen = "1p,black",
close = True,
fill = "+z",
cmap = True,
aspatial = "Z=pop_est")
fig.colorbar(position = "JMR", frame = 'a10+lPopulation in millions')
###############################################################################
fig.shift_origin(yshift = "-11c")
# first round data, then convert to int
world["pop_est_round"] = round(world["pop_est"])
world["pop_est_int"] = world.pop_est_round.astype(int)
fig.basemap(region = [-12, 35, 35, 60],
projection = "M12c",
frame = True)
fig.plot(data = world,
pen = "1p,black",
close = True,
fill = "+z",
cmap = True,
aspatial = "Z=pop_est_int")
fig.colorbar(position = "JMR", frame = 'a10+lPopulation in millions')
fig.show()Full error message
No response
System information
PyGMT information:
version: v0.9.0
System information:
python: 3.9.16 | packaged by conda-forge | (main, Feb 1 2023, 21:39:03) [GCC 11.3.0]
executable: /home/michael/anaconda3/envs/pygmt/bin/python
machine: Linux-5.15.0-67-generic-x86_64-with-glibc2.31
Dependency information:
numpy: 1.23.2
pandas: 1.4.3
xarray: 2022.6.0
netCDF4: 1.6.3
packaging: 21.3
contextily: 1.3.0
geopandas: 0.11.1
ghostscript: 9.54.0
GMT library information:
binary version: 6.4.0
cores: 8
grid layout: rows
image layout:
library path: /home/michael/anaconda3/envs/pygmt/lib/libgmt.so
padding: 2
plugin dir: /home/michael/anaconda3/envs/pygmt/lib/gmt/plugins
share dir: /home/michael/anaconda3/envs/pygmt/share/gmt
version: 6.4.0
Activity
seisman commentedon Apr 17, 2023
For GeoPandas objects, PyGMT calls the tempfile_from_json function to save the GeoPandas object into a ".gmt" file (using the
OGR_GMTdriver). In your case, it's equivalent to:and pass the file
world.gmtto theFigure.plotmodule.If you open the file
world.gmt, you will see:For unknown reasons,
pop_est_intis instringtype. If you manually changepop_est_int's type todoubleorinteger, you will see that passing the fileworld.gmttoFigure.plotwill work as expected.seisman commentedon Apr 18, 2023
In other words, we need to understand why
pop_est_intis saved instringtype (maybe a geopandas bug?) and how to correct set the data type.seisman commentedon Apr 20, 2023
Here is a simplified version to reproduce the issue:
The first few lines of the
world.gmtfile has:As you can see,
world["gdp_md_est"]is inint64type, but is stored as "string" type inworld.gmt.seisman commentedon Apr 20, 2023
One more update here.
geopandascallsfionato save a GeoDataFrame object to a.gmtfile. The following script shows how it works.The variable
schema_intis theschemaguessed by geopandas. As you can see, I tried to change thegdp_md_est's type fromint32andint64.The
gdp_md_estcolumn has the correctintegertype inworld_int32.gmt, and has the wrongstringtype in bothworld_int.gmtandworld_int64.gmt.Looks like a fiona issue?
michaelgrund commentedon Jun 23, 2023
Opened an issue in the fiona repo @seisman. Will wait for an answer and hopefully fix before continuing with the corresponding gallery example.
michaelgrund commentedon Jun 30, 2023
Update on the integer issue. This behavior is expected. The OGR_GMT driver only supports 32 bit integers, floats, and datetimes. All other types are converted to strings: https://github.com/OSGeo/gdal/blob/6e9103bd5acb1ff8da305c4e77fa30335ffa3b70/ogr/ogrsf_frmts/gmt/ogrgmtlayer.cpp#L769. This means we have to decide how we want to handle other types as input @GenericMappingTools/pygmt-maintainers.
seisman commentedon Jun 30, 2023
We don't have many options:
pop_est_int) even it's marked as string typeFor option 3, I thought
world["pop_est_int"] = world.pop_est_round.astype("int32")should work, but it doesn't.seisman commentedon Jul 4, 2023
This issue is almost the same as geopandas/geopandas#967 and the workaround here geopandas/geopandas#967 (comment) works.
See #2592 for the patch.
weiji14 commentedon Jul 4, 2023
Need to patch upstream
geopandasfor this. The PR at geopandas/geopandas#2950 should make this work on PyGMT v0.9.0:produces

However, I did
.astype(pd.Int32Dtype())instead of.astype("int32"). Also, we'll still need #2592 if other dtypes like int64 are passed in.