使用sf包连接线和面

时间:2019-07-20 01:18:22

标签: r sf

我有一个道路网络文件和一个地域边界文件,我试图在与地界线相交的地方分割道路线。我成功完成了这一部分(见图1和2);但是,当我查询区域边界内的分割路段时,希望得到2作为答案,但得到1。请参见图3,该图显示了被识别为区域内的线段。谁能帮助我找出我的错误?

这是可复制的代码:

library(sf)
library(lwgeom)
library(dplyr)


# Data
territory <- structure(list(ZIP = "75420", geometry = structure(list(structure(list(
    list(structure(c(-95.74414, -95.76316, -95.764285, -95.752844, 
    -95.751832, -95.746896, -95.734855, -95.734006, -95.720971, 
    -95.710768, -95.710244, -95.706801, -95.706764, -95.730535, 
    -95.73184, -95.735787, -95.736036, -95.752266, -95.754151, 
    -95.745854, -95.74414, 33.125956, 33.129143, 33.135049, 33.140063, 
    33.15044, 33.154678, 33.146631, 33.1575, 33.158712, 33.145052, 
    33.124472, 33.083699, 33.080782, 33.079652, 33.084479, 33.085235, 
    33.089607, 33.087144, 33.117521, 33.119458, 33.125956), .Dim = c(21L, 
    2L)))), class = c("XY", "MULTIPOLYGON", "sfg"))), class = c("sfc_MULTIPOLYGON", 
"sfc"), precision = 0, bbox = structure(c(xmin = -95.764285, 
ymin = 33.079652, xmax = -95.706764, ymax = 33.158712), class = "bbox"), crs = structure(list(
    epsg = 4269L, proj4string = "+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs"), class = "crs"), n_empty = 0L)), row.names = 1L, sf_column = "geometry", agr = structure(c(ZIP = NA_integer_), .Label = c("constant", 
"aggregate", "identity"), class = "factor"), class = c("sf", 
"tbl_df", "tbl", "data.frame"))

road <- structure(list(OBJECTID = c(79016, 90587), geometry = structure(list(
    structure(c(-95.7339628709177, -95.7339308839088, -95.7335394637999, 
    -95.7291771125812, -95.725511451557, -95.7251052514439, -95.7246057013052, 
    -95.7238895811079, -95.7233289209554, -95.7223534606923, 
    -95.7219188005766, -95.7206659102471, -95.7205855502261, 
    -95.7201317201089, -95.7198334900309, -95.719688639993, -95.7188433397763, 
    -95.7178544795268, -95.7172555493757, -95.7166149992163, 
    -95.7111202278623, -95.7091030273542, -95.708638687236, -95.6956240838902, 
    -95.6913087527785, -95.6866614315863, -95.6822310204523, 
    -95.6780874393942, -95.6755406367445, 33.1234771073936, 33.1234661883929, 
    33.1233325683844, 33.1217477882711, 33.1204159381758, 33.1202741881663, 
    33.1201072881561, 33.119891498146, 33.1197560881444, 33.1195569481486, 
    33.1194925881552, 33.1193739181869, 33.1193672681891, 33.1193601082074, 
    33.1193384282161, 33.1193278982204, 33.1193397482593, 33.1194187883172, 
    33.1194662183522, 33.1195541383968, 33.1205214788194, 33.1206983289405, 
    33.1207185489645, 33.1207512695345, 33.1207270097168, 33.1207837799288, 
    33.1208809801393, 33.1210125703438, 33.1211047254717), .Dim = c(29L, 
    2L), class = c("XY", "LINESTRING", "sfg")), structure(c(-95.8017879888424, 
    -95.7970917606366, -95.7901262788488, -95.7885860284511, 
    -95.7875129581737, -95.7872352381013, -95.7857790777185, 
    -95.7826624768956, -95.7803956062984, -95.7507657384807, 
    -95.747458297608, -95.7452040270131, -95.7443153667774, -95.7431703664698, 
    -95.741351865972, -95.7346825781178, 33.1314989909815, 33.131512857188, 
    33.1315456274965, 33.1315130075572, 33.1314869875988, 33.1314697176075, 
    33.1313299176441, 33.130971567711, 33.1307314277636, 33.1274417984215, 
    33.1270760184951, 33.1268266585453, 33.1267092185614, 33.1264929885698, 
    33.1259993385545, 33.1237227794093), .Dim = c(16L, 2L), class = c("XY", 
    "LINESTRING", "sfg"))), class = c("sfc_LINESTRING", "sfc"
), precision = 0, bbox = structure(c(xmin = -95.8017879888424, 
ymin = 33.1193278982204, xmax = -95.6755406367445, ymax = 33.1315456274965
), class = "bbox"), crs = structure(list(epsg = 4326L, proj4string = "+proj=longlat +datum=WGS84 +no_defs"), class = "crs"), n_empty = 0L)), row.names = 1:2, sf_column = "geometry", agr = structure(c(OBJECTID = NA_integer_), .Label = c("constant", 
"aggregate", "identity"), class = "factor"), class = c("sf", 
"tbl_df", "tbl", "data.frame"))

# Initial plot (Figure 1)
plot(territory$geometry, col = "grey")
plot(road$geometry, col = c("red", "blue"), lwd = 3, add = TRUE)

# Splitting
road_splits <- st_collection_extract(st_split(road, territory), "LINESTRING")

# Goal plot (Figure 2)
plot(territory$geometry, col = "grey")
plot(road_splits$geometry, col = c("red", "blue", "green", "orange"), lwd = 3, add = TRUE)

# Identify road segments that fall within the territory boundary
roads_within_territory <- st_within(road_splits, st_transform(territory, crs = 4326), sparse = FALSE)[, 1]

# Plot road segments identified as being within the territory boundary
plot(territory$geometry, col = "grey")
plot(slice(road_splits, 1)$geometry, col = "red", lwd = 3, add = TRUE)

Figure 1

Figure 2

enter image description here

此外,如果我想引入两行分开的间隙(请参见图1),该怎么做?

1 个答案:

答案 0 :(得分:1)

要获取位于区域内的线段,可以使用st_intersection。这只会保留多边形内的几何形状,我认为这是您在上一个绘图中试图实现的。

# transform data to same crs and change id column to factor
road <- st_transform(road, 4326) %>%
  mutate(OBJECTID = factor(OBJECTID))

territory <- st_transform(territory, 4326)

# Splitting
road_splits <- st_intersection(road, territory)

#plot
library(ggplot2)
ggplot() + 
  geom_sf(data =territory)+
  geom_sf(data =road_splits, aes(col=OBJECTID))

enter image description here