提取简单要素数据框中点的坐标

5
我有2条线,需要提取这2条线相交的坐标。我试图使用R中的sf包来实现这一点。我可以做到相交和绘图,但是无法提取坐标。当每条线有一个交点时,它确实有效,但是当有多个交点时(在实际数据中有很多),每条线的交点坐标存储在单个字段中。 inters_pt看起来像: class geometry A c(3, 3, 3.2, 4.2) B c(1.5, 1.5) A c(2, 4)
我收到以下消息: Error in st_coordinates.sfc(st_geometry(x)) : not implemented for objects of class sfc_GEOMETRY
我想提取每个交点的坐标并保留'类'列中的属性。 输出应该如下所示: class geometry A (3, 3.2) A (3, 4.2) B (1.5, 1.5) A (2, 4)
尽管最初我认为它会像这样: class geometry A (3, 3) A (3.2, 4.2)
但我错了。
搜索让我到这里来了: https://github.com/r-spatial/sf/issues/114 但那里的解决方案要么删除点(st_cast(mp,“POINT”)),失去属性“class”,要么抛出以下错误:
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
1个回答

9

首先,值得使用sf从点创建线段,这可以减少所需的代码量:

library(sf)
library(dplyr)

#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))
) 

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))
) 

points_to_line <- function(data, group = "id"){
  data <- data %>% 
    group_by_at(group) %>%
    summarise(do_union = FALSE) %>%
    st_cast("LINESTRING") %>%
    ungroup %>%
    select(-do_union)
}

crs <- "+proj=utm +zone=32 +datum=WGS84"
coords <- c("x", "y")
lines1_sf <- st_as_sf(lines1.df, coords = coords, crs = crs) %>%
  points_to_line()
lines2_sf <- st_as_sf(lines2.df, coords = coords, crs = crs) %>%
  points_to_line()

#add attributes
lines2_sf$class <- c("A", "B")

#find intersecting points
#intersect 
inters_pt <- st_intersection(lines1_sf, lines2_sf)

在这里,你需要先转换为MULTIPOINT,然后再转换为POINT。

#extract the coordinates from the geometry column
inters_pt <- inters_pt %>%
  st_cast("MULTIPOINT") %>%
  st_cast("POINT")

> st_coordinates(inters_pt) 
#    X   Y
#1 3.0 3.2
#2 3.0 4.2
#3 2.0 4.0
#4 1.5 1.5

你的代码更易读了。但是,在创建sf行时出现错误:“无法找到函数‘select’的继承方法,签名为‘"sf"’”。然而,将其转换为多点再转换为点确实完美地工作了!谢谢! - egilio
1
选择函数存在命名空间冲突。尝试更改为 dplyr::select - sebdalgarno

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接