【发布时间】:2020-04-15 05:27:59
【问题描述】:
我有一张多条河流的地图,我想向几何数据框添加一个 ID 列,添加一个定义每条河流的分组变量。我只有线几何信息,没有其他元数据,这可能吗?
一个例子:
dat <- structure(list(geometry = structure(list(structure(c(169.023627307075,
169.02315299228, -45.3068517761089, -45.3081870363656), .Dim = c(2L,
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(169.01854883529,
169.018930977689, 169.02315299228, -45.3083004691879, -45.3083134946477,
-45.3081870363656), .Dim = 3:2, class = c("XY", "LINESTRING",
"sfg")), structure(c(169.02315299228, 169.0330663306, -45.3081870363656,
-45.3144702778175), .Dim = c(2L, 2L), class = c("XY", "LINESTRING",
"sfg")), structure(c(169.015997396195, 169.022945130719, -45.3119974282578,
-45.3168289670259), .Dim = c(2L, 2L), class = c("XY", "LINESTRING",
"sfg")), structure(c(169.022945130719, 169.032555385154, -45.3168289670259,
-45.3163448854193), .Dim = c(2L, 2L), class = c("XY", "LINESTRING",
"sfg")), structure(c(169.01868555271, 169.022945130719, -45.3174947235968,
-45.3168289670259), .Dim = c(2L, 2L), class = c("XY", "LINESTRING",
"sfg"))), n_empty = 0L, crs = structure(list(input = "4326",
wkt = "GEOGCS[\"GCS_WGS_1984\",\n DATUM[\"WGS_1984\",\n SPHEROID[\"WGS_84\",6378137,298.257223563]],\n PRIMEM[\"Greenwich\",0],\n UNIT[\"Degree\",0.017453292519943295],\n AUTHORITY[\"EPSG\",\"4326\"]]"), class = "crs"), class = c("sfc_LINESTRING",
"sfc"), precision = 0, bbox = structure(c(xmin = 169.015997396195,
ymin = -45.3174947235968, xmax = 169.0330663306, ymax = -45.3068517761089
), class = "bbox"))), row.names = c(NA, 6L), class = c("sf",
"data.frame"), sf_column = "geometry", agr = structure(integer(0), .Label = c("constant",
"aggregate", "identity"), class = "factor"))
library(ggplot2)
library(sf)
ggplot() +
geom_sf(data = dat, size = 1) +
coord_sf()
在这个简单的示例中,有两条“河流”,但我无法在数据框中识别它们。我想要这样的专栏:
dat$ID <- c("A", "A", "A", "B", "B", "B")
我能想到的唯一方法是比较线端的坐标值,但这似乎不是很有效。非常感谢任何帮助。
【问题讨论】: