knitr::opts_chunk$set(echo = TRUE, eval=F)
library(osmdata)
library(sf)

Source

Open Street Map (OSM) {.tabset}

https://openstreepmap.org

Online street map consists of:

BBox

osmdata::getbb("new taipei") -> newTaipeiBBox
newTaipeiBBox
class(newTaipeiBBox)
attributes(newTaipeiBBox)

Features

stringr::str_subset(osmdata::available_features(), "boundary|admin")
source("https://www.dropbox.com/s/8ndtvzqbdb4cb93/data_visulaization_pk.R?dl=1")
osmdata::getbb("new taipei") -> newTaipeiBBox
x <- opq(newTaipeiBBox) %>% 
     add_osm_feature(key="admin_level", value="5") %>%
     osmdata::osmdata_sf()

x$osm_multipolygons %>%
  osm_geom_rename() %>% ggplot()+geom_sf()

Function help

A good function help consists of

Exercise {.tabset}

Write a function called osm_read_clip and insert Roxygen function help explanation details.

Pseudocodes 1

osm_read_clip <- function(){
  # read clipboard from OSM bbox information from openstreetmap.org
  clip_bbox <- 
    {
      clipr::read_clip()
    }
  # use stringr carefully rearrange your text clipboard into the format that can be the add_osm_feature's bbox input argument
  bbox4osm <- 
    {

    }
  clipr::write_clip(bbox4osm)
}

Pseudocodes 2

# read clipboard from OSM bbox information from openstreetmap.org
  clip_bbox <- 
    {
      clipr::read_clip()
    }
clip_bbox
# use stringr carefully rearrange your text clipboard into the format that can be the add_osm_feature's bbox input argument
  bbox4osm <- 
    {
       legitimateClip <- subset(clip_bbox, clip_bbox!="") 

       names(legitimateClip) <- c("ymax", "xmin", "xmax", "ymin")

       clipMatrix <- 
         rbind(
          legitimateClip[c("xmin", "xmax")],
          legitimateClip[c("ymin", "ymax")]
        )
       rownames(clipMatrix) <- c("x","y")
       colnames(clipMatrix) <- c("min","max")

       clipMatrix
    }
bbox4osm_deparsed <-
  deparse(bbox4osm)
clipr::write_clip(bbox4osm_deparsed)

Test Function

osm_read_bboxclip <- function()
{

  # read clipboard from OSM bbox information from openstreetmap.org
  clip_bbox <- {
    clipr::read_clip()
  }

  # use stringr carefully rearrange your text clipboard into the format that can be the add_osm_feature's bbox input argument
  bbox4osm <- {
    legitimateClip <- 
      as.numeric(stringr::str_subset(clip_bbox,"[0-9]+.[0-9]*"))

    names(legitimateClip) <- c("ymax", "xmin", "xmax", "ymin")

    clipMatrix <-
      rbind(
        legitimateClip[c("xmin", "xmax")],
        legitimateClip[c("ymin", "ymax")]
      )
    rownames(clipMatrix) <- c("x", "y")
    colnames(clipMatrix) <- c("min", "max")

    clipMatrix
  }

  bbox4osm_deparsed <-
    deparse(bbox4osm)

  clipr::write_clip(bbox4osm_deparsed)
}
osm_read_bboxclip()
targetBbox <- structure(c(121.5547, 25.0494, 121.5907, 25.0783), .Dim = c(2L, 
2L), .Dimnames = list(c("x", "y"), c("min", "max")))
stringr::str_subset(
  osmdata::available_features(), 
  stringr::regex(
    "cycle", ignore_case = T
  )
)
osmdata::opq(targetBbox) %>%
  osmdata::add_osm_feature(
    key='highway',
    value='cycleway'
   ) %>%
  osmdata::osmdata_sf() -> minShengNeighborhood
ggplot()+
  geom_sf(data = minShengNeighborhood$osm_lines)

OSM procedure encapsulated

osm <- OSM()
osm$check$browse_osmWeb()
osm$buildfrom_clip()
undebug(osm$check$find_features)
osm$check$find_features("university") # none
osm$check$browse_features() # visit website
osm$check$find_features("amenity") # yes
osm$buildfrom_clip()
osm$query()
osm$add_features(
  key="amenity", value="university"
)
osm$export_sf() -> ntpu_sf
ggplot()+
  geom_sf(
    data=ntpu_sf$osm_multipolygons %>% 
      osm_rename()
  )


tpemartin/econDV1091 documentation built on Feb. 17, 2021, 6:37 a.m.