# This script cleans lvl2.
library(sf)
library(sp)
library(dplyr)
library(stringr)
library(ggplot2)
library(worrms)
fish <- c("Myxini", "Petromyzontida", "Hyperoartia", "Chondrichthyes", "Actinopterygii",
"Sarcopterygii")
lvl2 <- lvl2 %>%
filter(!is.na(coordinateUncertaintyInMeters) & coordinateUncertaintyInMeters <= 30, # Removes all observations with coordinate uncertainty of value NA or greater than 30.
class != "Aves", !(class %in% fish), # Filters out bird and fish observations.
!is.na(species)) # Filters NA observations.
# Creates a test matrix if lvl2 observations are within cnc_area.
# XXX Should add 30m buffer to cnc_area?
test <- lvl2 %>%
st_as_sf(coords = c("decimalLongitude", "decimalLatitude"), # Converts lvl2 to sf object with same crs as cnc_area.
crs = st_crs(cnc_area)) %>%
st_within(y = cnc_area, sparse = FALSE) # Creates matrix of TRUE/FALSE if points are in polygon.
# Filters lvl2 according to test matrix.
cnc_pts <- lvl2[test[, 1] == 1,]
# Creates a list of the unique species in cnc_pts.
unique_species <- unique(cnc_pts$species)
#--------------------------------------------------------------------------------------------------------------------------
# XXX Functionalize, and look for a way to speed up (might not be possible to speed up, as it's calling to API with limits).
# Initialize an empty vector to hold WORRMS lookup results.
marine_animals_check <- vector(mode = 'numeric')
# For every entry in species...
for (i in 1:length(unique_species)) {
check <- NULL
tryCatch(
expr = {
worrms::wm_records_name(species[i], fuzzy=FALSE) # Uses species name to query World Registry of Marine Species for a record, and assigns response to empty list value.
message(paste("Iteration", i, "Successful"))
check <<- 1 # 1 if no error.
},
error = function(e) {
message(paste("Error caught on iteration", i, ":", e))
check <<- 0 # 0 if error, meaning not an entry in WORRMS.
},
finally = {
marine_animals_check <<- append(marine_animals_check, check)
}
)
}
# XXX It's working, but I don't trust it to work programmatically, at least as of yet.
# XXX If going to functionalize this, will need to get rid of the <<- (global assignment), and figure out how to assign within function.
#--------------------------------------------------------------------------------------------------------------------------
# Filters for marine animals.
marine_animals <- data.frame(species = unique_species, marine = marine_animals_check) %>%
filter(marine == 1)
save(marine_animals, file="data/marine-animals.rda")
# Filters lvl2 species to be species not in marine_animals.
lvl2_terra <- lvl2 %>%
filter(!(species %in% marine_animals$species))
save(lvl2_terra, file = "data/lvl2_terra.rda")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.