This page is intended as a demonstration of geoscripting potential. This is done by providing geo-spatial showcases (e.g. data manipulation, visualization, analysis). Every showcase is followed by:
Note that most of the examples given here are using packages that are now deprecated. They are remains from old tutorials and exercises from this course in the past. Nonetheless, they are still useful for demonstration and inspiration!
In the script below we do the following:
# Load libraries
library(raster)
library(spatstat)
library(rgeos)
# You can choose your own country here
bel <- raster::getData('alt', country = 'BEL', mask = TRUE) # SRTM 90m height data
belshp <- raster::getData('GADM', country = 'BEL', level = 2) # administrative boundaries
# Create random points
dran <- runifpoint(500, win = as.vector(extent(bel)))
# Make the random point spatial points
S <- SpatialPoints(data.frame(x = dran$x, y = dran$y),
proj4string = CRS(proj4string(belshp)))
# Select only the ones within belgium
Sint <- gIntersection(S, belshp)
# Create a map
plot(bel)
plot(belshp, add = TRUE)
plot(
Sint,
add = TRUE,
col = "red",
pch = 19,
cex = 0.2
)
Now we can sample the height data, and plot following random point id:
out <- extract(bel, Sint, df = TRUE)
colnames(out) <- c("id", "height")
head(out)
## id height
## 1 1 8
## 2 2 0
## 3 3 31
## 4 4 0
## 5 5 0
## 6 6 10
plot(out, type = "p", pch = 19)
This example can easily be run for every country of the world with minimal human intervention, and therefore also constitute an other case of automation.
The example below illustrates how a small function allows to quickly visualize temporal profile of a raster time-series.
# Download a spatio-temporal dataset
download.file(url = 'https://raw.githubusercontent.com/loicdtx/bfastSpatial/master/data/tura.rda', destfile = 'tura.rda', method = 'auto')
load('tura.rda')
There is now a variable named tura
in your environment,
it is a time-series of NDVI over an area of Ethiopia.
library(zoo)
# Define the function to extract and plot the time series
click2ts <- function(x) {
val <- click(x, n = 1)
z <- getZ(x)
plot(
zoo(t(val), z),
type = 'p',
pch = 20,
xlab = 'Time',
ylab = 'NDVI (-)'
)
}
Then run the code block below. R will be waiting for you to click on the map, and quickly after the time-series will appear.
plot(tura, 1)
click2ts(tura)
The example below uses a time-series of MODIS VCF data, which corresponds to percentage tree cover, to investigate temporal trends of tree cover over the Netherlands.
# Load libraries
library(zoo)
library(lubridate)
library(raster)
# Download data (if it doesn't work, try with method='wget')
download.file(url = 'https://raw.githubusercontent.com/GeoScripting-WUR/Scripting4Geo/gh-pages/data/MODIS_VCF_2000-2010_NL.rds',
destfile = 'MODIS_VCF_2000-2010_NL.rds',
method = 'auto')
# Read the data
modis <- readRDS('MODIS_VCF_2000-2010_NL.rds')
# Clean data (values > 100 correspond to water)
modis[modis > 100] <- NA
# Visualize
plot(modis, 1)
# For the example we will reduce the extent so that processing does not take too long
e <- extent(340101, 370323, 5756221, 5787772)
plot(e, add = TRUE)
modis_sub <- crop(modis, e)
plot(modis_sub, 1)
# Define function to calculate temporal trends
fun <- function(x) {
ts <- zoo(x, time)
df <- data.frame(t = decimal_date(index(ts)), vcf = c(ts))
out <- try(lm(vcf ~ t, data = df)$coefficients[2], silent = T)
if (class(out) == 'try-error')
out <- NA
return(out)
}
# Run the function spatially
time <- getZ(modis)
out <- calc(x = modis_sub, fun = fun)
# Visualize output
plot(out)
hist(out, main = 'Tree cover change at 250 m resolution (2000-2010)', xlab = 'Percentage change')
You should see a map displaying the tree cover percentage temporal trends for the 2000-2010 period.
Check out the nice new leaflet package for R, that creates interactive web maps with the JavaScript ‘Leaflet’ Library.
library(leaflet)
m <- leaflet()
m <- addTiles(m)
m <- addMarkers(m, lng = 5.665349, lat = 51.987870, popup = "Wageningen University")
m
See here for the result of the above code: Wageningen Leaflet Demo
For more information: http://www.htmlwidgets.org/showcase_leaflet.html
The above map is nice, but still pretty static; it only displays two variables, which we did not choose. The year the first park was created for the dot color, and the total number of parks in the country for the dot size. To make that map a bit more interactive we can embed it in a shiny App. Shiny is among the latest developments of the R world, supported by RStudio; it provides the possibility to create interactive plots. This app can either be run in an RStudio session or online.
Advanced examples are possible, as shown here, with an integration of shiny and leaflet. (Example provided by RStudio)
Note that all examples shown in this demo have at least one characteristic in common; they are all reproducible. While you may not always remember the parameters you have selected when analyzing or creating map in a GIS environment, scripting leaves a fully reproducible mark of what you have done.
Geoscripting allows us to: