Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataFrame with WKT Column to GeoPandas Geometry

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
like image 819
TOuyang Avatar asked Dec 06 '22 08:12

TOuyang


1 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')
like image 156
PeaceLeka Avatar answered Dec 08 '22 00:12

PeaceLeka