18

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
TOuyang
  • 321
  • 1
  • 2
  • 8

2 Answers2

25

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')
PeaceLeka
  • 1,138
  • 1
  • 9
  • 11
14

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.

iamtodor
  • 734
  • 8
  • 21
martinfleis
  • 7,124
  • 2
  • 22
  • 30