Learn R Programming

AgePopDenom (version 0.4.0)

rasterize_data: Rasterize Spatial Data

Description

This function converts spatial data with x, y coordinates and a value field into a raster using a specified resolution and CRS.

Usage

rasterize_data(x_coords, y_coords, values, cell_size = 5000, crs, fun = mean)

Value

A `terra::SpatRaster` object.

Arguments

x_coords

Numeric vector of x-coordinates (e.g., longitude).

y_coords

Numeric vector of y-coordinates (e.g., latitude).

values

Numeric vector of values associated with each point.

cell_size

Numeric. Grid cell size in meters (default: 5000).

crs

Character, the coordinate reference system in EPSG format (e.g., "EPSG:3857").

fun

Function to aggregate values in cells (default is `mean`).

Examples

Run this code

# \donttest{
x_coords <- runif(100, -100, 100)
y_coords <- runif(100, -50, 50)
values <- rnorm(100, mean = 10, sd = 5)

rasterize_data(x_coords, y_coords, values,
               cell_size = 5000, crs = "EPSG:3857", fun = mean)
# }

Run the code above in your browser using DataLab