最近邻连接两个sfc_LINESTRING对象。

huangapple go评论59阅读模式
英文:

nearest neighbour join between two sfc_LINESTRING objects

问题

我有两个相邻的区域,每个区域上都有一个连接的网格,每个网格都采用sfc_LINESTRING的结构。一个区域较大,具有较粗的网格,另一个区域较小,具有较细的网格。

我希望能够进行连接,以便来自较大网格的至少一个点与来自较小网格的至少一个点相连接。总体目标是能够在两个采样区域的所有节点上创建一个网络,以便使用sfnetworks进行网络分析。

有没有一种简便的方法可以实现这一点?我无法让st_joinmerge在这些对象上正常工作。

以下是一些代码示例:

英文:

I have two adjacent areas, on which I have a connected grid that each take the structure of sfc_LINESTRING. One area is larger and has a coarser grid, and the other is smaller with a finer grid.

I want to be able to do a join so at least one of the points from the larger grid is connected to at least one of the points from the smaller grids. The overall goal is to be able to have a connection between the two grids so I can use sfnetworks to make a network that includes all the nodes from both sampled areas.

Is there an easy way to go about doing this? I couldn't get st_join or merge to work on these objects.

here's some code:

library(sf)
library(nngeo)

nc = st_read(system.file("shape/nc.shp", package="sf"))
nc_utm = st_transform(nc, crs="+proj=utm +zone=18 +datum=NAD83 +unit=m") # using UTM

# crop the area to two smaller areas for sampling the grids
area_1 <- st_crop(nc_utm, xmin = 0, xmax = 20000, 
                  ymin = 3950000, ymax = 4000000)
area_2 <- st_crop(nc_utm, xmin = 20100, xmax = 25000, 
                  ymin = 3960000, ymax = 3980000)

# sample the grids and connect each
grid_1 <- sf::st_sample(
  area_1, size = 25, type = 'regular') %>% 
  sf::st_as_sf() %>%
  nngeo::st_connect(.,.,k = 9)

grid_2 <- sf::st_sample(
  area_2, size = 25, type = 'regular') %>% 
  sf::st_as_sf() %>%
  nngeo::st_connect(.,.,k = 9)

# plot it 
ggplot() + 
  geom_sf(data = nc_utm) +
  geom_sf(data = area_1, fill = 'blue', alpha = 0.3) + 
  geom_sf(data = area_2, fill = "red", alpha = 0.3) + 
  geom_sf(data = grid_1, colour = "blue") + 
  geom_sf(data = grid_2, colour = "red") +
  coord_sf(datum = "+proj=utm +zone=18 +datum=NAD83 +unit=m",
           xlim = c(-10000, 30000), ylim = c(3940000, 4001000))

答案1

得分: 1

@mrhellman 提供了解决方法,st_connect(st_combine(grid_1), st_combine(gird_2)) 返回一个连接上述两个栅格的单个线串。

以下是演示它如何与附加区域一起工作以显示其可扩展性的方式:

grid_3 <- sf::st_sample(
  area_3, size = 25, type = 'regular') %>%
  sf::st_as_sf() %>%
  nngeo::st_connect(.,.,k = 9)

grid_connect <- nngeo::st_connect(st_combine(grid_1), st_combine(grid_2))

grid_connect2 <- nngeo::st_connect(st_combine(grid_1), st_combine(grid_3))

all_grids <- c(grid_1, grid_2, grid_3, grid_connect, grid_connect2)

可以将其可视化为单个栅格:

ggplot() + 
  geom_sf(data = nc_utm) +
  geom_sf(data = area_1, fill = 'blue', alpha = 0.3) + 
  geom_sf(data = area_2, fill = 'red', alpha = 0.3) + 
  geom_sf(data = area_3, fill = 'yellow', alpha = 0.3) + 
  geom_sf(data = grid_1, colour = 'blue') + 
  geom_sf(data = grid_2, colour = 'red') +
  geom_sf(data = all_grids, colour = 'purple', size = 2) +
  coord_sf(datum = '+proj=utm +zone=18 +datum=NAD83 +unit=m',
           xlim = c(-10000, 30000), ylim = c(3940000, 4001000))

[![在此输入图片描述][1]][1]


<details>
<summary>英文:</summary>

@mrhellman gave the tip that solved this for me. `st_connect(st_combine(grid_1), st_combine(gird_2))` returns a single linestring that connects the two grids above. 

Here&#39;s how it works with an additional area to show that it scales: 

grid_3 <- sf::st_sample(
area_3, size = 25, type = 'regular') %>%
sf::st_as_sf() %>%
nngeo::st_connect(.,.,k = 9)

grid_connect <- nngeo::st_connect(st_combine(grid_1), st_combine(grid_2))

grid_connect2 <- nngeo::st_connect(st_combine(grid_1), st_combine(grid_3))

all_grids <- c(grid_1, grid_2, grid_3, grid_connect, grid_connect2)


And can be visualized as one single grid: 

ggplot() +
geom_sf(data = nc_utm) +
geom_sf(data = area_1, fill = 'blue', alpha = 0.3) +
geom_sf(data = area_2, fill = "red", alpha = 0.3) +
geom_sf(data = area_3, fill = "yellow", alpha = 0.3) +
geom_sf(data = grid_1, colour = "blue") +
geom_sf(data = grid_2, colour = "red") +
geom_sf(data = all_grids, colour = "purple", size = 2) +
coord_sf(datum = "+proj=utm +zone=18 +datum=NAD83 +unit=m",
xlim = c(-10000, 30000), ylim = c(3940000, 4001000))

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/gERYZ.png

</details>



huangapple
  • 本文由 发表于 2023年6月6日 01:54:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定