R/00.09_import_stations.R

#' @import dplyr
import_stations <- function(entite = NULL) {
  cat("\nLa liste et la localisation des stations (shapefile) sont téléchargées à l'adresse suivante:\nhttp://www.sandre.eaufrance.fr/atlas/srv/fre/catalog.search;jsessionid=1kndqm358nekh5fb8q9qcqbo#/metadata/daccef28-6b30-4441-a641-b04afe15e82f\n")
  
  rcs <- readr::read_delim(file = system.file("extdata",
                                       "RCS_liste.txt",
                                       package = "bnvd"),
                    delim = "\t", 
                    col_types = paste(rep("c", 5), collapse = "")) %>% 
    select(code_station = CODE_STATION,
           nom_station = NOM_STATION,
           bassin = BASSIN,
           type_national = TYPE_NATIONAL,
           her1 = HER1)
  
  st <- sf::st_read(system.file("extdata",
                      "StationMesureEauxSurface.shp",
                      package = "bnvd"),
          stringsAsFactors = FALSE, quiet = TRUE) %>%
    select(code_station = CdStationM)
  
  if (!is.null(entite)) {
    st <- sf::st_join(st, entite)
  }
  
  stations <- bind_cols(as.data.frame(st) %>% 
                          select(-geometry),
                        as.data.frame(sf::st_coordinates(st))) %>% 
    mutate(epsg = sf::st_crs(st)$epsg)
  
  left_join(x = rcs,
            y = stations,
            by = "code_station") %>% 
    mutate(departement = case_when(
      code_station %in% c("01059000", "01089000") ~ "NORD",
      code_station %in% c("02001700") ~ "BAS-RHIN",
      code_station %in% c("02099800", "02100150", "02103800") ~ "MOSELLE",
      code_station %in% c("02115950", "02124000") ~ "ARDENNES",
      code_station %in% c("05184000") ~ "HAUTE-GARONNE",
      code_station %in% c("06020100") ~ "DOUBS",
      code_station %in% c("06166720") ~ "PYRENEES-ORIENTALES",
      code_station %in% c("06999119") ~ "HAUTE-SAVOIE"
    ))
}
CedricMondy/bnvd documentation built on June 25, 2019, 5:57 p.m.