library(OSMtidy) knitr::opts_chunk$set(echo = TRUE, out.height = "75%", out.width = "75%")
If you've not done so already, you can install OSMtidy using the package devtools: devtools::install_github("avisserquinn/OSMtidy")
.
This vignette provides an example of how to prepare a shapefile for use in OSMtidy. If you don't have a shapefile, OSMtidy includes example data from the UK's Ordnance Survey.
The example data is a shapefile of wards across the UK. The data is from the UK's Ordinance Survey Open Data (https://www.ordnancesurvey.co.uk/business-government/products/boundaryline) and is made available under the UK Government’s OGL licence; see http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/ for details.
Access the file path of the example data with the code below.
files <- system.file("extdata", "", package = "OSMtidy") %>% list.files(pattern = "district_borough_unitary_ward_region", full.names = TRUE) files
You can read in a shapefile to R using a function from the R package sf, st_read()
. As a dependency of OSMtidy, sf should already be installed and loaded.
If using the example data, you can use the filepath saved above. Alternatively, you can paste the filepath in as a string, e.g. "myShapefile.shp"
.
shp <- st_read(files[[3]]) # The third element in the vector has the extension .shp
shp
At the top of the output it says that this is a Simple feature collection with 7125 features and 15 fields. Each feature represents a ward in the UK. We need to choose one location to focus on - in these vignettes we will look at a ward in the City of Edinburgh (Scotland) called Leith Walk.
In the next code chunk, we reduce the data down to the location of interest using tidyverse notation.
shp <- shp %>% filter(str_detect(FILE_NAME, "CITY_OF_EDINBURGH")) %>% filter(str_detect(NAME, "Leith Walk")) %>% select(geometry) shp
Because the shapefile was imported to R as a spatial feature, using sf, we can plot it in ggplot using geom_sf()
.
shp %>% ggplot() + geom_sf() + theme_void()
You can simplify the jagged outlines of the ward using st_simplify()
. The final step is to transform the projection to EPSG:4326 (https://epsg.io/4326) using st_transform()
.
shp <- shp %>% st_simplify(dTolerance = 25) %>% st_transform(4326) shp
shp %>% ggplot() + geom_sf() + theme_void()
The shapefile is now ready for use in OSMtidy. Shapefiles can be exported using the sf function st_write()
.
shp %>% st_write("exampleEdinburgh.shp", delete_dsn = TRUE, delete_layer = TRUE, quiet = TRUE)
Now you’re ready to run OSMtidy! See Vignette 2 for a general walkthrough of the OSMtidy workflow.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.