首页 > 解决方案 > R, sf: 与多边形的边界相交线,提取这些交点的坐标

问题描述

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

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

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)

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

标签: rsf

解决方案


您没有提供数据,所以我将使用以下提供的数据集: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)

在此处输入图像描述


推荐阅读