我可以在不同的 EPSG 中按坐标类别连接两个数据集吗?

Can I join two datasets by coordinates categories in different EPSGs?

感谢您花时间阅读本文

我正在尝试连接两个不同的数据集,其中唯一可能的常见列是坐标。但是,其中一个数据集使用 normal 坐标系(例如 lat=39.35678,long=-8.99740),而另一个使用 EPSG 25830(例如 x=236044.949,y=4141285.671)。我对 R 和空间数据很陌生,所以我不太了解 spTransform 的文档,但我确实需要加入这两个数据集,坐标是我唯一可以使用的公共变量。我一直在尝试将 data_1 上的列与 EPSG:25830 转换为 EPSG:4326(data_2 使用)。

这是我的意思的一个例子(以及我一直在努力解决它的方法)

这是第一个数据集,EPSG:25830

structure(list(TOTAL_PLAZAS = c(4, 8, 6, 4, 6, 6), X = c("234755.4852", 
"235629.3447", "235170.6602", "235074.569", "235480.4626", "239104.22"
), Y = c("4143050.4408", "4142032.4727", "4142819.3263", "4142736.735", 
"4142705.8228", "4140674.42"), SRID = c("25830", "25830", "25830", 
"25830", "25830", "25830")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

这是另一个数据框,具有通常的坐标

structure(list(accommodates = c(4L, 3L, 2L, 6L, 6L, 4L), longitude = c(-5.99975, 
-5.99533, -5.98537, -5.99795, -5.99379, -5.99497), latitude = c(37.39358, 
37.39898, 37.38816, 37.39794, 37.39941, 37.38551)), class = c("rowwise_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L), groups = structure(list(
    .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame")))

这是我一直在尝试的方法,但到目前为止它只将原始数据框转换为空间点数据框,这对我没有用,因为我只需要具有转换坐标的原始数据集才能加入它与其他数据集。

coordinates(data_1) <- c("X","Y")
proj4string(data_1) <- CRS("+init=epsg:25830") 
CRS.new <- CRS("+init=epsg:4326") 
dnew <- spTransform(data1, CRS.new)

再次感谢!!

您可以使用 sf 包将数据转换为空间对象并执行所有需要的空间操作。您的数据的唯一问题是两个数据集的空间点位置不相交。因此,如果您使用 st_join 进行空间连接,实际上不会发生任何连接。我认为您可能对使用缓冲区 st_buffer 强制两个数据集之间的交集感兴趣。

# Create simple feature object from a simple table indicating X and Y coordinates, as well as its crs.
data1 <- data1 |>
  st_as_sf(coords = c("X", "Y"),
           crs = 25830) |>
  # Then transform it to the desired crs.
  st_transform(crs = 4326)

# Do the same for data2
data2 <- data2 |>
  st_as_sf(coords = c("longitude", "latitude"),
           crs = 4326) 

# Check if data intersects
st_intersects(data1, data2)
# This returns a list with empty entries meaning that there is no intersection among any of the spatial objects contained in data1 and data2

# Do join by spatial coordinates
resul <- data1 |>
  # Maybe you are interested in using a buffer to force an intersection between the two objects
  st_buffer(dist = 200) |>
  # Do spatial join
  st_join(data2)

# Finally, you can plot the data to visualize the spatial intersections
library(tmap)

tm_shape(data1|>
           st_buffer(dist = 200)) +
  # plot data1 with buffer as green filled circles
  tm_fill(col = "forestgreen") +
  tm_shape(data2) + 
  # plot data2 as red dots
  tm_dots(col = "firebrick2",
          size = 0.3)