R,sf:与多边形边界相交的线,提取这些交点的坐标

R, sf: intersect lines with the borders of multipolygons, extract coordinates of those intersections

我是 SF 和堆栈的新手,希望我的问题足够清楚。 我能够创建一组线,将一个点连接到美国各地的一组点。 我可以将美国各县读入多边形。 我的目标是找到并定位我创建的跨越县边界的所有点。

到目前为止,我已经能够从这些点创建线条:

points_to_lines <- dt %>% 
  st_as_sf(coords = c("lon", "lat"), crs = 4326) %>% 
  group_by(lineid) %>%
  summarize(do_union = FALSE) %>% lineid
  st_cast("LINESTRING")

这是行首

Simple feature collection with 1628 features and 1 field
geometry type:  LINESTRING
dimension:      XY
bbox:           xmin: 30.1127 ymin: -91.32484 xmax: 37.23671 ymax: -82.31262
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
# A tibble: 1,628 x 2
   lineid                                 geometry
    <int>                         <LINESTRING [°]>
 1      1  (33.51859 -86.81036, 36.16266 -86.7816)
 2      2 (33.51859 -86.81036, 34.61845 -82.47791)

这是县数据集的头部

Reading layer `US_county_1930_conflated' from data source `~/county_gis/1930' using driver `ESRI Shapefile'
Simple feature collection with 3110 features and 18 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -7115608 ymin: -1337505 xmax: 2258244 ymax: 4591848
epsg (SRID):    NA
proj4string:    +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs

我天真地试图给他们两个相同的坐标集,然后 st_intersects 他们。非稀疏矩阵接缝表示所有线只与一个县相交。

gis1930_p <- st_set_crs(gis1930, 4326) %>% st_transform(4326)
st_intersects(points, gis1930_p, sparse=FALSE)

我还在各县的顶部绘制了线条,但只绘制了美国各县的地图。

plot(gis1930_p[0], reset = FALSE)
plot(points[0], add = TRUE)

任何帮助将不胜感激,如果我可以提供任何其他详细信息,请告诉我。

您没有提供数据,所以我将使用以下提供的数据集:https://gis.stackexchange.com/a/236922/32531

你主要需要的是st_intersection函数:

library(sf)

line_1 <- st_as_sfc("LINESTRING(458.1 768.23, 455.3 413.29, 522.3 325.77, 664.8 282.01, 726.3 121.56)")
poly_1 <- st_as_sfc("MULTIPOLYGON(((402.2 893.03, 664.8 800.65, 611.7 666.13, 368.7 623.99, 215.1 692.06, 402.2 893.03)), ((703.9 366.29, 821.2 244.73, 796.1  25.93, 500.0 137.76, 703.9 366.29)))")
pnts   <- st_intersection(line_1, 
                          st_cast(poly_1, "MULTILINESTRING", group_or_split = FALSE))

plot(poly_1)
plot(line_1, add = TRUE)
plot(pnts, add = TRUE, col = "red", pch = 21)