Title: | Spatial Interpolation by Inverse Path Distance Weighting |
---|---|
Description: | Functions are provided to interpolate geo-referenced point data via Inverse Path Distance Weighting. Useful for coastal marine applications where barriers in the landscape preclude interpolation with Euclidean distances. |
Authors: | Jemma Stachelek [aut, cre] |
Maintainer: | Jemma Stachelek <[email protected]> |
License: | GPL (>= 2) |
Version: | 2.0-0 |
Built: | 2025-01-27 04:12:17 UTC |
Source: | https://github.com/jsta/ipdw |
Generate a cost raster from an object of class sf with point or polygon geometries
costrasterGen(xymat, pols, extent = "polys", projstr, resolution = 1)
costrasterGen(xymat, pols, extent = "polys", projstr, resolution = 1)
xymat |
Matrix of coordinates or an sf object with point geometries |
pols |
sf object with polygon geometries |
extent |
Define extent based on extent of xymat/sf (points) or pols (polys). Default is polys. |
projstr |
proj4 string defining the output projection. A warning will be thrown if projstr does not match the projection of the extent target. Pass NULL for non-geographic grids. |
resolution |
Numeric defaults to 1. See |
Ensure that the projection of the xymat coordinates and pols match. If they do not match use the st_transform
command.
RasterLayer
## Not run: library(sf) Sr1 <- st_polygon(list(cbind(c(0, 0, 1, 1, 0), c(0, 12, 12, 0, 0)))) Sr4 <- st_polygon(list(cbind(c(9, 9, 10, 10, 9), c(0, 12, 12, 0, 0)))) Sr2 <- st_polygon(list(cbind(c(1, 1, 9, 9, 1), c(11, 12, 12, 11, 11)))) Sr3 <- st_polygon(list(cbind(c(1, 1, 9, 9, 1), c(0, 1, 1, 0, 0)))) Sr5 <- st_polygon(list(cbind(c(4, 4, 5, 5, 4), c(4, 8, 8, 4, 4)))) pols <- st_as_sf(st_sfc(Sr1, Sr2, Sr3, Sr4, Sr5, crs = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")) # using a matrix object xymat <- matrix(3, 3, nrow = 1, ncol = 2) costras <- costrasterGen(xymat, pols, projstr = NULL) # plotting plot(costras) points(xymat) ## End(Not run)
## Not run: library(sf) Sr1 <- st_polygon(list(cbind(c(0, 0, 1, 1, 0), c(0, 12, 12, 0, 0)))) Sr4 <- st_polygon(list(cbind(c(9, 9, 10, 10, 9), c(0, 12, 12, 0, 0)))) Sr2 <- st_polygon(list(cbind(c(1, 1, 9, 9, 1), c(11, 12, 12, 11, 11)))) Sr3 <- st_polygon(list(cbind(c(1, 1, 9, 9, 1), c(0, 1, 1, 0, 0)))) Sr5 <- st_polygon(list(cbind(c(4, 4, 5, 5, 4), c(4, 8, 8, 4, 4)))) pols <- st_as_sf(st_sfc(Sr1, Sr2, Sr3, Sr4, Sr5, crs = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")) # using a matrix object xymat <- matrix(3, 3, nrow = 1, ncol = 2) costras <- costrasterGen(xymat, pols, projstr = NULL) # plotting plot(costras) points(xymat) ## End(Not run)
Generate error statistics from validation point datasets overlaid on a raster surface
errorGen( finalraster, validation.sf_ob, validation.data, plot = FALSE, title = "" )
errorGen( finalraster, validation.sf_ob, validation.data, plot = FALSE, title = "" )
finalraster |
RasterLayer object |
validation.sf_ob |
sf object with points geometry |
validation.data |
data.frame |
plot |
logical. Plot comparison? |
title |
Plot labels |
List of error statistics
library(sf) validation.data <- data.frame(rnorm(10, mean = 0.2, sd = 1)) names(validation.data) <- c("validation") validation.sf_ob <- validation.data validation.data <- as.numeric(unlist(validation.data)) xy <- data.frame(x = c(0:9), y = rep(1, 10)) validation.sf_ob <- st_as_sf(cbind(validation.sf_ob, xy), coords = c("x", "y")) m <- matrix(NA, 1, 10) out.ras <- raster(m, xmn = 0, xmx = ncol(m), ymn = 0, ymx = nrow(m)) out.ras[] <- validation.data + rnorm(ncell(out.ras), mean = 0.01, sd = 0.2) valid.stats <- errorGen(out.ras, validation.sf_ob, validation.data, plot = TRUE, title = "Validation Plot") valid.stats
library(sf) validation.data <- data.frame(rnorm(10, mean = 0.2, sd = 1)) names(validation.data) <- c("validation") validation.sf_ob <- validation.data validation.data <- as.numeric(unlist(validation.data)) xy <- data.frame(x = c(0:9), y = rep(1, 10)) validation.sf_ob <- st_as_sf(cbind(validation.sf_ob, xy), coords = c("x", "y")) m <- matrix(NA, 1, 10) out.ras <- raster(m, xmn = 0, xmx = ncol(m), ymn = 0, ymx = nrow(m)) out.ras[] <- validation.data + rnorm(ncell(out.ras), mean = 0.01, sd = 0.2) valid.stats <- errorGen(out.ras, validation.sf_ob, validation.data, plot = TRUE, title = "Validation Plot") valid.stats
Interpolate geo-referenced point data using inverse path distance weighting.
ipdw( sf_ob, costras, range, paramlist, overlapped = FALSE, yearmon = "default", removefile = TRUE, step = 16, dist_power = 1, trim_rstack = FALSE )
ipdw( sf_ob, costras, range, paramlist, overlapped = FALSE, yearmon = "default", removefile = TRUE, step = 16, dist_power = 1, trim_rstack = FALSE )
sf_ob |
sf object with point geometries |
costras |
RasterLayer. Cost raster |
range |
numeric. Range of interpolation neighborhood |
paramlist |
character. String representing parameter names |
overlapped |
logical. Default is FALSE, specify TRUE if some points lie on top of barriers |
yearmon |
character. String specifying the name of the sf_ob |
removefile |
logical. Remove files after processing? |
step |
numeric. Number of sub loops to manage memory during raster processing. |
dist_power |
numeric. Distance decay power (p) |
trim_rstack |
logical. Trim the raster output by the convex hill of sf_ob |
This is a high level function that interpolates an sf object with point geometries in a single pass.
Points must be located within a single contiguous area. The presence of "landlocked" points will cause errors. It may be necessary to increase the value assigned to land areas when using a large range value in combination with a large sized cost rasters (grain x extent). In these cases, the value of land areas should be increased to ensure that it is always greater than the maximum accumulated cost path distance of any given geo-referenced point.
RasterLayer
# see vignette
# see vignette
This function takes a rasterstack of pathdistances and generates surfaces by weighting parameter values by these distances
ipdwInterp( sf_ob, rstack, paramlist, overlapped = FALSE, yearmon = "default", removefile = TRUE, dist_power = 1, trim_rstack = FALSE )
ipdwInterp( sf_ob, rstack, paramlist, overlapped = FALSE, yearmon = "default", removefile = TRUE, dist_power = 1, trim_rstack = FALSE )
sf_ob |
sf object with point geometries |
rstack |
RasterStack of path distances |
paramlist |
character. String representing parameter names |
overlapped |
logical. Default is FALSE, specify TRUE if some points lie on top of barriers |
yearmon |
character. String specifying the name of the sf object |
removefile |
logical. Remove files after processing? |
dist_power |
numeric. Distance decay power (p) |
trim_rstack |
logical. Trim the raster stack by the convex hull of sf_ob |
Under the hood, this function evaluates:
where d
is the distance between prediction and measurement points,
v_i
is the measured parameter value, and p
is a power parameter.
RasterLayer
library(sf) sf_ob <- data.frame(rnorm(2)) xy <- data.frame(x = c(4, 2), y = c(8, 4)) sf_ob <- st_as_sf(cbind(sf_ob, xy), coords = c("x", "y")) m <- matrix(NA, 10, 10) costras <- raster(m, xmn = 0, xmx = ncol(m), ymn = 0, ymx = nrow(m)) # introduce spatial gradient costras[] <- runif(ncell(costras), min = 1, max = 10) for (i in 1:nrow(costras)) { costras[i, ] <- costras[i, ] + i costras[, i] <- costras[, i] + i } rstack <- pathdistGen(sf_ob, costras, 100, progressbar = FALSE) final.raster <- ipdwInterp(sf_ob, rstack, paramlist = c("rnorm.2."), overlapped = TRUE) plot(final.raster) plot(sf_ob, add = TRUE)
library(sf) sf_ob <- data.frame(rnorm(2)) xy <- data.frame(x = c(4, 2), y = c(8, 4)) sf_ob <- st_as_sf(cbind(sf_ob, xy), coords = c("x", "y")) m <- matrix(NA, 10, 10) costras <- raster(m, xmn = 0, xmx = ncol(m), ymn = 0, ymx = nrow(m)) # introduce spatial gradient costras[] <- runif(ncell(costras), min = 1, max = 10) for (i in 1:nrow(costras)) { costras[i, ] <- costras[i, ] + i costras[, i] <- costras[, i] + i } rstack <- pathdistGen(sf_ob, costras, 100, progressbar = FALSE) final.raster <- ipdwInterp(sf_ob, rstack, paramlist = c("rnorm.2."), overlapped = TRUE) plot(final.raster) plot(sf_ob, add = TRUE)
Generate a stack of path accumulated distance raster objects
pathdistGen(sf_ob, costras, range, yearmon = "default", progressbar = TRUE)
pathdistGen(sf_ob, costras, range, yearmon = "default", progressbar = TRUE)
sf_ob |
sf object with point geometries |
costras |
RasterLayer cost raster |
range |
numeric. Range of interpolation neighborhood |
yearmon |
character. String specifying the name of the sf_ob |
progressbar |
logical show progressbar during processing? |
RasterStack object of path distances
library(sf) sf_ob <- data.frame(rnorm(2)) xy <- data.frame(x = c(4, 2), y = c(8, 4)) sf_ob <- st_as_sf(cbind(sf_ob, xy), coords = c("x", "y")) m <- matrix(NA, 10, 10) costras <- raster(m, xmn = 0, xmx = ncol(m), ymn = 0, ymx = nrow(m)) costras[] <- runif(ncell(costras), min = 1, max = 10) # introduce spatial gradient for (i in 1:nrow(costras)) { costras[i, ] <- costras[i, ] + i costras[, i] <- costras[, i] + i } rstack <- pathdistGen(sf_ob, costras, 100, progressbar = FALSE)
library(sf) sf_ob <- data.frame(rnorm(2)) xy <- data.frame(x = c(4, 2), y = c(8, 4)) sf_ob <- st_as_sf(cbind(sf_ob, xy), coords = c("x", "y")) m <- matrix(NA, 10, 10) costras <- raster(m, xmn = 0, xmx = ncol(m), ymn = 0, ymx = nrow(m)) costras[] <- runif(ncell(costras), min = 1, max = 10) # introduce spatial gradient for (i in 1:nrow(costras)) { costras[i, ] <- costras[i, ] + i costras[, i] <- costras[, i] + i } rstack <- pathdistGen(sf_ob, costras, 100, progressbar = FALSE)
Remove NA points features and drop corresponding raster stack layers
rm_na_pointslayers(param_name, sf_ob, rstack)
rm_na_pointslayers(param_name, sf_ob, rstack)
param_name |
character name of data column |
sf_ob |
sf object with point geometries |
rstack |
RasterStack or RasterBrick |