diff --git a/.gitignore b/.gitignore index 7668a11..41e5f3e 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,9 @@ shef* *shinyapp* intro-spatial.R test.Rmd +test.geojson +lines.geojson +AFG_adm1.RData +FRA_adm1.RData +arast.* +bikeWY.geojson diff --git a/R-spatial-pres.Rmd b/R-spatial-pres.Rmd index b68b978..8b90b3e 100644 --- a/R-spatial-pres.Rmd +++ b/R-spatial-pres.Rmd @@ -1,7 +1,7 @@ --- title: "An introduction to R for Spatial data" author: "Robin Lovelace" -date: "Sheffield, May 15th 2015" +date: "Newcastle, June 2nd 2015" output: slidy_presentation: fig_height: 4 @@ -23,19 +23,37 @@ This course is brought to you the Consumer Data Research Centre project based at the University of Leeds and UCL. It is funded by the ESRC's ([Big Data Network](http://www.esrc.ac.uk/research/major-investments/Big-Data/BDN-Phase2.aspx)) +Packages +================= + + + +```{r, eval=FALSE} +pkgs <- c("rgdal", # (can be tricky) + "rgeos", + "ggmap", + "tmap",) +install.packages(pkgs) +``` + + Course agenda ======================================================= During this course we will cover these topics +- Introducing R as a GIS: Introduction and example with London (Part I handout), 09:00 - 11:00 am +- The structure of spatial objects in R (Part II) 11:15 - lunch +- Loading and interrogating spatial data (Part III) Day 1 14:00 - 15:15 +- Visualising spatial datasets (Part III) Day 1 15:30 - 11:00 Day 2 + + +Day 2 +========== + +- Acquiring external data with R Day 2 11:15 - lunch + +After that: you choose! + +- Point pattern analysis and spatial interpolation +- Geographical models in R +- Webmaps + ```{r, echo=FALSE} # During this course we will cover these topics # diff --git a/R/middle-earth.R b/R/middle-earth.R new file mode 100644 index 0000000..77e13fc --- /dev/null +++ b/R/middle-earth.R @@ -0,0 +1,12 @@ +library(rgeos) +plot(lnd, col = "grey") +cent_lnd <- gCentroid(lnd) +points(cent_lnd, cex = 3) +lnd_buffer <- gBuffer(spgeom = cent_lnd, width = 10000) # set 10 km buffer +lnd_central <- lnd[lnd_buffer,] +lnd_cents <- SpatialPoints(coordinates(lnd), proj4string = CRS(proj4string(lnd))) +sel <- lnd_cents[lnd_buffer,] +lnd_central <- lnd[sel,] +plot(lnd_central, add = T, col = "red") +plot(lnd_buffer, add = T, col = "white") +text(coordinates(cent_lnd), "Middle Earth") diff --git a/data/.gitignore b/data/.gitignore new file mode 100644 index 0000000..4de86b9 --- /dev/null +++ b/data/.gitignore @@ -0,0 +1,3 @@ +CTYUA* +cuas.zip +Boundary_Dataset* diff --git a/vignettes/dl-las-stats19.Rmd b/vignettes/dl-las-stats19.Rmd new file mode 100644 index 0000000..3cd9cf0 --- /dev/null +++ b/vignettes/dl-las-stats19.Rmd @@ -0,0 +1,68 @@ +--- +title: "Download LAs and point-pattern analysis" +author: "Robin Lovelace" +date: "June 3, 2015" +output: html_document +--- + +```{r, eval=FALSE} +downloader::download("https://geoportal.statistics.gov.uk/Docs/Boundaries/County_and_unitary_authorities_(E+W)_2012_Boundaries_(Generalised_Clipped).zip", "data/cuas.zip") +unzip("data/cuas.zip", exdir = "data") +cuas <- raster::shapefile("data/CTYUA_DEC_2012_EW_BGC.shp") +plot(cuas) +cuas@data$CTYUA12NM +WY <- cuas[cuas$CTYUA12NM == "Surrey",] +plot(WY) +``` + +Then follow this link to find out how to get data for road traffic +'accidents': https://github.com/Robinlovelace/bikeR/blob/master/video-routes/load-stats19.R + +After running the code in this file, the +challenge is to create a dataset of traffic +casualties like this: + +```{r} +library(spatstat) +library(maptools) +library(rgeos) +library(raster) +# downloader::download("https://github.com/Robinlovelace/bikeR/raw/master/geodata/bikeWY.geojson", "bikeWY.geojson") +ac <- rgdal::readOGR("../bikeWY.geojson", "OGRGeoJSON") +ac <- spTransform(ac, CRS("+init=epsg:27700")) + +# estimate 2d density +acp <- as.ppp(ac) +adens <- density.ppp(x = acp, sigma = 50, eps = 50) +plot(adens) +arast <- raster(adens) +plot(arast) + +writeRaster(x = arast, filename = "arast.tif", overwrite = T) +dsg <- as(arast, "SpatialGridDataFrame") +dsg <- as.image.SpatialGridDataFrame(dsg) +dcl <- contourLines(dsg, nlevels = 10) +sldf <- ContourLines2SLDF(dcl) +plot(sldf[8,]) # the most intense accident hotspot +h1 <- gPolygonize(sldf[8,]) +spChFIDs(h1) <- 1 +h2 <- gPolygonize(sldf[7,]) +spChFIDs(h1) <- 2 +plot(h2) +h3 <- gPolygonize(sldf[6,]) +spChFIDs(h3) <- seq(101, 100 + length(h3)) +hspots <- spRbind(h3, h2) +h4 <- gPolygonize(sldf[5,]) + +h5 <- gPolygonize(sldf[3,]) # the right contour to save +length(h5) +proj4string(h5) <- proj4string(ac) +nacs <- aggregate(ac, h5, length) +nacs <- spTransform(nacs, CRS("+init=epsg:27700")) +nacs$area <- gArea(nacs, byid = T) +plot(nacs) +plot(adens, add = T) +plot(nacs, add = T) +``` + + diff --git a/vignettes/get-data.Rmd b/vignettes/get-data.Rmd new file mode 100644 index 0000000..404b5a8 --- /dev/null +++ b/vignettes/get-data.Rmd @@ -0,0 +1,49 @@ +--- +title: "Geographical data out of R" +author: "Robin Lovelace" +date: "June 3, 2015" +output: html_document +--- + +## Raster package + +```{r} +lnd_raster <- raster::shapefile("data/") +``` + + +```{r} +library(raster) +fr <- getData(name = "GADM", country = "FRA", level = 1) +allcountries <- ccodes() +allcountries[i,2] +plot(fr) +names(fr@data) +fr$NAME_1 + +world <- getData(name = "GADM") + +afg <- getData(country = "AFG", level = 1) +?getData +plot(cc) + + +object.size(afg) / 1000000 +afgs <- rgeos::gSimplify(afg, tol = 0.01) +afg <- SpatialPolygonsDataFrame(afgs, afg@data) +object.size(afg) / 1000000 + +# library(devtools) +# install_github("rstudio/leaflet") +library(leaflet) +leaflet() %>% + addTiles() %>% + addPolygons(data = afg, popup = afg$NAME_1) +``` + +> **Challenge:** download data for 2 different countries and 2 different +levels. + +> **Challenge:** Try to merge some data you've found from the internet with this geographical data and plot the result using your prefered method out of tmap, base graphics, leaflet or ggmap. + + diff --git a/vignettes/multipart-geojson.Rmd b/vignettes/multipart-geojson.Rmd new file mode 100644 index 0000000..e6833d2 --- /dev/null +++ b/vignettes/multipart-geojson.Rmd @@ -0,0 +1,45 @@ +--- +title: "Multipart polygons R" +author: "Robin Lovelace" +date: "June 2, 2015" +output: html_document +--- + +Based on this: +https://stackoverflow.com/questions/30583048/convert-features-of-a-multifeature-geojson-into-r-spatial-objects/30593077#30593077 + +Normally you can read geojson files into R with trusty `readOGR`, as illustrated [here][1]. + +However, this fails for multifeature geojsons. + +Reproducible example: + +```{r} +downloader::download("https://github.com/Robinlovelace/Creating-maps-in-R/raw/master/data/test-multifeature.geojson", "test.geojson") +try(test <- rgdal::readOGR("test.geojson", "OGRGeoJSON")) # fails with: +``` + +The error message is clear-enough and indicates a solution: split the features. +Aside from doing this with regex, I don't know how, however. + +Any ideas very much welcome. + +The amazing thing: [GitHub displays the data natively on the browser][2], whereas R cannot even (seemingly) read it in! + +Alternative way to a solution: + +```{r} +test <- geojsonio::geojson_read("test.geojson") +``` + +## Solution from Spacedman + +```{r} +mess <- 'ogr2ogr -where "OGR_GEOMETRY=\'LINESTRING\'" -f "GeoJSON" lines.geojson test.geojson' +system(mess) +testl <- rgdal::readOGR("lines.geojson") +``` + + + [1]: https://github.com/Robinlovelace/Creating-maps-in-R/blob/master/vignettes/geoJSON.Rmd + [2]: https://github.com/Robinlovelace/Creating-maps-in-R/blob/master/data/test-multifeature.geojson