[英]R GIS intersecting lines and polygons (large data)
GIS and R question: GIS和R问题:
I need to intersect many lines with many polygons .我需要将许多线与许多多边形相交。
Ideally I would like to do this in R.理想情况下,我想在 R 中执行此操作。
Where one line crosses a polygon boundary, I would like that line to be split (making two features / lines).在一条线穿过多边形边界的地方,我希望这条线被分割(制作两条特征/线)。
I would then like to attribute the new (often smaller) lines with the attributes of the polygon that they are now within.然后,我想将新的(通常更小的)线与它们现在所在的多边形的属性一起归因。
If the lines lie outside a polygon, I would expect the new attributes attached from the polygon to the lines would be NULL or NA or empty.如果线条位于多边形之外,我希望从多边形附加到线条的新属性将为 NULL 或 NA 或空。
I am happy to load the data as either shapefiles or geopackages .我很高兴将数据加载为shapefiles或geopackages 。
What is the best tool to do this with in R?在 R 中执行此操作的最佳工具是什么?
(I'm happy to use packages such as Raster , rgeos and SF etc. However, ideally, if we can avoid calling QGIS or other software unless it is very stable that would be great. I don't have a licence for Arc) (我很乐意使用Raster 、 rgeos和SF等软件包。但是,理想情况下,如果我们可以避免调用 QGIS 或其他软件,除非它非常稳定,否则会很棒。我没有 Arc 的许可证)
Many thanks indeed.非常感谢。
Let's collect some online data, a polygon and a couple of lines.让我们收集一些在线数据,一个多边形和几条线。 We will use
osmdata
to fetch OSM
data for my village.我们将使用
osmdata
为我的村庄获取OSM
数据。
library(osmdata)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
library(tidyverse)
Let's find a bounding box and add a bit of space to it让我们找到一个边界框并为其添加一点空间
Lbb <- getbb("Lubnów trzebnicki Poland")
addM <- matrix(data = c(-0.01, -0.01, 0.01, 0.01), nrow = 2, ncol = 2)
newBB <- Lbb + addM
Let's fetch highways (lines, multilines) and boundaries (polygons, multipolygons) which we will use in our calculations让我们获取我们将在计算中使用的高速公路(线、多线)和边界(多边形、多多边形)
highways <- opq (newBB) |>
add_osm_feature (key = "highway") |>
osmdata_sf()
highways <- highways$osm_lines |>
select(osm_id, name, geometry) |>
st_as_sf()
boundaries <- opq(newBB) |>
add_osm_feature (key = "boundary", value = "administrative") |>
osmdata_sf()
boundariesWsi <- boundaries$osm_multipolygons |>
filter(boundary == "administrative" & admin_level == 8 & name == "Lubnów")
And let's plot it for visualization让我们绘制它以进行可视化
plot(boundariesWsi$geometry, col = "gray90")
plot(highways$geometry, lwd = 0.6, lty = 4, add = TRUE)
We will create a supporting MULTILINESTRING
corresponding to village boundaries, which we will use to find only those ways, which croses the boundary我们将创建一个对应于村庄边界的支持
MULTILINESTRING
,我们将使用它来仅找到那些跨越边界的方式
boundariesWsiAsLines <- st_cast(boundariesWsi, "MULTILINESTRING")
Now, let's find those interestingHighways
which crosses the village boundaries:现在,让我们找到那些穿越村庄边界的
interestingHighways
高速公路:
interestingHighways <- highways |>
filter(st_intersects(boundariesWsiAsLines$geometry, highways$geometry, sparse = FALSE))
and finally, find IN and OUT parts of those highways:最后,找到这些高速公路的 IN 和 OUT 部分:
a<- st_as_sf(st_intersection(interestingHighways$geometry, boundariesWsi$geometry))
b<- st_as_sf(st_difference(interestingHighways$geometry, boundariesWsi$geometry))
plot(a, lwd = 4, col = "darkblue", add = TRUE)
plot(b, lwd = 4, col = "darkgreen", add = TRUE)
Instead of one multipolygon, you can have a list of multipolygons.您可以拥有一个多面体列表,而不是一个多面体。 To assign the attributes to ways, you can use
dplyr::mutate()
.要将属性分配给方式,您可以使用
dplyr::mutate()
。
Created on 2022-01-31 by the reprex package (v2.0.1)由reprex 包(v2.0.1) 创建于 2022-01-31
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.