# /!\/!\/!\ à exécuter si nécessaire
# install.packages("tidyverse", type = "win.binary")

# 0 - Nettoyer son env de travail
rm(list=ls())

# Charger librairies
library(dplyr)
library(sf)
library(leaflet)
library(ggplot2)
library(ggspatial)
library(osmdata)
library(osrm)
library(tidyr)
library(readxl)

# 0 -Fixer le repertoire de travail
setwd("~/git/MASTER_1_R/data")

1 - Creation de donnees (from scratch)

# Recolte des donnees
# Parc de l'Hôtel de Ville 49.44254170087927, 1.1013442922359218
# Square Charles Verdrel 49.44491092198989, 1.0933064445697365
# Esplanade du Champ de Mars 49.43684894602264, 1.1041573107722227
# Parc de la Presqu’île Rollet 49.44214567455017, 1.0630771863412742
# Jardin des Plantes de Rouen 49.42203509935082, 1.0763288491493366

nom <- c("Parc de l'Hôtel de Ville","Square Charles Verdrel","Esplanade du Champ de Mars","Parc de la Presqu’île Rollet", "Jardin des Plantes de Rouen")
lat <- c(49.44254170087927,49.44491092198989,49.43684894602264,49.44214567455017,49.42203509935082)
lon <- c(1.1013442922359218,1.0933064445697365,1.1041573107722227,1.0630771863412742,1.0763288491493366)
id <- seq(1,5)

# Creation du dataframe
df <- data.frame(ID = id,Place = nom, X = lon, Y = lat)

# Creation du spatial dataframe
df_sf <- st_as_sf(df,
                  coords = c("X", "Y"),
                  crs = 4326)

2 - Cartographier nos points via Leaflet

# Cartographie de nos points
map1 <- leaflet() %>% 
  addProviderTiles("Esri.WorldImagery") %>% 
  addCircles(data=df_sf, color="red", radius = 100)
map1

3 - Requeter OSM et télécharger des données vectorielles

db_rouen <- getbb("Rouen") %>% 
  opq() %>% 
  add_osm_feature(key="amenity", value="restaurant")
  
restaurant <- osmdata_sf(db_rouen)
restaurant_point <- restaurant$osm_points

db_rouen <- getbb("Rouen") %>% 
  opq() %>% 
  add_osm_feature(key="amenity", value="pub")

pub <- osmdata_sf(db_rouen)
pub_point <- pub$osm_points

4 - Afficher les résultats de votre requêtes

leaflet() %>% 
  addProviderTiles("Esri.WorldImagery") %>% 
  addCircles(data=restaurant_point, color="red", radius = 3) %>% 
  addCircles(data=pub_point, color="green", radius = 3)

5 - Exporter les données

st_write(pub_point, "~/git/MASTER_1_R/data/PUB_ROUEN_POINT_WGS84.shp", delete_dsn=T)
Deleting source `/home/gilleol2/git/MASTER_1_R/data/PUB_ROUEN_POINT_WGS84.shp' using driver `ESRI Shapefile'
Writing layer `PUB_ROUEN_POINT_WGS84' to data source 
  `/home/gilleol2/git/MASTER_1_R/data/PUB_ROUEN_POINT_WGS84.shp' using driver `ESRI Shapefile'
Writing 26 features with 28 fields and geometry type Point.
st_write(restaurant_point, "~/git/MASTER_1_R/data/RESTAURANT_ROUEN_POINT_WGS84.shp", delete_dsn=T)
Deleting source `/home/gilleol2/git/MASTER_1_R/data/RESTAURANT_ROUEN_POINT_WGS84.shp' using driver `ESRI Shapefile'
Writing layer `RESTAURANT_ROUEN_POINT_WGS84' to data source 
  `/home/gilleol2/git/MASTER_1_R/data/RESTAURANT_ROUEN_POINT_WGS84.shp' using driver `ESRI Shapefile'
Writing 447 features with 75 fields and geometry type Point.