Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用sf包连接线和多边形_R_Sf - Fatal编程技术网

R 使用sf包连接线和多边形

R 使用sf包连接线和多边形,r,sf,R,Sf,我有一个道路网文件和一个区域边界文件,我正在尝试分割道路线与区域边界相交的位置。我成功地完成了这一部分(见图1和图2);但是,当我查询领土边界内的分割路段时,我希望答案是2,但我得到的是1。请参见图3,该图显示了被标识为在区域内的线段。有人能帮我找出我的错误吗 以下是可复制代码: library(sf) library(lwgeom) library(dplyr) # Data territory <- structure(list(ZIP = "75420", geometry =

我有一个道路网文件和一个区域边界文件,我正在尝试分割道路线与区域边界相交的位置。我成功地完成了这一部分(见图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)
库(sf)
图书馆(lwgeom)
图书馆(dplyr)
#资料

territory要获取位于territory内的线段,我们可以使用
st_交点
。这将只保留多边形中的几何体,我认为这是您在上一个绘图中试图实现的

#将数据转换为相同的crs,并将id列更改为因子
道路%
突变(OBJECTID=因子(OBJECTID))

谢谢你,克里斯!我还尝试将territory层的属性连接到边界内的道路<代码>st_连接(道路分割、区域、连接=st_内)
未给出正确答案。你知道为什么在这种情况下,
内的st_不起作用吗?为什么不使用
st_相交
?我认为
st_in
将只包括完全位于多边形内的几何图形。我注意到
st_intersection
将区域文件中的列固定到道路文件中。因此,我不需要执行联接操作!不管怎样,谢谢你的回答。仍然困扰我的问题是,为什么我在上面发布的代码中选择了一条道路线。如果选择了完全位于多边形内的线,则不应选择任何线@阿什沃德:这看起来确实很奇怪。我无法完全复制您的示例,因为这行代码不适合我:
st_collection_extract(st_split(road,territory),“LINESTRING”)
它在st_split中返回
错误。sfc(st_geometry(x),y):length(y)==1不是真的