knitr::opts_chunk$set(echo = TRUE, eval=F)
library(osmdata) library(sf)
Online street map consists of:
Bounding Box (bbox): latitudes and longtitudes that crop out the area of geographic data user request.
Features: the features on the map users are interested in. For example, administrative boundaries, parks, buildings, etc. (https://wiki.openstreetmap.org/wiki/Map_Features)
osmdata::getbb("new taipei") -> newTaipeiBBox newTaipeiBBox
class(newTaipeiBBox) attributes(newTaipeiBBox)
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()
A good function help consists of
A good function name (probably not a help writing problem)
Title: short and eye-catching
Argument: What kind of object? What does it do? Any attributes that are required?
Return: What users expect to get? class? dimensions?
Example: Action speaks louder than words.
Write a function called osm_read_clip and insert Roxygen function help explanation details.
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) }
# 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)
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 <- 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() )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.