X Tutup
--- title: Raster Data date: 2018-10-04 subtitle: Working with Raster Data reading: - The [Spatial Features Package (sf)](https://r-spatial.github.io/sf/){target='blank'} tasks: - Reproject spatial data using `st_transform()` - Intersect a vector and raster layer - Save your script as a .R or .Rmd in your course repository --- ```{r setup, include=FALSE, purl=F} source("functions.R") source("knitr_header.R") ``` # Reading ```{r reading,results='asis',echo=F,purl=F} md_bullet(rmarkdown::metadata$reading) ``` # Background # Objective > 1) Generate a polygon that includes all land in NY that is within 10km of the Canadian border and 2) calculate it's area in km^2. How much land will you need to defend from the Canadians? # Tasks ```{r tasks,results='asis',echo=F, purl=F} md_bullet(rmarkdown::metadata$tasks) ``` [ Download starter R script (if desired)](`r output_nocomment`){target="_blank"}
The details below describe one possible approach. ## Libraries You will need to load the following packages ```{r warning=FALSE, message=FALSE} library(raster) library(spData) library(tidyverse) ``` Loading the `spData()` package returns a warning: `To access larger datasets in this package, install the spDataLarge package...`. This is not required for this course - you can use the standard lower resolution files. ## Data ```{r message=F} #load 'world' data from spData package data(world) ``` ## Steps 1. `world` dataset 1. transform to the albers equal area projection: ```{r} albers="+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs" ``` 2. use `st_set_geometry()` to specify that the `geom` column contains the `geometry`. This will also rename the column from `geom` to `geometry` to make it easier to use `ggplot()` 3. filter the world dataset to include only `name_long=="Canada"` 4. buffer canada to 10km (10000m) 2. WorldClim object 1. Use `tmax=getData(...)` to download the WorldClim maximum temperature dataset at the lowest resolution (10 degrees). You will have to replace the `...` with the correct parameters to specify the `name="worldclim", var="tmax", res=10`. 2. Inspect the new `tmax` object. How many layers does it have? What do these represent? 3. Calculate the mean annual maximum temperature using `mean()` 4.
Your final result should look something like this: ```{r purl=F, echo=F, message=FALSE, warning=FALSE} world_sp <- as(world,"Spatial") tmax <- getData(name = "worldclim", var="tmax", res=10) gain(tmax) <- 0.1 tmax_mean <- mean(tmax) names(tmax_mean) <- "Tmax" world_clim <- tmax_mean %>% raster::extract(y=world_sp, fun=mean, na.rm=T, small=T, sp=T)%>% st_as_sf() ggplot(world_clim,aes(fill=Tmax))+ geom_sf()+ scale_fill_viridis_c() ``` ```{r} world_clim%>% group_by(continent)%>% arrange(Tmax)%>% top_n(n=1,wt=Tmax)%>% select(name_long,continent,Tmax)%>% knitr::kable() ```
Build a leaflet map of the same dataset. ```{r, purl=F, echo=F} library(leaflet) library(htmlwidgets) l=ny_border%>% st_transform("+proj=longlat +datum=WGS84")%>% leaflet() %>% addTiles() %>% addPolygons(color = "#444444", weight = 1, fillOpacity = 0.5, fillColor = "red", popup = paste("Danger Zone")) f <-"CS05_leaflet.html" saveWidget(l,file.path(normalizePath(dirname(f)),basename(f)),libdir="externals",selfcontained = T) ```
X Tutup