Skip to content

Commit

Permalink
Updates and new vignettes
Browse files Browse the repository at this point in the history
  • Loading branch information
Robinlovelace committed Jun 3, 2015
1 parent 850b0b2 commit 119abf5
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ shef*
*shinyapp*
intro-spatial.R
test.Rmd
test.geojson
lines.geojson
AFG_adm1.RData
FRA_adm1.RData
arast.*
bikeWY.geojson
35 changes: 32 additions & 3 deletions R-spatial-pres.Rmd
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

<!--
- An introduction to R (14:00 - 15:00)
- Loading, interrogating and manipulating spatial data (15:00 - 16:00)
- Making maps with ggplot2 (16:00 - 16:30)
- Questions, answers and experimenting with R
<!--
LUNCH
- Making maps in ggplot2 (13:30 - 14:30)
Expand All @@ -44,6 +62,17 @@ LUNCH
- Taking it further + Q & A (16:00 onwards)
-->

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
#
Expand Down
12 changes: 12 additions & 0 deletions R/middle-earth.R
Original file line number Diff line number Diff line change
@@ -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")
3 changes: 3 additions & 0 deletions data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CTYUA*
cuas.zip
Boundary_Dataset*
68 changes: 68 additions & 0 deletions vignettes/dl-las-stats19.Rmd
Original file line number Diff line number Diff line change
@@ -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)
```


49 changes: 49 additions & 0 deletions vignettes/get-data.Rmd
Original file line number Diff line number Diff line change
@@ -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.

45 changes: 45 additions & 0 deletions vignettes/multipart-geojson.Rmd
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 119abf5

Please sign in to comment.