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

如何阻止geopandas.GeoDataFrame的to_file()方法添加name属性?

解决GeoPandas导出GeoJSON时自动添加"name"属性的问题

要阻止to_file()自动添加"name"属性,有两种实用方法:

方法一:使用to_json()替代to_file()

geopandas.GeoDataFrame的to_json()方法默认生成不包含"name"字段的GeoJSON内容,直接将结果写入文件即可:

import geopandas as gpd

# 假设你的GeoDataFrame是df
with open("a_random_name.geojson", "w", encoding="utf-8") as f:
    f.write(df.to_json())

方法二:通过layer参数控制(针对to_file())

to_file()底层依赖Fiona库,"name"属性的值默认取自文件名(作为图层名)。你可以通过指定layer参数为空字符串,让Fiona不添加这个属性:

df.to_file(filename="a_random_name.geojson", driver="GeoJSON", layer="")

验证后会发现,导出的GeoJSON文件中,FeatureCollection部分不再包含"name": "a_random_name"这一行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 13:45:30