Randomly sampling an irregular raster extent in R - r

Is there a function in the R raster package that is analogous to sampleRandom but which extracts n random pixel values from within an irregularly shaped polygon feature rather than a rectangular extent object?
I know there are alternative approaches such as generating random points within a polygon and then use the extract() function to get pixel values, but am wondering if there is a more direct path I have missed.
Thanks

No, there is not a single function for this.

Related

raster:: extract produces an empty list

I am trying to extract the values of pixels in a DSM(CHM) within digitized tree crowns.
first I set my working directory read in the shapefile and raster.
TreeCrowns <-shapefile("plot1sag_shape/plot1sag.shp")
CHM <- raster('272280split4.tif')
Then I try to extract the pixel values
pixel <- raster::extract(CHM, TreeCrowns, method= 'simple', weights=FALSE, fun=NULL)
But I get an empty list with all NULL values for every polygon. I have confirmed that the CHM and polygons are in the same location. What can I do to fix this?
Since your shapefile consists of polygon, the extract() function need to know how to summarise the pixel values across a polygon via the fun= argument. Since you provide fun=NULL, the function interpret as returning NULL values to summarise the pixel values.
Try fun=mean or fun=sum (and they mean different thing so see which one suits you).
That probably happens because the polygons and the raster do not overlap. Can you show(CHM) and TreeCrowns? Have you looked at
plot(CHM)
lines(TreeCrowns)
Or are your polygons very small relative to the raster cells? In that case try argument small=TRUE

How to get covariate data from a geographic raster for `ppm`?

I want to fit a Poisson point-process model with spatstat::ppm and I'm unsure what is the best way to feed covariate data to the function. I understand that spatstat expects planar coordinates, so I have transformed my point location data to a planar crs before creating a ppp point pattern object. The covariate data are in a raster stack with unprojected geographic coordinates and I understand that projecting rasters is generally ill-advised. I extracted covariate values for the point locations from the raster using the points' original geographic coordinates and raster::extract. So far so good. The issue is ...
it is not sufficient to have observed the covariate only at the points
of the data point pattern; the covariate must also have been observed
at other locations in the window. -ppm helpfile
I appear to have two options for providing the covariate data to the data argument.
A pixel image; seems ill-advised because of raster projection issues.
A list of functions (one per covariate) that can be evaluated at any location (x,y) to obtain corresponding covariate values. This seems like the way to go, but my attempt at writing such a function turns out to be ridiculously slow. It calls raster::extract for each coordinate pair after transforming the coordinates to the raster's crs. While raster::extract is reasonably fast when given a large number of points, there appears to be a substantial overhead for each call. According to microbenchmark, the coordinate transformation takes about 4ms and the extraction takes about 582ms for a single covariate, or about 4 seconds for each point to get all 7 covariates. I don't know how many times ppm will want to call this, but if it's even once per point in the pattern, it'll take too long.
Is there some way I can find out what is the complete set of points that ppm will query for covariate data so that I can extract those beforehand with a single call?
It seems like my use case (covariates in a geographic raster) should be pretty common, so I'm guessing there's an established way to do this right. What is it?
Thanks for a well written question clearly identifying you need. It would have been even better with a simple reproducible example using e.g. built-in data from raster and spatstat or artificially generated data. In lack of the reproducible example my answer will not contain any code but outline what you could do.
First step in ppm is to make a quadrature scheme or class quad or logiquad depending on which maximum likelihood approximation is used in ppm. These can be generated directly by the user via quadscheme or quadscheme.logi. The quadrature scheme contains all the points where ppm will evaluate the covariates. You can extract the coordinates of the quadrature scheme using the function coords. If you construct a data.frame with all covariates evaluated at these points you can supply that as the data argument to ppm while the quadrature scheme is the first argument. To understand things better try to read the Details section of help(ppm.quad).
Another approach which may give you the optimal use of your data is to extract the grid points of you current raster stack together with all the covariate values and project this point data. Then convert it to a simple data.frame with columns x, y, covar1, covar2, etc. Then you can use x and y together with your point observations of interest to create a quadrature scheme manually and the remaining columns can be supplied as data to ppm.
It would be interesting to compare the results from both these approaches as well as the results from just projecting the raster stack and converting it to a list of im objects.

Calculate distance to nearest neighbor in very large raster

I have a large raster (145.927.240 cells) with categorical data. The data can be found here:
https://developers.google.com/earth-engine/datasets/catalog/ESA_GLOBCOVER_L4_200901_200912_V2_3
For each cell I would like to calculate the distance to the nearest neighbor of each class. What is the most efficient (i.e. feasible) way to do this? I've looked for suitable packages, but so far I haven't found one that does what I want (with a raster of that size).
To give some context:
I would like to combine several raster files, convert them to a data table to use them as input in different models and then convert the result back to a raster file.

How to use the function r.cost to get the least-cost path between two polygons?

I am a beginner in GRASS but I would like to get the least-cost path between two polygons. More exactely, I would like to get the smallest cost from any point situated at the edge of one polygon (polygon A) to any point situated at the edge of another polygon (polygon B).
Until now, I used the function CostDistance and CostPath of ArcGIS by using a cost raster where each cell had a cost value, a shapefile for the first polygon, and a shapefile for the second polygon. I would like to do the same thing with GRASS. I think that the function r.cost allows to do this. But I don't know how to specify in parameters the two polygons in GRASS ?
Have you got an example of how to use r.cost with two polygons in R with package spgrass6?
Thanks very much for your help.
If the use of GRASS is not mandatory and sticking with R is sufficient, you should check the marmap package. Section 2.4 of the vignette (vignette("marmap")) is entitled:
2.4 Using bathymetric data for least-cost path analysis
The marmap package allows for computation of least-cost path constrained within a range of depth/altitude between any number of points. The two key functions here are trans.mat() to create a transition matrix similar to the cost-raster you mention. Then, lc.dist() computes the least-cost distance and allows to plot the path between points.
Detailed examples are provided in the marmap vignette.

Using point coordinates and diameter to calculate areal projection of points

I have data on a number of ecological variables associated with spatial points. Each point has x & y coordinates relative to the bounding box, however the points represent circular areas of varying diameter. What I'm trying to achieve is to project the area occupied by each point onto the observation window so that we can subsequently pixellate the area and retrieve the extent of overlap of the area of each point with each pixel (grid cell). In the past I have been able to achieve this with transect data by converting to a psp line object and then using the pixellate function in the spatstat package but am unsure how to proceed with these circular areas. It feels like I should be using polygon classes but again I am unsure how to define them. Any suggestion would be greatly appreciated.
In the spatstat package, the function discs will take locations (x,y) and radii r (or diameters, areas etc) and generate either polygonal or pixel-mask representations of the circles, and return them either as separate objects or as a single combined object.

Resources