4

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.

2
  • Do you have one polyline with two parts or two polylines?
    – BERA
    Nov 10 '20 at 7:42
  • Is it possible to share your data (or any part of it) to produce the same error and to check what is going on? Nov 10 '20 at 21:11
5

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.

4

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.

1
  • Thanks. I was wrong about my data containing only LINESTRING. While my original data contained only LINESTRING, MULTILINESTRINGs were generated when I performed a clip using overlay ('intersection') operation. gdf.explode() solved my issue.
    – PPR
    Nov 10 '20 at 23:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.