Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Création des contours communaux + arrondissements municipaux (France métropolitaine et DROM) en projection WGS 1984
library(httr)
library(sf)
library(dplyr)
library(purrr)
# API de l'IGN
api_ignadmin <- "https://wxs.ign.fr/administratif/geoportail/wfs"
url <- parse_url(api_ignadmin)
# import des contours départementaux de france métropolitaine et DROM-COM
url$query <- list(service = "wfs",
request = "GetFeature",
srsName = "EPSG:4326",
typename = "ADMINEXPRESS-COG.LATEST:departement")
req <- build_url(url)
DEP <- read_sf(req)
# liste des départements
dep <- DEP %>% st_drop_geometry() %>% pull(insee_dep)
# import des contours communaux france métropolitaine et DROM-COM
# boucle par département pour eviter la limite des 1000 entités
f_req_comm <- function(code_dep){
url$query <- list(service = "wfs",
request = "GetFeature",
srsName = "EPSG:4326",
cql_filter = paste0("insee_dep = '",code_dep,"'"),
typename = "ADMINEXPRESS-COG.LATEST:commune")
req <- build_url(url)
read_sf(req)
}
COMM <- dep %>% map(f_req_comm) %>% bind_rows()
# conversion des geometries
COMM <- st_cast(COMM, "GEOMETRYCOLLECTION") %>%
st_collection_extract("POLYGON") %>%
# fusion en multipolygones
group_by(id, insee_com, nom, nom_m,statut, insee_reg, insee_dep, insee_can, insee_arr, siren_epci, population) %>%
summarise() %>%
ungroup()
# import des contours des arrondissements municipaux de Paris / Lyon / Marseille
url$query <- list(service = "wfs",
request = "GetFeature",
srsName = "EPSG:4326",
typename = "ADMINEXPRESS-COG.LATEST:arrondissement_municipal")
req <- build_url(url)
ARM <- read_sf(req)
# conversion des geometries en polygones
ARM <- st_cast(ARM, "GEOMETRYCOLLECTION") %>%
st_collection_extract("POLYGON") %>%
# fusion en multipolygones
group_by(id, insee_com, nom, nom_m, insee_arm, population) %>%
summarise() %>%
ungroup()
# suppression des communes Paris / Lyon / Marseille
COMMARR_GEO_FRMETDROM <-
COMM %>%
filter(!insee_com %in% c("13055", "69123","75056")) %>%
mutate(type = "COM") %>%
# integration des arrondissements municipaux de Paris / Lyon / Marseille
# et récupération des infos supra-communales sur ces arrondissements
rbind.data.frame(ARM %>%
left_join(COMM %>%
st_drop_geometry() %>%
select(insee_com, insee_com, insee_can, insee_arr, insee_dep, insee_reg, siren_epci, statut),
by = "insee_com") %>%
select(-insee_com) %>%
rename(insee_com = insee_arm) %>%
mutate(type = "ARM"))
# noms de champs en majuscules
COMMARR_GEO_FRMETDROM <- COMMARR_GEO_FRMETDROM %>%
rename_at(c("id","nom","insee_com","nom", "nom_m","statut" ,"population","insee_reg", "insee_dep", "insee_can", "insee_arr","siren_epci","type", "population"),
.funs = toupper)
# suppression des fichiers intermédiaires
rm(COMM, ARM, DEP)
# export shp
st_write(COMMARR_GEO_FRMETDROM,
layer_options = "ENCODING=UTF-8",
"./COMMUNE_FRMETDROM.shp")
# export geojson
st_write(COMMARR_GEO_FRMETDROM,
layer_options = "ENCODING=UTF-8",
"./COMMUNE_FRMETDROM.geojson")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment