本文介紹了如何將GeoDataFrame導(dǎo)入MySQL?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習吧!
問題描述
我有一個具有以下結(jié)構(gòu)的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數(shù)據(jù)庫中,并且我的連接使用的是SQLAlChemy,但收到以下錯誤:AttributeError:‘GeometryDtype’對象沒有屬性‘base’。
我一直在四處尋找,發(fā)現(xiàn)它solution,但我找不到正確的語法使其適用于MySQL。
mysql
嘗試了很多方法之后,我注意到推薦答案函數(shù)沒有生成正確的mysql語法使其正常工作。同樣,對于更改為WKB的方法,如果我保留文本原樣(請參見問題中的圖片),MySQL仍然無法將該列識別為幾何圖形。
對我起作用的是將幾何字段更改為字符串,并在python中更新它,使其看起來如下所示:
之后,我繼續(xù)使用下面的代碼,其中我將數(shù)據(jù)幀發(fā)送到MySQL,然后更新表以設(shè)置幾何列:
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) ;''')
這篇關(guān)于如何將GeoDataFrame導(dǎo)入MySQL?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,