使用R将SWEREF99 TM坐标转WGS84出错,求排查解决方法
问题原因与解决方法
核心错误
你在定义坐标时颠倒了x(东向)和y(北向)的赋值:
- SWEREF99 TM(EPSG:3006)的坐标规则是:
x对应东向值,y对应北向值 - 你代码中将北向坐标
6855790赋给了x,东向坐标557541赋给了y,导致坐标位置完全偏离,转换结果错误。
修正后的代码
# 安装并加载sf包(按需执行) chooseCRANmirror(ind=1) install.packages('sf') library(sf) # 正确对应x=东向,y=北向 data.frame(x = 557541, y = 6855790) |> st_as_sf(coords = c('x', 'y')) |> st_set_crs(3006) |> st_transform(4326)
执行后将得到预期结果:
## Simple feature collection with 1 feature and 0 fields ## Geometry type: POINT ## Dimension: XY ## Bounding box: xmin: 16.09256 ymin: 61.83058 xmax: 16.09256 ymax: 61.83058 ## Geodetic CRS: WGS 84 ## geometry ## 1 POINT (16.09256 61.83058)
大规模数据集处理(结合dplyr)
如果你的数据集包含east(东向)和north(北向)列,直接按正确对应关系转换即可:
library(dplyr) library(sf) # 假设你的数据集名为coord_data,包含east和north列 coord_data |> st_as_sf(coords = c('east', 'north'), crs = 3006) |> st_transform(4326) |> # 可选:将转换后的坐标拆分为单独的lon/lat列 mutate( lon = st_coordinates(geometry)[,1], lat = st_coordinates(geometry)[,2] )
替代方案(不使用sf)
如果不想用sf包,可以使用terra包(rgdal的替代工具):
install.packages('terra') library(terra) # 创建点矢量对象 pt <- vect(cbind(557541, 6855790), crs = "EPSG:3006") # 转换坐标系 pt_wgs84 <- project(pt, "EPSG:4326") # 提取坐标值 coordinates(pt_wgs84)
内容的提问来源于stack exchange,提问作者JonasEngstrom
相关产品推荐
相关产品推荐

