从点创建多线,按ID和SF包分组 [英] Create Multilines from Points, grouped by ID with sf package

查看:97
本文介绍了从点创建多线,按ID和SF包分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个点集合,其中包含一个 LINEID 和一个 ID_SEQ 。 LINEID确定唯一的Line-IDS,而ID_SEQ确定Line-ID中点的顺序。



我想将点转换为线,按ID_SEQ排序并按LINEID分组



使用软件包 sp ,我可以实现所需的结果,但是我想使用 sf 包。我在这里错过了什么?



这是一些虚拟数据,所需结果用 sp 函数和两个尝试对 sf 做相同的操作,但是这些图显示了不同的结果。

  library(sp)
library(sf)

sfpoints<-{
structure(list(LINEID = c(4L,4L,4L,4L,4L,4L ,4L,4L,4L,
4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,10L,10L,
10L),ID_SEQ = c(10L,11L,12L,13L,16L,30L,31L,32L,33L,
34L,35L,36L,37L,38L,39L,40L,41L,42L,43L,44L,45L,46L,
1L,2L,3L),几何=结构(list(structure(c(15(423(c(15.423568,
47.06248),class = c( XY, POINT, sfg))),结构( c(15.423644,
47.062523),class = c( XY, POINT, sfg))),结构(c(15.423691,
47.062553),class = c( XY, POINT, sfg)),结构(c(15.423712,
47.06257),class = c( XY, POINT, sfg)),结构(c(15.423716,
47.062576),类= c( XY, POINT, sfg)),str ucture(c(15.423712,
47.062588),class = c( XY, POINT, sfg)),结构(c(15.423731,
47.062595),class = c( XY , POINT, sfg)),结构(c(15.423779,
47.062626),class = c( XY, POINT, sfg)),结构(c(15.423835,
47.062664),class = c( XY, POINT, sfg)))结构(c(15.423879,
47.062714),class = c( XY, POINT, sfg)),结构(c(15.423897,
47.062767),class = c( XY, POINT, sfg)))结构(c(15.423862,
47.062828),类= c( XY, POINT, sfg))),结构(c(15.423783,
47.062897),class = c( XY, POINT, sfg)),结构( c(15.423681,
47.062973),class = c( XY, POINT, sfg))),结构(c(15.423564,
47.06306),class = c( XY, POINT, sfg)),结构(c( XY, POINT, sfg)))结构,结构(c(15.42331,
47.06327),类= c( XY, POINT, sfg)),结构(c(15.423186,
47.063385),类= c( XY, POINT, sfg )),结构(c(15.423062,
47 .063496),class = c( XY, POINT, sfg)),结构(c(15.422941,
47.063602),class = c( XY, POINT, sfg )),结构(c(15.422699,
47.063824),class = c( XY, POINT, sfg)),结构(c(15.422699,
47.063824),class = c ( XY, POINT, sfg))),结构(c(15.422518,
47.061687),class = c( XY, POINT, sfg)),结构(c( 15.422617,
47.06179),class = c( XY, POINT, sfg)),结构(c(15.422717,
47.061893),class = c( XY, POINT , sfg))),class = c( sfc_POINT,
sfc),precision = 0,bbox = structure(c(15.422518,47.061687,
15.423897,47.063824),。名称= c( xmin, ymin, xmax, ymax
),类= bbox),crs =结构(列表(epsg = NA_integer _,
proj4string = NA_character_) ,.Names = c( epsg, proj4string
),class = crs),n_empty = 0L)),.Names = c( LINEID, ID_SEQ,
geometry),row.names = c(NA,-25L),class = c( sf, data.frame
),sf_column = geometry,agr = structure(c(NA_integer_, NA_integer_
),.Names = c( LINEID, ID_SEQ),.Label = c( constant, aggregate,
identity),class = factor)) }

par(mfrow = c(1,3))

## SP-方式
tstssp<-as(sfpoints, Spatial)
tstssp<-SpatialLines(lapply(split(tstssp,tstssp $ LINEID),function(x)
Lines(list(Line(coordinates(x))),x $ LINEID [1L])))
plot(tstssp,col = 1:2,lwd = 3,main = SP期望的结果)


## SF-方法???
tst<-sfpoints%>%
group_by(LINEID)%&%;%
st_coordinates()%>%
st_linestring()
plot(st_geometry (tst),col = 1:2,main = SF错误的结果)

tst<-sfpoints%&%;%
group_by(LINEID)%>%
summarise()%>%
st_cast( LINESTRING)
plot(st_geometry(tst),col = 1:2,main = SF错误结果)

解决方案

正如Edzer在


I have a collection of Points, with a LINEID and an ID_SEQ. The LINEID determines the unique Line-IDS, while the ID_SEQ determines the order of the points in a Line-ID.

