rasterpic is a tiny package with one single goal: to transform an image into a SpatRaster
object (see ?terra::SpatRaster
).
Install rasterpic from CRAN:
install.packages("rasterpic")
You can install the developing version of rasterpic with:
# install.packages("pak") pak::pak("dieghernan/rasterpic")
Alternatively, you can install rasterpic using the r-universe:
# Install rasterpic in R: install.packages("rasterpic", repos = c( "https://dieghernan.r-universe.dev", "https://cloud.r-project.org" ) )
This package allows you to create cool maps by using a wide variety of objects:
sf
, sfc
, sfg
or bbox
.SpatRaster
, SpatVector
, SpatExtent
.c(xmin, ymin, xmax, yman)
An example using an sf
object:
library(rasterpic) library(sf) library(terra) # The flag of the United Kingdom img <- system.file("img/UK_flag.png", package = "rasterpic") uk <- read_sf(system.file("gpkg/UK.gpkg", package = "rasterpic")) class(uk) #> [1] "sf" "tbl_df" "tbl" "data.frame" # Rasterize! uk_flag <- rasterpic_img(uk, img) uk_flag #> class : SpatRaster #> size : 400, 800, 3 (nrow, ncol, nlyr) #> resolution : 5398.319, 5398.319 (x, y) #> extent : -2542183, 1776472, 6430573, 8589900 (xmin, xmax, ymin, ymax) #> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857) #> source(s) : memory #> colors RGB : 1, 2, 3 #> names : r, g, b #> min values : 0, 14, 35 #> max values : 255, 255, 255 # Plot it! # Using ggplot2 + tidyterra library(tidyterra) library(ggplot2) autoplot(uk_flag) + geom_sf(data = uk, color = alpha("blue", 0.5))
We can also play with other parameters, as well as modifying the alignment of the image with respect to the object:
# Align, crop and mask uk_flag2 <- rasterpic_img(uk, img, halign = 0.2, crop = TRUE, mask = TRUE) autoplot(uk_flag2) + geom_sf(data = uk, fill = NA)
rasterpic can parse the following image formats:
png
files.jpg/jpeg
files.tif/tiff
files.Hernangómez D (2025). rasterpic: Convert Digital Images into SpatRaster Objects. doi:10.32614/CRAN.package.rasterpic, https://dieghernan.github.io/rasterpic/.
A BibTeX entry for LaTeX users is:
@Manual{R-rasterpic,
title = {{rasterpic}: Convert Digital Images into {SpatRaster} Objects},
doi = {10.32614/CRAN.package.rasterpic},
author = {Diego Hernangómez},
year = {2025},
version = {0.3.0},
url = {https://dieghernan.github.io/rasterpic/},
abstract = {Generate SpatRaster objects, as defined by the terra package, from digital images, using a specified spatial object as a geographical reference.},
}
All contributions to this project are gratefully acknowledged using the allcontributors
package following the allcontributors specification. Contributions of any kind are welcome!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4