| Title: | Convert Images to Spatially Referenced 'SpatRaster' Objects |
|---|---|
| Description: | Convert digital images to spatially referenced 'SpatRaster' objects, as defined by the 'terra' package, using coordinates from supported spatial input classes. Supported inputs include numeric coordinate vectors and objects from the 'sf', 'terra' and 'stars' packages. The main function is an S3 generic, allowing other packages to extend support to additional spatial classes. |
| Authors: | Diego Hernangómez [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-8457-4658>) |
| Maintainer: | Diego Hernangómez <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.0 |
| Built: | 2026-10-12 23:12:28 UTC |
| Source: | https://github.com/dieghernan/rasterpic |
SpatRaster
Geotag an image and return a SpatRaster using coordinates
from a supported spatial input class.
rasterpic_img() is an S3 generic. See S3 methods for supported input
classes.
rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ... ) ## Default S3 method: rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ... ) ## S3 method for class 'sf' rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ..., mask = FALSE, inverse = FALSE ) ## S3 method for class 'sfc' rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ..., mask = FALSE, inverse = FALSE ) ## S3 method for class 'sfg' rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ..., mask = FALSE, inverse = FALSE, crs = NULL ) ## S3 method for class 'stars' rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ... ) ## S3 method for class 'bbox' rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ..., crs = NULL ) ## S3 method for class 'numeric' rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ..., crs = NULL ) ## S3 method for class 'SpatRaster' rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ... ) ## S3 method for class 'SpatVector' rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ..., mask = FALSE, inverse = FALSE ) ## S3 method for class 'SpatExtent' rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ..., crs = NULL )rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ... ) ## Default S3 method: rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ... ) ## S3 method for class 'sf' rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ..., mask = FALSE, inverse = FALSE ) ## S3 method for class 'sfc' rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ..., mask = FALSE, inverse = FALSE ) ## S3 method for class 'sfg' rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ..., mask = FALSE, inverse = FALSE, crs = NULL ) ## S3 method for class 'stars' rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ... ) ## S3 method for class 'bbox' rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ..., crs = NULL ) ## S3 method for class 'numeric' rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ..., crs = NULL ) ## S3 method for class 'SpatRaster' rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ... ) ## S3 method for class 'SpatVector' rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ..., mask = FALSE, inverse = FALSE ) ## S3 method for class 'SpatExtent' rasterpic_img( x, img, halign = 0.5, valign = 0.5, expand = 0, crop = FALSE, ..., crs = NULL )
x |
An R object. See S3 methods for supported classes. |
img |
An image to geotag. It can be a local file or a URL, for example
|
halign |
A number between |
valign |
A number between |
expand |
A nonnegative expansion factor for the bounding box of |
crop |
Logical. Should the raster be cropped to the (expanded) bounding
box of |
... |
Further arguments passed to methods. |
mask |
Logical for vector methods. Should the raster be
masked to the shape of |
inverse |
Logical. Only used when |
crs |
Character string describing a CRS. This parameter only applies
when |
vignette("rasterpic", package = "rasterpic") explains the effect of
parameters halign, valign, expand, crop and mask with examples.
All methods share the parameters, return value and examples documented on this page. rasterpic groups them by input behavior:
Vector methods for the sf classes sf,
sfc and sfg and the terra class
SpatVector can mask the image to the input shape.
Extent-like methods for bbox, numeric coordinate vectors,
the stars class stars and the
terra classes SpatRaster and
SpatExtent use the input bounding box.
The default method reports unsupported classes. Other packages can provide methods for additional spatial classes.
This function preserves the CRS of x when applicable. For optimal results,
do not use geographic coordinates (longitude/latitude).
crs can be in WKT format, as an "authority:number" code such as
"EPSG:4326" or as a PROJ-string such as "+proj=utm +zone=12". It can also
be retrieved with:
See the Value and Notes sections in terra::crs().
A SpatRaster object containing the image layers of img:
If img has at least 3 layers, the result names layers 1 to 3 "r", "g"
and "b" for the red, green and blue channels and names layer 4 "alpha"
when applicable.
If img already has an RGB specification (this may be the case for
tif/tiff files), the result keeps that specification.
If img has at least 3 layers, the resulting SpatRaster has an RGB
specification as explained in terra::RGB().
vignette("rasterpic", package = "rasterpic") for examples.
From sf:
vignette("sf1", package = "sf") to understand how sf organizes
R objects.
From stars:
From terra:
For plotting:
Other image geotagging tools:
asp_ratio()
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") # Use the default configuration. ex1 <- rasterpic_img(x, img) ex1 autoplot(ex1) + geom_sf(data = x, fill = NA, color = "white", linewidth = 0.5) # Expand the bounding box. ex2 <- rasterpic_img(x, img, expand = 0.5) autoplot(ex2) + geom_sf(data = x, fill = NA, color = "white", linewidth = 0.5) # Align the image to the left edge. ex3 <- rasterpic_img(x, img, halign = 0) autoplot(ex3) + geom_sf(data = x, fill = NA, color = "white", linewidth = 0.5) + labs(title = "Align") # Crop to the bounding box. ex4 <- rasterpic_img(x, img, crop = TRUE) autoplot(ex4) + geom_sf(data = x, fill = NA, color = "white", linewidth = 0.5) + labs(title = "Crop") # Mask to the vector shape. ex5 <- rasterpic_img(x, img, mask = TRUE) autoplot(ex5) + geom_sf(data = x, fill = NA, color = "white", linewidth = 0.5) + labs(title = "Mask") # Mask inside the vector shape. ex6 <- rasterpic_img(x, img, mask = TRUE, inverse = TRUE) autoplot(ex6) + geom_sf(data = x, fill = NA, color = "white", linewidth = 0.5) + labs(title = "Mask Inverse") # Combine cropping and inverse masking. ex7 <- rasterpic_img(x, img, crop = TRUE, mask = TRUE, inverse = TRUE) autoplot(ex7) + geom_sf(data = x, fill = NA, color = "white", linewidth = 0.5) + labs(title = "Combine")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") # Use the default configuration. ex1 <- rasterpic_img(x, img) ex1 autoplot(ex1) + geom_sf(data = x, fill = NA, color = "white", linewidth = 0.5) # Expand the bounding box. ex2 <- rasterpic_img(x, img, expand = 0.5) autoplot(ex2) + geom_sf(data = x, fill = NA, color = "white", linewidth = 0.5) # Align the image to the left edge. ex3 <- rasterpic_img(x, img, halign = 0) autoplot(ex3) + geom_sf(data = x, fill = NA, color = "white", linewidth = 0.5) + labs(title = "Align") # Crop to the bounding box. ex4 <- rasterpic_img(x, img, crop = TRUE) autoplot(ex4) + geom_sf(data = x, fill = NA, color = "white", linewidth = 0.5) + labs(title = "Crop") # Mask to the vector shape. ex5 <- rasterpic_img(x, img, mask = TRUE) autoplot(ex5) + geom_sf(data = x, fill = NA, color = "white", linewidth = 0.5) + labs(title = "Mask") # Mask inside the vector shape. ex6 <- rasterpic_img(x, img, mask = TRUE, inverse = TRUE) autoplot(ex6) + geom_sf(data = x, fill = NA, color = "white", linewidth = 0.5) + labs(title = "Mask Inverse") # Combine cropping and inverse masking. ex7 <- rasterpic_img(x, img, crop = TRUE, mask = TRUE, inverse = TRUE) autoplot(ex7) + geom_sf(data = x, fill = NA, color = "white", linewidth = 0.5) + labs(title = "Combine")