I want to transform Points to Lines, ordered by ID_SEQ and grouped by LINEID.

With the package sp, I am able to achieve the desired results, but I want to do it with the sf package. What am I missing here?

Here is some dummy data, the desired result illustrated with sp-functions and two attempts to do the same with sf, but the plots show different results.

library(sp)
library(sf)

sfpoints <- {
structure(list(LINEID = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 10L, 10L, 
10L), ID_SEQ = c(10L, 11L, 12L, 13L, 16L, 30L, 31L, 32L, 33L, 
34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 
1L, 2L, 3L), geometry = structure(list(structure(c(15.423568, 
47.06248), class = c("XY", "POINT", "sfg")), structure(c(15.423644, 
47.062523), class = c("XY", "POINT", "sfg")), structure(c(15.423691, 
47.062553), class = c("XY", "POINT", "sfg")), structure(c(15.423712, 
47.06257), class = c("XY", "POINT", "sfg")), structure(c(15.423716, 
47.062576), class = c("XY", "POINT", "sfg")), structure(c(15.423712, 
47.062588), class = c("XY", "POINT", "sfg")), structure(c(15.423731, 
47.062595), class = c("XY", "POINT", "sfg")), structure(c(15.423779, 
47.062626), class = c("XY", "POINT", "sfg")), structure(c(15.423835, 
47.062664), class = c("XY", "POINT", "sfg")), structure(c(15.423879, 
47.062714), class = c("XY", "POINT", "sfg")), structure(c(15.423897, 
47.062767), class = c("XY", "POINT", "sfg")), structure(c(15.423862, 
47.062828), class = c("XY", "POINT", "sfg")), structure(c(15.423783, 
47.062897), class = c("XY", "POINT", "sfg")), structure(c(15.423681, 
47.062973), class = c("XY", "POINT", "sfg")), structure(c(15.423564, 
47.06306), class = c("XY", "POINT", "sfg")), structure(c(15.423437, 
47.063164), class = c("XY", "POINT", "sfg")), structure(c(15.42331, 
47.06327), class = c("XY", "POINT", "sfg")), structure(c(15.423186, 
47.063385), class = c("XY", "POINT", "sfg")), structure(c(15.423062, 
47.063496), class = c("XY", "POINT", "sfg")), structure(c(15.422941, 
47.063602), class = c("XY", "POINT", "sfg")), structure(c(15.422821, 
47.063717), class = c("XY", "POINT", "sfg")), structure(c(15.422699, 
47.063824), class = c("XY", "POINT", "sfg")), structure(c(15.422518, 
47.061687), class = c("XY", "POINT", "sfg")), structure(c(15.422617, 
47.06179), class = c("XY", "POINT", "sfg")), structure(c(15.422717, 
47.061893), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT", 
"sfc"), precision = 0, bbox = structure(c(15.422518, 47.061687, 
15.423897, 47.063824), .Names = c("xmin", "ymin", "xmax", "ymax"
), class = "bbox"), crs = structure(list(epsg = NA_integer_, 
proj4string = NA_character_), .Names = c("epsg", "proj4string"
), class = "crs"), n_empty = 0L)), .Names = c("LINEID", "ID_SEQ", 
"geometry"), row.names = c(NA, -25L), class = c("sf", "data.frame"
), sf_column = "geometry", agr = structure(c(NA_integer_, NA_integer_
), .Names = c("LINEID", "ID_SEQ"), .Label = c("constant", "aggregate", 
"identity"), class = "factor"))}

par(mfrow=c(1,3))

## SP - way
tstssp <- as(sfpoints, "Spatial")
tstssp <- SpatialLines(lapply(split(tstssp, tstssp$LINEID), function(x) 
  Lines(list(Line(coordinates(x))), x$LINEID[1L])))
plot(tstssp, col=1:2, lwd=3, main="SP-Desired Result")


## SF - way ???
tst <- sfpoints %>% 
  group_by(LINEID) %>% 
  st_coordinates() %>%
  st_linestring()
plot(st_geometry(tst), col=1:2 , main="SF-Wrong Result")

tst <- sfpoints %>% 
  group_by(LINEID) %>%
  summarise() %>%
  st_cast("LINESTRING")
plot(st_geometry(tst), col=1:2, main="SF-Wrong Result")

解决方案

As Edzer explained in this issue, you have to supply the summarise function with the argument do_union = FALSE:

tst <- sfpoints %>% 
  group_by(LINEID) %>%
  summarise(do_union = FALSE) %>%
  st_cast("LINESTRING")
plot(st_geometry(tst), col=1:2)

这篇关于从点创建多线,按ID和SF包分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