1

I used to have a Pandas data frame with geometry column (I guess you call it as GeoPandas data frame) where all the instances were multiline string with shapely.geometry data type.

When I saved it as a csv (with pandas.to_csv function) I guess python converts them into string type, because when I try to reload it (with pandas.read_csv function), read data frame has a string type data under geometry column.

How can I convert string type MultilineString rows under geometry column into shapely.geometry object.

active.geometry[0]

Gives:

'MULTILINESTRING ((4.6902978485015 50.8812913672182, 4.69041863376864 50.8813473567576, 
4.69048964428198 50.8814069648068, 4.69054925191282 50.8814797434961, 4.69058047643031 50.8815178675679, 4.69103203247511 50.8820691915014, 4.69112606776955 50.8821748368663, 4.69118560934351 50.8822527991125, 4.69122586159577 50.8823195293716, 4.69133401466373 50.8824824286198, 4.69153580362294 50.8827908199779, 4.69155260179444 50.8828164920095))'

BUT, this:

active.geometry[0][0]

Gives

'M'

instead of a shapely object of the first line string that the multiline string objects is composed of.

I also tried shapely.wkt function but it outputs only the first lineString, instead all the lineStrings that multiline string has

2 Answers 2

6

You need to convert your pandas DataFrame to a geopandas GeoDataFrame to access the geometry column as shapely object. This post describes how to achieve it.

one solution:

from shapely import wkt
df = pd.read_csv('myFile.csv')
df['geometry'] = df['geometry'].apply(wkt.loads)
gdf = gpd.GeoDataFrame(df, crs='epsg:4326')
0
2

In addition to the answer of CodeBard:

When Pandas read a csv file, the columns are strings or numbers, never shapely geometries. A Pandas DataFrame with a geometry column (string) is not a GeoPandas GeoDataFrame (geometry column, shapely geometry)

import pandas as pd
df = pd.read_csv("lat_long.csv")
df
          lat       lon           geometry
0  41.389474  2.156421  POINT (2.15642 41.38947)
1  41.383093  2.181116  POINT (2.18112 41.38309)
df.geometry[0]
'POINT (2.156421 41.389474)'
df.dtypes
lat         float64
lon         float64
geometry    object # not a shapely geometry, in Pandas, an object is a string and it performs strings operations
dtype: object

With the the solution of CodeBard

df['geometry'] = df['geometry'].apply(wkt.loads)
df.geometry[0]
<shapely.geometry.point.Point object at 0x1207ab828>
df.geometry[0].wkt
'POINT (2.156421 41.389474)'
# but
df.dtypes
lat         float64
lon         float64
geometry    object # 

Even if the rows of the column are shapely geometries, this is still not a shapely geometry field

This is why GeoPandas was created (add a geopspatial component to Pandas). With a GeoDataFrame,

gdf = gpd.GeoDataFrame(df)
gdf.geometry[0]
<shapely.geometry.point.Point object at 0x1207ab828>
gdf.dtypes
lat          float64
lon          float64
geometry     geometry #  shapely geometry field here

But when you print or export the GeoDataFrame to a csv file (text file), the geometry field must be a string, the wkt representation of the shapely geometry (= geometry.wkt, POINT (2.15642 41.38947)), and not the shapely geometry (Point(2.15642,41.38947))

 from shapely.geometry import Point
 pt = Point(2.15642,41.38947)
 pt
 <shapely.geometry.point.Point object at 0x10e12ab00>
 pt.wkt
 'POINT (2.15642 41.38947)'
 print(pt)
 POINT (2.15642 41.38947)


gdf
          lat       lon           geometry
0  41.389474  2.156421  POINT (2.15642 41.38947)
1  41.383093  2.181116  POINT (2.18112 41.38309)
1
  • thanks for the elaborate answer!
    – yer
    Commented Jul 20, 2021 at 10:14

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.