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

如何从GeoJSON提取名称与对应坐标至DataFrame(用于Plotly绘图)

提取GeoJSON中的名称与经纬度并转换为DataFrame

需求说明

现有FeatureCollection类型的GeoJSON文件,需提取每个要素的name属性,以及对应MultiLineString几何中的所有经纬度坐标,将名称与每一组经纬度一一关联,最终整理成DataFrame格式用于Plotly绘图。

已知GeoJSON结构如下:

{    
    "type": "FeatureCollection",    
    "name": "sf_example",    
    "crs": {        
        "type": "name",        
        "properties": {            
            "name": "urn:ogc:def:crs:OGC:1.3:CRS84"        
        }    
    },    
    "features": [
        {            
            "type": "Feature",            
            "properties": {                
                "name": "FirstRoad"            
            },            
            "geometry": {                
                "type": "MultiLineString",                
                "coordinates": [[[-0.209607278927995, 51.516851589085569], [-0.209607278927995, 51.516851589085569], [-0.210671042775843, 51.51666770991379], [-0.217526409795308, 51.515064252076257]]]        
            }    
        }, 
        {            
            "type": "Feature",            
            "properties": {                
                "name": "SecondRoad"            
            },            
            "geometry": {                
                "type": "MultiLineString",                
                "coordinates": [[[-0.208969020619286, 51.516005738748845], [-0.208969020619286, 51.516005738748845], [-0.211096548314982, 51.512688382789257]]]        
            }    
        }, 
        {            
            "type": "Feature",            
            "properties": {                
                "name": "ThirdRoad"            
            },            
            "geometry": {                
                "type": "MultiLineString",                
                "coordinates": [[[-0.204725784826204, 51.517020757267986], [-0.204725784826204, 51.517020757267986], [-0.207798880386654, 51.517211990108905]]]        
            }    
        }
    ]
}

已通过以下代码读取数据:

import json
with open('sf_example.geojson', "r") as read_file:    
    data = json.load(read_file)

解决方案

通过嵌套循环遍历所有要素和坐标点,收集数据后转换为DataFrame:

import pandas as pd

# 初始化空列表存储数据
road_data = []

# 遍历每个Feature
for feature in data['features']:
    road_name = feature['properties']['name']
    # 遍历MultiLineString中的每一条LineString
    for line in feature['geometry']['coordinates']:
        # 遍历当前LineString中的每个坐标点(注意GeoJSON是[lon, lat]顺序)
        for lon, lat in line:
            road_data.append({
                'name': road_name,
                'lat': lat,
                'lon': lon
            })

# 转换为DataFrame
df = pd.DataFrame(road_data)

输出结果

执行代码后得到的DataFrame结构如下:

namelatlon
FirstRoad51.516851589085569-0.209607278927995
FirstRoad51.516851589085569-0.209607278927995
FirstRoad51.51666770991379-0.210671042775843
FirstRoad51.515064252076257-0.217526409795308
SecondRoad51.516005738748845-0.208969020619286
SecondRoad51.516005738748845-0.208969020619286
SecondRoad51.512688382789257-0.211096548314982
ThirdRoad51.517020757267986-0.204725784826204
ThirdRoad51.517020757267986-0.204725784826204
ThirdRoad51.517211990108905-0.207798880386654

该格式可直接用于Plotly绘图,例如使用px.line_mapbox绘制道路轨迹。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 13:02:34