Skip to content

Instantly share code, notes, and snippets.

@cybernar
Last active March 14, 2023 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cybernar/9bf2e0e89d7977588fa927d4594f48f7 to your computer and use it in GitHub Desktop.
Save cybernar/9bf2e0e89d7977588fa927d4594f48f7 to your computer and use it in GitHub Desktop.
Projets UMR 2023
#library(openxlsx)
library(tidyverse)
library(readxl)
library(nominatimlite)
library(sf)
library(htmltools)
# lire fichier excel
f_xlsx <- "data/00_Ressources_propres_UMR_2021_2023_corrige.xlsx"
#tbl_proj <- readxl::read_xlsx(f_xlsx, skip=1)
typecol <- c(
A='text', B='text', C='skip', D='skip', E='skip',
F='skip', G='skip', H='guess', I='list', J='skip',
K='skip', L='skip', M='skip', N='skip', O='skip',
P='skip', Q='skip', R='skip', S='skip', T='skip',
U='skip', V='text')
nomcol <- c('nom', 'porteur', 'fin', 'site_d_etude', 'lien')
tbl_proj <- readxl::read_xlsx(
f_xlsx, sheet = 1,
col_names=nomcol, col_types=typecol, skip=2) %>%
mutate(rid=row_number(), .before=1) %>%
filter(!is.na(site_d_etude))
# nouveau tableau avec 1 ligne par site d'etude
tbl_proj2 <- tbl_proj %>%
separate_longer_delim(cols='site_d_etude', delim=',') %>%
mutate(site_d_etude=str_trim(site_d_etude))
# liste des sites d'etude
liste_lieux1 <- tbl_proj2$site_d_etude %>%
factor() %>% levels()
# préciser localisation pour noms de lieux trop imprécis
liste_lieux2 <- case_match(
liste_lieux1,
'Afrique de l’Est' ~ "Nairobi",
'Afrique de l’Ouest' ~ "Bamako",
'Amazonie' ~ "Manaus",
'Congo' ~ "Congo-Brazzaville",
'Europe' ~ "Bruxelles",
'Méditerranée française' ~ "Marseille",
'Pacifique' ~ "Kiribati",
'Territoires Britanniques de l\'Atlantique Sud' ~ "Géorgie du Sud-et-les îles Sandwich du Sud",
.default = liste_lieux1
)
liste_lieux2
# chercher lat lon dans nominatim
tbl_nomin <- nominatimlite::geo_lite(liste_lieux2)
tbl_nomin2 <- tbl_nomin %>%
add_column(liste_lieux1, .before = 'query') %>%
mutate(lat=round(lat,2), lon=round(lon,2))
tbl_nomin2 %>% View()
# enregistrer tbl en geojson
sf_nomin2 <- st_as_sf(tbl_nomin2, coords = c("lon", "lat"), crs=4326)
st_write(
sf_nomin2,
dsn = "sites_projet.geojson", layer = "sites_projet.geojson",
delete_dsn = TRUE)
# purrr
genere_li <- function(x, l) {
if(is.na(l)) {
return(tags$li(x, .noWS="outside"))
} else {
return(tags$li(tags$a(x, href=l, .noWS="outside"), .noWS="outside"))
}
}
liste_projets <- function(tbl, site) {
l_li <- purrr::map2(tbl$nom, tbl$lien, genere_li)
if (length(l_li) > 0) {
return(tagList(
strong(site),
tags$ul(l_li, .noWS="outside")
) %>% as.character()
)
} else {
return(tagList(
strong(site),
p("-")
) %>% as.character()
)
}
}
# agréger les projets par site et générer listes html
tbl_sites <- tbl_proj2 %>%
select(nom, site_d_etude, lien) %>%
group_by(site_d_etude) %>%
nest()
tbl_sites <- tbl_sites %>%
mutate(
desc_html=map2_chr(data, site_d_etude, liste_projets)
)
tbl_sites_projets <- inner_join(
tbl_sites, tbl_nomin2, by=(c("site_d_etude"="liste_lieux1"))) %>%
select(site_d_etude, desc_html, lon, lat)
# conversion geojson
sf_sites_projets <-
st_as_sf(tbl_sites_projets, coords = c("lon", "lat"), crs=4326)
st_write(
sf_sites_projets,
dsn = "sites_projets.geojson", layer = "sites_projets.geojson",
delete_dsn = TRUE)
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment