MyNixOS website logo
Description

Convert Digital Images into 'SpatRaster' Objects.

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

rasterpic

CRANstatus CRANresults R-CMD-check R-hub codecov r-universe CodeFactor DOI Project Status: Active – The project has reached a stable, usablestate and is being activelydeveloped. status

rasterpic is a tiny package with one single goal: to transform an image into a SpatRaster object (see ?terra::SpatRaster).

Installation

Install rasterpic from CRAN:

install.packages("rasterpic")

You can install the developing version of rasterpic with:

remotes::install_github("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"
))

Example

This package allows you to create cool maps by using a wide variety of objects:

  • Spatial object created with the sf package: sf, sfc, sfg or bbox.
  • Spatial objects created with the terra package: SpatRaster, SpatVector, SpatExtent.
  • A vector of coordinates with the form 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 
#> dimensions  : 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       : lyr.1, lyr.2, lyr.3 
#> 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)

Image formats admitted

rasterpic can parse the following image formats:

  • png files.
  • jpg/jpeg files.
  • tif/tiff files.

Citation

Hernangómez D (2024). rasterpic: Convert Digital Images into SpatRaster Objects. doi:10.5281/zenodo.5910095, https://dieghernan.github.io/rasterpic/.

A BibTeX entry for LaTeX users is:

@Manual{R-rasterpic,
  title = {{rasterpic}: Convert Digital Images into {SpatRaster} Objects},
  author = {Diego Hernangómez},
  year = {2024},
  version = {0.2.5},
  doi = {10.5281/zenodo.5910095},
  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.},
}
Metadata

Version

0.2.5

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows