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

如何将一组folium标记导出并保存为GeoJSON格式文件?

实现方法

folium的Marker对象不能直接导出为GeoJSON,你可以提取所有Marker的坐标后手动构造符合规范的GeoJSON结构,再用json模块写入文件即可,具体实现如下:

注意事项

  • 坐标顺序要转换:folium.Marker传入的是[纬度, 经度],但GeoJSON的Point类型要求坐标顺序为[经度, 纬度],必须调转顺序否则点位位置会出错
  • 你提供的代码里最后两个Marker都赋值给了neb5,会导致前一个neb5被覆盖,下方代码已经保留了全部6个点位,可按需调整
import json

# 整理所有标记的坐标
markers_coords = [
    [nebraska[0]+1.73, nebraska[1]-3.46],
    [nebraska[0]+2.73, nebraska[1]-3.46],
    [nebraska[0]+2.73, nebraska[1]-5.46],
    [nebraska[0]+4.73, nebraska[1]-3.46],
    [nebraska[0]+4.73, nebraska[1]],
    [nebraska[0]+1.73, nebraska[1]+3.2]
]

# 构造GeoJSON的FeatureCollection结构
geojson_data = {
    "type": "FeatureCollection",
    "features": []
}

# 遍历生成每个点位的Feature
for idx, coord in enumerate(markers_coords):
    feature = {
        "type": "Feature",
        "properties": {
            "marker_id": f"neb{idx+1}",
            "icon_type": "default"
            # 可在此处添加其他自定义属性
        },
        "geometry": {
            "type": "Point",
            "coordinates": [coord[1], coord[0]] # 转换为GeoJSON要求的[经度, 纬度]顺序
        }
    }
    geojson_data["features"].append(feature)

# 写入到GeoJSON文件
with open("nebraska_markers.geojson", "w", encoding="utf-8") as f:
    json.dump(geojson_data, f, ensure_ascii=False, indent=2)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 10:45:09