5

How can I access the .coords property of a LineString in GeoDataFrames?

I can access other properties, such as area, length, etc, as documented in the Shapely's documentation. See reproducible example below:

# create example GeoDataFrame with LineString`
line_new = LineString([(0.3, 0.6), (1.5, 1.6), (0.9,2.5)])
df = gpd.GeoDataFrame()
df.loc[0, 'geometry'] = line_new

This works:df.geometry.length

0    2.643715
dtype: float64

But this does not: df.geometry.coords. It returns the following error:

    return object.__getattribute__(self, name)

AttributeError: 'GeoSeries' object has no attribute 'coords'

My understanding from the GeoPandas Documentation was that the 'geometry' GeoSeries in a GeoDataFrame was a Shapely object, and thus the respective properties/methods would be available:

The GeoSeries class implements nearly all of the attributes and methods of Shapely objects.

Is the coords attribute simply one of the few that are not implemented?

Is this the same as what is being asked in How do I extract points from a LineString or Polygon data frame and make a dictionary of point data frames??

3 Answers 3

9

A GeoSerie is a list of shapely geometries, therefore

  1. with shapely

    from shapely.geometry import LineString, shape
    
    line_new = LineString([(0.3, 0.6), (1.5, 1.6), (0.9,2.5)])
    type(shape(line_new))
    <class 'shapely.geometry.linestring.LineString'>
    list(shape(line_new).coords)
    [(0.3, 0.6), (1.5, 1.6), (0.9, 2.5)]
    
  2. with your GeoDataFrame (list of shapely geometries)

    type(df.geometry)
    <class 'geopandas.geoseries.GeoSeries'>
    df['geometry'].coords
    ....
    AttributeError: 'GeoSeries' object has no attribute 'coords'
    

First row of the GeoSerie = first shapely geometry

type(df.geometry.iloc[0])
<class 'shapely.geometry.linestring.LineString'>
list(df.geometry.iloc[0].coords)
[(0.3, 0.6), (1.5, 1.6), (0.9, 2.5)]
2
0

If the answer by @gene doesn't work, check here: How to convert GeoSeries polygons to lists of coordinates?

Where they suggest df.geometry.exterior.coords.xy which will spit out arrays with the boundary coordinates.

0

Things have changed since this question was asked. Use geopandas.GeoSeries.get_coordinates() to get coordinates.

Documentation link here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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