Geopandas 无法读取 geojson

问题描述 投票:0回答:1

我正在尝试读取使用这些步骤创建的 geojson

import geopandas as gpd

vec_data = gpd.read_file("map.shp") 
vec_data.head()
vec_data['cols_name'].unique()
sel_crop = vec_data[vec_data.cols_name == 'Permanent Grassland']
sel_crop.to_file("Permanent_Grassland.geojson", driver='GeoJSON')
feature = gpd.read_file(Permanent_Grassland.geojson)

但我收到以下错误:

{
    "name": "DataSourceError",
    "message": "Failed to read GeoJSON data",
    "stack": "---------------------------------------------------------------------------
DataSourceError                           Traceback (most recent call last)
Cell In[8], line 1
----> 1 feature = gpd.read_file(path_feature)

File c:\\Users\\bventura\\AppData\\Local\\anaconda3\\Lib\\site-packages\\geopandas\\io\\file.py:294, in _read_file(filename, bbox, mask, columns, rows, engine, **kwargs)
    291             from_bytes = True
    293 if engine == \"pyogrio\":
--> 294     return _read_file_pyogrio(
    295         filename, bbox=bbox, mask=mask, columns=columns, rows=rows, **kwargs
    296     )
    298 elif engine == \"fiona\":
    299     if pd.api.types.is_file_like(filename):

File c:\\Users\\bventura\\AppData\\Local\\anaconda3\\Lib\\site-packages\\geopandas\\io\\file.py:547, in _read_file_pyogrio(path_or_bytes, bbox, mask, rows, **kwargs)
    538     warnings.warn(
    539         \"The 'include_fields' and 'ignore_fields' keywords are deprecated, and \"
    540         \"will be removed in a future release. You can use the 'columns' keyword \"
   (...)
    543         stacklevel=3,
    544     )
    545     kwargs[\"columns\"] = kwargs.pop(\"include_fields\")
--> 547 return pyogrio.read_dataframe(path_or_bytes, bbox=bbox, **kwargs)

File c:\\Users\\bventura\\AppData\\Local\\anaconda3\\Lib\\site-packages\\pyogrio\\geopandas.py:261, in read_dataframe(path_or_buffer, layer, encoding, columns, read_geometry, force_2d, skip_features, max_features, where, bbox, mask, fids, sql, sql_dialect, fid_as_index, use_arrow, on_invalid, arrow_to_pandas_kwargs, **kwargs)
    256 if not use_arrow:
    257     # For arrow, datetimes are read as is.
    258     # For numpy IO, datetimes are read as string values to preserve timezone info
    259     # as numpy does not directly support timezones.
    260     kwargs[\"datetime_as_string\"] = True
--> 261 result = read_func(
    262     path_or_buffer,
    263     layer=layer,
    264     encoding=encoding,
    265     columns=columns,
    266     read_geometry=read_geometry,
    267     force_2d=gdal_force_2d,
    268     skip_features=skip_features,
    269     max_features=max_features,
    270     where=where,
    271     bbox=bbox,
    272     mask=mask,
    273     fids=fids,
    274     sql=sql,
    275     sql_dialect=sql_dialect,
    276     return_fids=fid_as_index,
    277     **kwargs,
    278 )
    280 if use_arrow:
    281     meta, table = result

File c:\\Users\\bventura\\AppData\\Local\\anaconda3\\Lib\\site-packages\\pyogrio\\raw.py:196, in read(path_or_buffer, layer, encoding, columns, read_geometry, force_2d, skip_features, max_features, where, bbox, mask, fids, sql, sql_dialect, return_fids, datetime_as_string, **kwargs)
     56 \"\"\"Read OGR data source into numpy arrays.
     57 
     58 IMPORTANT: non-linear geometry types (e.g., MultiSurface) are converted
   (...)
    191 
    192 \"\"\"
    194 dataset_kwargs = _preprocess_options_key_value(kwargs) if kwargs else {}
--> 196 return ogr_read(
    197     get_vsi_path_or_buffer(path_or_buffer),
    198     layer=layer,
    199     encoding=encoding,
    200     columns=columns,
    201     read_geometry=read_geometry,
    202     force_2d=force_2d,
    203     skip_features=skip_features,
    204     max_features=max_features or 0,
    205     where=where,
    206     bbox=bbox,
    207     mask=_mask_to_wkb(mask),
    208     fids=fids,
    209     sql=sql,
    210     sql_dialect=sql_dialect,
    211     return_fids=return_fids,
    212     dataset_kwargs=dataset_kwargs,
    213     datetime_as_string=datetime_as_string,
    214 )

File c:\\Users\\bventura\\AppData\\Local\\anaconda3\\Lib\\site-packages\\pyogrio\\_io.pyx:1239, in pyogrio._io.ogr_read()

File c:\\Users\\bventura\\AppData\\Local\\anaconda3\\Lib\\site-packages\\pyogrio\\_io.pyx:219, in pyogrio._io.ogr_open()

DataSourceError: Failed to read GeoJSON data"
}

在下面,您可以找到 shapefile 的第一行

{
    "type": "FeatureCollection",
    "name": "Permanent_Grassland",
    "crs": {
        "type": "name",
        "properties": {
            "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
        }
    },
    "features": [
        {
            "type": "Feature",
            "properties": {
                "ID": 3097,
                "DESCR_IT": "Prato stabile",
                "DESCR_DE": "Wiese (Dauerwiese)",
                "AREA": 440.93,
                "RIF_DATE": "20210413154430",
                "DESCR_ENG": "Meadow (permanent meadow)",
                "cols_name": "Permanent Grassland"
            },
            "geometry": {
                "type": "MultiPolygon",
                "coordinates": [
                    [
                        [
                            [
                                10.518715381443991,
                                46.693024346333097
                            ],
                            [
                                10.518776526157543,
                                46.693110649102266
                            ],
                            [
                                10.518884355244863,
                                46.69321532083277
                            ],
                            [
                                10.518931889188543,
                                46.693259326907466
                            ],
                            [
                                10.519043615333992,
                                46.693279082109612
                            ],
                            [
                                10.519113405222871,
                                46.693239907829479
                            ],
                            [
                                10.519156145906413,
                                46.693220261486537
                            ],
                            [
                                10.519085550826658,
                                46.693123465078543
                            ],
                            [
                                10.518963038138031,
                                46.692938263595963
                            ]
                        ]
                    ]
                ]
            ]
        }
    }
]
:
}
python geopandas shapefile
1个回答
0
投票

您的文件名应该被“引用”。在脚本的最后一行:

feature = gpd.read_file("Permanent_Grassland.geojson")

而不是

feature = gpd.read_file(Permanent_Grassland.geojson)
© www.soinside.com 2019 - 2024. All rights reserved.