interpolateAndApplyWithinSpatial: Generic function to interpolate from a polygon to points...

Description Usage Arguments Value Examples

View source: R/R-GIS.R

Description

Generic function to interpolate from a polygon to points lying inside it This function was designed to solve the following problem. Suppose you have counts of the number of entities inside a polygon (N). To compute distances to a point, you might take the distance from the polygon centroid. But this is too simplistic–it discards the positional uncertainty inherent in not knowing the exact location of each entity which makes up the count. Instead, we repeatedly sample N points from the census block group centroids which lie within our polygon, weight them by their population, and compute distances from there.

Usage

1
2
interpolateAndApplyWithinSpatial(crude, fine, FUN, nSampleCol,
  samplesize = 30, simplify = FALSE, ...)

Arguments

crude

A SpatialPolygonsDataFrame containing the variable of interest

fine

an sp object whose points or polygons lie inside the polygons of crude

FUN

An function taking at least two arguments (a single polygon of crude in the first position, and the within-polygon elements of fine in the second)

nSampleCol

Column name in crude containing number of elements of fineWithin to sample per polygon

samplesize

The number of replicates per element of crude to draw

simplify

Whether to simplify to an array or not

...

Arguments to be passed to FUN

Value

list of length length(crude) where each element is a list of length samplesize containing the results of FUN for that crude-element/sample

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Not run because too time-consuming
## Not run: 
require(fields)
require(rgdal)
distanceMatrix <- function( points1, points2, dist.fn=rdist.earth ) {
 cat( "Generating distance matrix for ",length(points1)," by ", length(points2), " matrix.\n" )
 if(!is.na(proj4string(points1))) {
   points1 <- sp::spTransform( points1, sp::CRS("+proj=longlat +datum=WGS84") )
 }
 if(!is.na(proj4string(points2))) {
   points2 <- sp::spTransform( points2, sp::CRS("+proj=longlat +datum=WGS84") )
 }
 dist.fn( coordinates(points1), coordinates(points2) )
}
# One option: Use the apply functionality
dist <- interpolateAndApplyWithinSpatial(
 crude=polySP, fine=pointSP,
 FUN=distanceMatrix,
 nSampleCol="z", samplesize=25,
 points2=pointSP2, simplify=TRUE
)
# Dist now is a list of 3 matrices, each with dimensions: N x length(pointSP2) x samplesize
# Each matrix represents N entities imputed from a single polygon,
#   so we can actually simplify further
library(abind)
distmat <- do.call( Curry(abind, along=1), dist )
mindist <- apply( distmat, 3, function(x) { # For each realization of the 'world'
  apply( x,  )
} )

## End(Not run) # end of dontrun

gsk3/taRifx.geo documentation built on May 17, 2019, 8:56 a.m.