make_transition: Create transition layer from polygon shapefile

View source: R/vis-make_transition.r

make_transitionR Documentation

Create transition layer from polygon shapefile

Description

Create transition layer for interpolate_path from polygon shapefile.

Usage

make_transition(in_file, output = "out.tif", output_dir = NULL,
  res = c(0.1, 0.1), all_touched = TRUE)

Arguments

in_file

A SpatialPolygonsDataFrame object or a character string with file path to polygon shapefile (with extension of *.shp).

output

character, name of output file with .tif extension

output_dir

character, directory where output file will be written. If NULL (default), then files will be written to temporary directory that will be deleted after R session is closed (see tempdir).

res

two element vector that specifies the x and y dimension of output raster cells. Units of res are same as input shapefile.

all_touched

logical. If TRUE (default) then any pixel touched by polygon 'in_file' will be coded as water in the output. Alternatively, pixel must be at least 50 polygon to be coded as water.

Details

make_transition uses gdal_rasterize to convert a polygon shapefile into a raster layer and geo-corrected transition layer interpolate_path. Raster cell values on land = 0 and water = 1. Function also writes a geotiff file (*.tif) of the input shapefile to the ouput directory. Both raster layer and geotif output have the same extents and geographic projection as input shapefile. Function requires that gdal is working on computer. To determine if gdal is installed on your computer, see gdal_rasterize.

Returned objects will be projected in longlat WGS84 (i.e., CRS("+init=epsg:4326"). If the input object is not in longlat WGS84 then transformation will be attempted and a warning will tell the user this was done. Input shapefile must include an optional *.prj file that specifies the geographic projection.

output transition layer is corrected for projection distortions using gdistance::geoCorrection. Adjacent cells are connected by 16 directions and transition function returns 0 (land) for movements between land and water and 1 for all over-water movements.

Value

A list with two elements:

transition

a geo-corrected transition raster layer where land = 0 and water=1 (see gdistance)

rast

rasterized input layer of class raster

Additonally, rasterized version of input shapefile (*.tif extension) is written to computer at output_dir

Author(s)

Todd Hayden, Tom Binder, Chris Holbrook

Examples

## Not run: 
#Example 1 - read from SpatialPolygonsDataFrame
# use example polygon for Great lakes

library(sp) #for loading greatLakesPoly
library(raster) # for plotting rasters

#get polygon of the Great Lakes
data(greatLakesPoly) #glatos example data; a SpatialPolygonsDataFrame

# make_transition layer
tst <- make_transition(greatLakesPoly, res = c(0.1, 0.1))

# plot raster layer
# notice land = 1, water = 0
plot(tst$rast)

#compare to polygon
plot(greatLakesPoly, add = TRUE)

# increase resolution and repeat if needed

#------------------------------------------
#Example 2 - read from ESRI Shapefile
# path to polygon shapefile
poly <- system.file("extdata", "shoreline.zip", package = "glatos")
poly <- unzip(poly, exdir = tempdir())

# make_transition layer
tst <- make_transition(poly[grepl("*.shp", poly)], res = c(0.1, 0.1))

# plot raster layer
# notice land = 0, water = 1
raster::plot(tst$rast)

# plot transition layer
raster::plot(raster::raster(tst$transition))

# increase resolution- this may take some time...
tst1 <- make_transition(poly[grepl("*.shp", poly)], res = c(0.01, 0.01))

# plot raster layer
raster::plot(tst1$rast)

# plot transition layer
raster::plot(raster::raster(tst1$transition))

## End(Not run)


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