TransWikia.com

"Multi-part geometries do not provide a coordinate sequence" error when extracting the long and lat of start and end points in GeoPandas

Geographic Information Systems Asked on May 3, 2021

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 "****libsite-packagesshapelygeometrybase.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.

2 Answers

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.

Correct answer by Nick on May 3, 2021

Although your data looks like containing only lines, its type can be multipart. If it is, row['geometry'].coords gives that error.

I'm not sure if it works in this case, try to use row['geometry'].geoms[0].coords. It's the same as list(row['geometry'])[0], though.

Answered by Kadir Şahbaz on May 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP