#' --- #' title: "More Raster" #' #' --- #' #' #' [ The R Script associated with this page is available here](`r output`). Download this file and open it (or copy-paste into a new script) with RStudio so you can follow along. #' #' #' ## Libraries #' ## ----message=F,warning=FALSE--------------------------------------------- library(knitr) library(dplyr) library(tidyr) library(ggplot2) library(raster) library(rasterVis) library(scales) library(rgeos) # load data for this course # devtools::install_github("adammwilson/DataScienceData") library(DataScienceData) #' #' ## Today's question #' #' ### How will future (projected) sea level rise affect Bangladesh? #' #' 1. How much area is likely to be flooded by rising sea level? #' 2. How many people are likely to be displaced? #' 3. Will sea level rise affect any major population centers? #' #' ## Bangladesh #' ## ------------------------------------------------------------------------ getData("ISO3")%>% as.data.frame%>% filter(NAME=="Bangladesh") #' #' #' #' #' ### Download Bangladesh Border #' #' Often good idea to keep data in separate folder. You will need to edit this for your machine! ## ------------------------------------------------------------------------ datadir="~/Downloads/data" if(!file.exists(datadir)) dir.create(datadir, recursive=T) #' Download country border. ## ---- eval=F------------------------------------------------------------- ## bgd=getData('GADM', country='BGD', level=0,path = datadir) #' #' Or load it from the data package. ## ------------------------------------------------------------------------ data(bangladesh) bgd=bangladesh #' ## ------------------------------------------------------------------------ bgd%>% gSimplify(0.01)%>% plot() #' #' ## Topography #' #' SRTM Elevation data with `getData()` as 5deg tiles. If you have trouble downloading using `getData()`, skip to the `data(bangladesh_dem)` line below #' ## ---- eval=F------------------------------------------------------------- ## bgdc=gCentroid(bgd)%>%coordinates() ## ## dem1=getData("SRTM",lat=bgdc[2],lon=bgdc[1],path=datadir) #' #' #' ### Mosaicing/Merging rasters #' #' Download the remaining necessary tiles #' ## ---- eval=F------------------------------------------------------------- ## dem2=getData("SRTM",lat=23.7,lon=85,path=datadir) #' #' Use `merge()` to join two aligned rasters (origin, resolution, and projection). Or `mosaic()` combines with a function. #' ## ---- eval=F------------------------------------------------------------- ## dem=merge(dem1,dem2) #' #' Or, load it from the data package. ## ------------------------------------------------------------------------ data(bangladesh_dem) dem=bangladesh_dem # rename for convenience #' ## ------------------------------------------------------------------------ plot(dem) bgd%>% gSimplify(0.01)%>% plot(add=T) #' #' ## Saving/exporting rasters #' #' Beware of massive temporary files! #' ## ------------------------------------------------------------------------ inMemory(dem) dem@file@name file.size(sub("grd","gri",dem@file@name))*1e-6 showTmpFiles() #' #' #' ## ------------------------------------------------------------------------ rasterOptions() #' Set with `rasterOptions(tmpdir = "/tmp")` #' #' #' #' Saving raster to file: _two options_ #' #' Save while creating ## ----eval=F-------------------------------------------------------------- ## dem=merge(dem1,dem2,filename=file.path(datadir,"dem.tif"),overwrite=T) #' #' Or after ## ----eval=F-------------------------------------------------------------- ## writeRaster(dem, filename = file.path(datadir,"dem.tif")) #' #' #' #' ### WriteRaster formats #' #' Filetype Long name Default extension Multiband support #' --- --- --- --- #' raster 'Native' raster package format .grd Yes #' ascii ESRI Ascii .asc No #' SAGA SAGA GIS .sdat No #' IDRISI IDRISI .rst No #' CDF netCDF (requires `ncdf`) .nc Yes #' GTiff GeoTiff (requires rgdal) .tif Yes #' ENVI ENVI .hdr Labelled .envi Yes #' EHdr ESRI .hdr Labelled .bil Yes #' HFA Erdas Imagine Images (.img) .img Yes #' #' `rgdal` package does even more... #' #' ### Crop to Coastal area of Bangladesh #' ## ------------------------------------------------------------------------ # crop to a lat-lon box dem=crop(dem,extent(90,91,21.5,24),filename=file.path(datadir,"dem_bgd.tif"),overwrite=T) plot(dem) bgd%>% gSimplify(0.01)%>% plot(add=T) #' #' # Use ggplot ## ----warning=F----------------------------------------------------------- gplot(dem,max=1e5)+ geom_tile(aes(fill=value))+ scale_fill_gradientn( colours=c("red","yellow","grey30","grey20","grey10"), trans="log1p",breaks= log_breaks(n = 5, base = 10)(c(1, 1e3)))+ coord_equal(ylim=c(21.5,24),xlim=c(90,91))+ geom_path(data=fortify(bgd), aes(x=long,y=lat,group=group),size=.5) #' #' #' #' # Terrain analysis (an aside) #' #' ## Terrain analysis options #' #' `terrain()` options: #' #' * slope #' * aspect #' * TPI (Topographic Position Index) #' * TRI (Terrain Ruggedness Index) #' * roughness #' * flowdir #' #' #' #' Use an even smaller region: ## ------------------------------------------------------------------------ reg1=crop(dem,extent(90.6,90.7,23.25,23.4)) plot(reg1) #' #' The terrain indices are according to Wilson et al. (2007), as in [gdaldem](http://www.gdal.org/gdaldem.html). #' #' #' #' ### Calculate slope #' ## ------------------------------------------------------------------------ slope=terrain(reg1,opt="slope",unit="degrees") plot(slope) #' #' #' #' ### Calculate aspect #' ## ------------------------------------------------------------------------ aspect=terrain(reg1,opt="aspect",unit="degrees") plot(aspect) #' #' #' #' ### TPI (Topographic Position Index) #' #' Difference between the value of a cell and the mean value of its 8 surrounding cells. #' ## ------------------------------------------------------------------------ tpi=terrain(reg1,opt="TPI") gplot(tpi,max=1e6)+geom_tile(aes(fill=value))+ scale_fill_gradient2(low="blue",high="red",midpoint=0)+ coord_equal() #' Negative values indicate valleys, near zero flat or mid-slope, and positive ridge and hill tops #' #' #' #'
#'
#' * Population
#' * Pollution
#' * Energy
#' * Agriculture
#' * Roads
#'
#'
#'
#' ### Gridded Population of the World
#'
#' Data _not_ available for direct download (e.g. `download.file()`) and are only available globally.
#'
#'
#'
#' The steps to aquire the full dataset are as follows:
#'
#' * Log into SEDAC with an Earth Data Account
#' [http://sedac.ciesin.columbia.edu](http://sedac.ciesin.columbia.edu)
#' * Download Population Density Grid for 2015
#' * Crop and mask to the country boundary for Bangladesh
#'
#' The masked data are available in the DataScienceData package in the `bangladesh_pop` dataset.
#'
#' ### Load population data
#'
#' Use `raster()` to load a raster from disk.
#'
## ---- eval=F-------------------------------------------------------------
## pop_global=raster(file.path(datadir,"gpw-v4-population-density-2015/gpw-v4-population-density_2015.tif"))
#'
## ------------------------------------------------------------------------
data(bangladesh_population)
#'
#' If the data package isn't working, download directly from github.
#'
## ---- eval=F-------------------------------------------------------------
## tf=tempfile()
## download.file("https://github.com/adammwilson/DataScienceData/raw/master/data/bangladesh_population.rda",destfile = tf)
## load(tf)
#'
## ------------------------------------------------------------------------
## make a virtual copy with a shorter name for convenience
pop=bangladesh_population
#'
#' ### Explore population data
#'
## ------------------------------------------------------------------------
gplot(pop,max=1e5)+geom_tile(aes(fill=value))+
scale_fill_gradientn(colours=c("grey90","grey60","darkblue","blue","red"),
trans="log1p",breaks= log_breaks(n = 5, base = 10)(c(1, 1e5)))+
coord_equal()
#'
#'
#'
#' ### Resample to DEM
#'
#' Compare the resolution and origin of `pop` and `dem`.
#'
## ------------------------------------------------------------------------
pop
dem
res(pop)
res(dem)
origin(pop)
origin(dem)
# Look at average cell area in km^2
cellStats(raster::area(pop),"mean")
cellStats(raster::area(dem),"mean")
#'
#' So to work with these rasters (population and elevation), it is easiest to "adjust" them to have the same resolution. But there is no good way to do this. Do you aggregate the finer raster or resample the coarser one?
#'
#' Assume equal density within each grid cell and resample
## ---- warning=F----------------------------------------------------------
pop_fine=pop%>%
resample(dem,method="bilinear")
gplot(pop_fine,max=1e5)+geom_tile(aes(fill=value))+
scale_fill_gradientn(
colours=c("grey90","grey60","darkblue","blue","red"),
trans="log1p",breaks= log_breaks(n = 5, base = 10)(c(1, 1e5)))+
coord_equal()
#'
#'
#'
#'
#' 50 different styles illustrated [here](https://cran.r-project.org/web/packages/plot3D/vignettes/volcano.pdf).
#'
#'
#'
#' Overlay population with `drape`
#'
## ---- eval=F-------------------------------------------------------------
## plot3D(dem,drape=pop, zfac=0.2)
## decorate3d()
#'
#' ## Raster overview
#'
#' * Perform many GIS operations
#' * Convenient processing and workflows
#' * Some functions (e.g. `distance()`) can be slow!