csv ValueError:GeoDataFrame不支持使用几何列名“geometry”的多列

nafvub8i  于 2023-01-28  发布在  其他
关注(0)|答案(2)|浏览(189)

我收到这个错误,当我试图上传一个csv文件作为一个geodataframe。根据其他问题的解决办法在这个网站上,这个方法应该做的伎俩。
下面是我使用的代码:将文件作为gdf上传,然后生成一个子集 Dataframe ,其中只显示一些列。

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 Dataframe 相同的csv文件,下面是pd Dataframe 中的第一行数据。

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中的列上创建一个新的geometry列。我可能完全错了。即使是这样,我也不知道该如何解决这个问题。
谢谢你的帮助!

rvpgvaaj

rvpgvaaj1#

  • 使用示例数据创建CSV。必须替换 geometry,因为示例不是有效的WKT字符串
  • 我重现了你的错误
  • 通过使用panda加载然后转换为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",
)
dsf9zpds

dsf9zpds2#

GeoPandas会在您读取CSV时自动添加几何字段。
因为你已经有了一个名为"geometry"的字段,GeoPandas抛出了一个异常。GeoPandas不会将"geometry"字段中的WKT字符串读取为geometry。
一些解决方法:

  1. GDAL/OGR CSV driver(geopandas使用)支持打开选项GEOM_POSSIBLE_NAMES,该选项允许您指定不同的字段名称以在其中查找几何图形。
gdf = gpd.read_file('/path/to/gdf.csv', 
                    GEOM_POSSIBLE_NAMES="geometry", 
                    KEEP_GEOM_COLUMNS="NO")

KEEP_GEOM_COLUMNS选项也是必需的,否则GDAL/OGR将返回一个"geometry"列以及几何,并且您仍将获得原始的ValueError: GeoDataFrame does not support multiple columns using the geometry column name 'geometry'.
1.或者,如果您可以控制CSV的原始写入,GDAL/OGR CSV driver documentation会注意到,如果字段名为"WKT",它将被读取为几何体:
读取名为"WKT"的字段时,假定包含WKT几何图形,但也被视为常规字段。
因此,一种选择是使用"WKT"作为几何列名称写出CSV,然后可以直接读取它:

gdf = gdf.rename_geometry('WKT')
gdf.to_csv('/path/to/gdf.csv', index=False)

# Then in later scripts you can just read it straight back in    
gdf = gpd.read_file('/path/to/gdf.csv')

1.最后一个选项是将CSV作为Pandas DataFrame读入(根据@RobRaymond的回答),然后从WKT手动创建几何图形并转换为GeoDataFrame。

import geopandas as gpd

df = gpd.read_file('/path/to/gdf.csv', ignore_geometry=True)

# Create geometry objects from WKT strings
df['geometry'] = gpd.GeoSeries.from_wkt(df['geometry'])

# Convert to GDF
gdf = gpd.GeoDataFrame(df)

相关问题