crw_in_polygon: Simulate a correlated random walk inside a polygon

View source: R/sim-crw_in_polygon.r

crw_in_polygonR Documentation

Simulate a correlated random walk inside a polygon

Description

Uses crw to simulate a random walk as series of equal-length steps with turning angles drawn from a normal distribution inside a polygon.

Usage

crw_in_polygon(polyg, theta = c(0, 10), stepLen = 100,
  initPos = c(NA, NA), initHeading = NA, nsteps = 30, EPSG = 3175,
  sp_out = TRUE, show_progress = TRUE)

Arguments

polyg

A SpatialPolygons object.
OR
A polygon defined as data frame with numeric columns x and y.

theta

A 2-element numeric vector with turn angle parameters (theta[1] = mean; theta[2] = sd) from normal distribution.

stepLen

A numeric scalar with total distance moved in each step. Units are same as the units of the coordinate reference system specified by argument EPSG (meters for the default Great Lakes projected coordinate system).

initPos

A 2-element numeric vector with initial position (initPos[1]=x, initPos[2]=y) in same units as polyg.

initHeading

A numeric scalar with initial heading in degrees.

nsteps

A numeric scalar with number of steps to simulate.

EPSG

Numeric EPSG code of the coordinate system used for simulations. Default is 3175, a projected coordinate system for the North American Great Lakes Basin and St. Lawrence River system. http://spatialreference.org/ref/epsg/nad83-great-lakes-and-st-lawrence-albers/

sp_out

Logical. If TRUE (default) then output is a SpatialPoints object. If FALSE, then output is a data.frame.

show_progress

Logical. Progress bar and status messages will be shown if TRUE (default) and not shown if FALSE.

Details

If initPos = NA, then a starting point is randomly selected within the polygon boundary. A path is simulated forward using the crw function. Initial heading is also randomly selected if initHeading = NA. When a step crosses the polygon boundary, a new heading for that step is drawn and the turn angle standard deviation is enlarged slightly for each subsequent point that lands outside the polygon.

If polyg object is a data frame with x and y columns and sp_out argument is TRUE, then SpatialPoints output object will have coordinate system of EPSG. Coordinate system on output will be same as input if polyg object is [sp]{SpatialPolygons}.

Value

A SpatialPoints object in the same CRS as the input polyg object.
OR
A two-column data frame containing:

x

x coordinates

y

y coordinates

in the same units as polyg.
See argument sp_out.

Note

The path is constructed in segments based on the minimum distance between the previous point and the closest polygon boundary.

Simulations are conducted within the coordinate system specified by argument EPSG. The default EPSG (3175), covers only the Great Lakes of North America. Simulations conducted in other areas will need to specify a valid EPSG for the study area.

Author(s)

C. Holbrook cholbrook@usgs.gov

See Also

crw

Examples


#Simple box example
mypolygon <- data.frame(x = c(-50,-50, 50, 50), y = c(-50,50,50,-50))
foo <- crw_in_polygon(mypolygon, theta = c(0, 20), stepLen = 10, 
  initPos=c(0,0), initHeading=0, nsteps=50)
class(foo) #note object is SpatialPoints
plot(sp::coordinates(foo), type = "o", pch = 20, asp = c(1,1), 
  xlim = range(mypolygon$x), ylim = range(mypolygon$y))
polygon(mypolygon, border = "red")


#Great Lakes Example
data(greatLakesPoly)

#simulate in great lakes polygon
foo2 <- crw_in_polygon(greatLakesPoly,theta=c(0,25), stepLen=10000,
  initHeading=0, nsteps=100, sp_out = TRUE)

#plot
sp::plot(greatLakesPoly, col = "lightgrey", border = "grey")
points(foo2,type="o", pch = 20, col = "red")

#zoom in
sp::plot(greatLakesPoly, col = "lightgrey", border = "grey", 
  xlim = sp::bbox(foo2)[1,], ylim = sp::bbox(foo2)[2,])
points(foo2,type="o", pch = 20, col = "red")


jsta/glatos documentation built on July 11, 2022, 7:01 a.m.