类型错误:列表索引必须是整数或切片,而不是 str

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

我正在尝试迭代不同的 GeoJSON 文件,但遇到此错误。

     20     for feature in features:
     21         # Extract properties and geometry
---> 22         properties = feature['properties']
     23         geometry = feature['geometry']
     24 

TypeError:列表索引必须是整数或切片,而不是 str。

我的 GeoJSON 文件结构,

{“类型”:“FeatureCollection”,“名称”:“10”,“crs”:{“类型”:“名称”,“属性”:{“名称”:“urn:ogc:def:crs:OGC :1.3:CRS84"}},"特征":[[{"类型":"特征","属性":{"Id":"75"},"几何":{"类型":"多多边形", “坐标”:[[[[[“71.000348”,“31.199419”],[“71.00038”,“31.198841”],[“71.000294”,“31.198832”],[“71.000294”,“31.197318”],[“ 71.0006","31.197379"],["71.000712","31.197094"],["71.000718","31.196727"],["71.001088","31.19675"],["71.001329","31.196746" ],[" 71.001426","31.196759"],["71.001442","31.197227"],["71.001463","31.197792"],["71.001409","31.198853"],["71.001356","31.1994 78"],[" 71.000348","31.199419"]]]]]}},{"type":"特征","properties":{"Id":"77"},"geometry":{"type":"MultiPolygon", "坐标":[[[[["72.469083","31.00806"],["72.469083","31.00806"],["72.470054","31.009012"],["72.470054","31.009012"],[" 72.471138","31.008148"],["72.471138","31.008148"],["72.47021","31.007159"],["72.47021","31.007159"],["72.469083","31.00806" ]]]] ]}}]]}

import json
import glob
import geopandas as gpd
import geemap

# Specify the folder path containing GeoJSON files
folder_path = 'D:/Test'

# Get a list of all GeoJSON files in the folder
geojson_files = glob.glob(folder_path + '/*.geojson')

# Iterate over each GeoJSON file
for geojson_file in geojson_files:
    with open(geojson_file, 'r') as f:
        data = json.load(f)
    
    # Access the features in the GeoJSON file
    features = data['features']

    for feature in features:
        # Extract properties and geometry
        properties = feature['properties']
        geometry = feature['geometry']

        # Convert to GeoDataFrame
        nReserve_feature = gpd.GeoDataFrame.from_features([{
            'geometry': geometry,
            'properties': properties
        }])

        # Load the GeoJSON string into Earth Engine
        nReserve_geojson = geemap.gdf_to_ee(nReserve_feature, geodesic=False)

我正在尝试使用唯一的 ID 迭代不同的 GeoJSON 文件的功能。

python string list geojson geemap
1个回答
0
投票

features = data['features']
替换为
features = data['features'][0]
以删除外部列表,因为特征的格式为 [[....]]

© www.soinside.com 2019 - 2024. All rights reserved.