ValueError: GeoDataFrame does not support multiple columns using the geometry column name 'geometry'

ValueError: GeoDataFrame does not support multiple columns using the geometry column name 'geometry'

当我尝试将 csv 文件作为 geodataframe.According 上传到此站点上的其他问题解决方案时,我收到此错误,此方法应该可以解决问题。

这是我用来执行以下操作的代码:将文件上传为 gdf,然后生成仅包含部分列的子集数据框。

cp_union = gpd.read_file(r'C:\Users\User\Desktop\CPAWS\terrestrial_outputs\cp_union.csv')
cp_union.crs = 'epsg:3005'

cp_trimmed = cp_union[['COSEWIC_status','reason_for_designation','cnm_eng','iucn_cat','mgmt_e','status_e','classification','sq_m']]

如标题所述,我收到的错误是:ValueError: GeoDataFrame does not support multiple columns using the geometry column name 'geometry'. 将 gdf ​​保存为 csv 然后将其重新加载为 gdf ​​的过程中是否存在某些部分会导致创建一个额外的几何列?

编辑

在另一个脚本中,我加载了与 pd 数据帧相同的 csv 文件。这是该 pd 数据帧中的第一行数据。

    Unnamed: 0                                                                0
fid_critic                                                                0
scntfc_nm                                              Castilleja victoriae
cnm_eng                                               Victoria's Owl-clover
cnm_fren                                            Castilléjie de Victoria
cswc_pop                                                                NaN
ch_stat                                                               Final
cb_site_nm                                                     Cattle Point
ch_detail                                                  Detailed Polygon
ch_variant                                                              NaN
ch_method                                                               NaN
hectares                                                             0.8559
utm_zone                                                                 10
utm_east                                                             478383
utm_north                                                           5365043
latitude                                                          48.438164
longitude                                                        -123.29226
shape_1                                                                 0.0
objectid                                                         10251681.0
area_sqm                                                          8478.6733
feat_len                                                           326.5008
fid_protec                                                               -1
name_e                                                                  NaN
name_f                                                                  NaN
aichi_t11                                                               NaN
iucn_cat                                                                NaN
oecm                                                                    NaN
o_area                                                                  0.0
loc_e                                                                   NaN
loc_f                                                                   NaN
type_e                                                                  NaN
mgmt_e                                                                  NaN
gov_type                                                                NaN
legisl_e                                                                NaN
status_e                                                                NaN
protdate                                                                  0
delisdate                                                                 0
owner_e                                                                 NaN
owner_f                                                                 NaN
subs_right                                                              NaN
comments                                                                NaN
url                                                                     NaN
shape_leng                                                              0.0
protected                                                                 0
shape_le_1                                                       320.859687
shape_area                                                      6499.790343
geometry                  POLYGON ((1200735.4438 384059.0133999996, 1200...
COSEWIC_status                                                   Endangered
reason_for_designation    This small annual herb is confined to a very s...
sq_m                                                            6499.790343
classification                                                            c
Name: 0, dtype: object

所以我这里唯一的理论是,当您将 gdf ​​保存为 csv 时,csv 包含一个名为 geometry 的列。然后,当您将该 csv 加载为 gdf ​​时,geopandas 会尝试在 csv 中已有的几何列之上创建一个新的几何列。我可能完全错了。即使是这样,我也不确定如何解决这个问题。

感谢您的帮助!

  • 使用您的示例数据创建 CSV。必须替换 geometry 因为示例不是有效的 WKT 字符串
  • re-produced 你的错误
  • 通过使用 pandas 加载然后转换为 geopandas
  • 解决

解决方案

df = pd.read_csv(f)
cp_union = gpd.GeoDataFrame(
    df.loc[:, [c for c in df.columns if c != "geometry"]],
    geometry=gpd.GeoSeries.from_wkt(df["geometry"]),
    crs="epsg:3005",
)

完整代码

import pandas as pd
import geopandas as gpd
import io
from pathlib import Path

# fmt: off
df_q = pd.read_csv(io.StringIO("""Unnamed: 0                                                                0
fid_critic                                                                0
scntfc_nm                                              Castilleja victoriae
cnm_eng                                               Victoria's Owl-clover
cnm_fren                                            Castilléjie de Victoria
cswc_pop                                                                NaN
ch_stat                                                               Final
cb_site_nm                                                     Cattle Point
ch_detail                                                  Detailed Polygon
ch_variant                                                              NaN
ch_method                                                               NaN
hectares                                                             0.8559
utm_zone                                                                 10
utm_east                                                             478383
utm_north                                                           5365043
latitude                                                          48.438164
longitude                                                        -123.29226
shape_1                                                                 0.0
objectid                                                         10251681.0
area_sqm                                                          8478.6733
feat_len                                                           326.5008
fid_protec                                                               -1
name_e                                                                  NaN
name_f                                                                  NaN
aichi_t11                                                               NaN
iucn_cat                                                                NaN
oecm                                                                    NaN
o_area                                                                  0.0
loc_e                                                                   NaN
loc_f                                                                   NaN
type_e                                                                  NaN
mgmt_e                                                                  NaN
gov_type                                                                NaN
legisl_e                                                                NaN
status_e                                                                NaN
protdate                                                                  0
delisdate                                                                 0
owner_e                                                                 NaN
owner_f                                                                 NaN
subs_right                                                              NaN
comments                                                                NaN
url                                                                     NaN
shape_leng                                                              0.0
protected                                                                 0
shape_le_1                                                       320.859687
shape_area                                                      6499.790343
geometry                  POLYGON ((5769135.557632876 7083849.386658552, 5843426.213336911 7098018.122146672, 5852821.812968816 7081377.7312996285, 5914814.478616157 7091734.620966213, 5883751.009067913 7017032.330573363, 5902031.719573214 6983898.953064103, 5864452.659165712 6922039.030140929, 5829585.402576889 6878872.269967912, 5835906.522449658 6846685.714836724, 5800391.382286092 6827305.509709548, 5765261.646424723 6876008.057438379, 5765261.402301509 6876010.894933639, 5765264.431247815 6876008.786040769, 5760553.056402712 6927522.42488809, 5720896.599172597 6983360.181762057, 5755349.303491102 7039380.015177476, 5769135.557632876 7083849.386658552))
COSEWIC_status                                                   Endangered
reason_for_designation    This small annual herb is confined to a very s...
sq_m                                                            6499.790343
classification                                                            c"""), sep="\s\s+", engine="python", header=None).set_index(0).T
# fmt: on
# generate a CSV file from sample data
f = Path.cwd().joinpath("SO_q.csv")
df_q.to_csv(f, index=False)

# replicate issue...
try:
    gpd.read_file(f)
except ValueError as e:
    print(e)

# now the actual solution
df = pd.read_csv(f)
cp_union = gpd.GeoDataFrame(
    df.loc[:, [c for c in df.columns if c != "geometry"]],
    geometry=gpd.GeoSeries.from_wkt(df["geometry"]),
    crs="epsg:3005",
)