PyGMT Multi-part geometries do not provide a coordinate sequence doubt

Dear all,
I hope everything is fine.
Reading the forum I found a solution to read and plot over a topographic map *shp files (https://flint.soest.hawaii.edu/t/shapefile-to-gmt-python/834/33).
My code is the same, trying to plot some geological faults.

import geopandas as gpd

shape_file = r'D:\Inves\MT\map_data\Fallas.shp'
gdf = gpd.read_file(shape_file)

gdf['geometry'].head()

region = [-70, -56.99, -23.03, -12.42]
fig = pygmt.Figure()
fig.grdimage(shading=True,  **KWARGS)  


fig.coast(
    region=[-70, -56.99, -23.03, -12.42],  
    projection="M6i",
    water="cornflowerblue",
    borders="1/0.5p",
    shorelines="1/0.5p",
    frame=['a2f2', 'WSne+t"Geological Faults"']
)

gdf.geometry.explode()
linestrings = [geom for geom in gdf.geometry]

for line in linestrings:
    x, y = line.coords.xy
    fig.plot(x=x, y=y, pen="thin")

However I got the following error:
NotImplementedError: Multi-part geometries do not provide a coordinate sequence

To try to avoid the error I applied the suggested solution on https://stackoverflow.com/questions/69331190/multilinestring-not-working-with-pythons-momepy-gdf-to-nx
Unfortunately I did not make it work, does anyone have an idea what am I doing wrong?, attach are the *shp file, thanks in advance
Faults.zip (918.5 KB)

1 Like

I’ve got good news for you, you can plot your geopandas.GeoDataFrame directly with fig.plot (assuming you’re using PyGMT v0.4.0 or newer) without using a for-loop. The old forum post you linked was before we had pygmt/geopandas integration.

Anyways, here’s the code:

import geopandas as gpd
import pygmt

shape_file = "https://flint.soest.hawaii.edu/uploads/short-url/dCdLw6IQUyvS4WL22aDW3OK0wwo.zip"
gdf = gpd.read_file(shape_file)

gdf['geometry'].head()

region = [-70, -56.99, -23.03, -12.42]
fig = pygmt.Figure()

fig.coast(
    region=[-70, -56.99, -23.03, -12.42],  
    projection="M6i",
    water="cornflowerblue",
    borders="1/0.5p",
    shorelines="1/0.5p",
    frame=['a2f2', 'WSne+t"Geological Faults"']
)

fig.plot(data=gdf)
fig.show()

produces:

3 Likes

@weiji14, Thank you for the information, great.
Stay safe and best regards,
Tonino

1 Like