I have 2 lines and I need to extract the coordinates where these 2 lines intersect. I'm trying to do this with the sf package in R. I can do the intersection and I can plot them but I can't extract the coordinates. It did work when there is only 1 intersecting point per line, but when there are more than one (and in the actual data there are many) the coordinates of intersecting points are stored in a single field per line.
inters_pt looks like: class geometry A c(3, 3, 3.2, 4.2) B c(1.5, 1.5) A c(2, 4)
I get the message:
Error in st_coordinates.sfc(st_geometry(x)) : not implemented for objects of class sfc_GEOMETRY
I'd like to extract the coordinates of each intersection and keep the attribute in the column 'class'.
The output should look like: class geometry A (3, 3.2) A (3, 4.2) B (1.5, 1.5) A (2, 4)
Although initially I thought it would look like this: class geometry A (3, 3) A (3.2, 4.2)
I was wrong.
A search got me here: https://github.com/r-spatial/sf/issues/114
But the solutions there either drop points (st_cast(mp, "POINT"), lose the attribute 'class' or throw the following error:
Error in vapply(lst, class, rep(NA_character_, 3)) : values must be length 3, but FUN(X[1]) result is length 2
#First create my data, I create points, and convert them into SpatialLines
lines1.df <- data.frame(
x = c(1,2,3,3,3,3,1,2,3),
y = c(1,2,2,3,4,5,4,4,4),
id = c(rep("A",6), rep("B",3))
)
#with Kyle Walker's functions I convert the points to lines
#https://rpubs.com/walkerke/points_to_line
library(sp)
library(maptools)
points_to_line <- function(data, long, lat, id_field = NULL, sort_field = NULL) {
# Convert to SpatialPointsDataFrame
coordinates(data) <- c(long, lat)
# If there is a sort field...
if (!is.null(sort_field)) {
if (!is.null(id_field)) {
data <- data[order(data[[id_field]], data[[sort_field]]), ]
} else {
data <- data[order(data[[sort_field]]), ]
}
}
# If there is only one path...
if (is.null(id_field)) {
lines <- SpatialLines(list(Lines(list(Line(data)), "id")))
return(lines)
# Now, if we have multiple lines...
} else if (!is.null(id_field)) {
# Split into a list by ID field
paths <- sp::split(data, data[[id_field]])
sp_lines <- SpatialLines(list(Lines(list(Line(paths[[1]])), "line1")))
# I like for loops, what can I say...
for (p in 2:length(paths)) {
id <- paste0("line", as.character(p))
l <- SpatialLines(list(Lines(list(Line(paths[[p]])), id)))
sp_lines <- spRbind(sp_lines, l)
}
return(sp_lines)
}
}
lines1 <- points_to_line(data = lines1.df,
long = "x",
lat = "y",
id_field = "id")
proj4string(lines1) <- CRS("+proj=utm +zone=32 +datum=WGS84")
library(sf)
lines1_sf <- st_as_sf(lines1,
crs = "+proj=utm +zone=32 +datum=WGS84")
lines2.df <- data.frame(
x = c(2,2,2.5,3.5,3.5,2.5,1,2),
y = c(3,4.2,4.2,4.2,3.2,3.2,2,1),
id = c(rep("A",6), rep("B",2))
)
lines2 <- points_to_line(data = lines2.df,
long = "x",
lat = "y",
id_field = "id")
proj4string(lines2) <- CRS("+proj=utm +zone=32 +datum=WGS84")
lines2_sf <- st_as_sf(lines2,
crs = "+proj=utm +zone=32 +datum=WGS84")
#add attributes
lines2_sf$class <- c("A", "B")
#plot both lines
library(ggplot2)
ggplot(lines1_sf) +
geom_sf(aes(color="red")) +
geom_sf(data=lines2_sf) +
coord_sf(crs = "+proj=utm +zone=32 +datum=WGS84") +
theme_bw()
#find intersecting points
#intersect
inters_pt <- st_intersection(lines2_sf, lines1_sf)
#plot shows the intersecting points
ggplot(lines1_sf) +
geom_sf(aes(color="red")) +
geom_sf(data=lines2_sf) +
geom_sf(data=inters_pt) +
coord_sf(crs = "+proj=utm +zone=32 +datum=WGS84") +
theme_bw()
#extract the coordinates from the geometry column
st_coordinates(inters_pt) #<-- gives error
st_cast(inters_pt, "POINT") #removes points
y = as(inters_pt, "Spatial") #loses the 'class' attribute
SpatialPoints(y)
st_cast(st_sfc(inters_pt), "POINT", group_or_split = FALSE) #error