Package 'camalienr'

Title: Functions Used in the Plant Module of the Biodiversa+ Pilot on Invasive Alien Species
Description: Tools used in the work with the vehicle mounted CamAlien system for monitoring plants. The package provides functions to interact with the CamAlien database.
Authors: Lars Dalby [aut, cre] (ORCID: <https://orcid.org/0000-0002-7270-6999>)
Maintainer: Lars Dalby <[email protected]>
License: MIT + file LICENSE
Version: 0.4.2
Built: 2026-06-14 07:59:47 UTC
Source: https://gitlab.au.dk/ecos/biodiversa/camalien/camalienr

Help Index


Convert data.frame to Simple Feature collection

Description

Convert data.frame to Simple Feature collection by converting the position column (pg_geometry) to sfc.

Usage

ca_as_sf(tbl)

Arguments

tbl

The data.frame to be converted

Value

tbl as Simple Feature collection


Connect to the camalien database

Description

Establish a connection to the camalien database. This function is a wrapper around DBI::dbConnect. The function will create a connection using RPostgres::Postgres as a driver. For the function to work you need to have the following environment variables set in your .REnviron file:

  • CAMALIEN_USER

  • CAMALIEN_PASSWORD

Usage

ca_connect()

Details

To modify or create an .REnviron file we suggest you use usethis::edit_r_environ. Calling this function from RStudio will open the file for you to edit. Simply insert the variables, save and restart R.

Value

PqDriver


Disconnect from the database

Description

This is a simple wrapper around DBI::dbDisconnect to use when disconnecting from a database. It is good practice to disconnect when no longer using the connection.

Usage

ca_disconnect(con)

Arguments

con

DBIConnection The connection object which in the case of camalien will be a PqConnection.

Value

TRUE, invisibly


Get tables from the camalien database

Description

Get tables from the data schema of the camalien database. This function only return a lazy table and will not download any data to your session. Use dplyr::collect when you actually want to download the data. Normally you would want to do some selection and filtering before calling collect()

Usage

ca_get_image(con)

ca_get_job(con)

ca_get_plantnetcall(con)

ca_get_detection(con)

ca_get_detectionsummary(con)

ca_get_imagemeta(con)

ca_get_species(con)

ca_get_chunk(con)

ca_get_partner(con)

Arguments

con

PqConnection The connection to the camalien database

Value

A lazy table


Create and return a table for filtering rows to a partner

Description

A common task is to filter e.g. detections made within one of the partner countries. This involves a number of joins to carry the partner name all the way through to the image or detection table. This function sets up the required joins and can filter the resulting table to a single partner by supplying the partner_name argument.

Usage

ca_get_join_tbl(con, partner_name = NULL)

Arguments

con

PqConnection The connection to the camalien database

partner_name

chr Name of the partner

Value

A lazy table


Extract polygon information from Plantnet response

Description

This function will extract the relevant information from a JSON file returned from the Plantnet API.

Usage

ca_get_polygon(pn_json)

Arguments

pn_json

The JSON object (read by e.g. jsonlite::read_json)

Value

list of center_x, center_y and size


Plot image and polygon with detection

Description

Draw an image and show the polygon where the detection was made as a polygon on top of the image.

Usage

ca_img_detection(data, height = 3000L, border = NA, col = NA, lwd = 4)

Arguments

data

data.frame Data.frame with imageurl, centerx, centery and size.

height

Integer specifying the height of the desired plot. Width is scaled proportionally.

border

chr Polygon outline color

col

chr Color to use if plotting filled polygons. The format is #RRGGBBAA where AA is the alpha channel for transparency

lwd

int Polygon outline line width

Examples

## Not run: 
images[1,] |>
  ca_img_detection(height = 500, border = "red")

images |>
  ca_img_detection(height = 500, col ="#FF000040")
  
## End(Not run)

Draw polygons on top of images

Description

Draw a polygon and annotate it with ID

Usage

ca_img_draw_polygon(
  center_x,
  center_y,
  size,
  border = "red",
  col,
  lwd = 4,
  scale
)

Arguments

center_x, center_y

int coordinates of the centre of the polygon

size

int size of the polygon

border

chr Polygon outline color

col

chr Color to use if plotting filled polygons. The format is #RRGGBBAA where AA is the alpha channel for transparency

lwd

int Polygon outline line width

scale

double Scaling factor if the image is not rendered full size


Plot grid of images

Description

Plot a grid of multiple images.

Usage

ca_img_grid(img_paths, ncol = 3, nrow = 3, height = 300L)

Arguments

img_paths

Vector of paths (can be URLs). Should be of length nrow * ncol.

ncol, nrow

Int, the number of rows and columns

height

Integer specifying the height of the desired plot. Width is scaled proportionally.

Examples

## Not run: 
images$imageurl |>
ca_img_grid(ncol = 5, nrow = 1)

## End(Not run)

Plot image

Description

Plot and optionally scale image

Usage

ca_img_plot(img, height = 3000L)

Arguments

img

magick-image object read by ca_img_read()

height

Integer specifying the height of the desired plot. Width is scaled proportionally.


Read image from URL

Description

Read image file for viewing or further manipulation

Usage

ca_img_read(url)

Arguments

url

URL to the image to be read.

Value

magick-image object


Read state files from CamAlien uploader

Description

A simple wrapper around jsonlite::read_json().

Usage

ca_json_read(path)

Arguments

path

chr Path to the json file to read.

Value

list with content of json file being read


Parse json files with info on chunks

Description

Parse json files with information about the individual chunks of images from the CamAlien uploader.

Usage

ca_parse_chunks(path)

Arguments

path

chr Path to the json file to read.

Details

The function will read all the chunk files in the directory at path.

Value

data.frame with content of json files being read


Parse json files with info on the job

Description

Parse json files with information about the job from the CamAlien uploader.

Usage

ca_parse_job(path)

Arguments

path

chr Path to the json file to read.

Value

data.frame with content of json file being read


Get statistics on images

Description

Calculate statistics on the images uploaded by each partner

Usage

ca_stat_images(con)

Arguments

con

PqConnection The connection to the camalien database

Value

lazy_tbl


Get statistics on upload jobs

Description

Calculate sizes on the upload jobs that each partner has started. There is some inaccuracies relating to this as we can't see if images in one job are also part of other jobs.

Usage

ca_stat_jobsize(con)

Arguments

con

PqConnection The connection to the camalien database

Details

The Danish images were uploaded with an early version of the uploader which did not report the size of the job and therefore total_size is NA here.

Value

lazy_tbl


Get statistics on tagged images

Description

Calculate the number and ratio of tagged images.

Usage

ca_stat_tagged(con)

Arguments

con

PqConnection The connection to the camalien database

Value

lazy_tbl with partner, n_tagged, n and prop


List of target species for all partners

Description

Each partner has their own list of invasive alien species which they targeted during the field campaign. The same species can be on the list of multiple partners. Target species can be an entire genus. The scientific and the canonical name are from a lookup on GBIF.

Usage

ias

Format

Data.frame with gbifid and name

partner

chr, The partner name

canonical_name

chr, The canonical name from GBIF with the modification that for genera we add .sp

scientificname

chr, The scientific taxon name

gbifid

int, The gbifid used by Plantnet and in the camalien database

pnid

int, The ID used by Plantnet (hence pnid) and in the camalien database

taxon_name_key

chr, The GBIF key for the taxon name

taxon_rank

chr, The taxon rank, either species or genus. Derived from GBIF rankMarker


Draw an ID box on an image

Description

Modified graphics::legend function to draw small boxes with ID information on top of images

Usage

id_box(
  x,
  y = NULL,
  legend,
  col = par("col"),
  border = "black",
  angle = 45,
  bty = "o",
  bg = "white",
  box.lwd = par("lwd"),
  box.lty = par("lty"),
  box.col = par("fg"),
  cex = 1,
  xjust = 0,
  yjust = 1,
  adj = c(0.25, 0),
  text.width = NULL,
  text.col = par("col"),
  text.font = NULL,
  ncol = 1,
  horiz = FALSE,
  inset = 0,
  xpd,
  seg.len = 1
)

Arguments

x, y

the x and y co-ordinates to be used to position the legend. They can be specified by keyword or in any way which is accepted by xy.coords: See ‘Details’.

legend

a character or expression vector of length 1\ge 1 to appear in the legend. Other objects will be coerced by as.graphicsAnnot.

col

the color of points or lines appearing in the legend.

border

the border color for the boxes (used only if fill is specified).

angle

angle of shading lines.

bty

the type of box to be drawn around the legend. The allowed values are "o" (the default) and "n".

bg

the background color for the legend box. (Note that this is only used if bty != "n".)

box.lty, box.lwd, box.col

the line type, width and color for the legend box (if bty = "o").

cex

character expansion factor relative to current par("cex"). Used for text, and provides the default for pt.cex.

xjust

how the legend is to be justified relative to the legend x location. A value of 0 means left justified, 0.5 means centered and 1 means right justified.

yjust

the same as xjust for the legend y location.

adj

numeric of length 1 or 2; the string adjustment for legend text. Useful for y-adjustment when labels are plotmath expressions.

text.width

the width of the legend text in x ("user") coordinates. (Should be positive even for a reversed x axis.) Can be a single positive numeric value (same width for each column of the legend), a vector (one element for each column of the legend), NULL (default) for computing a proper maximum value of strwidth(legend)), or NA for computing a proper column wise maximum value of strwidth(legend)).

text.col

the color used for the legend text.

text.font

the font used for the legend text, see text.

ncol

the number of columns in which to set the legend items (default is 1, a vertical legend).

horiz

logical; if TRUE, set the legend horizontally rather than vertically (specifying horiz overrides the ncol specification).

inset

inset distance(s) from the margins as a fraction of the plot region when legend is placed by keyword.

xpd

if supplied, a value of the graphical parameter xpd to be used while the legend is being drawn.

seg.len

the length of lines drawn to illustrate lty and/or lwd (in units of character widths).


Extract of detections and image metadata

Description

Small example dataset with scores, boxes and urls.

Usage

images

Format

Data.frame with the following columns:

callid

chr, ID of the Plantnet call

score

float, The model confidence score from Plantnet

boxsize

int, Size of the polygon where the detection was made

centerx

int, The center x coordinate of the polygon

centery

int, The center y coordinate of the polygon

speciesid

int, The plantnet species ID

imageid

int, The image ID

imageurl

int, The public URL to the image file


List of partners and their acronyms

Description

List of partners and their acronyms

Usage

partners

Format

Data.frame with partner and partner_acronym

partner

chr, Name of the partner

partner_acronym

chr, The acronym used in the Biodiversa+ project