Maps

Author

Sara S

library(tidyverse) # Maps using ggplot + geom_s
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(osmdata) # Fetch map data from osmdata.org
Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library(tmap)
Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
remotes::install_github('r-tmap/tmap')
library(sf)
Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
bbox_2 <- osmdata::getbb("Jayanagar, Bangalore, India")
bbox_2
       min      max
x 77.58528 77.58929
y 12.93650 12.94369
locations <- 
  osmdata::opq(bbox = bbox_2) %>% 
  osmdata::add_osm_feature(key = "amenity", 
                           value = c("restaurant", "atm", "college")) %>% 
  osmdata_sf() %>%  # Convert to Simple Features format
  purrr::pluck("osm_points") # Pull out the data frame of interest
###
dat_buildings <-
  osmdata::opq(bbox = bbox_2) %>% 
  osmdata::add_osm_feature(key = "building") %>% 
  osmdata_sf() %>% 
  purrr::pluck("osm_polygons") 
str(locations)+
str(dat_buildings)
Classes 'sf' and 'data.frame':  0 obs. of  2 variables:
 $ osm_id  : chr 
 $ geometry:sfc_POINT of length 0 - attr(*, "sf_column")= chr "geometry"
 - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA
  ..- attr(*, "names")= chr "osm_id"
Classes 'sf' and 'data.frame':  551 obs. of  10 variables:
 $ osm_id         : chr  "309506831" "309506832" "309506833" "333080068" ...
 $ name           : chr  NA NA NA "OYO Hotel Fiesta Grand" ...
 $ addr:city      : chr  NA NA NA NA ...
 $ addr:street    : chr  NA NA NA NA ...
 $ amenity        : chr  NA NA NA NA ...
 $ building       : chr  "yes" "yes" "yes" "commercial" ...
 $ building:levels: chr  NA NA NA NA ...
 $ man_made       : chr  NA NA NA NA ...
 $ name:kn        : chr  NA NA NA NA ...
 $ geometry       :sfc_POLYGON of length 551; first list element: List of 1
  ..$ : num [1:5, 1:2] 77.6 77.6 77.6 77.6 77.6 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:5] "3148538750" "3148538751" "3148538752" "3148538753" ...
  .. .. ..$ : chr [1:2] "lon" "lat"
  ..- attr(*, "class")= chr [1:3] "XY" "POLYGON" "sfg"
 - attr(*, "sf_column")= chr "geometry"
 - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA
  ..- attr(*, "names")= chr [1:9] "osm_id" "name" "addr:city" "addr:street" ...
integer(0)
ggplot()+
  geom_sf(data= locations, fill= "gold", color=  "grey", linewidth= 0.025)

ggplot()+
  geom_sf(data= dat_buildings, fill = "purple")

##ggplot()+
 ## geom_sf(data = world)+
  ##geom_sf(data = india)+
  ##geom_sf(data = points_sf, colour= "red", size = 4)+
  ##coord_sf(crs="proj=laea
           ##+lat_0=23 + lon_0=77
           ##+x_0=4321000 #<< +y_0=3210000
           ##+ellps=GRS80+ units=m
           ##+no_defs")