Skip to content

Instantly share code, notes, and snippets.

@datagistips
Last active September 3, 2021 10:01
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 datagistips/eb78b461bbd3b97505695851ef3d960e to your computer and use it in GitHub Desktop.
Save datagistips/eb78b461bbd3b97505695851ef3d960e to your computer and use it in GitHub Desktop.
Récupère le contour d'une commune grâce à geo.api.gouv.fr
library(jsonlite)
library(sf)
library(leaflet)
library(glue)
library(magrittr)
getContourCommune <- function(code_insee) {
# geo.api.gouv.fr arguments
# geometry = contour pour avoir le contour
# fields = geometry car seule la géométrie nous intéresse
url <- glue("https://geo.api.gouv.fr/communes/{code_insee}?format=geojson&geometry=contour&fields=geometry")
coords <- fromJSON(url)$geometry$coordinates[1,,]
geometry <- st_polygon(list(coords)) %>% st_sfc %>% st_set_crs(4326)
geometry
}
# Commune d'Aix-en-Provence
geom <- getContourCommune(code_insee = "13001")
bb <- st_bbox(geom) %>% as.numeric
# Carte Leaflet
leaflet() %>%
addTiles() %>%
fitBounds(bb[1], bb[2], bb[3], bb[4]) %>% # centrée sur la commune
addPolygons(data = geom) # avec le contour de la commune
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment