Skip to content

Instantly share code, notes, and snippets.

@datagistips
Created February 1, 2023 18:05
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/799ca3e4c4af32dfd97dc8a2b4959674 to your computer and use it in GitHub Desktop.
Save datagistips/799ca3e4c4af32dfd97dc8a2b4959674 to your computer and use it in GitHub Desktop.
Utilisation de l'API IGN pour récupérer une route depuis un point A à un point B
start <- "2.337306%2C48.849319"
end <- "2.367776%2C48.852891"
library(jsonlite)
get_route <- function(start, end) {
url <- glue("https://wxs.ign.fr/calcul/geoportail/itineraire/rest/1.0.0/route?resource=bdtopo-osrm&start={start}&end={end}&profile=car&optimization=fastest&constraints=%7B%22constraintType%22%3A%22banned%22%2C%22key%22%3A%22wayType%22%2C%22operator%22%3A%22%3D%22%2C%22value%22%3A%22autoroute%22%7D&getSteps=true&getBbox=true&distanceUnit=kilometer&timeUnit=hour&crs=EPSG%3A4326")
j <- read_json(url)
coords <- j$geometry$coordinates
out <- lapply(coords, function(elt) {
long <- elt[[1]]
lat <- elt[[2]]
cbind(long, lat)
})
res <- do.call(rbind, out)
g <- st_linestring(res)
return(g)
}
get_route(start, end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment