knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(endemicr)
library(sf)

Get data sources

# prepare hawaii boundaries
hawaii_boundaries <- list(
  "../inst/boundary_main_islands/2010_Census_County_Boundaries.shp",
  "../inst/boundary_nwhi/Coastline_-_Northwest_Hawaiian_Islands__NWHI_.shp")

# prepare fish range as string
fish_range <- "../tests/testdata/example_fish/example_fish.shp"
bad_fish <- "../tests/testdata/problem_fish/data_0.shp"

Read and plot Hawaii

# prepare extent
hawaii_extent <- lapply(hawaii_boundaries, function(x) {
  st_as_sfc(
    st_bbox(
      st_union(st_read(x))
    )
  )
})

# bind extents and get full extent
hawaii_extent <- Reduce(c, hawaii_extent)
hawaii_extent <- st_as_sfc(
  st_bbox(hawaii_extent)
)

# plot for sanity
plot(hawaii_extent, 
     col = "aliceblue", 
     border = "white",
     main = "Does this look like the right place?"); axis(1); axis(2)

# add islands
lapply(hawaii_boundaries, function(x) {
  x <- st_read(x)
  plot(x, add = TRUE, col = "grey", max.plot = 1)
})

Look at fish range

# read in the example fish Chaetodon ornatissimus
fish_range_sf <- st_read(fish_range)

# plot
plot(fish_range_sf$geometry, 
     col = "red",
     border = NA,
     main = "The global range of this fish", xaxs = "r"); axis(1); axis(2)

How much of the global range is Hawaii?

Hawaii is defined as a 50km buffer around the Hawaii boundaries. This area is divided into the main and north-west Hawaiian islands by a line between Niihau and Nihoa. For demonstration we add a line after Laysan.

Make a buffer around Hawaii

# get a buffer around hawaii boundaries
# this is done automatically in the function check_hawaii_endemic
hawaii_buffer <- lapply(hawaii_boundaries, function(x) {
  x <- st_read(x)
  x <- st_transform(x, 2782)
  x <- st_buffer(x, 50 * 1000)
  x <- st_union(x)
  x <- st_transform(x, 4326)
})

# Reduce and join
hawaii_buffer <- Reduce(c, hawaii_buffer)

# notice the overlap
plot(hawaii_buffer,
     col = "grey90",
     main = "Notice the overlap"); axis(1); axis(2)

Demarcate the regions

# the demarcation points are function defaults for now
hawaii_regions <- demarcate_regions(
  area_of_interest = hawaii_buffer,
  region_demarcation_points = list(
    main_islands = c(-160.1, 21.8),
    nwhi = c(-161.9, 23)
))

# plot for sanity check
plot(hawaii_regions$regions, 
     reset = F,
     main = "Does this look alright?"); axis(1); axis(2)
plot(hawaii_regions$demarcation_areas,
     col = NA,
     add = T)

Get p(global range) in each area

# proportionof the global range in each area of hawaii
# this is an endemic fish
range_in_hawaii <- check_endemic_hawaii(
  hawaii_land_features = hawaii_boundaries,
  buffer_distance_km = 500, 
  species_range = bad_fish,
  region_demarcation_points = list(
    main_islands = c(-160.1, 21.8),
    nwhi = c(-161.9, 23)
))

# print this
range_in_hawaii
# check a global fish
range_in_hawaii <- check_endemic_hawaii(
  hawaii_land_features = hawaii_boundaries,
  buffer_distance_km = 500, 
  species_range = fish_range,
  region_demarcation_points = list(
    main_islands = c(-160.1, 21.8),
    nwhi = c(-161.9, 23)
))


pratikunterwegs/endemicr documentation built on April 2, 2021, 11:57 a.m.