osm_keyValueDL: Easily Download Open Street Map Data

View source: R/OSMhelpers.R

osm_keyValueDLR Documentation

Easily Download Open Street Map Data

Description

[Experimental]

Downloads multiple vector datasets from Open Street Maps given a dataframe of key-value pairs and a bounding box.

Usage

osm_keyValueDL(keyValDF, bbox)

Arguments

keyValDF

Dataframe with two columns, 'key' and 'value'. Can be created with osm_keyValueDF()

bbox

the bounding box for the download area

Value

A list with class osmdata containing the spatial data within its elements.

Creation notes

First created in 2020-11-03 with the script OSMdownloadAndSFtoSQLInterface.R in the MARC-KC/HelpLibrary Repository

Author(s)

Jacob Peterson

Examples

## Not run: 
library(sf)
# devtools::install_github('yonghah/esri2sf')
library(esri2sf)
library(dplyr)
library(osmdata)


#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Download MARC Boundaries ####
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MARCprojection <- paste0(
"+proj=tmerc +lat_0=36.16666666666666 +lon_0=-94.5 +k=0.9999411764705882 ", 
"+x_0=850000 +y_0=0 +ellps=GRS80 +datum=NAD83 +to_meter=0.3048006096012192 ", 
"+no_defs")


#For fixing column names form the table joins
removeColnamePrefix <- function(df) {
  names(df) <- sub('.*\\.', '', names(df)) %>%
    ave(., ., FUN = function(i) paste0(i, '_', seq_along(i))) %>%
    gsub("_1$", "", .)
  df
}

MARCjurisdictions <- esri2sf::esri2sf(url = 
"https://gis2.marc2.org/arcgis/rest/services/HumanServices/COVIDv2/MapServer/0") %>%
  sf::st_make_valid() %>% #fixes some issues with data coming from ESRI
  sf::st_transform(MARCprojection) %>%#puts it in MARC cordinates
  removeColnamePrefix() #For fixing column names form the table joins
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Create Bounding Box ####
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bb_sf <- MARCjurisdictions %>% sf::st_transform(crs = 4326) %>% sf::st_bbox()
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Download Data for multiple Key Value Sets ####
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
df <- tibble::tribble(
  ~key,            ~value,
  "highway",       "motorway",
  "highway",       "trunk",
  "highway",       "primary",
)


#download data
test <- osm_keyValueDL(df, bb_sf)

#simple plot
test$osm_lines %>% sf::st_geometry() %>% plot()
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

## End(Not run)

MARC-KC/marcR documentation built on June 2, 2022, 9:31 p.m.