You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何读取含列表类型属性的GeoJSON文件?遇OGR类型不支持问题求助

解决GeoPandas读取GeoJSON数组字段时的KeyError问题

OGR(GeoPandas底层依赖的地理数据解析库)不支持GeoJSON中的数组类型属性,读取时会直接跳过这类字段,因此你会收到Skipping field paths: unsupported OGR type: 5的警告,后续访问paths字段自然触发KeyError。以下是两种可行的解决方法:

方法一:手动解析GeoJSON文件补全数组字段

直接用标准JSON库读取完整数据,提取数组字段后合并到GeoDataFrame中:

import json
import geopandas as gpd
from shapely.geometry import shape

# 读取GeoJSON文件
with open("example.geojson", "r", encoding="utf-8") as f:
    geojson_data = json.load(f)

# 分离属性与几何数据
feature_properties = []
geometries = []
for feature in geojson_data["features"]:
    feature_properties.append(feature["properties"])
    geometries.append(shape(feature["geometry"]))

# 创建包含完整属性的GeoDataFrame
tiling_grid = gpd.GeoDataFrame(
    feature_properties,
    geometry=geometries,
    crs=geojson_data["crs"]["properties"]["name"]
)

# 现在可正常访问paths字段
for index, row in tiling_grid.iterrows():
    tile_id = row['tile_id']
    tile_geometry = row['geometry']
    paths = row['paths']  # 无KeyError

方法二:导出时提前转换数组格式(从根源避免问题)

如果是你自己生成的GeoJSON,导出前将数组转为OGR支持的字符串类型,读取后再还原:

导出时处理:

# 假设原始GeoDataFrame为gdf,包含paths数组字段
gdf["paths_str"] = gdf["paths"].apply(lambda arr: ",".join(arr))
# 导出转换后的字段
gdf.to_file("example_fixed.geojson", columns=["tile_id", "geometry", "paths_str"])

读取时还原:

tiling_grid = gpd.read_file("example_fixed.geojson")
# 将字符串转回数组
tiling_grid["paths"] = tiling_grid["paths_str"].apply(lambda s: s.split(","))
# 可选:删除临时字符串字段
tiling_grid.drop("paths_str", axis=1, inplace=True)

内容的提问来源于stack exchange,提问作者beatrice

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 13:12:34