将投影转换为地理坐标 R

Convert projected to geographic coordinates R

一方面我有开放街道地图坐标,另一方面我有以下来自 Qgis 的投影坐标。

 coords.x1 coords.x2
1      306908   9809006
2      347492   9540309
3      544430   9618657
4      662290   9613788

两个坐标都在刚果民主共和国。我需要将投影坐标从Qgis转换为地理坐标,因为我的最终目标是计算距离。

我尝试了以下代码:

proj4string(data)
data_sf <- st_transform(data, "+proj=longlat +ellps=WGS84 +datum=WGS84")

但是这两行都给我以下错误:

Error in (function (classes, fdef, mtable): unable to find an inherited method for function ‘proj4string’ for signature ‘"data.frame"
Error in UseMethod("st_transform"): 
  no method for 'st_transform' applicable for an object of class "data.frame

感谢您的帮助!

假设您的意思是您的坐标是以刚果共和国使用的地理坐标系格式编写的(例如 EPSG:3341),并且您希望将数据重新投影到 WSG1984(espg :4326)

#example data (please provide it next time using dput())
data <- structure(list(coords.x1 = c(306908, 347492, 544430, 662290), 
    coords.x2 = c(9809006, 9540309, 9618657, 9613788)), class = "data.frame", row.names = c(NA, 
-4L))

#load packages
library(sp)
library(sf)

#create coordinates
coordinates(data) <- ~coords.x1+coords.x2

#convert to spatial feature
data <- st_as_sf(data)

#set the geographic coordinate system (espg:4326 is now assumed since its used for Kongo specifically)
st_crs(data) <- st_crs(3341)

#reproject to wsg1984
data <- st_transform(data, 4326)

如果您希望坐标采用 table 格式,您可以使用 st_coordinates()

st_coordinates(data)

         X         Y
1 14.26357 -1.728190
2 14.62523 -4.157976
3 16.39871 -3.450498
4 17.45939 -3.493481