Thanks for taking your time to read this
I am trying to join two different datasets where the only common possible columns are coordinates. However, one of those datasets uses a normal system of coordinates (e.g. lat=39.35678, long=-8.99740) while the other uses EPSG 25830 (e.g. x=236044.949, y=4141285.671). I am quite new to R and spacial data so I don't quite get the documentation on spTransform, but I really need to join those two datasets and the coordinates are the only common variable i can use. I've been trying to transform the columns on data_1 with EPSG:25830 to EPSG:4326 (the one data_2 uses).
Here's an example of what I mean (and how I've been trying to solve it)
Here's the first dataset, with the 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"))
And here's the other dataframe, with the usual coordinates
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")))
This is what I've been trying, but so far it only transforms the original dataframe into a spatial points data frame, which is no use for me since i just need the original dataset with converted coordinates in order to join it with the other dataset.
coordinates(data_1) <- c("X","Y")
proj4string(data_1) <- CRS(" init=epsg:25830")
CRS.new <- CRS(" init=epsg:4326")
dnew <- spTransform(data1, CRS.new)
Many thanks again!!
CodePudding user response:
You can use the sf
package to convert the data into spatial objects and do all the desired spatial operations. The only problem with your data is that the spatial point locations of both datasets do not intersect. Thus, if you do a spatial join with st_join
, no join will actually occur. I think that you might be interested in using a buffer st_buffer
to force an intersection between the two datasets.
# 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)