library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.3
## -- Attaching packages ------------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.0     v purrr   0.3.3
## v tibble  3.0.0     v dplyr   0.8.5
## v tidyr   1.0.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.4.0
## Warning: package 'ggplot2' was built under R version 3.6.3
## Warning: package 'tibble' was built under R version 3.6.3
## Warning: package 'tidyr' was built under R version 3.6.3
## Warning: package 'dplyr' 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(ggplot2)
library(DT)
## Warning: package 'DT' was built under R version 3.6.3
available_tags("highway")
##  [1] "bridleway"              "bus_guideway"           "bus_stop"              
##  [4] "construction"           "corridor"               "crossing"              
##  [7] "cycleway"               "elevator"               "emergency_access_point"
## [10] "emergency_bay"          "escape"                 "footway"               
## [13] "give_way"               "living_street"          "milestone"             
## [16] "mini_roundabout"        "motorway"               "motorway_junction"     
## [19] "motorway_link"          "passing_place"          "path"                  
## [22] "pedestrian"             "platform"               "primary"               
## [25] "primary_link"           "proposed"               "raceway"               
## [28] "residential"            "rest_area"              "road"                  
## [31] "secondary"              "secondary_link"         "service"               
## [34] "services"               "speed_camera"           "steps"                 
## [37] "stop"                   "street_lamp"            "tertiary"              
## [40] "tertiary_link"          "toll_gantry"            "track"                 
## [43] "traffic_mirror"         "traffic_signals"        "trailhead"             
## [46] "trunk"                  "trunk_link"             "turning_circle"        
## [49] "turning_loop"           "unclassified"
available_features()
##   [1] "4wd_only"                "abandoned"              
##   [3] "abutters"                "access"                 
##   [5] "addr"                    "addr:city"              
##   [7] "addr:conscriptionnumber" "addr:country"           
##   [9] "addr:district"           "addr:flats"             
##  [11] "addr:full"               "addr:hamlet"            
##  [13] "addr:housename"          "addr:housenumber"       
##  [15] "addr:inclusion"          "addr:interpolation"     
##  [17] "addr:place"              "addr:postcode"          
##  [19] "addr:province"           "addr:state"             
##  [21] "addr:street"             "addr:subdistrict"       
##  [23] "addr:suburb"             "admin_level"            
##  [25] "aeroway"                 "agricultural"           
##  [27] "alt_name"                "amenity"                
##  [29] "area"                    "atv"                    
##  [31] "backward"                "barrier"                
##  [33] "basin"                   "bdouble"                
##  [35] "bicycle"                 "bicycle_road"           
##  [37] "biergarten"              "boat"                   
##  [39] "border_type"             "boundary"               
##  [41] "bridge"                  "building"               
##  [43] "building:fireproof"      "building:flats"         
##  [45] "building:levels"         "building:min_level"     
##  [47] "building:soft_storey"    "bus_bay"                
##  [49] "busway"                  "charge"                 
##  [51] "construction"            "covered"                
##  [53] "craft"                   "crossing"               
##  [55] "crossing:island"         "cuisine"                
##  [57] "cutting"                 "cycleway"               
##  [59] "denomination"            "destination"            
##  [61] "diet"                    "direction"              
##  [63] "dispensing"              "disused"                
##  [65] "disused:shop"            "drink"                  
##  [67] "drive_in"                "drive_through"          
##  [69] "ele"                     "electric_bicycle"       
##  [71] "electrified"             "embankment"             
##  [73] "embedded_rails"          "emergency"              
##  [75] "end_date"                "entrance"               
##  [77] "est_width"               "fee"                    
##  [79] "fire_object:type"        "fire_operator"          
##  [81] "fire_rank"               "foot"                   
##  [83] "footway"                 "ford"                   
##  [85] "forestry"                "forward"                
##  [87] "frequency"               "fuel"                   
##  [89] "gauge"                   "golf_cart"              
##  [91] "goods"                   "hazmat"                 
##  [93] "healthcare"              "healthcare:counselling" 
##  [95] "healthcare:speciality"   "height"                 
##  [97] "hgv"                     "highway"                
##  [99] "historic"                "horse"                  
## [101] "ice_road"                "incline"                
## [103] "industrial"              "inline_skates"          
## [105] "inscription"             "internet_access"        
## [107] "is_in:city"              "is_in:country"          
## [109] "junction"                "kerb"                   
## [111] "landuse"                 "lanes"                  
## [113] "lanes:bus"               "lanes:psv"              
## [115] "layer"                   "leaf_cycle"             
## [117] "leaf_type"               "leisure"                
## [119] "lhv"                     "lit"                    
## [121] "location"                "man_made"               
## [123] "maxaxleload"             "maxheight"              
## [125] "maxlength"               "maxspeed"               
## [127] "maxstay"                 "maxweight"              
## [129] "maxwidth"                "military"               
## [131] "minspeed"                "mofa"                   
## [133] "moped"                   "motor_vehicle"          
## [135] "motorboat"               "motorcar"               
## [137] "motorcycle"              "motorroad"              
## [139] "mountain_pass"           "mtb:description"        
## [141] "mtb:scale:imba"          "mtb_scale"              
## [143] "name"                    "narrow"                 
## [145] "natural"                 "noexit"                 
## [147] "non_existent_levels"     "note"                   
## [149] "nudism"                  "office"                 
## [151] "official_name"           "old_name"               
## [153] "oneway"                  "opening_hours"          
## [155] "operator"                "organic"                
## [157] "oven"                    "overtaking"             
## [159] "parking:condition"       "parking:lane"           
## [161] "passing_places"          "place"                  
## [163] "power"                   "priority_road"          
## [165] "produce"                 "proposed"               
## [167] "protected_area"          "psv"                    
## [169] "public_transport"        "railway"                
## [171] "railway:preserved"       "railway:track_ref"      
## [173] "recycling_type"          "ref"                    
## [175] "religion"                "residential"            
## [177] "resource"                "roadtrain"              
## [179] "route"                   "sac_scale"              
## [181] "service"                 "service_times"          
## [183] "shelter_type"            "shop"                   
## [185] "sidewalk"                "site"                   
## [187] "ski"                     "smoothness"             
## [189] "social_facility"         "speed_pedelec"          
## [191] "start_date"              "step_count"             
## [193] "substation"              "surface"                
## [195] "tactile_paving"          "tank"                   
## [197] "tidal"                   "toilets:wheelchair"     
## [199] "toll"                    "tourism"                
## [201] "tracks"                  "tracktype"              
## [203] "traffic_calming"         "traffic_sign"           
## [205] "trail_visibility"        "tunnel"                 
## [207] "turn"                    "type"                   
## [209] "usage"                   "vehicle"                
## [211] "vending"                 "voltage"                
## [213] "water"                   "wheelchair"             
## [215] "wholesale"               "width"                  
## [217] "winter_road"             "wood"
getbb("Porto Alegre Brazil")
##         min       max
## x -51.30344 -51.01885
## y -30.26945 -29.93247
limites = as.data.frame(getbb("Porto Alegre Brazil"))
vias_primarias <- getbb("Porto Alegre Brazil")%>%
  opq()%>%
  add_osm_feature(key = "highway", 
                  value = c("motorway", "primary", 
                            "secondary", "tertiary")) %>%
  osmdata_sf()
vias_primarias
## Object of class 'osmdata' with:
##                  $bbox : -30.2694499,-51.3034404,-29.9324744,-51.0188522
##         $overpass_call : The call submitted to the overpass API
##                  $meta : metadata including timestamp and version numbers
##            $osm_points : 'sf' Simple Features Collection with 26946 points
##             $osm_lines : 'sf' Simple Features Collection with 7239 linestrings
##          $osm_polygons : 'sf' Simple Features Collection with 14 polygons
##        $osm_multilines : NULL
##     $osm_multipolygons : NULL
df = as.data.frame(vias_primarias$osm_lines)
DT::datatable(df[1:50, ], filter = 'top', options = list(
  language = list(url = '//cdn.datatables.net/plug-ins/1.10.11/i18n/Portuguese.json'),
  pageLength = 8, autoWidth = TRUE
))
library(sf)
## Warning: package 'sf' was built under R version 3.6.3
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(mapview)
plot(vias_primarias$osm_lines)
## Warning: plotting the first 10 out of 105 attributes; use max.plot = 105 to plot
## all

plot(vias_primarias$osm_lines[, 7])

plot(vias_primarias$osm_lines[, 2])

plot(vias_primarias$osm_lines[, "lanes"])

plot(vias_primarias$osm_lines[, "maxspeed"])

