从字符串类型转换几何列时出错 - GeoPandas

Error converting geometry column from string type - GeoPandas

我有一个 pandas DataFrame,其中包含带有多边形的几何列。我正在尝试将其转换为 geopandas 数据框,并且需要将 geometry 的列类型从 string.

更改为

我在尝试从 shapely 应用 Polygon 时遇到此错误。

import geopandas as gpd 
from shapely.geometry import Polygon

df_geo['geometry'][0]

POLYGON ((-83.230504 42.242044, -83.228256 42.242185, -83.222044 42.242297, -83.221459 42.242307, -83.21916399999999 42.242432, -83.21682 42.242508, -83.21650799999999 42.23718, -83.216279 42.232113, -83.216138 42.227846, -83.219548 42.227773, -83.229843 42.227545, -83.229902 42.228622, -83.230504 42.242044))
                     

# convert geometry to Polygon

df_geo['geometry'] = df_geo['geometry'].apply(lambda x: Polygon(eval(x)))

df_geo



Traceback (most recent call last):

  File "/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3444, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "/var/folders/d0/gnksqzwn2fn46fjgrkp6045c0000gn/T/ipykernel_23632/494222449.py", line 3, in <module>
    df_geo['geometry'] = df_geo['geometry'].apply(lambda x: Polygon(eval(x)))

  File "/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/core/series.py", line 4357, in apply
    return SeriesApply(self, func, convert_dtype, args, kwargs).apply()

  File "/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/core/apply.py", line 1043, in apply
    return self.apply_standard()

  File "/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/core/apply.py", line 1098, in apply_standard
    mapped = lib.map_infer(

  File "pandas/_libs/lib.pyx", line 2859, in pandas._libs.lib.map_infer

  File "/var/folders/d0/gnksqzwn2fn46fjgrkp6045c0000gn/T/ipykernel_23632/494222449.py", line 3, in <lambda>
    df_geo['geometry'] = df_geo['geometry'].apply(lambda x: Polygon(eval(x)))

  File "<string>", line 1
    POLYGON ((-83.230504 42.242044, -83.228256 42.242185, -83.222044 42.242297, -83.221459 42.242307, -83.21916399999999 42.242432, -83.21682 42.242508, -83.21650799999999 42.23718, -83.216279 42.232113, -83.216138 42.227846, -83.219548 42.227773, -83.229843 42.227545, -83.229902 42.228622, -83.230504 42.242044))
                         ^
SyntaxError: invalid syntax

如果这确实是一个字符串,那么就是 WKT。这可以使用 shaplely https://shapely.readthedocs.io/en/stable/manual.html#well-known-formats

进行转换
import geopandas as gpd
import shapely
import pandas as pd
import io

df_geo = pd.read_csv(
    io.StringIO(
        """
index~geometry
0~POLYGON ((-83.230504 42.242044, -83.228256 42.242185, -83.222044 42.242297, -83.221459 42.242307, -83.21916399999999 42.242432, -83.21682 42.242508, -83.21650799999999 42.23718, -83.216279 42.232113, -83.216138 42.227846, -83.219548 42.227773, -83.229843 42.227545, -83.229902 42.228622, -83.230504 42.242044))"""
    ),
    sep="~",
)

# # convert geometry to Polygon
df_geo=gpd.GeoDataFrame(df_geo, geometry=df_geo["geometry"].apply(shapely.wkt.loads), crs="epsg:4326")

df_geo.explore(height=300, width=300)