| 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 |
Convert data.frame to Simple Feature collection by converting the position column (pg_geometry) to sfc.
ca_as_sf(tbl)ca_as_sf(tbl)
tbl |
The data.frame to be converted |
tbl as Simple Feature collection
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
ca_connect()ca_connect()
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.
PqDriver
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.
ca_disconnect(con)ca_disconnect(con)
con |
DBIConnection The connection object which in the case of camalien will be a PqConnection. |
TRUE, invisibly
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()
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)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)
con |
PqConnection The connection to the camalien database |
A lazy table
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.
ca_get_join_tbl(con, partner_name = NULL)ca_get_join_tbl(con, partner_name = NULL)
con |
PqConnection The connection to the camalien database |
partner_name |
chr Name of the partner |
A lazy table
This function will extract the relevant information from a JSON file returned from the Plantnet API.
ca_get_polygon(pn_json)ca_get_polygon(pn_json)
pn_json |
The JSON object (read by e.g. jsonlite::read_json) |
list of center_x, center_y and size
Draw an image and show the polygon where the detection was made as a polygon on top of the image.
ca_img_detection(data, height = 3000L, border = NA, col = NA, lwd = 4)ca_img_detection(data, height = 3000L, border = NA, col = NA, lwd = 4)
data |
data.frame Data.frame with |
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 |
## Not run: images[1,] |> ca_img_detection(height = 500, border = "red") images |> ca_img_detection(height = 500, col ="#FF000040") ## End(Not run)## Not run: images[1,] |> ca_img_detection(height = 500, border = "red") images |> ca_img_detection(height = 500, col ="#FF000040") ## End(Not run)
Draw a polygon and annotate it with ID
ca_img_draw_polygon( center_x, center_y, size, border = "red", col, lwd = 4, scale )ca_img_draw_polygon( center_x, center_y, size, border = "red", col, lwd = 4, scale )
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 a grid of multiple images.
ca_img_grid(img_paths, ncol = 3, nrow = 3, height = 300L)ca_img_grid(img_paths, ncol = 3, nrow = 3, height = 300L)
img_paths |
Vector of paths (can be URLs). Should be of length
|
ncol, nrow
|
Int, the number of rows and columns |
height |
Integer specifying the height of the desired plot. Width is scaled proportionally. |
## Not run: images$imageurl |> ca_img_grid(ncol = 5, nrow = 1) ## End(Not run)## Not run: images$imageurl |> ca_img_grid(ncol = 5, nrow = 1) ## End(Not run)
Plot and optionally scale image
ca_img_plot(img, height = 3000L)ca_img_plot(img, height = 3000L)
img |
magick-image object read by |
height |
Integer specifying the height of the desired plot. Width is scaled proportionally. |
Read image file for viewing or further manipulation
ca_img_read(url)ca_img_read(url)
url |
URL to the image to be read. |
magick-image object
A simple wrapper around jsonlite::read_json().
ca_json_read(path)ca_json_read(path)
path |
chr Path to the json file to read. |
list with content of json file being read
Parse json files with information about the individual chunks of images from the CamAlien uploader.
ca_parse_chunks(path)ca_parse_chunks(path)
path |
chr Path to the json file to read. |
The function will read all the chunk files in the directory at
path.
data.frame with content of json files being read
Parse json files with information about the job from the CamAlien uploader.
ca_parse_job(path)ca_parse_job(path)
path |
chr Path to the json file to read. |
data.frame with content of json file being read
Calculate statistics on the images uploaded by each partner
ca_stat_images(con)ca_stat_images(con)
con |
PqConnection The connection to the camalien database |
lazy_tbl
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.
ca_stat_jobsize(con)ca_stat_jobsize(con)
con |
PqConnection The connection to the camalien database |
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.
lazy_tbl
Calculate the number and ratio of tagged images.
ca_stat_tagged(con)ca_stat_tagged(con)
con |
PqConnection The connection to the camalien database |
lazy_tbl with partner, n_tagged, n and prop
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.
iasias
Data.frame with gbifid and name
chr, The partner name
chr, The canonical name from GBIF with the modification that for genera we add .sp
chr, The scientific taxon name
int, The gbifid used by Plantnet and in the camalien database
int, The ID used by Plantnet (hence pnid) and in the camalien database
chr, The GBIF key for the taxon name
chr, The taxon rank, either species or genus. Derived from GBIF rankMarker
Modified graphics::legend function to draw small boxes with ID information on top of images
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 )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 )
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
|
legend |
a character or expression vector
of length |
col |
the color of points or lines appearing in the legend. |
border |
the border color for the boxes (used only if
|
angle |
angle of shading lines. |
bty |
the type of box to be drawn around the legend. The allowed
values are |
bg |
the background color for the legend box. (Note that this is
only used if |
box.lty, box.lwd, box.col
|
the line type, width and color for
the legend box (if |
cex |
character expansion factor relative to current
|
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 |
adj |
numeric of length 1 or 2; the string adjustment for legend
text. Useful for y-adjustment when |
text.width |
the width of the legend text in x ( |
text.col |
the color used for the legend text. |
text.font |
the font used for the legend text, see |
ncol |
the number of columns in which to set the legend items (default is 1, a vertical legend). |
horiz |
logical; if |
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 |
seg.len |
the length of lines drawn to illustrate |
Small example dataset with scores, boxes and urls.
imagesimages
Data.frame with the following columns:
chr, ID of the Plantnet call
float, The model confidence score from Plantnet
int, Size of the polygon where the detection was made
int, The center x coordinate of the polygon
int, The center y coordinate of the polygon
int, The plantnet species ID
int, The image ID
int, The public URL to the image file
List of partners and their acronyms
partnerspartners
Data.frame with partner and partner_acronym
chr, Name of the partner
chr, The acronym used in the Biodiversa+ project