如何阻止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
相关产品推荐
相关产品推荐

