问题描述
我看到过关于此主题的类似帖子(例如,请参阅 here 和 here),但不是特定于 sf-tidyverse 生态系统的帖子。
我有一系列湖泊、每个湖泊内的一系列样本点以及每个湖泊中的一个“焦点”,代表船只下水的位置。 我想计算从船下水到每个采样点的“船夫最短旅行距离”。但是,我想以某种方式使用湖泊多边形“绑定”这些距离,以便无法跨陆地计算距离。我可以想象这是通过让“直线”蛇沿着湖边尽可能长的时间来完成的,然后它才能恢复为一条直线。我还可以想象“直线”被分解成围绕任何介入土地弯曲的线段。我对各种实现持开放态度!
我在其他地方(例如 here)看到过将边界多边形转换为栅格的想法,使水成为一个值,使土地成为另一个更高的值,然后进行“成本最低的距离” ,”在那里,穿越土地的成本令人望而却步。但是,我不知道如何在光栅/科幻生态系统中实际做到这一点。
这是我用来制作这张地图的代码:
library(dplyr)
library(sf)
library(ggplot2)
Moose.ssw = sswMN.sf %>% filter(lake == "Moose")
Moose.lake = MN_lakes4 %>% filter(str_detect(map_label,"Moose")) %>% filter(cty_name == "Beltrami")
Moose.access = try3 %>% filter(LAKE_NAME == "Moose") %>% filter(COUNTYNAME == "Beltrami")
Moose.Box = st_bBox(Moose.ssw)
ggplot() +
geom_sf(data = Moose.lake,color="lightblue") +
geom_sf(data = Moose.access,color = "red",size = 2) +
geom_sf(data = Moose.ssw,mapping = aes(color= Nitellopsis_obtusa_n)) +
coord_sf(xlim = c(Moose.Box[1],Moose.Box[3]),ylim = c(Moose.Box[2],Moose.Box[4]))
这里有一些很好地计算直线距离的代码——也许它可以以某种方式包装?
Moose.pt.dists = st_distance(Moose.access,Moose.ssw,by_element = TRUE)
制作上述数据对象所需的文件可以从我的 Github 页面中提取(它们是通过 dput()
.Link to my Github.
我是一名可靠的 R 程序员,但我对地理空间工作不熟悉,所以如果我能指出一个富有成效的方向,我可能就可以用自己的方式来解决这个问题!
解决方法
这是一个使用 sfnetworks 的解决方案,它非常适合 tidyverse。
下面的代码应该
- 定期对湖泊的边界框进行采样(创建均匀分布的点)
- 将他们与最近的邻居联系起来
- 删除跨越陆地的连接
- 沿着剩余的路线找到从船下水道到采样位置的最短路径。
结果并不准确,但非常接近。您可以通过增加采样点数来提高精度。以下使用1000点。
library(tidyverse)
library(sf)
library(sfnetworks)
library(nngeo)
# set seed to make the script reproducible,# since there is random sampling used
set.seed(813)
# Getting your data:
x <- dget("https://raw.githubusercontent.com/BajczA475/random-data/main/Moose.lake")
# Subset to get just one lake
moose_lake <- x[5,]
boat_ramp <- dget("https://raw.githubusercontent.com/BajczA475/random-data/main/Moose.access")
sample_locations <- dget("https://raw.githubusercontent.com/BajczA475/random-data/main/Moose.ssw")
sample_bbox <- dget("https://raw.githubusercontent.com/BajczA475/random-data/main/Moose.box")
# sample the bounding box with regular square points,then connect each point to the closest 9 points
# 8 should've worked,but left some diagonals out.
sq_grid_sample <- st_sample(st_as_sfc(st_bbox(moose_lake)),size = 1000,type = 'regular') %>% st_as_sf() %>%
st_connect(.,.,k = 9)
# remove connections that are not within the lake polygon
sq_grid_cropped <- sq_grid_sample[st_within(sq_grid_sample,moose_lake,sparse = F),]
# make an sfnetwork of the cropped grid
lake_network <- sq_grid_cropped %>% as_sfnetwork()
# find the (approximate) distance from boat ramp to point 170 (far north)
pt170 <- st_network_paths(lake_network,from = boat_ramp,to = sample_locations[170,]) %>%
pull(edge_paths) %>%
unlist()
lake_network %>%
activate(edges) %>%
slice(pt170) %>%
st_as_sf() %>%
st_combine() %>%
st_length()
#> 2186.394 [m]
看起来从船下水到湖最北端的采样位置 170 大约有 2186m。
# Plotting all of the above,path from boat ramp to point 170:
ggplot() +
geom_sf(data = sq_grid_sample,alpha = .05) +
geom_sf(data = sq_grid_cropped,color = 'dodgerblue') +
geom_sf(data = moose_lake,color = 'blue',fill = NA) +
geom_sf(data = boat_ramp,color = 'springgreen',size = 4) +
geom_sf(data = sample_locations[170,],color = 'deeppink1',size = 4) +
geom_sf(data = lake_network %>%
activate(edges) %>%
slice(pt170) %>%
st_as_sf(),color = 'turquoise',size = 2) +
theme_void()
虽然上面只找到并绘制了从船只发射到一个采样点的距离,但网络可以找到所有距离。您可能需要使用 *apply
或 purrr
以及一个小的自定义函数来查找发射到所有样本点的“一对多”距离。
sfnetworks
上的 This page 将有助于编写一对多解决方案。