Skip to content

Instantly share code, notes, and snippets.

View datagistips's full-sized avatar

mathieu rajerison datagistips

View GitHub Profile
@datagistips
datagistips / routing-IGN-API.R
Created February 1, 2023 18:05
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]]
@datagistips
datagistips / mairies.R
Last active January 25, 2023 16:07
Récupérer les mairies de France avec geo.api.gouv.fr
# Auparavant, on trouvait facilement les emplacements des mairies depuis Le Répertoire Géographique des Communes
# https://geo.data.gouv.fr/fr/datasets/2d67a8d0da4ba99004a88cbe93fc0e0fceb43bb8
# La donnée n'est plus disponible
# Mais l'API geo.api.gouv.fr permet de retrouver l'emplacement des mairies.
# Cependant, il y a des soucis avec la requête https://geo.api.gouv.fr/communes?geometry=mairie&format=geojson
# qui retourne 'Bad request'
# On utilise ici une itération le long des différents départements
# On exporte le résultat en GéoPackage
library(sf)
@datagistips
datagistips / CompareCities.R
Last active January 13, 2023 20:08
Compare city sizes by putting a city onto another
library(tidyverse)
library(jsonlite)
library(glue)
library(sf)
library(leaflet)
make_url <- function(cityname) {
cityname <- URLencode(cityname)
glue("https://nominatim.openstreetmap.org/search?city={cityname}&format=geojson&polygon_geojson=1")
}
@datagistips
datagistips / banomap.R
Created January 7, 2023 17:23
A simple Shiny App to Geocode Addresses and See them on a Map
library(shiny)
library(banR) # Pour le géococage et l'appel à l'API BANO
library(leaflet) # Pour la carte interactive
# ui est la partie affichage
ui <- fluidPage(
fluidRow(column(12,
h1("Géocodage d'adresse BANO"),
textInput("adresse", label = NULL, "", placeholder = "Tapez une adresse"),
@datagistips
datagistips / birdseye.R
Created October 17, 2022 07:10
Get Birds Eye Imagery for Mount St Michel (Brittany, France)
library(magick)
library(rjson)
library(glue)
birdseye <- function(coords, key) {
# This script is an R adaptation from python # https://gist.github.com/WvanWaas/b0e60d88f22d13a9d431a53208c8ed7c
# You must first have a Bing Maps account : https://learn.microsoft.com/en-us/bingmaps/getting-started/bing-maps-dev-center-help/getting-a-bing-maps-key
# Get latlon as text
@datagistips
datagistips / TableSchema2GoogleForm.js
Last active September 26, 2022 07:11
Google Apps Script to Create a Call for Comments Google Form from a TableSchema
// 1. Go to Google Drive
// 2. New > Google Apps Script > Name it 'Auto Google Form - Schema'
// 3. Copy-Paste below code
// 4. Select 'process' function next to 'Debugging' button
// 4. Save
// 5. Press 'Execute'
function getItem(field) {
var s1 = field.name + ' ('+field.title+')';
@datagistips
datagistips / birdseye.md
Created February 1, 2022 11:28
Birds Eye notepad
library(jsonlite)
library(glue)
library(leaflet)
library(magrittr)
library(htmltools)
# Teste si la chaîne est une référence cadastrale
isParcelle <- function(s) {
grepl("^([013-9]\\d|2[AB1-9])\\d{3}(0|[A-Z])[A-Z][0-9]{4}[a-z]?$", s)
}
@datagistips
datagistips / getContourCommune.R
Last active September 3, 2021 10:01
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
library(sf)
library(raster)
library(dplyr)
library(tidyr)
waffle <- function(f, the_stats, the_res = 10000) {
## Create Raster
r <- raster(extent(f))