Converts SpatialPolygons layer to raster.

spPoly2Rast(
  polyv,
  polyv_dsn = NULL,
  polyv.att,
  polyv.lut = NULL,
  rastfn.template = NULL,
  validate = FALSE,
  NODATA = NULL,
  outfolder = NULL,
  outfn = "polyrast",
  outext = "img",
  outfn.pre = NULL,
  outfn.date = FALSE,
  overwrite = FALSE
)

Arguments

polyv

sf R object or String. Polygon data to convert to raster. Can be a spatial polygon object, full pathname to a shapefile, or name of a layer within a database.

polyv_dsn

String. Data source name (dsn; e.g., sqlite or shapefile pathname) of layer to convert. The dsn varies by driver. See gdal OGR vector formats (https://www.gdal.org/ogr_formats.html). Optional if polyv is sf object.

polyv.att

String. Name of attribute in polyv to rasterize.

polyv.lut

Data frame. Lookup table of codes, if polyv.att is character or want to group codes. The lookup table must be data.frame including polyv.att and another column with classes.

rastfn.template

String. Full path name of raster to use as template for new raster.

validate

Logical. If TRUE, validates polyv and clippolyv before clipping. Uses sf::st_make_valid with default parameters (geos_method='valid_structure', geos_keep_collapsed=FALSE).

NODATA

Number. The NODATA value for background values. If NODATA is NULL, and a NODATA value is defined on the rastfn.template raster, the default is the defined NODATA value, else it is defined based on its datatype (see DEFAULT_NODATA for default data values).

outfolder

String. If exportshp=TRUE, name of output folder. If NULL, the working directory is used.

outfn

String. Name of output raster. If NULL, default is 'polyrast'.

outext

String. Name of raster extension (fmt).

outfn.pre

String. Add a prefix to output name (e.g., "01").

outfn.date

Logical. If TRUE, add date to end of outfile (e.g., outfn_'date'.csv).

overwrite

Logical. If TRUE and exportshp=TRUE, overwrite files in outfolder.

Value

A list containing raster and raster information derived from the original polygon.

Note

On-the-fly projection conversion
The spTransform (sf) method is used for on-the-fly map projection conversion and datum transformation using PROJ.4 arguments. Datum transformation only occurs if the +datum tag is present in the both the from and to PROJ.4 strings. The +towgs84 tag is used when no datum transformation is needed. PROJ.4 transformations assume NAD83 and WGS84 are identical unless other transformation parameters are specified. Be aware, providing inaccurate or incomplete CRS information may lead to erroneous data shifts when reprojecting. See spTransform help documentation for more details.

If exportshp=TRUE:
The st_write (sf) function is called. The ArcGIS driver truncates variable names to 10 characters or less. Variable names are changed before export using an internal function (trunc10shp). If Spatial object has more than 1 record, it will be returned but not exported.

Author

Tracey S. Frescino

Examples

# \donttest{
# Get polygon vector layer from FIESTA external data
WYbhdistfn <- system.file("extdata",
                          "sp_data/WYbighorn_districtbnd.shp", 
                          package = "FIESTA")

# Turn polygon into raster
# Note: raster values must be numeric, therefore names were changed to
# numeric codes based on lookup table produced from the following code.                      
new_rast <- spPoly2Rast(polyv = WYbhdistfn,
                        polyv.att = "DISTRICTNA",
                        outfolder = tempdir())
#> creating lookup table of codes
#> rasterize polyogns...
#> ================================================================================
# }