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

如何在Plotly GeoJSON地图上叠加经纬度数据点

解决方案

你只需要在现有地图基础上添加一个新的Scatter图层,即可渲染元数据中的点位及ID标签,以下是完整修正后的代码:

import geojson
import plotly.graph_objects as go
import pandas as pd  # 新增导入pandas

with open("data/ireland-counties.geojson") as f:
    gj = geojson.load(f)

pts = []
for feature in gj['features']:
    if feature['geometry']['type'] == 'Polygon':
        pts.extend(feature['geometry']['coordinates'][0])    
        pts.append([None, None])#mark the end of a polygon   
        
    elif feature['geometry']['type'] == 'MultiPolygon':
        for polyg in feature['geometry']['coordinates']:
            pts.extend(polyg[0])
            pts.append([None, None])#end of polygon
    elif feature['geometry']['type'] == 'LineString': 
        pts.extend(feature['geometry']['coordinates'])  # 修正原代码变量名错误(points改为pts)
        pts.append([None, None])
    else: pass           

x, y = zip(*pts)    

fig = go.Figure()
# 添加基础郡边界地图
fig.add_scatter(x=x, y=y, mode='lines', line_color='#999999', line_width=1.5)

# 叠加元数据点位及ID标签
metadata = pd.DataFrame({'ID':['AH1','AH2','DO20','DO25','TI1','TI2'],
             'Latitude':[52.457079,52.457674,52.722615,52.724745,51.616257,51.616090],
             'Longitude':[-9.309839,-9.309434,-9.500108,-9.499674,-8.806805,-8.805225]})

fig.add_scatter(
    x=metadata['Longitude'],
    y=metadata['Latitude'],
    mode='markers+text',  # 同时显示标记点和文本
    marker=dict(color='#ff4444', size=8),  # 设置标记点样式
    text=metadata['ID'],  # 显示ID文本
    textposition='top center',  # 文本位置在标记上方居中
    name='点位'  # 图层名称
)

# 优化布局
fig.update_layout(
    width=600,  
    height=800,
    title='爱尔兰郡地图及点位分布',
    showlegend=False
)
fig.show()

关键说明:

  • 修正了原代码中LineString分支的变量名错误(原代码使用未定义的points,改为pts)
  • 新增的Scatter图层使用元数据的Longitude作为x轴、Latitude作为y轴,与基础地图坐标系保持一致
  • 通过mode='markers+text'实现点位和ID标签的同时显示,可根据需求调整标记颜色、大小及文本位置
  • 可通过update_layout进一步优化地图的标题、图例等显示效果

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 01:24:51