DataFrame with WKT Column to GeoPandas Geometry

Question:

I wrote a script to query a PostGIS database, returning a Pandas dataframe like this:

   ID  ...                          WKT
0   1  ...  LINESTRING(1.5047434 42.6319022,1.5053385 42.6...
1   2  ...  LINESTRING(1.5206333 42.5291144,1.5206306 42.5...

Now I am trying to write this into a shapefile with GeoPandas, according to their documentation:

We use shapely.wkt sub-module to parse wkt format:

from shapely import wkt

df['Coordinates'] = geopandas.GeoSeries.from_wkt(df['Coordinates'])

But when I tried to do the same, I got:

AttributeError: type object 'GeoSeries' has no attribute 'from_wkt'

My GeoPandas:

geopandas                 0.8.1                      py_0    conda-forge
Asked By: TOuyang

||

Answers:

Use shapely.wkt.loads to create the geometry column.

import geopandas as gpd
from shapely import wkt


df['geometry'] = df.WKT.apply(wkt.loads)
df.drop('WKT', axis=1, inplace=True) #Drop WKT column

# Geopandas GeoDataFrame
gdf = gpd.GeoDataFrame(df, geometry='geometry')

#Export to shapefile
gdf.to_file('myshapefile.shp')
Answered By: PeaceLeka

geopandas.GeoSeries.from_wkt API has been added in GeoPandas 0.9.0. It does not exist in older versions, that is why it does not work in 0.8.1.

Answered By: martinfleis
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.