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

如何在地图上可视化绘制由经纬度点生成的LineString线串

问题排查与解决思路

错误根源:坐标顺序错位

你当前生成LineString的代码存在核心错误:shapely、geopandas的几何构造要求坐标顺序为(经度, 纬度),但你的代码传入的是(纬度, 经度),坐标全部错位,就会出现所有线路都指向同一个点位的异常表现。

第一步:数据清洗与几何字段修正

先过滤move=0的无效记录,再修正LineString的构造逻辑:

import pandas as pd
import geopandas as gpd
from shapely.geometry import LineString

# 过滤move为0的线路
erg2_filter = erg2[erg2['move'] != 0].reset_index(drop=True)

# 修正坐标顺序构造LineString
erg2_filter['geometry'] = erg2_filter.apply(
    lambda x: LineString([
        (x['longitude_origin'], x['latitude_origin']), 
        (x['longitude_destination'], x['latitude_destination'])
    ]), 
    axis=1
)

# 转为标准GeoDataFrame,指定WGS84坐标系(EPSG:4326)
gdf = gpd.GeoDataFrame(erg2_filter, crs="EPSG:4326")

# 校验几何是否正确:打印前5条数据的起点ID和对应起点坐标,确认和原始经纬度匹配
print(gdf[['origin', 'geometry']].head(5).apply(lambda x: (x['origin'], x['geometry'].coords[0]), axis=1))

绘制方案选择

直接绘制你构造的LineString即可,逻辑更简洁,还可以直接关联每条记录的move属性做配色、线条粗细的映射。如果需要额外标注起点终点,再叠加散点层即可,两者不冲突。

正确绘制代码示例

静态图(matplotlib+geopandas)

import matplotlib.pyplot as plt

# 加载底图(可选,没有底图可以跳过这一步直接绘制线路)
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# 裁剪底图到你数据的坐标范围
ax = world.cx[
    gdf.total_bounds[0]-1 : gdf.total_bounds[2]+1, 
    gdf.total_bounds[1]-1 : gdf.total_bounds[3]+1
].plot(figsize=(12,8), color='whitesmoke', edgecolor='lightgray')

# 绘制线路,可按move值配色
gdf.plot(
    ax=ax, 
    column='move', # 按move字段区分颜色
    cmap='RdYlBu_r', 
    linewidth=0.8, 
    alpha=0.7, 
    legend=True
)

# 可选:叠加起点(绿色)、终点(红色)散点
gdf.geometry.apply(lambda x: x.coords[0]).apply(lambda x: plt.scatter(x[0], x[1], c='green', s=10, alpha=0.6))
gdf.geometry.apply(lambda x: x.coords[1]).apply(lambda x: plt.scatter(x[0], x[1], c='red', s=10, alpha=0.6))

plt.axis('off')
plt.show()

交互式图(folium,支持悬浮查看属性)

import folium

# 计算地图中心点
center_lat = (gdf['latitude_origin'].mean() + gdf['latitude_destination'].mean())/2
center_lon = (gdf['longitude_origin'].mean() + gdf['longitude_destination'].mean())/2
m = folium.Map(location=[center_lat, center_lon], zoom_start=10, tiles='CartoDB Positron')

# 批量添加线路
for _, row in gdf.iterrows():
    # 转换为folium要求的[纬度, 经度]格式
    coords = [[p[1], p[0]] for p in list(row['geometry'].coords)]
    folium.PolyLine(
        locations=coords,
        weight=1.5,
        opacity=0.7,
        tooltip=f"起点ID:{row['origin']} | 终点ID:{row['destination']} | 移动量:{row['move']}"
    ).add_to(m)

# 保存为HTML文件打开即可查看
m.save('移动线路可视化.html')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 16:57:03