Last active
September 3, 2021 10:01
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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