# Load some packages: 

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.3
## -- Attaching packages ----------------------------------------------------------------------------------- tidyverse 1.3.0 --
## <U+2713> ggplot2 3.2.1     <U+2713> purrr   0.3.3
## <U+2713> tibble  3.0.3     <U+2713> dplyr   0.8.4
## <U+2713> tidyr   1.0.0     <U+2713> stringr 1.4.0
## <U+2713> readr   1.3.1     <U+2713> forcats 0.4.0
## Warning: package 'tibble' was built under R version 3.6.3
## -- Conflicts -------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(osmdata)
## Warning: package 'osmdata' was built under R version 3.6.3
## Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library(extrafont)
## Registering fonts with R
# Area that we want to collect spatial data: 

city_location <- "Ha Noi city Vietnam"

# OSM objects that we want to get spatial: 

all_street_types <- c("motorway", "primary", "secondary", "tertiary")

secondary_streets <- c("residential", "living_street", "unclassified", "service", "footway")

# Collect spatial data for OSM objects selected: 

streets <- getbb(city_location) %>%
  opq() %>%
  add_osm_feature(key = "highway",value = all_street_types) %>%
  osmdata_sf()

small_streets <- getbb(city_location) %>%
  opq() %>%
  add_osm_feature(key = "highway", value = secondary_streets) %>%
  osmdata_sf()

river <- getbb(city_location)%>%
  opq() %>%
  add_osm_feature(key = "waterway", value = "river") %>%
  osmdata_sf()

# Long and Lat limits: 

geo_spatial_limits <- getbb(city_location)

geo_spatial_limits
##         min       max
## x 105.28490 106.02007
## y  20.56452  21.38528
# Select colours and font for mapping: 

color1 <- "cyan"
color2 <- "#ffbe7f"
back_color <- "#1d1330"
my_font <- "Arial"

# Make a draft version of streetmap for Ho Chi Minh city: xlim = c(106.60, 106.75), ylim = c(10.75, 10.86)
#ha noi : , ; ,  xlim = c(105.78, 105.88), ylim = c(20.98, 21.08)

ggplot() +
  geom_sf(data = streets$osm_lines, inherit.aes = FALSE, color = color1, size = 0.4, alpha = 0.8) +
  geom_sf(data = small_streets$osm_lines, inherit.aes = FALSE, color = color2, size = 0.2, alpha = 0.6) +
  geom_sf(data = river$osm_lines, inherit.aes = FALSE, color = "blue", size = 1) +
  coord_sf(xlim = c(105.78, 105.88), ylim = c(21.00, 21.085), expand = FALSE) + 
  theme(plot.background = element_rect(fill = back_color, colour = NA),
        panel.background = element_rect(fill = back_color, colour = NA), 
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        panel.grid = element_blank()) + 
  theme(plot.title = element_text(family = my_font, size = 10, color = "white")) + 
  theme(plot.margin = unit(rep(0.5, 4), "cm")) + 
  labs(title = "R for Mapping of Ha Noi from Open Street Map Database")
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database

#ggsave("mtcars.png")