The first stage when doing GIS in R is to identify and load the necessary packages:
library(sp) # foundational package defining sp classes
library(tmap) # cutting edge GIS functionality
library(geojsonio) # for saving as geojson
##
## We recommend using rgdal v1.1-1 or greater, but we don't require it
## rgdal::writeOGR in previous versions didn't write
## multipolygon objects to geojson correctly.
## See https://stat.ethz.ch/pipermail/r-sig-geo/2015-October/023609.html
##
## Attaching package: 'geojsonio'
## The following object is masked from 'package:base':
##
## pretty
Next we need some data:
f = "statistical-gis-boundaries-london.zip"
if(!file.exists(f)){
u = "https://s3-eu-west-1.amazonaws.com/londondatastore-upload/statistical-gis-boundaries-london.zip"
download.file(url = u, destfile = "statistical-gis-boundaries-london.zip")
unzip("statistical-gis-boundaries-london.zip")
}
Load the data into R:
lnd = read_shape("statistical-gis-boundaries-london/ESRI/London_Borough_Excluding_MHW.shp")
What’s the bounding box?
bb(lnd)
## min max
## x 503568.2 561957.5
## y 155850.8 200933.9
Transform the CRS:
lnd_wgs = spTransform(lnd, CRSobj = CRS("+init=epsg:4326"))
bb(lnd_wgs)
## min max
## x -0.5103751 0.3340155
## y 51.2867602 51.6918741
Plot the result (on a basemap):
osm_tiles = read_osm(bb(lnd_wgs))
tm_shape(osm_tiles) +
tm_raster() +
tm_shape(lnd_wgs) +
tm_borders()
Save the output:
geojson_write(lnd_wgs, file = "../data/lnd.geojson")
## Success! File is at ../data/lnd.geojson
## <geojson>
## Path: ../data/lnd.geojson
## From class: SpatialPolygonsDataFrame