ncss-tech / stats_for_soil_survey

S4SS: Statistics for Soil Survey

Home Page:http://ncss-tech.github.io/stats_for_soil_survey/book/intro.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fast raster / velox example in Spatial Data lecture

brownag opened this issue · comments

The {velox} package (https://github.com/hunzikp/velox) has unmet dependencies and is no longer on CRAN for R 4.0.2

We will need to update/remove "2.2.3 FAST raster sampling: velox" section.

I suggest using {fasterize} and {exactextractr} for a new demo on fast raster manipulation -- both of thes packages are very useful for manipulating of raster data for arbitrary features quickly.

library(raster)
library(fasterize)
library(exactextractr)

library(sf)
library(dplyr)
library(ggplot2)
library(rasterVis)

### LOAD SSURGO DATA AND CONVERT TO 10m RASTER

# some polygons associated with an MLRA project
polygons <- sf::read_sf("E:/CA649/workflow/output/working_progress.shp") 

# extract musym information, create factor
polygons$musym <- as.factor(polygons$musym)
polynames <- levels(polygons$musym)
polycodes <- 1:length(polynames)

### convert SSURGO feature class to raster (10m) quickly with fasterize()
rpolygons <- fasterize::fasterize(polygons, field = "musym", raster(polygons, res = 10))
rpolygons <- ratify(rpolygons)

# set up raster attribute table for symbols
rat <- levels(rpolygons)[[1]]
rat$musym <- polynames
rat$code <- polycodes
levels(rpolygons) <- rat

musym.colors <- viridis::viridis(length(polynames))
names(musym.colors) <- as.character(polycodes)

# use rasterVis gplot() [ wrapper around ggplot() for RasterLayer ]
rasterVis::gplot(rpolygons) +
  geom_raster(aes(fill = as.factor(value)), show.legend = T) +
  scale_fill_discrete(type = musym.colors, labels = polynames,
                      na.value = "transparent") +
  coord_equal()

image

### SUMMARIZE SLOPE GRADIENT BY DELINEATION

# load a slope raster (10m resolution)
rslope <- raster("C:/Geodata/project_data/MUSum_10m_MLRA/Slope_SON_int_AEA.tif")

# quickly extract raster values by delineation
rslope_ext <- exact_extract(rslope, polygons, include_xy = TRUE)

# note: we get a coverage fraction and an XY position for each value 
head(rslope_ext[[1]])

# iterate over delineations, calculate the [weighted] quantiles for data from each delineation
rslope_qua <- do.call('rbind', lapply(rslope_ext, function(x) {
  quantile(x$value * x$coverage_fraction, probs = c(0,0.05,0.5,0.95,1), names = FALSE)
}))

# combine spatial data with delineation summaries
rslope_cmb <- cbind(polygons, Q = rslope_qua)

# lets look at just a single mapunit symbol
rslope_cmb_sub <- filter(rslope_cmb, musym == "7092")

# colored by median slope
ggplot() + 
  geom_sf(data = rslope_cmb_sub, aes(fill = Q.3))

image

Note that the above logic around the weighting of exact_extract values would be improved with {Hmisc} wtd.quantile

Actually added a weighted quantile implementation to exactextractr recently. The Hmisc implementation has some issues.

Whats up with the Hmisc function?

It gives a garbage result if the weights aren't integers: harrelfe/Hmisc#97

Dang it, thanks for pointing that out. I'll follow-up with Frank. I've used Hmisc for over 15 years now and this is the first that I have heard about it. Seems like there are other issues to be aware of, or at least as reported on the GH page.

c9e3bb4 removed velox from the packages covered in the precourse.

Since {velox} requires compilation and is no longer on CRAN it is a bit of a wrinkle for the setup we have students do. It can be sourced from MRAN -- but my thought is that if we need section 2.2.3 at all for addressing the notion of faster raster operations we should use packages on CRAN. My vote is for a sf+exactextractr example.

@smroecker -- as coordinator for the stats class -- do you have an opinion on where to go with this issue?

Agreed, been on my TODO list for a while now. I'll make those changes.

I concur, exactextractr and fasterize appear to be better alternatives than velox. Let's swap out.