开发者

Create transition object based on ocean current and direction for use in shortest path analysis

开发者 https://www.devze.com 2022-12-07 22:32 出处:网络
My ultimate goal is to use gdistance::shortestPath() to calculate the shortest path distance from a point on one side of an island to a point on the other side of the same island, while not travelling

My ultimate goal is to use gdistance::shortestPath() to calculate the shortest path distance from a point on one side of an island to a point on the other side of the same island, while not travelling over land (i.e. travel by sea), while accounting for ocean current speed and current direction.

An intermediate of the above is to generate a transition layer, which is subsequently passed to shortestPath(). I am having trouble understanding how to pass multiple rasters to gdistance::transition().

I have the following raster layers:

  • ocean current speed
  • ocean current bearing
  • map of ocean with an island

A small example:

library(dplyr)
library(sf)
library(terra)
library(gdistance)
library(raster)

# Ocean current speed raster
ocean_spd <- terra::rast(nrow = 100,
                         ncol = 100, 
                         xmin = 0,
                         xmax = 1,
                         ymin = 0,
                         ymax = 1,
                         crs = NA) 

# set all ocean values to 0.5 (m/s)
ocean_spd <- terra::setValues(ocean_spd, 0.5) 

# Ocean current bearing raster
# set all current bearing to 90 (left to right)
ocean_dir <- terra::setValues(ocean_spd, 90) 


# island raster
# island corners
poly_df <- data.frame(x = c(0.5, 0.5, 0.8, 0.8),
                      y = c(0.5,0.8, 0.8, 0.5))

# island as a polygon
poly_sf <- poly_df %>% 
  sf::st_as_sf(coords = c("x","y")) %>%
  summarise(geometry = sf::st_combine(geometry)) %>%
  sf::st_cast("POLYGON") %>%
  sf::st_make_valid()

# convert polygon to raster
island_mask <- terra::rasterize(poly_sf, ocean_spd)
plot(island_mask)

# island cells = 999 and ocean cell = 1
island_mask[island_mask == 1] <- 999
island_mask[is.nan(island_mask)] <- 1

# remove island from current and bearing rasters
ocean_spd[island_mask == 999] <- NA
ocean_dir[ocean_dir == 999] <- NA

# convert rast objects to raster - needed by transition()
island_mask <- raster(island_mask)
ocean_spd <- raster(ocean_spd)
ocean_dir <- raster(ocean_dir)

Calculate transition layer - this is where I am stuck, it is not clear to me how I integrate the island raster, the ocean speed raster and the ocean current bearing raster to get the transition object.

trans_obj <- transition(island_mask, transitionFunction = ?, 16, symm = FALSE)

From here I would then use the transition object (trans_obj) to calculate shortest path between two points on the island - via the ocean

pt_dist <- gdistance::shortestPath(trans_obj,
                                 开发者_JAVA百科  poly_df[1,],
                                   poly_df[2,],
                                   output = "SpatialLines")

# get the distance
sf::st_as_sf(pt_dist) %>%
  sf::st_length()

My question:

how can I pass the island raster, the ocean speed raster and the ocean current bearing raster to transition() to generate the transition object? Or is there some other middle step that I need to do to combine my three rasters before passing them to transition()?

Any advice would be much appreciated.

0

精彩评论

暂无评论...
验证码 换一张
取 消