-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
850b0b2
commit 119abf5
Showing
7 changed files
with
215 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
CTYUA* | ||
cuas.zip | ||
Boundary_Dataset* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |