knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)
library(tableauchangementstemporels)
# later library(mapselector)
library(tidyverse)

dd <- tableauchangementstemporels::inatQC %>% head(50)

dd_sf <- dd %>% 
  sf::st_as_sf(coords = c("longitude", "latitude")) %>% # set coordinates
  sf::st_set_crs(value = "+proj=longlat +datum=WGS84 +no_defs")

dd_joined <- dd_sf %>% sf::st_join(mapselector::CERQ["NOM_PROV_N"])

dd_joined %>% 
  as.data.frame %>% 
  count(region, NOM_PROV_N)

OK now try for the whole thing:

# Voila the code that could be adapted into a function within mapselector.. assuming that it isn't very wrong.
inat_NOM_PROV_N <- tableauchangementstemporels::inatQC %>% 
  # needs to have a long and lat column 
  sf::st_as_sf(coords = c("longitude", "latitude")) %>% # set coordinates
  # this CRS could be set in some general place in mapselector and then modified only once.
  sf::st_set_crs(value = "+proj=longlat +datum=WGS84 +no_defs") %>% 
  sf::st_join(mapselector::CERQ["NOM_PROV_N"])


inat_NOM_PROV_N %>% 
  as.data.frame %>% 
  count(region, NOM_PROV_N) %>% 
  knitr::kable(.)

does it in fact work?

There is a warning / message that comes with this approach, generated by sf::st_join. This is catalogued in this issue which links to this article and this one, both from the official documentation from r-spatial.

Now try to put this as a function, to be added to mapselector:

add_region <- function(data_of_obs){
  assertthat::assert_that(
    assertthat::has_name(data_of_obs, c("longitude", "latitude"))
  )
  data_of_obs %>% 
    # needs to have a long and lat column 
    sf::st_as_sf(coords = c("longitude", "latitude")) %>% # set coordinates
    # this CRS could be set in some general place in mapselector and then modified only once.
    sf::st_set_crs(value = "+proj=longlat +datum=WGS84 +no_defs") %>% 
    sf::st_join(mapselector::CERQ["NOM_PROV_N"])
}

tableauchangementstemporels::inatQC %>% 
  add_region %>% 
  head(5) %>% 
  knitr::kable(.)


ReseauBiodiversiteQuebec/tableauchangementstemporels documentation built on April 29, 2022, 4:02 p.m.