r - 从点创建多线,使用 sf 包按 ID 分组
问题描述
我有一个点集合,带有一个LINEID和一个ID_SEQ。LINEID 确定唯一的 Line-IDS,而 ID_SEQ 确定 Line-ID 中点的顺序。
我想将点转换为线,按 ID_SEQ 排序并按 LINEID 分组。
使用 package sp
,我能够达到预期的效果,但我想使用sf
package 来做到这一点。我在这里想念什么?
这是一些虚拟数据,使用 -functions 说明了所需的结果,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), 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")
解决方案
正如 Edzer 在本期中所解释的,您必须为summarise
函数提供参数do_union = FALSE
:
tst <- sfpoints %>%
group_by(LINEID) %>%
summarise(do_union = FALSE) %>%
st_cast("LINESTRING")
plot(st_geometry(tst), col=1:2)
推荐阅读
- ios - UIPickerView 中的 UILabel 上的额外空间
- c++ - 如果参数包右侧包含简单类型参数,为什么可变参数模板参数包保持相同大小?
- r - 使用 dplyr 对两列数据框中的对象进行分类
- unit-testing - 测试在其主体中调用 boto3 的函数时如何模拟 boto3 调用
- r - 在R中使用列作为x轴绘制数据?
- laravel - 覆盖 Laravel Nova 删除操作
- python - 如何创建具有下方填充曲线的 3D 图形并定位 y 刻度?
- node.js - npm install 不再有效,但在一小时前
- android - Flutter 视频背景播放器代替图片背景
- c - 如何在clang中获得编译时间?