plot(vias_primarias$osm_lines[, "highway"])

vias_secundarias <- getbb("Porto Alegre Brazil")%>%
  opq()%>%
  add_osm_feature(key = "highway", 
                  value = c("residential", "living_street",
                            "unclassified",
                            "service", "footway")) %>%
  osmdata_sf()
df = as.data.frame(vias_secundarias$osm_lines)
DT::datatable(df[1:50, ], filter = 'top', options = list(
  language = list(url = '//cdn.datatables.net/plug-ins/1.10.11/i18n/Portuguese.json'),
  pageLength = 8, autoWidth = TRUE
))
plot(vias_secundarias$osm_lines[, 44])

agua <- getbb("Porto Alegre Brazil")%>%
  opq()%>%
  add_osm_feature(key = "waterway", value = "river") %>%
  osmdata_sf()
ggplot() +
  geom_sf(data = vias_primarias$osm_lines,
          inherit.aes = FALSE,
          color = "black",
          size = .4,
          alpha = .8) +
  coord_sf(xlim = c(-51.30344, -51.01885), 
           ylim = c(-30.26945, -29.92247),
           expand = FALSE) 

ggplot() +
  geom_sf(data = vias_primarias$osm_lines,
          inherit.aes = FALSE,
          color = "black",
          size = .4,
          alpha = .8) +
  geom_sf(data = vias_secundarias$osm_lines, inherit.aes = FALSE,
          color = "black", size = .4, alpha = .6) +  
  geom_sf(data = agua$osm_lines, inherit.aes = FALSE,  color = "black", size = .2,  alpha = .5) +  
  coord_sf(xlim = c(limites[1,1], limites[1,2]),  
           ylim = c(limites[2,1], limites[2,2]), 
           expand = FALSE) 

ggplot() +
  geom_sf(data = vias_primarias$osm_lines,
          inherit.aes = FALSE,
          color = "steelblue",          size = .4,
          alpha = .8) +
  geom_sf(data = vias_secundarias$osm_lines,
          inherit.aes = FALSE,
          color = "black",
          size = .4,
          alpha = .6) +
  geom_sf(data = agua$osm_lines,
          inherit.aes = FALSE,
          color = "black",
          size = .2,
          alpha = .5) +
  coord_sf(xlim = c(limites[1,1], limites[1,2]),  
           ylim = c(limites[2,1], limites[2,2]), 
           expand = FALSE) 

ggplot() +
  geom_sf(data = vias_primarias$osm_lines,
          inherit.aes = FALSE,
          color = "steelblue",
          size = .4,
          alpha = .8) +
  geom_sf(data = vias_secundarias$osm_lines,
          inherit.aes = FALSE,
          color = "black",
          size = .4,
          alpha = .6) +
  geom_sf(data = agua$osm_lines,
          inherit.aes = FALSE,
          color = "black",
          size = .2,
          alpha = .5) +
  coord_sf(xlim = c(limites[1,1], limites[1,2]),  
           ylim = c(limites[2,1], limites[2,2]), 
           expand = FALSE)  +
  theme_void()

ggplot() +
  geom_sf(data = vias_primarias$osm_lines,
          inherit.aes = FALSE,
          color = "#7fc0ff",          size = .4,
          alpha = .8) +
  geom_sf(data = vias_secundarias$osm_lines,
          inherit.aes = FALSE,
          color = "#ffbe7f",          size = .2,
          alpha = .6) +
  geom_sf(data = agua$osm_lines,
          inherit.aes = FALSE,
          color = "#ffbe7f",          size = .2,
          alpha = .5) +
  coord_sf(xlim = c(limites[1,1], limites[1,2]),  
           ylim = c(limites[2,1], limites[2,2]), 
           expand = FALSE)  +
  theme_void() +
  theme(    plot.background = element_rect(fill = "#282828")  )

ggsave("map.png", width = 6, height = 6)