geometry
column is not in WKT format. Therefore you need a custom method similar to geopandas.point_from_xy
. You can use the following script. polygons_from_custom_xy_string
method is a string-specific. In case of the geometry
column has different xy string (for example 'x y, x y, ..'
), you should modify s.split()
in xy_list_from_string
method.
import pandas as pd
import geopandas as gpd
from shapely.geometry import Polygon
def polygons_from_custom_xy_string(df_column):
def chunks(lst, n):
for i in range(0, len(lst), n):
yield lst[i:i + n]
def xy_list_from_string(s):
# 'x y x y ...' -> [[x, y], [x, y], ...]
return list(chunks([float(i) for i in s.split()], 2))
def poly(s):
""" returns shapely polygon from point list"""
ps = xy_list_from_string(s)
return Polygon([[p[0], p[1]] for p in ps])
polygons = [poly(r) for r in df_column]
return polygons
df = pd.read_csv("file/path")
gdf = gpd.GeoDataFrame(df, geometry=polygons_from_custom_xy_string(df["geometry"]))
gdf.plot()