如何将 geoDataFrame 导入 MySQL?

How can I import a geoDataFrame to MySQL?

我有一个具有以下结构的 geoDataFrame:

<class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 28 entries, 0 to 27
Data columns (total 3 columns):
 #   Column       Non-Null Count  Dtype   
---  ------       --------------  -----   
 0   Name         28 non-null     object  
 1   Description  28 non-null     object  
 2   geometry     28 non-null     geometry
dtypes: geometry(1), object(2)
memory usage: 800.0+ bytes

这是我的 df 的照片:

我正在尝试使用 gdf.to_sql 将其保存在 MySQL 数据库中,我的连接使用 SQLAlchemy 但我收到以下错误:AttributeError: 'GeometryDtype' object has no attribute 'base'.

我四处寻找并找到了这个 ,但我无法找到使其适用于 MySQL 的正确语法。

在尝试了很多事情之后,我注意到 to_sql 函数没有生成正确的 MySQL 语法以使其正常工作。此外,如果我按原样保留文本(请参见问题中的图片),使用更改为 wkb MySQL 的方法仍然无法将该列识别为几何图形。

对我有用的是将几何字段更改为字符串并在 python 中对其进行更新,因此它看起来像这样:

之后,我继续使用下面的代码,将数据帧发送到 MySQL,然后更新 table 以设置几何列:

regions.to_sql('pr_regions', con=conn, schema='eq_pr_db',
               if_exists='replace', index=False)

#add column type Polygon

conn.execute('''ALTER TABLE `eq_pr_db`.`pr_regions` 
                ADD COLUMN `geom` Polygon;''')

#populate new column by applying the ST_GeomFromText function to transform the string to geometry type.

conn.execute('''UPDATE `eq_pr_db`.`pr_regions`
                SET geom =  ST_GeomFromText(geometry) ;''')