GeoPandas – Resolve Multi-Part Geometries Error When Extracting Coordinates

geopandasshapely

I am trying to extract the start and endpoint coordinates in GeoPandas. Following this link, I am using this code to extract the start and endpoints for GeoJSON files.

def add_start_end_nodes_to_gdf(self, gdf=None):
# This function adds start and end nodes to geodataframe

    if gdf is None: gdf=self.GeoData.gdf

    gdf['start_node'] = None
    gdf['end_node'] = None

    for index, row in gdf.iterrows():
        coords = [(coords) for coords in list(row['geometry'].coords)]
        start_node, end_node = [coords[i] for i in (0, -1)]
        gdf.at[index, 'start_node'] = start_node
        gdf.at[index, 'end_node'] = end_node

This is giving me an error

coords = [(coords) for coords in list(row['geometry'].coords)]
  File "****lib\site-packages\shapely\geometry\base.py", line 841, in coords
    "Multi-part geometries do not provide a coordinate sequence")
NotImplementedError: Multi-part geometries do not provide a coordinate sequence

What is happening here and how can I extract the start and endpoint of any polylines? I tried this list(row['geometry'])[0] to get the first line in the Multi-part geometries (polylines) with no success. My file contains only lines.

Best Answer

Try doing gdf.explode() to convert any multipart geometries to single part before your for loop.

From the documentation:

GeoDataFrame.explode(self)

Explode muti-part geometries into multiple single geometries.

Each row containing a multi-part geometry will be split into multiple rows with single geometries, thereby increasing the vertical size of the GeoDataFrame.

The index of the input geodataframe is no longer unique and is replaced with a multi-index (original index with additional level indicating the multiple geometries: a new zero-based index for each single part geometry per multi-part geometry).

You could potentially use the multi-index mentioned above to get a start/end point for the original multipart geometries. Take the 'start' point of the first element and the 'end' point of the last element, for example. But this may or may not make sense to do depending on what your data represents, and if the ordering of the elements in the multipart geometries is meaningful.