Data Sources

Load and Simplify EEZ

library(tidyverse)
library(sf)
library(rmapshaper) # bash prep: sudo apt-get update; sudo apt-get install libv8-dev
library(RPostgreSQL)
library(geojsonio)
library(DT)

# set working dir relative to this rmarkdown file
if (basename(getwd())=='sdg14') setwd('technical')

# setup ~/mbon_data_big: cd ~; ln -s /mbon/data_big mbon_data_big
eez_shp         = '~/mbon_data_big/technical/boundaries/eez/eez.shp'
eez_s005_shp    = '~/mbon_data_big/technical/boundaries/eez_derived/eez_s005.shp'
esp_s005_shp    = '~/mbon_data_big/technical/boundaries/eez_derived/esp_s005.shp'
esp_ck_csv      = '~/mbon_data_big/technical/boundaries/eez_derived/esp_ck.csv'

if (!file.exists(eez_s005_shp)){
  eez = st_read(eez_shp)
  
  # simplify eez
  eez_s005 = eez %>%
    geojson_json() %>% # convert to geojson for faster ms_simplify; 69.6 sec
    ms_simplify(keep=0.05, keep_shapes=T, explode=F) %>% # simplify; 11.9 minutes
    geojson_sp() %>% st_as_sf() # convert back to simple features
  
  # write to shapefile
  st_write(eez_s005, eez_s005_shp)
} else {
  eez_s005 = st_read(eez_s005_shp)
}
## Reading layer `eez_s005' from data source `/mbon/data_big/technical/boundaries/eez_derived/eez_s005.shp' using driver `ESRI Shapefile'
## converted into: MULTIPOLYGON
## Simple feature collection with 281 features and 24 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -180 ymin: -85.5625 xmax: 180 ymax: 86.99373
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
# plot of EEZ by area
#plot(eez_s005['Area_km2']) # SLOW!

# show eez table
eez_s005 %>%
  select(-geometry) %>%
  datatable()
# show disputed & joint regimes vs 200 nm
table(eez_s005$Pol_type)
## 
##        200NM     Disputed Joint regime 
##          230           31           20

Extract OBIS from Spanish Regions

library(robis) # devtools::install_github('iobis/robis')

if (!all(file.exists(esp_s005_shp), file.exists(esp_ck_csv))){
  # filter for all Spanish claims
  esp = eez_s005 %>%
    filter(Sovereign1 == 'Spain' | Sovereign2 == 'Spain' | Sovereign3 == 'Spain')
  
  # iterate over all Spanish regions
  for (i in 1:nrow(esp)){ # i = 1
    cat(sprintf('%03d: %s\n', i, esp$GeoName[i]))
    geom = st_as_text(sf::st_geometry(esp))[i]
    
    # fetch OBIS data for geometry
    ck = checklist(geometry = geom)
    
    # add MRGID and merge with others
    if (nrow(ck) > 0 ){
      ck$MRGID = esp$MRGID[i]
      if (i == 1){
        esp_ck = ck
      } else {
        esp_ck = esp_ck %>%
          bind_rows(ck)
      }
    }
  }
  
  # calculate species richness per region
  esp = esp %>%
    left_join(
      esp_ck %>%
      group_by(MRGID) %>%
      filter(rank_name == 'Species') %>%
      summarize(
        n_spp = n()),
      by='MRGID') %>%
    mutate(
      n_spp = ifelse(is.na(n_spp), 0, n_spp),
      n_spp_log = log10(n_spp + 1))
  
  # write shp, csv
  st_write(esp, esp_s005_shp)
  write_csv(esp_ck, esp_ck_csv)
  
} else {
  
  # read shp, csv
  esp    = st_read(esp_s005_shp)
  esp_ck = read_csv(esp_ck_csv)
}
## Reading layer `esp_s005' from data source `/mbon/data_big/technical/boundaries/eez_derived/esp_s005.shp' using driver `ESRI Shapefile'
## converted into: MULTIPOLYGON
## Simple feature collection with 10 features and 26 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -21.92006 ymin: 24.58474 xmax: 6.3 ymax: 46.874
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
## Parsed with column specification:
## cols(
##   .default = col_character(),
##   id = col_integer(),
##   valid_id = col_integer(),
##   parent_id = col_integer(),
##   worms_id = col_integer(),
##   records = col_integer(),
##   datasets = col_integer(),
##   redlist = col_logical(),
##   hab = col_logical(),
##   gisd = col_logical(),
##   MRGID = col_integer()
## )
## See spec(...) for full column specifications.
# show esp table
esp %>%
  select(MRGID, GeoName, n_spp) %>%
  datatable()

Map Species Richness in Spanish Regions

library(leaflet)
library(htmltools)

esp    = st_read(esp_s005_shp)
## Reading layer `esp_s005' from data source `/mbon/data_big/technical/boundaries/eez_derived/esp_s005.shp' using driver `ESRI Shapefile'
## converted into: MULTIPOLYGON
## Simple feature collection with 10 features and 26 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -21.92006 ymin: 24.58474 xmax: 6.3 ymax: 46.874
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
# setup color palette
pal = colorNumeric(
  'Spectral',   # RColorBrewer::display.brewer.all()
  esp$n_spp_log, na.color = 'transparent')

# interactive map
leaflet(esp) %>% 
  addProviderTiles("Stamen.TonerLite") %>%
  addPolygons(
    group = 'regions',
    label = ~mapply(function(n, v) {
      HTML(sprintf("<em>%s:</em> %s", htmlEscape(n), htmlEscape(v)))},
      GeoName, n_spp, SIMPLIFY = F),
    stroke = TRUE, opacity=0.5, weight=2, fillOpacity = 0.5, smoothFactor = 0.5,
    color=~pal(n_spp_log)) %>%
  addLegend(
    "bottomright", pal = pal, opacity = 0.5,
    values = ~n_spp_log, title = '# species (log10)')

Map Spain & Neighbor EEZs

# # get Spain's neighborhood
# nbr = eez_s005 %>%
#   filter(
#     Sovereign1 == 'Spain' | Sovereign2 == 'Spain' | Sovereign3 == 'Spain' |
#     )
#