Creates continuous density raster surfaces by joining vector spatial layers with tabular data frameworks, normalizing values by ellipsoidal polygon area calculations. Reconciles discrete boundary-constrained census or survey counts with uniform grids. Accepts spatial objects in-memory or via file paths. Methods are based on spatial rasterization workflows implemented in the 'terra' package Hijmans (2025) < https://rspatial.github.io/terra/>.
Census and survey counts are usually published per administrative unit (districts, states, tracts), while environmental data come as regular raster grids. GeoDensityR joins a table of counts to a polygon layer, divides each count by the polygon's area on the ellipsoid, and writes the resulting density onto a raster of the resolution you choose. The result is a terra SpatRaster that can be stacked with climate or land-cover layers.
From CRAN:
install.packages("GeoDensityR")
Development version from GitHub:
# install.packages("remotes")
remotes::install_github("sahalpaladan/GeoDensityR")
library(GeoDensityR)
library(terra)
# Four districts as polygons, and a table of populations
districts <- terra::vect(
c("POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))",
"POLYGON((2 0, 4 0, 4 2, 2 2, 2 0))",
"POLYGON((0 2, 4 2, 4 3, 0 3, 0 2))",
"POLYGON((0 3, 4 3, 4 4, 0 4, 0 3))"),
crs = "EPSG:4326"
)
districts$district_id <- c("D1", "D2", "D3", "D4")
census <- data.frame(
ID = c("D1", "D2", "D3", "D4"),
population = c(120000, 45000, 300000, 80000)
)
dens <- generate_density_raster(
spatial_target = districts,
attribute_table = census,
join_spatial = "district_id",
join_attribute = "ID",
value_col = "population",
resolution = 0.1
)
terra::plot(dens, main = "People per square kilometre")
spatial_target can be a terra::SpatVector, an sf object or a path to a shapefile, and attribute_table can be a data frame or a path to a CSV file. Use output = "density.tif" to write the result to disk.
resolution is in the units of the layer's coordinate reference system. For degrees, reproject first: v <- terra::project(terra::vect("boundaries.shp"), "EPSG:4326"). Areas are always computed on the ellipsoid."36,438" is read as text. Remove the separators first: tab$count <- as.numeric(gsub(",", "", tab$count)).NA and a warning.See vignette("GeoDensityR") for a longer walk-through.
Paladan S, Chanda MM (2026). GeoDensityR: Generate Density Rasters from Polygon and Census Data. R package version 0.2.2. https://doi.org/10.32614/CRAN.package.GeoDensityR
Bug reports and pull requests are welcome. See CONTRIBUTING.md.
MIT