Max*_*uso 3 csv geometry dataframe geopandas
当我尝试将 csv 文件作为地理数据框上传时,我收到此错误。根据本网站上的其他问题解决方案,此方法应该可以解决问题。
\n这是我用来执行的代码:将文件上传为 gdf,然后生成仅包含某些列的子集数据帧。
\ncp_union = gpd.read_file(r'C:\\Users\\User\\Desktop\\CPAWS\\terrestrial_outputs\\cp_union.csv')\ncp_union.crs = 'epsg:3005'\n\ncp_trimmed = cp_union[['COSEWIC_status','reason_for_designation','cnm_eng','iucn_cat','mgmt_e','status_e','classification','sq_m']]\nRun Code Online (Sandbox Code Playgroud)\n如标题中所述,我收到的错误是这样的:将ValueError: GeoDataFrame does not support multiple columns using the geometry column name 'geometry'.gdf 保存为 csv 然后将其重新加载为 gdf 的过程中是否有某些部分会导致创建额外的几何列?
编辑
\n在另一个脚本中,我加载了与 pd 数据框相同的 csv 文件。这是该 pd 数据帧中的第一行数据。
\n Unnamed: 0 0\nfid_critic 0\nscntfc_nm Castilleja victoriae\ncnm_eng Victoria's Owl-clover\ncnm_fren Castill\xc3\xa9jie de Victoria\ncswc_pop NaN\nch_stat Final\ncb_site_nm Cattle Point\nch_detail Detailed Polygon\nch_variant NaN\nch_method NaN\nhectares 0.8559\nutm_zone 10\nutm_east 478383\nutm_north 5365043\nlatitude 48.438164\nlongitude -123.29226\nshape_1 0.0\nobjectid 10251681.0\narea_sqm 8478.6733\nfeat_len 326.5008\nfid_protec -1\nname_e NaN\nname_f NaN\naichi_t11 NaN\niucn_cat NaN\noecm NaN\no_area 0.0\nloc_e NaN\nloc_f NaN\ntype_e NaN\nmgmt_e NaN\ngov_type NaN\nlegisl_e NaN\nstatus_e NaN\nprotdate 0\ndelisdate 0\nowner_e NaN\nowner_f NaN\nsubs_right NaN\ncomments NaN\nurl NaN\nshape_leng 0.0\nprotected 0\nshape_le_1 320.859687\nshape_area 6499.790343\ngeometry POLYGON ((1200735.4438 384059.0133999996, 1200...\nCOSEWIC_status Endangered\nreason_for_designation This small annual herb is confined to a very s...\nsq_m 6499.790343\nclassification c\nName: 0, dtype: object\nRun Code Online (Sandbox Code Playgroud)\n所以我这里唯一的理论是,当您将 gdf 保存为 csv 时,csv 包含一个名为 Geometry 的列。然后,当您将该 csv 作为 gdf 加载时,geopandas 会尝试在 csv 中已有的几何列之上创建一个新的几何列。我对此可能完全错误。即使是这样,我也不知道如何解决这个问题。
\n谢谢您的帮助!
\n当您读取 CSV 时,GeoPandas 会自动添加几何字段。
\n由于您已经有一个名为“几何”的字段,GeoPandas 引发了一个异常。GeoPandas 不会将“几何”字段中的 WKT 字符串读取为几何。
\n一些解决方法:
\nGDAL/OGR CSV 驱动程序(geopandas 使用的)支持 open 选项GEOM_POSSIBLE_NAMES,该选项允许您指定不同的字段名称来查找几何图形。
gdf = gpd.read_file(\'/path/to/gdf.csv\', \n GEOM_POSSIBLE_NAMES="geometry", \n KEEP_GEOM_COLUMNS="NO") \nRun Code Online (Sandbox Code Playgroud)\n该KEEP_GEOM_COLUMNS选项也是必需的,否则 GDAL/OGR 将返回“几何”列以及几何图形,并且您仍然可以获得原始数据ValueError: GeoDataFrame does not support multiple columns using the geometry column name \'geometry\'.
或者,如果您可以控制 CSV 的原始写入,则 GDAL/OGR CSV 驱动程序文档指出,如果字段名为“WKT”,它将被读取为几何图形:
\n\n\n当读取名为 \xe2\x80\x9cWKT\xe2\x80\x9d 的字段时,假定包含 WKT 几何图形,但也被视为常规字段。
\n
因此,一种选择是使用“WKT”作为几何列名称写出 CSV,然后可以直接读回:
\ngdf = gdf.rename_geometry(\'WKT\')\ngdf.to_csv(\'/path/to/gdf.csv\', index=False)\n\n# Then in later scripts you can just read it straight back in \ngdf = gpd.read_file(\'/path/to/gdf.csv\')\nRun Code Online (Sandbox Code Playgroud)\n最后一个选项是将 CSV 作为 Pandas DataFrame 读取(根据@RobRaymond\'s 答案),然后从 WKT 手动创建几何图形并转换为 GeoDataFrame。这是一种替代方法:
\nimport geopandas as gpd\n\ndf = gpd.read_file(\'/path/to/gdf.csv\', ignore_geometry=True)\n\n# Create geometry objects from WKT strings\ndf[\'geometry\'] = gpd.GeoSeries.from_wkt(df[\'geometry\'])\n\n# Convert to GDF\ngdf = gpd.GeoDataFrame(df)\nRun Code Online (Sandbox Code Playgroud)\n| 归档时间: |
|
| 查看次数: |
5246 次 |
| 最近记录: |