Title: Convert Digital Images into 'SpatRaster' Objects
Version: 0.4.0
Description: Generate 'SpatRaster' objects, as defined by the 'terra' package, from digital images, using a specified spatial object as a geographical reference.
License: MIT + file LICENSE
URL: https://dieghernan.github.io/rasterpic/, https://github.com/dieghernan/rasterpic
BugReports: https://github.com/dieghernan/rasterpic/issues
Depends: R (≥ 4.1.0)
Imports: png, sf (≥ 1.0.0), terra (≥ 1.8-21)
Suggests: ggplot2, knitr, quarto, testthat (≥ 3.0.0), tidyterra
VignetteBuilder: knitr, quarto
Config/Needs/check: curl
Config/Needs/coverage: curl
Config/Needs/website: dieghernan/gitdevr, tmap, mapsf, maptiles, devtools, curl, remotes, cpp11
Config/testthat/edition: 3
Config/testthat/parallel: true
Encoding: UTF-8
RoxygenNote: 7.3.3
X-schema.org-keywords: cran, jpeg, jpg, maps, png, r, r-package, r-stats, raster, rstats, sf, terra, tif, tiff, cran-r
NeedsCompilation: no
Packaged: 2026-03-21 11:10:46 UTC; diego
Author: Diego Hernangómez ORCID iD [aut, cre, cph]
Maintainer: Diego Hernangómez <diego.hernangomezherrero@gmail.com>
Repository: CRAN
Date/Publication: 2026-03-21 11:20:02 UTC

rasterpic: Convert Digital Images into 'SpatRaster' Objects

Description

logo

Generate 'SpatRaster' objects, as defined by the 'terra' package, from digital images, using a specified spatial object as a geographical reference.

Author(s)

Maintainer: Diego Hernangómez diego.hernangomezherrero@gmail.com (ORCID) [copyright holder]

See Also

Useful links:


Compute aspect ratio for spatial input

Description

Helper function. Ratio is computed as width / height (or cols / rows).

Usage

asp_ratio(x)

Arguments

x

A SpatRaster object, an sf/sfc object, or a numeric vector of length 4 with coordinates c(xmin, ymin, xmax, ymax), as created by sf::st_bbox().

Value

The aspect ratio.

Examples


library(terra)

x <- rast(system.file("tiff/elev.tiff", package = "rasterpic"))
plot(x)
asp_ratio(x)


Convert an image to a geo-tagged SpatRaster

Description

Geotags an image based on the coordinates of a given spatial object.

Usage

rasterpic_img(
  x,
  img,
  halign = 0.5,
  valign = 0.5,
  expand = 0,
  crop = FALSE,
  mask = FALSE,
  inverse = FALSE,
  crs = NULL
)

Arguments

x

R object that may be:

img

An image to be geotagged. It can be a local file or an online file (e.g. "https://i.imgur.com/6yHmlwT.jpeg"). The following image extensions are accepted:

  • png.

  • jpeg/jpg.

  • tiff/tif.

halign

Numeric between 0 and 1 giving horizontal alignment of img relative to x. 0 aligns x with the left edge, 1 aligns with the right edge, and 0.5 centers horizontally.

valign

Numeric between 0 and 1 giving vertical alignment of img relative to x. 0 aligns x with the bottom edge, 1 aligns with the top edge, and 0.5 centers vertically.

expand

An expansion factor of the bounding box of x. 0 means that no expansion is added, 1 means that the bounding box is expanded to double the original size. See Details.

crop

Logical. Should the raster be cropped to the (expanded) bounding box of x? See Details.

mask

Logical, applicable only if x is a sf, sfc or SpatVector object. Should the raster be masked to x? See Details.

inverse

Logical. It only affects when mask = TRUE. If TRUE, areas on the raster that do not overlap with x are masked.

crs

Character string describing a coordinate reference system. This parameter only affects when x is a SpatExtent, sfg, bbox or a vector of coordinates. See CRS section.

Details

vignette("rasterpic", package = "rasterpic") explains, with examples, the effect of parameters halign, valign, expand, crop and mask.

CRS

The function preserves the Coordinate Reference System of x if applicable. For optimal results do not use geographic coordinates (longitude/latitude).

crs can be in a WKT format, as a "authority:number" code such as "EPSG:4326", or a PROJ-string format such as "+proj=utm +zone=12". It can also be retrieved with:

See Value and Notes on terra::crs().

Value

A SpatRaster object (see terra::rast()) where each layer corresponds to a color channel of img:

The resulting SpatRaster will have an RGB specification as explained in terra::RGB().

See Also

vignette("rasterpic", package = "rasterpic") for examples.

From sf:

From terra:

For plotting:

Examples


library(sf)
library(terra)
library(ggplot2)
library(tidyterra)


x_path <- system.file("gpkg/UK.gpkg", package = "rasterpic")
x <- st_read(x_path, quiet = TRUE)
img <- system.file("img/vertical.png", package = "rasterpic")

# Default config
ex1 <- rasterpic_img(x, img)

ex1

autoplot(ex1) +
  geom_sf(data = x, fill = NA, color = "white", linewidth = .5)


# Expand
ex2 <- rasterpic_img(x, img, expand = 0.5)

autoplot(ex2) +
  geom_sf(data = x, fill = NA, color = "white", linewidth = .5)


# Align
ex3 <- rasterpic_img(x, img, halign = 0)

autoplot(ex3) +
  geom_sf(data = x, fill = NA, color = "white", linewidth = .5)
labs(title = "Align")

# Crop
ex4 <- rasterpic_img(x, img, crop = TRUE)

autoplot(ex4) +
  geom_sf(data = x, fill = NA, color = "white", linewidth = .5) +
  labs(title = "Crop")

# Mask
ex5 <- rasterpic_img(x, img, mask = TRUE)

autoplot(ex5) +
  geom_sf(data = x, fill = NA, color = "white", linewidth = .5) +
  labs(title = "Mask")

# Mask inverse
ex6 <- rasterpic_img(x, img, mask = TRUE, inverse = TRUE)

autoplot(ex6) +
  geom_sf(data = x, fill = NA, color = "white", linewidth = .5) +
  labs(title = "Mask Inverse")

# Combine Mask inverse and crop
ex7 <- rasterpic_img(x, img, crop = TRUE, mask = TRUE, inverse = TRUE)

autoplot(ex7) +
  geom_sf(data = x, fill = NA, color = "white", linewidth = .5) +
  labs(title = "Combine")

# RGB channels ------
plot(ex1)
ex_rgb <- ex1
has.RGB(ex_rgb)
RGB(ex_rgb)

# Modify RGB channels
RGB(ex_rgb) <- c(2, 3, 1)
RGB(ex_rgb)

plot(ex_rgb)

# Remove RGB channels
RGB(ex_rgb) <- NULL
has.RGB(ex_rgb)
RGB(ex_rgb)

# Note the difference with terra::plot
plot(ex_rgb)