##Title: DWV - OSM Data.R
#Author: Jack Reilly
#Purpose: Street Maps with OSM
#Requires: no external data; must install osmdata within script
#Output: map PDFs, saved to ~/desktop/working/dwv26

#And ggplot2tor:
#https://ggplot2tutor.com/tutorials/streetmaps

library(remotes)
#remotes::install_github("ropensci/osmdata")

library(tidyverse)
library(osmdata) # package for working with streets
library(showtext) # for custom fonts
library(ggmap)
#library(rvest) #not needed here

#Set the working directory
setwd("~/desktop/working/dwv26")

#you can find all the available features of OSM here:
#https://wiki.openstreetmap.org/wiki/Map_features
#or here (can be slow, uncomment to see):
#available_features()

#Also use openstreemap.org to inspect features

available_tags("highway")

#getbb = "Get Bounding Box"
syrbounds<-getbb("Syracuse New York")
syrbounds

#Now we're going to pipe commands together to build our map
#Essentially: get our bounding box, <pass>, build an openstreemap query (opq), <pass>, 
#add features, <pass>, return query as formatted SF object

syr_big_streets <- syrbounds %>%
  opq() %>%
  add_osm_feature(key = "highway",
                  value = c("motorway", "trunk", "primary", "motorway_link", "trunk_link", "primary_link")) %>%
  osmdata_sf()

#Now let's see what we have!
syr_big_streets

#Remember - the core components of graphing.  Points, Lines, Polygons (and iterations on those)
# osm_lines, osm_points, osm_polygons

#Time to do some plotting with our old friend, GGPlot
#geom_sf - plot "simple feature" objects (the kind used in GIS)

ggplot() +
  geom_sf(data = syr_big_streets$osm_lines,
          inherit.aes = FALSE,
          color = "black",
          size = 1)

#Can play with aesthetics, like always

ggplot() +
  geom_sf(data = syr_big_streets$osm_lines,
          inherit.aes = FALSE,
          color = "purple",
          size = 8)

#Lets get some more data
syr_med_streets <- syrbounds%>%
  opq()%>%
  add_osm_feature(key = "highway", 
                  value = c("secondary", "tertiary", "secondary_link", "tertiary_link")) %>%
  osmdata_sf()


syr_small_streets <- syrbounds%>%
  opq()%>%
  add_osm_feature(key = "highway", 
                  value = c("residential", "living_street",
                            "unclassified",
                            "service", "footway")) %>%
  osmdata_sf()

syr_coast <- syrbounds%>%
  opq()%>%
  add_osm_feature(key = "natural", value="coastline") %>%
  osmdata_sf()

syr_water <- syrbounds%>%
  opq()%>%
  add_osm_feature(key = "natural", value="water") %>%
  osmdata_sf()

syr_railway <- syrbounds%>%
  opq()%>%
  add_osm_feature(key = "railway", value="rail") %>%
  osmdata_sf()

#Let's layer all these together

ggplot() +
  geom_sf(data = syr_coast$osm_polygons,
          inherit.aes = FALSE,
          color = "steelblue",
          size = 1) +
  geom_sf(data = syr_coast$osm_lines,
          inherit.aes = FALSE,
          color = "steelblue",
          size = 1) +
  geom_sf(data = syr_railway$osm_lines,
          inherit.aes = FALSE,
          color = "black",
          size = .2,
          linetype="dotdash",
          alpha = .5) +
  geom_sf(data = syr_med_streets$osm_lines,
          inherit.aes = FALSE,
          color = "black",
          size = .3,
          alpha = .5) +
  geom_sf(data = syr_small_streets$osm_lines,
          inherit.aes = FALSE,
          color = "#666666",
          size = .2,
          alpha = .3) +
  geom_sf(data = syr_big_streets$osm_lines,
          inherit.aes = FALSE,
          color = "black",
          size = .5,
          alpha = .6) +
  coord_sf(xlim = syrbounds[1,], 
           ylim = syrbounds[2,],
           expand = FALSE)

#Very swanky.  How about a title and getting rid of that chartjunk?
#Add a fancy looking font

font_add_google(name = "Lato", family = "lato") # add custom fonts
showtext_auto()

ggplot() +
  geom_sf(data = syr_coast$osm_polygons,
          inherit.aes = FALSE,
          color = "steelblue",
          size = .3,
          fill = "white") +
  geom_sf(data = syr_coast$osm_lines,
          inherit.aes = FALSE,
          color = "steelblue",
          size = .3) +
  geom_sf(data = syr_water$osm_polygons,
          inherit.aes = FALSE,
          color = "steelblue",
          size = .15) +
  geom_sf(data = syr_railway$osm_lines,
          inherit.aes = FALSE,
          color = "red",
          size = .2,
          linetype="dotdash",
          alpha = .9) +
  geom_sf(data = syr_med_streets$osm_lines,
          inherit.aes = FALSE,
          color = "black",
          size = .3,
          alpha = .5) +
  geom_sf(data = syr_small_streets$osm_lines,
          inherit.aes = FALSE,
          color = "#666666",
          size = .2,
          alpha = .3) +
  geom_sf(data = syr_big_streets$osm_lines,
          inherit.aes = FALSE,
          color = "black",
          size = .5,
          alpha = .6) +
  coord_sf(xlim = syrbounds[1,], 
           ylim = syrbounds[2,],
           expand = FALSE) +
  theme_void() + # get rid of background color, grid lines, etc.
  theme(plot.title = element_text(size = 20, family = "lato", face="bold", hjust=.5),
        plot.subtitle = element_text(family = "lato", size = 8, hjust=.5, margin=margin(2, 0, 5, 0))) +
  labs(title = "SYRACUSE", subtitle = "43.0481° N / 76.1474° W")

