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

使用GeoPandas to_file方法导出GeoJSON时触发TypeError

解决GeoDataFrame导出GeoJSON时的TypeError问题

你的问题核心在于GeoJSON格式仅支持单个几何列,而你的GeoDataFrame同时包含geometry和centroid两个几何类型列,这会导致GeoPandas在导出时无法识别数据类型。以下是几种可行的解决方法:

方法1:将centroid转为普通坐标列后导出

把centroid几何列拆解为经度、纬度数值列,删除原几何列后再导出:

# 提取centroid的经纬度
gdf['centroid_lon'] = gdf['centroid'].x
gdf['centroid_lat'] = gdf['centroid'].y

# 删除centroid几何列
gdf = gdf.drop('centroid', axis=1)

# 导出GeoJSON
gdf.to_file("output.geojson", driver="GeoJSON")

方法2:将centroid转为WKT字符串存储

如果需要保留centroid的完整几何信息,可将其转为WKT格式的字符串(非几何类型):

# 转换centroid为WKT字符串
gdf['centroid_wkt'] = gdf['centroid'].to_wkt()

# 删除原centroid几何列
gdf = gdf.drop('centroid', axis=1)

# 导出
gdf.to_file("output.geojson", driver="GeoJSON")

方法3:升级GeoPandas及依赖库

旧版本的GeoPandas对多几何列的处理存在兼容性问题,尝试升级到最新版:

pip install --upgrade geopandas shapely

另外,确认导出时没有误将centroid指定为geometry参数,比如不要写gdf.to_file(..., geometry='centroid'),除非你只需要导出centroid作为几何列。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 08:37:42