sf2raster {SUNGEO} | R Documentation |
Convert simple features object into regularly spaced raster
Description
This function takes in an sf
spatial object (polygon or point) and returns a regularly spaced RasterLayer. Reverse translation option allows users to create an sf
polygon object from the regularly spaced RasterLayer. This function can also conver the sf
object into a cartogram with a user-specified variable name.
Usage
sf2raster(
polyz_from = NULL,
pointz_from = NULL,
input_variable = NULL,
reverse = FALSE,
poly_to = NULL,
return_output = NULL,
return_field = NULL,
aggregate_function = list(function(x) mean(x, na.rm = TRUE)),
reverse_function = list(function(x) mean(x, na.rm = TRUE)),
grid_dim = c(1000, 1000),
cartogram = FALSE,
carto_var = NULL,
message_out = TRUE,
return_list = FALSE
)
Arguments
polyz_from |
Source polygon layer. |
pointz_from |
Source point layer. |
input_variable |
Name of input variable from source layer. Character string. |
reverse |
Reverse translation from raster layer to |
poly_to |
Destination polygon layer for reverse conversion. Must be specified if |
return_output |
Return output for reverse conversion. Must be specified if |
return_field |
Return field for reverse conversion. Must be specified if |
aggregate_function |
Aggregation function to be applied to variables specified in |
reverse_function |
Aggregation function for reverse conversion. Must be specified if |
grid_dim |
Dimensions of raster grid. Numerical vector of length 2 (number of rows, number of columns). Default is |
cartogram |
Cartogram transformation. Logical. Default is |
carto_var |
Input variable for cartogram transformation. Must be specified if |
message_out |
Print informational messages. Logical. Default is |
return_list |
Return full set of results, including input polygons, centroids and field raster. Default is |
Value
If return_list=FALSE
(default) and reverse=FALSE
(default), returns RasterLayer
object, with cell values corresponding to input_variable
.
If return_list=TRUE
and input layer is polygon, returns a list containing
"return_output". Output raster, with values corresponding to
input_variable
.RasterLayer
object."return_centroid". Raster of centroids, with values corresponding to
input_variable
.RasterLayer
object."poly_to". Source polygons, with columns corresponding to
input_variable
and auto-generated numerical IDField
.sf
object."return_field". Output raster, with values corresponding to auto-generated numerical ID
Field
.RasterLayer
object.
If return_list=TRUE
and input layer is points, returns a list containing
"return_output". Output raster, with values corresponding to
input_variable
.RasterLayer
object."return_point". Source points, with column corresponding to
input_variable
.
If reverse=TRUE
, returns an sf
polygon layer, with columns corresponding to input_variable
and auto-generated numerical ID Field
.
Examples
# Rasterization of polygon layer.
## Not run:
data(clea_deu2009)
out_1 <- sf2raster(polyz_from = utm_select(clea_deu2009),
input_variable = "to1")
terra::plot(out_1)
## End(Not run)
# Rasterization of point layer
## Not run:
data(clea_deu2009_pt)
out_2 <- sf2raster(pointz_from = utm_select(clea_deu2009_pt),
input_variable = "to1",
grid_dim = c(25,25))
terra::plot(out_2)
## End(Not run)
# Cartogram (vote turnout scaled by number of valid votes)
## Not run:
out_3 <- sf2raster(polyz_from = utm_select(clea_deu2009),
input_variable = "to1",
cartogram = TRUE,
carto_var = "vv1")
terra::plot(out_3)
## End(Not run)
# Polygonization of cartogram raster
## Not run:
out_4a <- sf2raster(polyz_from = utm_select(clea_deu2009),
input_variable = "to1",
cartogram = TRUE,
carto_var = "vv1",
return_list = TRUE)
out_4 <- sf2raster(reverse = TRUE,
poly_to = out_4a$poly_to,
return_output = out_4a$return_output,
return_field = out_4a$return_field)
terra::plot(out_4)
## End(Not run)