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

渥太华交通数据在Leaflet地图上定位偏移问题排查

渥太华交通数据地图点位偏移问题解决

问题描述

我尝试将渥太华的交通数据映射至地图,先转换为经纬度后使用leaflet包绘图。图表整体显示正常,但所有点位均向西偏移,未居中于渥太华区域。已尝试调整不同CRS参数,问题仍未解决。数据为渥太华2017年交通路口车流量数据,输出图片显示所有点位向西偏离渥太华核心区域。

使用的代码如下:

# Check the initial UTM to WGS84 transformation
data_sf <- st_as_sf(data, coords = c("X", "Y"), crs = 26918) 
data_latlon <- st_transform(data_sf, crs = 4326)  

# Extract coordinates for the transformed data
coords <- st_coordinates(data_latlon)
data$Longitude <- coords[, 1]
data$Latitude <- coords[, 2]

# Create the map using Leaflet
leaflet(data) %>%
  addTiles() %>%
  addCircleMarkers(
    ~Longitude, ~Latitude,
    radius = 4,
    color = "red",
    fillOpacity = 0.7,
    stroke = FALSE,
    popup = ~paste(
      "<b>Intersection:</b>", Intersection,
      "<br><b>Coordinates:</b>", Longitude, ",", Latitude
    )
  ) %>%
  setView(
    lng = -75.7,   # Adjust center longitude (Ottawa region)
    lat = 45.4,    # Adjust center latitude (Ottawa region)
    zoom = 12
  )

问题原因

核心问题是初始CRS参数选错了。渥太华位于UTM 17N带,对应的EPSG代码是26917,而你使用的26918是UTM 18N带(覆盖魁北克东部等更靠东的区域),这直接导致坐标转换后整体向西偏移。

修正方案

步骤1:替换正确的初始CRS参数

将crs = 26918改为crs = 26917,这是渥太华所在区域的正确UTM坐标系。

步骤2:简化代码(可选但推荐)

无需手动提取经纬度,leaflet支持直接传入sf对象,会自动处理坐标系转换,减少出错概率。

修正后的完整代码

# 正确转换坐标系:UTM 17N (EPSG:26917) 转 WGS84 (EPSG:4326)
data_sf <- st_as_sf(data, coords = c("X", "Y"), crs = 26917) 

# 直接使用sf对象创建leaflet地图
leaflet(data_sf) %>%
  addTiles() %>%
  addCircleMarkers(
    radius = 4,
    color = "red",
    fillOpacity = 0.7,
    stroke = FALSE,
    popup = ~paste(
      "<b>路口名称:</b>", Intersection,
      "<br><b>坐标:</b>", st_coordinates(data_sf)[,1], ",", st_coordinates(data_sf)[,2]
    )
  ) %>%
  setView(
    lng = -75.7,
    lat = 45.4,
    zoom = 12
  )

额外说明

  • UTM按经度每6度划分一个带,渥太华经度约为-75°,属于UTM 17N(覆盖范围-78°至-72°W)
  • 保持数据为sf对象格式进行可视化,能避免手动处理坐标时的潜在错误,代码也更简洁

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 05:43:15