ggsave(file="Syracuse.pdf", units="in", width=6, height=7)

#Let's make this a map *of* something!


syr_fire <- syrbounds%>%
  opq()%>%
  add_osm_feature(key = "amenity", value=c("fire_station")) %>%
  osmdata_sf()

syr_fire

ggplot() +
  geom_sf(data = syr_coast$osm_polygons,
          inherit.aes = FALSE,
          color = "steelblue",
          size = .3,
          fill = "white") +
  geom_sf(data = syr_coast$osm_lines,
          inherit.aes = FALSE,
          color = "steelblue",
          size = .3) +
  geom_sf(data = syr_water$osm_polygons,
          inherit.aes = FALSE,
          color = "steelblue",
          size = .15) +
  geom_sf(data = syr_railway$osm_lines,
          inherit.aes = FALSE,
          color = "red",
          size = .2,
          linetype="dotdash",
          alpha = .9) +
  geom_sf(data = syr_med_streets$osm_lines,
          inherit.aes = FALSE,
          color = "black",
          size = .3,
          alpha = .5) +
  geom_sf(data = syr_small_streets$osm_lines,
          inherit.aes = FALSE,
          color = "#666666",
          size = .2,
          alpha = .3) +
  geom_sf(data = syr_big_streets$osm_lines,
          inherit.aes = FALSE,
          color = "black",
          size = .5,
          alpha = .6) +
  geom_sf(data = syr_fire$osm_points,
          inherit.aes = FALSE,
          color = "red4",
          size = 5,
          alpha = .6) +
  coord_sf(xlim = syrbounds[1,], 
           ylim = syrbounds[2,],
           expand = FALSE) +
  theme_void() + # get rid of background color, grid lines, etc.
  theme(plot.title = element_text(size = 20, family = "lato", face="bold", hjust=.5),
        plot.subtitle = element_text(family = "lato", size = 8, hjust=.5, margin=margin(2, 0, 5, 0))) +
  labs(title = "FIRE STATIONS", subtitle = "SYRACUSE")

ggsave(file="firesyracuse.pdf", units="in", width=6, height=7)




#A Smaller View

orangebounds<-getbb("Syracuse University")

is_big_streets <- orangebounds %>%
  opq() %>%
  add_osm_feature(key = "highway",
                  value = c("motorway", "primary", "motorway_link", "primary_link", "trunk", "trunk_link")) %>%
  osmdata_sf()

is_med_streets <- orangebounds%>%
  opq()%>%
  add_osm_feature(key = "highway", 
                  value = c("secondary", "tertiary", "secondary_link", "tertiary_link")) %>%
  osmdata_sf()


is_small_streets <- orangebounds%>%
  opq()%>%
  add_osm_feature(key = "highway", 
                  value = c("residential", "living_street",
                            "unclassified",
                            "service", "footway"
                  )) %>%
  osmdata_sf()


is_railway <- orangebounds %>%
  opq()%>%
  add_osm_feature(key = "railway", value="rail") %>%
  osmdata_sf()


ggplot() +
  geom_sf(data = is_railway$osm_lines,
          inherit.aes = FALSE,
          color = "red",
          size = .2,
          linetype="dotdash",
          alpha = .9) +
  geom_sf(data = is_med_streets$osm_lines,
          inherit.aes = FALSE,
          color = "black",
          size = .3,
          alpha = .5) +
  geom_sf(data = is_small_streets$osm_lines,
          inherit.aes = FALSE,
          color = "#666666",
          size = .2,
          alpha = .3) +
  geom_sf(data = is_big_streets$osm_lines,
          inherit.aes = FALSE,
          color = "black",
          size = .5,
          alpha = .6) +
  coord_sf(xlim = orangebounds[1,], 
           ylim = orangebounds[2,],
           expand = FALSE) +
  theme_void() + # get rid of background color, grid lines, etc.
  theme(plot.title = element_text(size = 20, family = "lato", face="bold", hjust=.5),
        plot.subtitle = element_text(family = "lato", size = 8, hjust=.5, margin=margin(2, 0, 5, 0))) +
  labs(title = "SYRACUSE UNIVERSITY", subtitle = "GO ORANGE")

ggsave(file="syruniv.pdf", units="in", width=6, height=7)

