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

如何用单个LineString展示双向道路两类交通数据?

解决双向道路交通数据绘图重叠问题

核心思路

将双向道路的单个LineString分别向两侧偏移,生成两个独立的几何对象,对应两个方向的交通数据,再分别绘制即可避免重叠。

具体步骤

1. 定义LineString偏移函数

利用Shapely的parallel_offset方法,根据需求向左右两侧偏移原始道路几何:

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

def offset_road(linestring, distance, side):
    # side可选'left'/'right',distance根据坐标系设置(投影坐标系用米,经纬度用度)
    return linestring.parallel_offset(distance, side)

2. 拆分双向道路数据

把原始数据拆分为两个方向的数据集,替换几何并匹配对应交通指标:

# 假设原始GeoDataFrame为gdf,包含:
# - geometry: 原始道路LineString
# - traffic_in: 驶入方向交通数据
# - traffic_out: 驶出方向交通数据

# 处理第一个方向(左侧偏移)
gdf_in = gdf.copy()
gdf_in['geometry'] = gdf_in['geometry'].apply(lambda x: offset_road(x, distance=0.0001, side='left'))
gdf_in['current_traffic'] = gdf_in['traffic_in']
gdf_in['dir'] = '驶入'

# 处理第二个方向(右侧偏移)
gdf_out = gdf.copy()
gdf_out['geometry'] = gdf_out['geometry'].apply(lambda x: offset_road(x, distance=0.0001, side='right'))
gdf_out['current_traffic'] = gdf_out['traffic_out']
gdf_out['dir'] = '驶出'

# 合并两个方向的数据
gdf_bidirectional = pd.concat([gdf_in, gdf_out], ignore_index=True)

3. 绘制分离的双向交通地图

用Geopandas分别绘制背景道路和双向交通数据,实现目标效果:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(12, 12))
# 绘制原始道路作为背景(浅灰色)
gdf.plot(ax=ax, color='#eeeeee', linewidth=1.5)
# 绘制双向交通数据,按流量配色
gdf_bidirectional.plot(
    ax=ax,
    column='current_traffic',
    cmap='coolwarm',
    linewidth=2,
    legend=True,
    legend_kwds={'label': '交通流量', 'shrink': 0.8}
)
plt.title('双向道路交通流量分布图')
plt.axis('off')
plt.show()

注意事项

  • 坐标系适配:如果使用经纬度坐标系(WGS84),偏移距离要换算为度数(如0.0001度≈10米);如果是投影坐标系(如UTM),直接用米作为单位(如1米)。
  • 单向道路过滤:如果数据中包含单向道路,可通过字段标记(如one_way)过滤,无需拆分偏移。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 21:57:53