| Title: | Urban Habitat Connectivity Analysis |
|---|---|
| Description: | Analyse and visualise habitat connectivity in urban landscapes, accounting for barriers and buffer distances. Includes Shiny app for interactive analysis and report generation. Based on methods developed by Kirk et al (2023) <doi:10.1016/j.mex.2022.101989>. |
| Authors: | Nicholas Tierney [aut, cre] (ORCID: <https://orcid.org/0000-0003-1460-8722>), Holly Kirk [aut, cph] (ORCID: <https://orcid.org/0000-0002-8724-3210>) |
| Maintainer: | Nicholas Tierney <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.1.0.9000 |
| Built: | 2026-10-12 00:02:42 UTC |
| Source: | https://github.com/urbio-ecology/urbioconnect |
Adds an area layer to the raster of the area of each patch.
add_patch_area(raster)add_patch_area(raster)
raster |
terra SpatRaster. In the workflow, this is the patch ID raster. |
terra SpatRaster with two layers: patch_id and area.
lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() buffered_habitat <- habitat_buffer(lizard_habitat, 5) barrier_mask <- create_barrier_mask(lizard_barrier) fragmented <- fragment_habitat(buffered_habitat, barrier_mask) remaining_habitat <- drop_habitat_under_barrier( habitat = lizard_habitat, barrier = lizard_barrier ) fragment_patches <- assign_patches_to_fragments( remaining_habitat = remaining_habitat, fragment = fragmented ) library(terra) add_patch_area(fragment_patches)lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() buffered_habitat <- habitat_buffer(lizard_habitat, 5) barrier_mask <- create_barrier_mask(lizard_barrier) fragmented <- fragment_habitat(buffered_habitat, barrier_mask) remaining_habitat <- drop_habitat_under_barrier( habitat = lizard_habitat, barrier = lizard_barrier ) fragment_patches <- assign_patches_to_fragments( remaining_habitat = remaining_habitat, fragment = fragmented ) library(terra) add_patch_area(fragment_patches)
Aggregate a raster with connected patch areas into a data frame where each row is a unique patch, and its area and area squared.
aggregate_connected_patches(raster)aggregate_connected_patches(raster)
raster |
terra SpatRaster. Raster with patch_id and area layers. |
Data frame with patch areas and areas squared.
lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() buffered_habitat <- habitat_buffer(lizard_habitat, 5) barrier_mask <- create_barrier_mask(lizard_barrier) fragmented <- fragment_habitat(buffered_habitat, barrier_mask) remaining_habitat <- drop_habitat_under_barrier( habitat = lizard_habitat, barrier = lizard_barrier ) fragment_patches <- assign_patches_to_fragments( remaining_habitat = remaining_habitat, fragment = fragmented ) library(terra) patch_areas <- add_patch_area(fragment_patches) aggregate_connected_patches(patch_areas)lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() buffered_habitat <- habitat_buffer(lizard_habitat, 5) barrier_mask <- create_barrier_mask(lizard_barrier) fragmented <- fragment_habitat(buffered_habitat, barrier_mask) remaining_habitat <- drop_habitat_under_barrier( habitat = lizard_habitat, barrier = lizard_barrier ) fragment_patches <- assign_patches_to_fragments( remaining_habitat = remaining_habitat, fragment = fragmented ) library(terra) patch_areas <- add_patch_area(fragment_patches) aggregate_connected_patches(patch_areas)
Assign patches to fragments
assign_patches_to_fragments(remaining_habitat, fragment)assign_patches_to_fragments(remaining_habitat, fragment)
remaining_habitat |
Terra SpatRaster. Remaining habitat. |
fragment |
Terra SpatRaster. Fragment geometry. |
Terra SpatRaster with patch IDs.
lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() buffered_habitat <- habitat_buffer(lizard_habitat, 5) barrier_mask <- create_barrier_mask(lizard_barrier) fragmented <- fragment_habitat(buffered_habitat, barrier_mask) remaining_habitat <- drop_habitat_under_barrier( habitat = lizard_habitat, barrier = lizard_barrier ) fragment_patches <- assign_patches_to_fragments( remaining_habitat = remaining_habitat, fragment = fragmented ) library(terra) plot(fragment_patches)lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() buffered_habitat <- habitat_buffer(lizard_habitat, 5) barrier_mask <- create_barrier_mask(lizard_barrier) fragmented <- fragment_habitat(buffered_habitat, barrier_mask) remaining_habitat <- drop_habitat_under_barrier( habitat = lizard_habitat, barrier = lizard_barrier ) fragment_patches <- assign_patches_to_fragments( remaining_habitat = remaining_habitat, fragment = fragmented ) library(terra) plot(fragment_patches)
Many shape files contain errors, places where the edges of a polygon cross over or polygons which overlap. This helps remove some of those errors by smoothing the edges of polygons, removing corners, and dissolving edges where polygons overlap. This reduces the complexity of the shape file, making future steps faster.
clean(spatial_data, ...)clean(spatial_data, ...)
spatial_data |
spatial data frame from sf. |
... |
extra options (currently not used) |
An sf object with simplified and validated geometry.
lizard_barrier_shp <- example_barrier_shp() clean(lizard_barrier_shp)lizard_barrier_shp <- example_barrier_shp() clean(lizard_barrier_shp)
Convert color name to hexadecimal
col2hex(color_name)col2hex(color_name)
color_name |
Character. Color name recognized by R. |
Character. Hexadecimal color code.
col2hex("forestgreen") col2hex("blue")col2hex("forestgreen") col2hex("blue")
We can measure the connectivity of a given habitat and barrier with
habitat_connectivity(). We can also compare the connectivity, say for
example if you have the same area habitat and barrier, but you want to
understand what the change in connectedness is when you remove, or add
some habitat, or some barrier(s). This function helps you do that, by
comparing a "scenario" connectivity object against a "baseline"
connectivity object (both created by habitat_connectivity() or
summarise_connectivity()).
compare_connectivity(scenario, baseline, scenario_name = NULL)compare_connectivity(scenario, baseline, scenario_name = NULL)
scenario |
A |
baseline |
A |
scenario_name |
Character. An optional label for the scenario, for
example "Bentley Project". Appears in the |
If you have raster or vector layers rather than connectivity objects, use
habitat_connectivity_comparison(), which runs habitat_connectivity() on
each scenario for you and then calls this function.
A compare_connectivity object: a tibble with four rows
(baseline, scenario, change, pct_change, in the measure column)
and the same metric columns as summarise_connectivity() output:
scenario_name, measure, species, interpatch_distance, n_patches,
effective_mesh_ha, prob_connectedness, patch_area_mean,
patch_area_total_ha, and data_resolution. The change row is
scenario - baseline, so a positive value means the scenario is higher
than the baseline; the pct_change row expresses that same change as
100 * change / baseline, which is the readable form for metrics whose
absolute deltas are very small. Metric values are held at full precision -
they are not rounded - so change is exact.
habitat_connectivity_comparison(), which starts from habitat and
barrier layers instead of connectivity objects,
habitat_connectivity(), and summarise_connectivity().
# build `connectivity` objects cheaply (no spatial pipeline) and compare them baseline <- summarise_connectivity(lizard_areas_connected) # a scenario in which one connected patch (the first row) is lost scenario <- summarise_connectivity(lizard_areas_connected[-1, ]) compare_connectivity(scenario = scenario, baseline = baseline) # label the scenario so it can be told apart from others compare_connectivity( scenario = scenario, baseline = baseline, scenario_name = "Bentley Project" )# build `connectivity` objects cheaply (no spatial pipeline) and compare them baseline <- summarise_connectivity(lizard_areas_connected) # a scenario in which one connected patch (the first row) is lost scenario <- summarise_connectivity(lizard_areas_connected[-1, ]) compare_connectivity(scenario = scenario, baseline = baseline) # label the scenario so it can be told apart from others compare_connectivity( scenario = scenario, baseline = baseline, scenario_name = "Bentley Project" )
Where compare_connectivity() compares a single scenario against a
baseline, this compares as many as you like, and labels each one. Give it a
named list of connectivity objects: the names become the
scenario_name column, so a scenario can be identified in the result.
compare_scenarios(baseline, scenarios)compare_scenarios(baseline, scenarios)
baseline |
A |
scenarios |
A named list of |
If you have raster or vector layers rather than connectivity objects,
run habitat_connectivity() on each one first, as in the second example
below.
A compare_connectivity object with four rows per scenario
(baseline, scenario, change, pct_change), labelled in the
scenario_name column. See compare_connectivity() for the columns.
compare_connectivity() for a single scenario, and
habitat_connectivity_comparison(), which starts from habitat and barrier
layers instead of connectivity objects.
baseline <- summarise_connectivity(lizard_areas_connected) compare_scenarios( baseline = baseline, scenarios = list( "Bentley Project" = summarise_connectivity(lizard_areas_connected[-1, ]), "Barrier upgrade" = summarise_connectivity( lizard_areas_connected[-(1:5), ] ) ) ) # starting from layers: build a `connectivity` object for each first wren_habitat <- example_wren_habitat() wren_barrier <- example_wren_barrier() wren_baseline <- habitat_connectivity( habitat = wren_habitat, barrier = wren_barrier, species = "Superb Fairy Wren", interpatch_distance = 200, verbose = FALSE ) bentley_project <- habitat_connectivity( habitat = example_wren_habitat_scenario(), barrier = wren_barrier, species = "Superb Fairy Wren", interpatch_distance = 200, verbose = FALSE ) compare_scenarios( baseline = wren_baseline, scenarios = list("Bentley Project" = bentley_project) )baseline <- summarise_connectivity(lizard_areas_connected) compare_scenarios( baseline = baseline, scenarios = list( "Bentley Project" = summarise_connectivity(lizard_areas_connected[-1, ]), "Barrier upgrade" = summarise_connectivity( lizard_areas_connected[-(1:5), ] ) ) ) # starting from layers: build a `connectivity` object for each first wren_habitat <- example_wren_habitat() wren_barrier <- example_wren_barrier() wren_baseline <- habitat_connectivity( habitat = wren_habitat, barrier = wren_barrier, species = "Superb Fairy Wren", interpatch_distance = 200, verbose = FALSE ) bentley_project <- habitat_connectivity( habitat = example_wren_habitat_scenario(), barrier = wren_barrier, species = "Superb Fairy Wren", interpatch_distance = 200, verbose = FALSE ) compare_scenarios( baseline = wren_baseline, scenarios = list("Bentley Project" = bentley_project) )
Labels the columns and says how precisely each should be shown, so a table
on screen and the same table in a report agree. Values come back
numeric: the rounding is described rather than applied, because
DT::datatable() needs numbers to stay numbers to sort them, while
knitr::kable() wants them already rounded. round_by() applies the
rounding when that is what you want.
connectivity_display(x, ...) ## S3 method for class 'connectivity' connectivity_display(x, ...) ## S3 method for class 'patch_size_tbl' connectivity_display(x, ...) ## S3 method for class 'compare_connectivity' connectivity_display(x, wide = TRUE, ...) ## Default S3 method: connectivity_display(x, ...)connectivity_display(x, ...) ## S3 method for class 'connectivity' connectivity_display(x, ...) ## S3 method for class 'patch_size_tbl' connectivity_display(x, ...) ## S3 method for class 'compare_connectivity' connectivity_display(x, wide = TRUE, ...) ## Default S3 method: connectivity_display(x, ...)
x |
A |
... |
Passed to methods. |
wide |
For a comparison, one row per metric and a column per measure,
which is the shape a reader wants. |
A list of
data, a tibble with display labels and numeric values
digits, a tibble of column, kind ("round" or "signif") and
digits, one row per column that needs rounding
round_by() to apply the rounding.
lizard <- habitat_connectivity( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) display <- connectivity_display(lizard) display$digits round_by(display)lizard <- habitat_connectivity( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) display <- connectivity_display(lizard) display$digits round_by(display)
The stem every download takes its name from, so a report, a zip and the folder inside that zip agree. The species is lower-cased with its spaces turned into dashes, which keeps it usable as a file name.
connectivity_file_stem(x)connectivity_file_stem(x)
x |
A |
A string, with no extension.
generate_connectivity_report() and
write_connectivity_report(), whose path defaults to this, and
zip_connectivity_assets(), which names the folder inside the archive
with it.
report_data <- connectivity_report_data( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) connectivity_file_stem(report_data)report_data <- connectivity_report_data( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) connectivity_file_stem(report_data)
Computes the probability two randomly chosen points within habitat
are connected, accounting for fragmentation. This requires the effective
mesh size (via effective_mesh_size()), and the area of patches. This means
that you can calculate the change in connectivity if you calculate the
effective mesh size of a new habitat/barrier plan, and then use the baseline
connectivity_probability(effective_mesh_size, area_baseline)connectivity_probability(effective_mesh_size, area_baseline)
effective_mesh_size |
As calculated by |
area_baseline |
Numeric vector. Area of a connected patch. This argument is called "baseline" as when you are doing design scenarios you must refer to the baseline area when calculating connectivity probability. See vignette, TODO. |
Numeric. Probability of connectedness (0-1).
effective_mesh <- effective_mesh_size( area = lizard_areas_connected$area ) connectivity_probability( effective_mesh_size = effective_mesh, area_baseline = lizard_areas_connected$area ) # if you wanted to compare to a scenario, you would consider the effective # mesh size to be the new scenario level, and the baseline as so: connectivity_probability( # scenario 1 effective_mesh_size = effective_mesh, area_baseline = lizard_areas_connected$area )effective_mesh <- effective_mesh_size( area = lizard_areas_connected$area ) connectivity_probability( effective_mesh_size = effective_mesh, area_baseline = lizard_areas_connected$area ) # if you wanted to compare to a scenario, you would consider the effective # mesh size to be the new scenario level, and the baseline as so: connectivity_probability( # scenario 1 effective_mesh_size = effective_mesh, area_baseline = lizard_areas_connected$area )
Runs the connectivity pipeline once per distance and keeps both the summary
and the spatial layers the maps need. habitat_connectivity() returns the
summary alone; this also keeps the buffered habitat and the patch-ID
raster for each distance, which is what the downloadable assets and the
report are built from.
connectivity_report_data( habitat, barrier, species, interpatch_distance, scenario = NULL, scenario_kind = NULL, scenario_name = NULL, verbose = TRUE )connectivity_report_data( habitat, barrier, species, interpatch_distance, scenario = NULL, scenario_kind = NULL, scenario_name = NULL, verbose = TRUE )
habitat |
Terra SpatRaster. The habitat layer. |
barrier |
Terra SpatRaster. The barrier layer. |
species |
Species name. E.g., "Superb Fairy Wren". |
interpatch_distance |
Numeric. The distance (in metres) at which habitat patches are considered connected. May be a scalar or a vector; a vector runs the pipeline once per distance. |
scenario |
Optional layer to compare against the baseline: an |
scenario_kind |
Which layer |
scenario_name |
Optional name for the scenario, carried in the comparison so several can be stacked. |
verbose |
Logical. Display progress messages (default: TRUE). |
A connectivity_report_data object: a list of
connectivity, a connectivity summary with one row per distance
habitat and barrier, the layers as supplied
buffered_habitat and patch_id_raster, lists of SpatRaster, one per
distance and named by it
species and interpatch_distance
and, when a scenario was given, the same view of the scenario
landscape: scenario_kind, scenario_habitat, scenario_barrier,
scenario_buffered_habitat, and comparison. Without one these are all
NULL. scenario_layer() returns whichever layer the scenario changed.
habitat_connectivity() when only the summary is wanted, and
habitat_connectivity_comparison() to compare two landscapes you have
already assembled yourself.
report_data <- connectivity_report_data( habitat = example_wren_habitat(), barrier = example_wren_barrier(), species = "Superb Fairy Wren", interpatch_distance = 200, verbose = FALSE ) report_data$connectivity names(report_data$patch_id_raster) # with a scenario, the baseline and the scenario are each run once and # the comparison is built from the two with_scenario <- connectivity_report_data( habitat = example_wren_habitat(), barrier = example_wren_barrier(), species = "Superb Fairy Wren", interpatch_distance = 200, scenario = example_wren_barrier_scenario(), scenario_kind = "barrier", verbose = FALSE ) with_scenario$comparisonreport_data <- connectivity_report_data( habitat = example_wren_habitat(), barrier = example_wren_barrier(), species = "Superb Fairy Wren", interpatch_distance = 200, verbose = FALSE ) report_data$connectivity names(report_data$patch_id_raster) # with a scenario, the baseline and the scenario are each run once and # the comparison is built from the two with_scenario <- connectivity_report_data( habitat = example_wren_habitat(), barrier = example_wren_barrier(), species = "Superb Fairy Wren", interpatch_distance = 200, scenario = example_wren_barrier_scenario(), scenario_kind = "barrier", verbose = FALSE ) with_scenario$comparison
Converts a barrier layer into a multiplier mask for connectivity analysis. Takes a raster where barriers are coded as 1 (and non-barriers as NA), and inverts it to produce a mask with NA values where barriers exist and 1 elsewhere. This format allows barriers to be applied by multiplying the mask with connectivity surfaces, effectively blocking movement through barrier cells.
create_barrier_mask(barrier)create_barrier_mask(barrier)
barrier |
Terra SpatRaster. Barrier layer with 1 = barrier, NA = no barrier. |
Terra SpatRaster. Mask with 1 where movement is allowed, NA where barriers exist.
lizard_barrier <- example_barrier() create_barrier_mask(lizard_barrier)lizard_barrier <- example_barrier() create_barrier_mask(lizard_barrier)
So a consumer that has to pivot or drop the identifiers can ask, rather than hardcoding the label text.
display_ids(display)display_ids(display)
display |
A list from |
A character vector of column labels.
lizard <- habitat_connectivity( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) display_ids(connectivity_display(lizard))lizard <- habitat_connectivity( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) display_ids(connectivity_display(lizard))
Essentially just performs a terra::mask() operation, to remove the habitat
parts that are under the mask.
drop_habitat_under_barrier(habitat, barrier_mask)drop_habitat_under_barrier(habitat, barrier_mask)
habitat |
Terra SpatRaster. Habitat layer. |
barrier_mask |
Terra SpatRaster. Barrier mask. |
Terra SpatRaster with habitat remaining after barrier removal.
lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() barrier_mask <- create_barrier_mask(lizard_barrier) remaining_habitat <- drop_habitat_under_barrier( habitat = lizard_habitat, barrier = lizard_barrier ) remaining_habitatlizard_habitat <- example_habitat() lizard_barrier <- example_barrier() barrier_mask <- create_barrier_mask(lizard_barrier) remaining_habitat <- drop_habitat_under_barrier( habitat = lizard_habitat, barrier = lizard_barrier ) remaining_habitat
Computes the effective mesh size metric for habitat connectivity, This
represents the probability that two randomly chosen points within habitat
remain connected. Intended for usage from objects created by
habitat_connectivity(). See examples below.
effective_mesh_size(area, area_baseline = area)effective_mesh_size(area, area_baseline = area)
area |
Numeric vector. Area of connected patches. |
area_baseline |
Optional. Defaults to |
Numeric. Effective mesh size, in hectares.
effective_mesh_size(lizard_areas_connected$area)effective_mesh_size(lizard_areas_connected$area)
Create Empty terra raster grid
empty_grid(habitat, resolution = 10)empty_grid(habitat, resolution = 10)
habitat |
SF object or terra SpatRaster. |
resolution |
Numeric. Cell size in meters (default: 10). |
Terra SpatRaster. Empty raster grid.
lizard_barrier_shp <- example_barrier_shp() empty_grid(lizard_barrier_shp, resolution = 10)lizard_barrier_shp <- example_barrier_shp() empty_grid(lizard_barrier_shp, resolution = 10)
We provide Habitat and Barrier data on various lizard species (nominally, Blue-tongued Lizard). The data was collected from Darebin Creek in Melbourne, which runs between Preston and West Heidelberg. For analysis purposes, a interpatch distance of 200 metres is recommended for lizard connectivity assessments.
example_habitat() example_barrier_shp() example_barrier() example_barrier_scenario()example_habitat() example_barrier_shp() example_barrier() example_barrier_scenario()
We provide helper functions to load the raster and shapefile data. These are required due to how the raster and vector data are stored. These functions provide easy access to example raster and shapefile data included with the package:
example_habitat() Returns a raster of lizard habitat data.
example_barrier_shp() Returns a shapefile of lizard barrier data as
an SF object.
example_barrier() Returns a raster of lizard barrier data.
example_barrier_scenario() Returns the barrier raster with a new road
cut through it, for comparing a scenario against the baseline.
A terra raster object or sf object depending on the function called
library(terra) # Load habitat raster lizard_habitat <- example_habitat() plot(lizard_habitat, col = "darkgreen", legend = FALSE, main = "Lizard Habitat") # Load barrier shapefile lizard_barrier_shp <- example_barrier_shp() plot(lizard_barrier_shp) # Load barrier raster lizard_barrier <- example_barrier() plot(lizard_barrier, col = c("grey", "white"), legend = FALSE, main = "Lizard Barriers") plot(lizard_barrier, col = c("grey", "white"), legend = FALSE, main = "Lizard Habitat and Barrier") plot(lizard_habitat, col = "darkgreen", legend = FALSE, add = TRUE)library(terra) # Load habitat raster lizard_habitat <- example_habitat() plot(lizard_habitat, col = "darkgreen", legend = FALSE, main = "Lizard Habitat") # Load barrier shapefile lizard_barrier_shp <- example_barrier_shp() plot(lizard_barrier_shp) # Load barrier raster lizard_barrier <- example_barrier() plot(lizard_barrier, col = c("grey", "white"), legend = FALSE, main = "Lizard Barriers") plot(lizard_barrier, col = c("grey", "white"), legend = FALSE, main = "Lizard Habitat and Barrier") plot(lizard_habitat, col = "darkgreen", legend = FALSE, add = TRUE)
We provide Habitat, Barrier, and scenario data on the species, "Superb Fairy Wren". The data was collected from City of Knox, a local government area in Melbourne, on the eastern suburbs, north of Dandenon and south of Ringwood. For analysis purposes, we have also implemented a scenario piece of data.
example_wren_habitat() example_wren_habitat_scenario() example_wren_barrier() example_wren_barrier_scenario()example_wren_habitat() example_wren_habitat_scenario() example_wren_barrier() example_wren_barrier_scenario()
We provide helper functions to load the raster and shapefile data. These are required due to how the raster and vector data are stored. These functions provide easy access to example raster and shapefile data included with the package:
example_wren_habitat() Returns a raster of wren habitat data.
example_wren_habitat_scenario() Returns a raster of wren habitat
scenario data.
example_wren_barrier() Returns a raster of wren barrier data.
example_wren_barrier_scenario() Returns a raster of wren barrier
scenario data.
A terra raster object or sf object depending on the function called
library(terra) # Load habitat raster wren_habitat <- example_wren_habitat() # Load barrier raster wren_barrier <- example_wren_barrier() # load barrier scenario wren_barrier_scenario <- example_wren_barrier_scenario() plot(wren_habitat, col = "darkgreen", legend = FALSE, main = "wren Habitat") plot( wren_barrier, col = c("grey", "white"), legend = FALSE, main = "wren Barriers") plot( wren_barrier, col = c("grey", "white"), legend = FALSE, main = "wren Habitat and Barrier" ) plot(wren_habitat, col = "darkgreen", legend = FALSE, add = TRUE) plot( wren_barrier_scenario, col = c("grey", "white"), legend = FALSE, main = "wren Habitat and Barrier scenario" ) plot(wren_habitat, col = "darkgreen", legend = FALSE, add = TRUE)library(terra) # Load habitat raster wren_habitat <- example_wren_habitat() # Load barrier raster wren_barrier <- example_wren_barrier() # load barrier scenario wren_barrier_scenario <- example_wren_barrier_scenario() plot(wren_habitat, col = "darkgreen", legend = FALSE, main = "wren Habitat") plot( wren_barrier, col = c("grey", "white"), legend = FALSE, main = "wren Barriers") plot( wren_barrier, col = c("grey", "white"), legend = FALSE, main = "wren Habitat and Barrier" ) plot(wren_habitat, col = "darkgreen", legend = FALSE, add = TRUE) plot( wren_barrier_scenario, col = c("grey", "white"), legend = FALSE, main = "wren Habitat and Barrier scenario" ) plot(wren_habitat, col = "darkgreen", legend = FALSE, add = TRUE)
A reprojected raster rarely has round cells: a nominally 10m grid comes out
as 9.99673 x 10.00151. That precision matters in the data and reads as
noise on a screen, so round it for display.
format_resolution(res, digits = 1)format_resolution(res, digits = 1)
res |
Either the numeric pair |
digits |
Decimal places to round to. Default 1. |
A character vector, e.g. "10 x 10".
format_resolution(c(9.99673, 10.00151)) format_resolution("9.99673x10.00151")format_resolution(c(9.99673, 10.00151)) format_resolution("9.99673x10.00151")
Takes a barrier mask (created with create_barrier_mask()) and fragments
up the habitat where they intersect.
fragment_habitat(buffered_habitat, barrier_mask)fragment_habitat(buffered_habitat, barrier_mask)
buffered_habitat |
Terra SpatRaster. Buffered habitat. |
barrier_mask |
Terra SpatRaster. Barrier mask. |
Terra SpatRaster with fragmented habitat.
lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() buffered_habitat <- habitat_buffer(lizard_habitat, 5) barrier_mask <- create_barrier_mask(lizard_barrier) fragmented <- fragment_habitat(buffered_habitat, barrier_mask)lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() buffered_habitat <- habitat_buffer(lizard_habitat, 5) barrier_mask <- create_barrier_mask(lizard_barrier) fragmented <- fragment_habitat(buffered_habitat, barrier_mask)
One document holding the maps, tables and summary for an analysis: the same
figures write_connectivity_assets() writes, laid out to read. HTML is a
single self-contained file; PDF is rendered through Typst, so no LaTeX is
needed.
generate_connectivity_report(x, path = NULL, format = report_format(path))generate_connectivity_report(x, path = NULL, format = report_format(path))
x |
A |
path |
File to write. The extension sets the format. Defaults to the
species, today's date and |
format |
The Quarto format, when |
write_connectivity_report() then render_connectivity_report(), in one
call, through a temporary directory. Use those two instead when you want
the Quarto source to keep or to change.
The report covers the tabular and visual results. The GIS layers travel
with zip_connectivity_assets(), since a GeoTIFF can't live inside a
document.
The format comes from path's extension, as it does for
ggplot2::ggsave(): "report.pdf" writes a PDF and "report.html" an
HTML file. Write both by calling this twice.
Any format Quarto can write to a single file works, so "report.docx"
and "report.rtf" also do what they look like. The Shiny app offers HTML
and PDF only. A .pdf renders through Typst rather than Quarto's
LaTeX-based pdf format, so no TeX install is needed.
Formats that keep their figures in a folder beside the document, such as
.md, are refused: a report has to be one file, and copying the document
alone would lose every figure. Note too that the tabbed sections are
HTML-only and fall back to plain headings everywhere else.
The absolute path written, invisibly.
connectivity_report_data() to build x,
write_connectivity_report() for the Quarto source, and
zip_connectivity_assets() for the GIS layers and full tables.
report_data <- connectivity_report_data( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) # rendering needs the Quarto command line tool if (quarto_available()) { generate_connectivity_report( report_data, file.path(tempdir(), "lizard-report.html") ) }report_data <- connectivity_report_data( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) # rendering needs the Quarto command line tool if (quarto_available()) { generate_connectivity_report( report_data, file.path(tempdir(), "lizard-report.html") ) }
Creates a visualisation of habitat, interpatch distance zone, and barriers using terra rasters.
gg_barrier_habitat_interpatch_dist( barrier, buffered, habitat, interpatch_distance, species, col_barrier = urbio_colours()$barrier, col_interpatch_dist = urbio_colours()$interpatch_distance, col_habitat = urbio_colours()$habitat, col_paper = NA )gg_barrier_habitat_interpatch_dist( barrier, buffered, habitat, interpatch_distance, species, col_barrier = urbio_colours()$barrier, col_interpatch_dist = urbio_colours()$interpatch_distance, col_habitat = urbio_colours()$habitat, col_paper = NA )
barrier |
Terra SpatRaster. Barrier layer (e.g., roads). |
buffered |
Terra SpatRaster. Buffered habitat layer. |
habitat |
Terra SpatRaster. Original habitat layer. |
interpatch_distance |
Numeric. The distance (in meters) where habitat
patches are considered connected. E.g., if set to 500, patches 498m apart
are connected, those 501m apart are not connected. This is passed
internally to a spatial operation known as "buffering", where this
distance is used as a radius from the edge of the habitat zone. This means
the specified |
species |
Character. Species name for plot title. |
col_barrier |
Character. Colour for barrier layer. Defaults to the
package palette, so a map drawn here matches the app, the downloads and
the report. See |
col_interpatch_dist |
Character. Colour for interpatch distance zone. |
col_habitat |
Character. Colour for habitat patches. |
col_paper |
Character. Background color (default: "white"). |
A ggplot2 object.
lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() lizard_buffered <- habitat_buffer(lizard_habitat, buffer_radius =10) gg_bar_hab_buf <- gg_barrier_habitat_interpatch_dist( barrier = lizard_barrier, buffered = lizard_buffered, habitat = lizard_habitat, interpatch_distance = 10, species = "Blue Tongue Lizard", col_barrier = "black", col_interpatch_dist = "lightgreen", col_habitat = "seagreen" ) gg_bar_hab_buf # add north arrow and scale bar with ggspatial library(ggspatial) library(tidyterra) gg_bar_hab_buf + annotation_north_arrow( style = north_arrow_fancy_orienteering() ) + annotation_scale()lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() lizard_buffered <- habitat_buffer(lizard_habitat, buffer_radius =10) gg_bar_hab_buf <- gg_barrier_habitat_interpatch_dist( barrier = lizard_barrier, buffered = lizard_buffered, habitat = lizard_habitat, interpatch_distance = 10, species = "Blue Tongue Lizard", col_barrier = "black", col_interpatch_dist = "lightgreen", col_habitat = "seagreen" ) gg_bar_hab_buf # add north arrow and scale bar with ggspatial library(ggspatial) library(tidyterra) gg_bar_hab_buf + annotation_north_arrow( style = north_arrow_fancy_orienteering() ) + annotation_scale()
The habitat or the barrier by itself, rather than combined as
gg_barrier_habitat_interpatch_dist() draws them. For looking at what went
into an analysis, and at a scenario layer against the layer it replaces.
gg_layer(layer, kind = c("habitat", "barrier"), title = NULL)gg_layer(layer, kind = c("habitat", "barrier"), title = NULL)
layer |
A |
kind |
Which layer this is, |
title |
Plot title. Defaults to |
kind picks the colours, so a layer looks the same here as it does in the
combined map. A barrier is white there, read as cuts through the interpatch
zone, so a barrier drawn alone is white on that same green: white on white
would be nothing at all.
A ggplot.
gg_barrier_habitat_interpatch_dist() for the combined map, and
urbio_colours() for the palette this draws from.
gg_layer(example_habitat(), "habitat") gg_layer(example_barrier(), "barrier") # a scenario layer, titled for what it changes gg_layer(example_barrier(), "barrier", title = "Scenario: new roads")gg_layer(example_habitat(), "habitat") gg_layer(example_barrier(), "barrier") # a scenario layer, titled for what it changes gg_layer(example_barrier(), "barrier", title = "Scenario: new roads")
Buffer around the habitat a given distance in metres using
terra::focalMat(d = buffer_radius, type = "circle"). This operation is
used to identify connected patches of habitat. Two patches will connect
when their edge-to-edge gap is <= 2 * buffer radius. So, we recommend
you specify the buffer_radius value to be half the interpatch distance,
which is the distance past which habitat patches are no longer considered
connected. For example, if your interpatch distance is 500m, set
buffer_radius = 250.
habitat_buffer(habitat, buffer_radius)habitat_buffer(habitat, buffer_radius)
habitat |
Terra SpatRaster. Habitat raster. |
buffer_radius |
Numeric. The radius in metres around the habitat.
Since patches of habitat will be connected when their edge-to-edge gap is
<= 2 * |
Terra SpatRaster with buffered habitat.
vignette("interpatch-distance-and-resolution") for the
relationship between interpatch distance, buffer radius, and resolution.
lizard_habitat <- example_habitat() library(terra) plot(lizard_habitat, col = "darkgreen", legend = FALSE) # run with a small buffer radius lizard_buff <- habitat_buffer(lizard_habitat, buffer_radius = 10) plot(lizard_buff, col = "lightgreen", legend = FALSE) plot(lizard_habitat, col = "darkgreen", legend = FALSE, add = TRUE)lizard_habitat <- example_habitat() library(terra) plot(lizard_habitat, col = "darkgreen", legend = FALSE) # run with a small buffer radius lizard_buff <- habitat_buffer(lizard_habitat, buffer_radius = 10) plot(lizard_buff, col = "lightgreen", legend = FALSE) plot(lizard_habitat, col = "darkgreen", legend = FALSE, add = TRUE)
This performs the entire connectivity workflow, returning a dataframe output. The steps are:
create_barrier_mask(): Creating barrier mask.
drop_habitat_under_barrier(): Removes Habitat underneath barrier.
habitat_buffer(): Buffers the habitat layer by the interpatch distance (m).
fragment_habitat(): Fragments habitat layer along barrier intersection.
assign_patches_to_fragments(): Assign patch ID to fragments.
aggregate_connected_patches(): Summarise area in each patch.
habitat_connectivity( habitat, barrier, species, interpatch_distance, verbose = TRUE )habitat_connectivity( habitat, barrier, species, interpatch_distance, verbose = TRUE )
habitat |
Terra SpatRaster. Habitat raster. |
barrier |
Terra SpatRaster. Barrier raster. |
species |
Species name. E.g., "Blue-tongued Lizard". |
interpatch_distance |
Numeric. The distance (in meters) where habitat
patches are considered connected. E.g., if set to 500, patches 498m apart
are connected, those 501m apart are not connected. This is passed
internally to a spatial operation known as "buffering", where this
distance is used as a radius from the edge of the habitat zone. This means
the specified |
verbose |
Logical. Display progress messages (default: TRUE). |
A connectivity object (one row): a tibble of landscape-level
connectivity metrics (patch count, effective mesh size, probability of
connectedness, mean and total patch area) for the species and
interpatch distance supplied. The per-patch areas the summary is built
from are stored in a list column, "patch_size" – retrieve them with
patch_sizes().
vignette("interpatch-distance-and-resolution") for the
relationship between interpatch distance, buffer radius, and resolution.
lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() connectivity <- habitat_connectivity( habitat = lizard_habitat, barrier = lizard_barrier, species = "Blue-tongued Lizard", interpatch_distance = 12 ) connectivity # get the patch size: patch_sizes(connectivity)[[1]]lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() connectivity <- habitat_connectivity( habitat = lizard_habitat, barrier = lizard_barrier, species = "Blue-tongued Lizard", interpatch_distance = 12 ) connectivity # get the patch size: patch_sizes(connectivity)[[1]]
Starts from habitat and barrier layers, rather than the connectivity
objects compare_connectivity() takes. Give it a scenario
habitat/barrier pair and a baseline habitat/barrier pair and it runs the
full connectivity pipeline on each (via habitat_connectivity()) and
compares them. interpatch_distance may be a vector, in which case the
comparison is run once per distance and the per-distance results are
stacked.
habitat_connectivity_comparison( habitat_scenario, barrier_scenario, habitat_baseline, barrier_baseline, species, interpatch_distance, scenario_name = NULL, verbose = TRUE )habitat_connectivity_comparison( habitat_scenario, barrier_scenario, habitat_baseline, barrier_baseline, species, interpatch_distance, scenario_name = NULL, verbose = TRUE )
habitat_scenario |
Terra SpatRaster. The scenario habitat layer. |
barrier_scenario |
Terra SpatRaster. The scenario barrier layer. |
habitat_baseline |
Terra SpatRaster. The baseline habitat layer. |
barrier_baseline |
Terra SpatRaster. The baseline barrier layer. |
species |
Species name. E.g., "Superb Fairy Wren". |
interpatch_distance |
Numeric. The distance (in metres) at which habitat
patches are considered connected. May be a scalar or a vector; a vector
runs the comparison once per distance. See |
scenario_name |
Character. An optional label for the scenario, for
example "Bentley Project". Passed to |
verbose |
Logical. Display progress messages (default: TRUE). |
Change exactly one layer between baseline and scenario so the difference is
attributable to a single variable. To assess a barrier change, pass the
same habitat to both habitat_baseline and habitat_scenario; to assess a
habitat change, pass the same barrier to both barrier_baseline and
barrier_scenario. If both the habitat and the barrier differ the
function aborts; if neither differs it warns (the scenario equals the
baseline, so every change value is zero).
Note that this runs the full connectivity pipeline 2 * length(interpatch_distance) times (buffering is the slow step), so a long
vector of distances will take proportionally longer.
A compare_connectivity object: a tibble with four rows
(baseline, scenario, change, pct_change) per interpatch distance. A
scalar interpatch_distance gives four rows; a length-3 vector gives
twelve rows (four per distance). See compare_connectivity() for the
columns.
compare_connectivity(), habitat_connectivity()
habitat_connectivity_comparison( habitat_scenario = example_wren_habitat(), barrier_scenario = example_wren_barrier_scenario(), habitat_baseline = example_wren_habitat(), barrier_baseline = example_wren_barrier(), species = "Superb Fairy Wren", interpatch_distance = 200, verbose = FALSE )habitat_connectivity_comparison( habitat_scenario = example_wren_habitat(), barrier_scenario = example_wren_barrier_scenario(), habitat_baseline = example_wren_habitat(), barrier_baseline = example_wren_barrier(), species = "Superb Fairy Wren", interpatch_distance = 200, verbose = FALSE )
Like habitat_connectivity(), but also returns the intermediate rasters
(buffered habitat, patch ID raster, barrier mask, remaining habitat) useful
for mapping and reporting.
habitat_connectivity_full( habitat, barrier, interpatch_distance, verbose = TRUE )habitat_connectivity_full( habitat, barrier, interpatch_distance, verbose = TRUE )
habitat |
Terra SpatRaster. Habitat raster. |
barrier |
Terra SpatRaster. Barrier raster. |
interpatch_distance |
Numeric. The distance (in meters) where habitat
patches are considered connected. E.g., if set to 500, patches 498m apart
are connected, those 501m apart are not connected. This is passed
internally to a spatial operation known as "buffering", where this
distance is used as a radius from the edge of the habitat zone. This means
the specified |
verbose |
Logical. Display progress messages (default: TRUE). |
Named list with elements: buffered_habitat, patch_id_raster,
areas_connected, barrier_mask, remaining_habitat.
lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() result <- habitat_connectivity_full( lizard_habitat, lizard_barrier, interpatch_distance = 10, verbose = FALSE ) names(result)lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() result <- habitat_connectivity_full( lizard_habitat, lizard_barrier, interpatch_distance = 10, verbose = FALSE ) names(result)
The layer-level counterpart to compare_scenarios(). Give it one baseline
habitat/barrier pair and named lists of scenario layers, and it runs the
connectivity pipeline on each and compares them all against that baseline.
habitat_connectivity_scenarios( habitat_baseline, barrier_baseline, species, habitat_scenarios = NULL, barrier_scenarios = NULL, interpatch_distance, verbose = TRUE )habitat_connectivity_scenarios( habitat_baseline, barrier_baseline, species, habitat_scenarios = NULL, barrier_scenarios = NULL, interpatch_distance, verbose = TRUE )
habitat_baseline |
Terra SpatRaster. The baseline habitat layer. |
barrier_baseline |
Terra SpatRaster. The baseline barrier layer. |
species |
Species name. E.g., "Superb Fairy Wren". |
habitat_scenarios |
A named list of habitat layers, one per scenario,
each compared against |
barrier_scenarios |
A named list of barrier layers, the mirror of
|
interpatch_distance |
Numeric. The distance (in metres) at which habitat patches are considered connected. May be a scalar or a vector; a vector runs every scenario once per distance. |
verbose |
Logical. Display progress messages (default: TRUE). |
A scenario changes exactly one layer: habitat scenarios are run against
the baseline barrier, and barrier scenarios against the baseline habitat.
There's no way to write a scenario that changes both, which is what
habitat_connectivity_comparison() has to check for.
The baseline is run once per distance and reused by every scenario, so comparing three scenarios at one distance runs the pipeline four times, not six.
A compare_connectivity object with four rows per scenario per
distance, labelled in the scenario_name column. Habitat scenarios come
first, then barrier scenarios, in the order given.
compare_scenarios() when you already have connectivity objects,
and habitat_connectivity_comparison() for a single scenario.
wren_habitat <- example_wren_habitat() wren_barrier <- example_wren_barrier() habitat_connectivity_scenarios( habitat_baseline = wren_habitat, barrier_baseline = wren_barrier, species = "Superb Fairy Wren", habitat_scenarios = list( "Bentley Project" = example_wren_habitat_scenario() ), barrier_scenarios = list( "Barrier upgrade" = example_wren_barrier_scenario() ), interpatch_distance = 200, verbose = FALSE )wren_habitat <- example_wren_habitat() wren_barrier <- example_wren_barrier() habitat_connectivity_scenarios( habitat_baseline = wren_habitat, barrier_baseline = wren_barrier, species = "Superb Fairy Wren", habitat_scenarios = list( "Bentley Project" = example_wren_habitat_scenario() ), barrier_scenarios = list( "Barrier upgrade" = example_wren_barrier_scenario() ), interpatch_distance = 200, verbose = FALSE )
Pre-computed per-patch areas for the lizard example data at a 50 metre
interpatch distance. This is a patch_size_tbl – the per-patch table
carried inside the connectivity object returned by
habitat_connectivity(), extracted with the patch_sizes() accessor.
Contains one row per connected habitat patch.
lizard_areas_connectedlizard_areas_connected
A patch_size_tbl with columns:
Integer. Connected fragment ID.
Numeric. Total area of the connected patch in square metres.
Generated from example_habitat() and example_barrier() at
50 metre interpatch distance.
habitat_connectivity(), patch_sizes(), summarise_connectivity()
# This was the code that was run to create this object. We don't run it # as it takes some time to run ## Not run: lizard_connectivity <- habitat_connectivity( habitat = example_habitat(), barrier = example_barrier(), species = "Blue-tongued Lizard", interpatch_distance = 50, verbose = FALSE ) lizard_areas_connected <- patch_sizes(lizard_connectivity)[[1]] ## End(Not run) lizard_areas_connected# This was the code that was run to create this object. We don't run it # as it takes some time to run ## Not run: lizard_connectivity <- habitat_connectivity( habitat = example_habitat(), barrier = example_barrier(), species = "Blue-tongued Lizard", interpatch_distance = 50, verbose = FALSE ) lizard_areas_connected <- patch_sizes(lizard_connectivity)[[1]] ## End(Not run) lizard_areas_connected
This is just a wrapper around mean(), however it is written to clearly
identify it's usage in the context of the area data. Intended for usage
from objects created by habitat_connectivity(). See examples below.
mean_patch_size(area, ...)mean_patch_size(area, ...)
area |
Numeric vector. Area of a connected patch. |
... |
extra arguments to pass to |
Numeric. Mean patch size.
mean_patch_size(lizard_areas_connected$area)mean_patch_size(lizard_areas_connected$area)
Identify the number of habitat patches. A wrapper around length(), but
named to establish its context. Intended for usage from objects created by
habitat_connectivity(). See examples below.
n_patches(area)n_patches(area)
area |
Numeric vector. Area of a connected patch. |
Integer. Number of patches.
n_patches(lizard_areas_connected$area)n_patches(lizard_areas_connected$area)
A tibble of connected patch areas (one row per patch)
that also carries the species and interpatch_distance the analysis
was run with as attributes. habitat_connectivity() builds one of these
internally for each call; retrieve it from the returned connectivity
summary with patch_sizes().
new_patch_size_tbl(data, species, interpatch_distance, res = NA_real_) patch_size_tbl(data, species, interpatch_distance, res = NA_real_)new_patch_size_tbl(data, species, interpatch_distance, res = NA_real_) patch_size_tbl(data, species, interpatch_distance, res = NA_real_)
data |
Data frame of connected patches. Must contain an |
species |
Character of length 1. Species the analysis was run for. |
interpatch_distance |
Numeric of length 1. The interpatch distance (m) the analysis used. |
res |
resolution in pixels - defaults to NA (numeric), not required for vector based approaches. |
Because it is a tibble subclass it behaves like a data frame directly –
$, [, DT::datatable(), utils::write.csv() and ggplot2 all work
without ceremony. Read the metadata back with pc_species() and
pc_interpatch_distance().
A patch_size_tbl object: a tibble with species and
interpatch_distance attributes.
A vector layer is rasterised; a SpatRaster is already cells, so it is
resampled onto the grid instead. This is the one place that decides what an
empty cell means, which is why background is required: NA for habitat,
because a cell with no habitat in it has no value, and 0 for a barrier,
because a cell with no barrier in it is not blocked.
onto_grid(layer, grid, background)onto_grid(layer, grid, background)
layer |
An |
grid |
A |
background |
Value for cells the layer does not cover. Ignored when
|
Use it to put a scenario layer on the grid of the layer it replaces. A
scenario rasterised onto a grid of its own extent is a different landscape,
and habitat_connectivity_comparison() will refuse to compare it.
A SpatRaster on grid's geometry.
prepare_rasters(), which uses this for both of its layers.
grid <- empty_grid(example_barrier_shp(), resolution = 10) # a vector layer is rasterised onto_grid(example_barrier_shp(), grid, background = 0) # a raster one is resampled onto_grid(example_habitat(), grid, background = NA)grid <- empty_grid(example_barrier_shp(), resolution = 10) # a vector layer is rasterised onto_grid(example_barrier_shp(), grid, background = 0) # a raster one is resampled onto_grid(example_habitat(), grid, background = NA)
connectivity objectA connectivity object (from habitat_connectivity() or
summarise_connectivity()) carries the underlying per-patch areas in a
patch_size list-column. patch_sizes() returns them.
patch_sizes(x)patch_sizes(x)
x |
A |
A list of patch_size_tbl() objects, one per row of x (always a
list, even for a single-row summary).
patch_size_tbl objectMetadata from a patch_size_tbl object
pc_species(x) pc_patches(x) pc_res(x) pc_interpatch_distance(x)pc_species(x) pc_patches(x) pc_res(x) pc_interpatch_distance(x)
x |
A |
pc_species() Returns the species (character, length 1).
pc_interpatch_distance() returns the interpatch distance (numeric,
length 1).
pc_res() returns the resolution (character, length 1 - e.g., "2x2").
pc_patches() returns the number of patches - computed live from the
number of rows (numeric, length 1).
Draws the plot gg_barrier_habitat_interpatch_dist() makes and writes it to
a PNG in the working directory, at the shared size from
urbio_figure_size(). Returns the file path, which is what a
targets::tar_file() target wants.
plot_barrier_habitat_interpatch_dist( barrier, buffered, habitat, interpatch_distance, species, col_barrier = urbio_colours()$barrier, col_interpatch_dist = urbio_colours()$interpatch_distance, col_habitat = urbio_colours()$habitat, col_paper = NA )plot_barrier_habitat_interpatch_dist( barrier, buffered, habitat, interpatch_distance, species, col_barrier = urbio_colours()$barrier, col_interpatch_dist = urbio_colours()$interpatch_distance, col_habitat = urbio_colours()$habitat, col_paper = NA )
barrier |
barrier layer |
buffered |
buffered layer |
habitat |
habitat layer |
interpatch_distance |
Numeric. The distance (in meters) where habitat
patches are considered connected. E.g., if set to 500, patches 498m apart
are connected, those 501m apart are not connected. This is passed
internally to a spatial operation known as "buffering", where this
distance is used as a radius from the edge of the habitat zone. This means
the specified |
species |
character, species name, e.g., "Superb Fairy Wren" |
col_barrier |
colour to colour the barrier layer. Defaults to the
package palette, see |
col_interpatch_dist |
colour to colour the interpatch distance layer |
col_habitat |
colour to colour the habitat layer |
col_paper |
colour to colour the paper layer of ggplot |
Named character vector. The file path, named by the interpatch distance.
## Not run: lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() buffered <- habitat_buffer(lizard_habitat, interpatch_distance = 10) # Creates plot-barrier-interpatch-dist-habitat-*.png in the working directory plot_barrier_habitat_interpatch_dist( barrier = lizard_barrier, buffered = buffered, habitat = lizard_habitat, interpatch_distance = 10, species = "Blue-tongued Lizard", col_barrier = "white", col_interpatch_dist = "lightgreen", col_habitat = "seagreen", col_paper = "grey50" ) ## End(Not run)## Not run: lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() buffered <- habitat_buffer(lizard_habitat, interpatch_distance = 10) # Creates plot-barrier-interpatch-dist-habitat-*.png in the working directory plot_barrier_habitat_interpatch_dist( barrier = lizard_barrier, buffered = buffered, habitat = lizard_habitat, interpatch_distance = 10, species = "Blue-tongued Lizard", col_barrier = "white", col_interpatch_dist = "lightgreen", col_habitat = "seagreen", col_paper = "grey50" ) ## End(Not run)
Creates faceted line plots showing how connectivity metrics change with different interpatch distances. This works best when you have multiple interpatch distances, otherwise it will just be a plot with one point.
plot_connectivity(results_connect_habitat)plot_connectivity(results_connect_habitat)
results_connect_habitat |
Data frame. Connectivity summary results with columns for species, interpatch distance, and various metrics. |
A ggplot2 object with faceted plots of connectivity metrics.
lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() results <- purrr::map( c(10, 20), function(d) { full <- habitat_connectivity_full(lizard_habitat, lizard_barrier, interpatch_distance = d, verbose = FALSE) summarise_connectivity( connectivity = full$areas_connected$area, interpatch_distance = d, data_resolution = 10, species = "Blue-tongued Lizard" ) } ) |> purrr::list_rbind() plot_connectivity(results)lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() results <- purrr::map( c(10, 20), function(d) { full <- habitat_connectivity_full(lizard_habitat, lizard_barrier, interpatch_distance = d, verbose = FALSE) summarise_connectivity( connectivity = full$areas_connected$area, interpatch_distance = d, data_resolution = 10, species = "Blue-tongued Lizard" ) } ) |> purrr::list_rbind() plot_connectivity(results)
Visualizes habitat patches colored by their connected fragment ID.
plot_patches(patch_id, interpatch_distance, species = "Species", n_cols = 7)plot_patches(patch_id, interpatch_distance, species = "Species", n_cols = 7)
patch_id |
Terra SpatRaster. Raster with patch IDs. |
interpatch_distance |
Numeric. The distance (in meters) where habitat
patches are considered connected. E.g., if set to 500, patches 498m apart
are connected, those 501m apart are not connected. This is passed
internally to a spatial operation known as "buffering", where this
distance is used as a radius from the edge of the habitat zone. This means
the specified |
species |
Character. Species name (default: "Species"). |
n_cols |
Integer. Number of colors to cycle through (default: 7). |
A ggplot2 object showing patches with distinct colors.
lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() interpatch_distance <- 20 buffer_radius <- interpatch_distance / 2 buffered_habitat <- habitat_buffer(lizard_habitat, buffer_radius) barrier_mask <- create_barrier_mask(lizard_barrier) fragmented <- fragment_habitat(buffered_habitat, barrier_mask) remaining_habitat <- drop_habitat_under_barrier( habitat = lizard_habitat, barrier = lizard_barrier ) fragment_patches <- assign_patches_to_fragments( remaining_habitat = remaining_habitat, fragment = fragmented ) |> add_patch_area() plot_patches(fragment_patches, interpatch_distance = interpatch_distance) #' add north arrow and scale bar with ggspatial library(ggspatial) library(tidyterra) plot_patches(fragment_patches, interpatch_distance = interpatch_distance) + annotation_north_arrow( style = north_arrow_fancy_orienteering() ) + annotation_scale()lizard_habitat <- example_habitat() lizard_barrier <- example_barrier() interpatch_distance <- 20 buffer_radius <- interpatch_distance / 2 buffered_habitat <- habitat_buffer(lizard_habitat, buffer_radius) barrier_mask <- create_barrier_mask(lizard_barrier) fragmented <- fragment_habitat(buffered_habitat, barrier_mask) remaining_habitat <- drop_habitat_under_barrier( habitat = lizard_habitat, barrier = lizard_barrier ) fragment_patches <- assign_patches_to_fragments( remaining_habitat = remaining_habitat, fragment = fragmented ) |> add_patch_area() plot_patches(fragment_patches, interpatch_distance = interpatch_distance) #' add north arrow and scale bar with ggspatial library(ggspatial) library(tidyterra) plot_patches(fragment_patches, interpatch_distance = interpatch_distance) + annotation_north_arrow( style = north_arrow_fancy_orienteering() ) + annotation_scale()
Put habitat and barrier layers on one grid. Either layer may be a vector
(sf or SpatVector), which is rasterised, or a SpatRaster, which is
resampled onto the grid.
prepare_rasters( habitat, barrier, data_resolution = 10, target_resolution = 500 )prepare_rasters( habitat, barrier, data_resolution = 10, target_resolution = 500 )
habitat |
SF object or |
barrier |
SF object or |
data_resolution |
Numeric. Fine resolution in meters. Default, 10. |
target_resolution |
Numeric. Coarse resolution in meters. Default, 500. |
List with habitat_raster and barrier_raster elements.
lizard_habitat_sf <- terra::as.polygons(example_habitat(), dissolve = TRUE) |> sf::st_as_sf() lizard_barrier_shp <- example_barrier_shp() prepare_rasters(lizard_habitat_sf, lizard_barrier_shp) # a raster layer is resampled onto the grid rather than rasterised prepare_rasters(example_habitat(), lizard_barrier_shp)lizard_habitat_sf <- terra::as.polygons(example_habitat(), dissolve = TRUE) |> sf::st_as_sf() lizard_barrier_shp <- example_barrier_shp() prepare_rasters(lizard_habitat_sf, lizard_barrier_shp) # a raster layer is resampled onto the grid rather than rasterised prepare_rasters(example_habitat(), lizard_barrier_shp)
Quarto is separate software, not an R package, so it can be missing. The
reports need it; nothing else in urbioconnect does. Ask this to decide
what to offer: the Shiny app disables its report buttons when it returns
FALSE, and reports defaults to it in
write_connectivity_assets().
quarto_available()quarto_available()
TRUE or FALSE.
generate_connectivity_report(), which needs it.
quarto_available()quarto_available()
Reads a shapefile and extracts only the spatial geometry, discarding attribute data.
read_geometry(shapefile) ## S3 method for class 'sf' read_geometry(shapefile) ## Default S3 method: read_geometry(shapefile)read_geometry(shapefile) ## S3 method for class 'sf' read_geometry(shapefile) ## Default S3 method: read_geometry(shapefile)
shapefile |
Character. File path to a shapefile, or an SF object. |
An sfc object containing only the spatial geometry.
# Read geometry from a file path barrier_path <- system.file("ex/lizard_barrier.shp", package = "urbioconnect") barrier_geom <- read_geometry(barrier_path) # Can also pass an existing SF object barrier_sf <- sf::st_read(barrier_path, quiet = TRUE) barrier_geom <- read_geometry(barrier_sf)# Read geometry from a file path barrier_path <- system.file("ex/lizard_barrier.shp", package = "urbioconnect") barrier_geom <- read_geometry(barrier_path) # Can also pass an existing SF object barrier_sf <- sf::st_read(barrier_path, quiet = TRUE) barrier_geom <- read_geometry(barrier_sf)
Renders a .qmd from write_connectivity_report(). The document carries
its own figure sizes, so rendering it yourself with the Render button or
quarto::quarto_render() gives the same figures; this adds the choice of
format by extension, the Typst routing for a .pdf, and puts the output
where you ask rather than beside the input.
render_connectivity_report(input, path = NULL, format = report_format(path))render_connectivity_report(input, path = NULL, format = report_format(path))
input |
The |
path |
File to write. The extension sets the format, as it does for
|
format |
The Quarto format, when |
The absolute path written, invisibly.
write_connectivity_report() to write the source.
report_data <- connectivity_report_data( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) qmd <- write_connectivity_report( report_data, file.path(tempdir(), "lizard-report.qmd") ) # rendering needs the Quarto command line tool if (quarto_available()) { render_connectivity_report(qmd) }report_data <- connectivity_report_data( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) qmd <- write_connectivity_report( report_data, file.path(tempdir(), "lizard-report.qmd") ) # rendering needs the Quarto command line tool if (quarto_available()) { render_connectivity_report(qmd) }
round_by() rounds and leaves the values numeric, which is right when each
column holds one metric. format_by() formats each value to a string
instead, which is what a column of several metrics needs: a numeric
column spanning 1e-5 to 1e4 prints wholly in scientific notation, so a
count of 3 reads as 3.00e+00. DT::datatable() formats cell by cell
and so needs neither; knitr::kable() formats by column and so needs
format_by() for a comparison.
round_by(display) format_by(display)round_by(display) format_by(display)
display |
A list from |
The data tibble: numeric from round_by(), character from
format_by().
lizard <- habitat_connectivity( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) round_by(connectivity_display(lizard))lizard <- habitat_connectivity( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) round_by(connectivity_display(lizard))
Launches the connectivity analysis Shiny application.
run_connectivity_app()run_connectivity_app()
No return value, called for side effects (launches Shiny app)
## Not run: run_connectivity_app() ## End(Not run)## Not run: run_connectivity_app() ## End(Not run)
An analysis carrying a scenario holds both of its landscapes, so the layer
that actually changed is whichever of them the scenario stood in for. Ask
rather than branching on scenario_kind at every call site.
scenario_layer(x)scenario_layer(x)
x |
A |
The scenario's SpatRaster, or NULL when there is no scenario.
connectivity_report_data(), and gg_layer() to draw it.
analysis <- connectivity_report_data( habitat = example_wren_habitat(), barrier = example_wren_barrier(), species = "Superb Fairy Wren", interpatch_distance = 200, scenario = example_wren_barrier_scenario(), scenario_kind = "barrier", verbose = FALSE ) scenario_layer(analysis)analysis <- connectivity_report_data( habitat = example_wren_habitat(), barrier = example_wren_barrier(), species = "Superb Fairy Wren", interpatch_distance = 200, scenario = example_wren_barrier_scenario(), scenario_kind = "barrier", verbose = FALSE ) scenario_layer(analysis)
Add patch area column
sf_add_patch_area(patches)sf_add_patch_area(patches)
patches |
SF object. Habitat patches. |
SF object with added area column in square meters.
lizard_habitat_sf <- terra::as.polygons(example_habitat(), dissolve = TRUE) |> sf::st_as_sf() ## Not run: lizard_barrier_shp <- example_barrier_shp() buffered <- sf_habitat_buffer(lizard_habitat_sf, buffer_radius = 10) fragments <- sf_fragment_habitat(buffered, lizard_barrier_shp) remaining <- sf_drop_habitat_under_barrier(lizard_habitat_sf, lizard_barrier_shp) patches <- sf_assign_patches_to_fragments(remaining, fragments) sf_add_patch_area(patches) ## End(Not run)lizard_habitat_sf <- terra::as.polygons(example_habitat(), dissolve = TRUE) |> sf::st_as_sf() ## Not run: lizard_barrier_shp <- example_barrier_shp() buffered <- sf_habitat_buffer(lizard_habitat_sf, buffer_radius = 10) fragments <- sf_fragment_habitat(buffered, lizard_barrier_shp) remaining <- sf_drop_habitat_under_barrier(lizard_habitat_sf, lizard_barrier_shp) patches <- sf_assign_patches_to_fragments(remaining, fragments) sf_add_patch_area(patches) ## End(Not run)
Groups habitat patches by their connected fragment ID and calculates total and squared areas for connectivity metrics.
sf_aggregate_connected_patches(patch_areas)sf_aggregate_connected_patches(patch_areas)
patch_areas |
SF object. Habitat patches with area column. |
Data frame with patch_id, area, and area_squared columns.
lizard_habitat_sf <- terra::as.polygons(example_habitat(), dissolve = TRUE) |> sf::st_as_sf() lizard_barrier_shp <- example_barrier_shp() ## Not run: buffered <- sf_habitat_buffer(lizard_habitat_sf, buffer_radius = 10) fragments <- sf_fragment_habitat(buffered, lizard_barrier_shp) remaining <- sf_drop_habitat_under_barrier(lizard_habitat_sf, lizard_barrier_shp) patches <- sf_assign_patches_to_fragments(remaining, fragments) |> sf_add_patch_area() sf_aggregate_connected_patches(patches) ## End(Not run)lizard_habitat_sf <- terra::as.polygons(example_habitat(), dissolve = TRUE) |> sf::st_as_sf() lizard_barrier_shp <- example_barrier_shp() ## Not run: buffered <- sf_habitat_buffer(lizard_habitat_sf, buffer_radius = 10) fragments <- sf_fragment_habitat(buffered, lizard_barrier_shp) remaining <- sf_drop_habitat_under_barrier(lizard_habitat_sf, lizard_barrier_shp) patches <- sf_assign_patches_to_fragments(remaining, fragments) |> sf_add_patch_area() sf_aggregate_connected_patches(patches) ## End(Not run)
Determines which connected fragment each remaining habitat patch belongs to based on spatial intersection.
sf_assign_patches_to_fragments(remaining, fragment_id)sf_assign_patches_to_fragments(remaining, fragment_id)
remaining |
SF object. Remaining habitat patches after barrier removal. |
fragment_id |
SF object. Fragment geometries with IDs. |
SF object with habitat patches labeled by their fragment ID.
lizard_habitat_sf <- terra::as.polygons(example_habitat(), dissolve = TRUE) |> sf::st_as_sf() lizard_barrier_shp <- example_barrier_shp() ## Not run: buffered <- sf_habitat_buffer(lizard_habitat_sf, buffer_radius = 10) fragments <- sf_fragment_habitat(buffered, lizard_barrier_shp) remaining <- sf_drop_habitat_under_barrier(lizard_habitat_sf, lizard_barrier_shp) sf_assign_patches_to_fragments(remaining, fragments) ## End(Not run)lizard_habitat_sf <- terra::as.polygons(example_habitat(), dissolve = TRUE) |> sf::st_as_sf() lizard_barrier_shp <- example_barrier_shp() ## Not run: buffered <- sf_habitat_buffer(lizard_habitat_sf, buffer_radius = 10) fragments <- sf_fragment_habitat(buffered, lizard_barrier_shp) remaining <- sf_drop_habitat_under_barrier(lizard_habitat_sf, lizard_barrier_shp) sf_assign_patches_to_fragments(remaining, fragments) ## End(Not run)
Removes all habitat areas that intersect with barriers and splits multipolygons into individual patches.
sf_drop_habitat_under_barrier(habitat, barrier)sf_drop_habitat_under_barrier(habitat, barrier)
habitat |
SF object. Original habitat geometry. |
barrier |
SF object. Barrier geometry. |
SF object with habitat patches that don't intersect barriers.
lizard_habitat_sf <- terra::as.polygons(example_habitat(), dissolve = TRUE) |> sf::st_as_sf() lizard_barrier_shp <- example_barrier_shp() sf_drop_habitat_under_barrier(lizard_habitat_sf, lizard_barrier_shp)lizard_habitat_sf <- terra::as.polygons(example_habitat(), dissolve = TRUE) |> sf::st_as_sf() lizard_barrier_shp <- example_barrier_shp() sf_drop_habitat_under_barrier(lizard_habitat_sf, lizard_barrier_shp)
Removes barrier areas from buffered habitat and splits the result into individual polygon fragments.
sf_fragment_habitat(habitat_buffered, barrier)sf_fragment_habitat(habitat_buffered, barrier)
habitat_buffered |
SF object. Buffered habitat geometry. |
barrier |
SF object. Barrier geometry (e.g., roads). |
SF object with individual habitat fragments, each with a unique ID.
lizard_habitat_sf <- terra::as.polygons(example_habitat(), dissolve = TRUE) |> sf::st_as_sf() lizard_barrier_shp <- example_barrier_shp() ## Not run: buffered <- sf_habitat_buffer(lizard_habitat_sf, interpatch_distance = 10) sf_fragment_habitat(buffered, lizard_barrier_shp) ## End(Not run)lizard_habitat_sf <- terra::as.polygons(example_habitat(), dissolve = TRUE) |> sf::st_as_sf() lizard_barrier_shp <- example_barrier_shp() ## Not run: buffered <- sf_habitat_buffer(lizard_habitat_sf, interpatch_distance = 10) sf_fragment_habitat(buffered, lizard_barrier_shp) ## End(Not run)
Creates a buffer around habitat polygons and unions overlapping areas into
a single polygon, using sf::st_buffer(). Unlike the raster
habitat_buffer(), this works in continuous coordinate space, so there is
no resolution constraint: any buffer_radius produces an exact buffer
and the sub-cell "no buffer" problem of the raster path does not apply. The
buffer arc is approximated by nQuadSegs straight segments per quarter
circle (here 5, i.e. a 20-sided polygon), which affects the smoothness of
the outline, not whether the buffer forms. See
vignette("interpatch-distance-and-resolution").
sf_habitat_buffer(habitat, buffer_radius)sf_habitat_buffer(habitat, buffer_radius)
habitat |
SF object. Habitat spatial data. |
buffer_radius |
Numeric. The radius in metres around the habitat.
Specify it as half the interpatch distance (see |
SF object with buffered and unioned habitat geometry.
lizard_habitat_sf <- terra::as.polygons(example_habitat(), dissolve = TRUE) |> sf::st_as_sf() ## Not run: sf_habitat_buffer(lizard_habitat_sf, buffer_radius = 10) ## End(Not run)lizard_habitat_sf <- terra::as.polygons(example_habitat(), dissolve = TRUE) |> sf::st_as_sf() ## Not run: sf_habitat_buffer(lizard_habitat_sf, buffer_radius = 10) ## End(Not run)
Performs complete habitat connectivity analysis using vector-based spatial operations. Buffers habitat, fragments it along barriers, and calculates areas of connected patches.
sf_habitat_connectivity(habitat, barrier, species, interpatch_distance)sf_habitat_connectivity(habitat, barrier, species, interpatch_distance)
habitat |
SF object. Original habitat spatial data. |
barrier |
SF object. Barrier spatial data (e.g., roads, waterways). |
species |
character. Species name. |
interpatch_distance |
Numeric. The distance (in meters) where habitat
patches are considered connected. E.g., if set to 500, patches 498m apart
are connected, those 501m apart are not connected. This is passed
internally to a spatial operation known as "buffering", where this
distance is used as a radius from the edge of the habitat zone. This means
the specified |
Data frame with connectivity metrics for each connected patch,
including patch_id, area, and area_squared.
lizard_barrier_shp <- example_barrier_shp() lizard_habitat_sf <- terra::as.polygons( example_habitat(), dissolve = TRUE ) |> sf::st_as_sf() result <- sf_habitat_connectivity( habitat = lizard_habitat_sf, barrier = lizard_barrier_shp, species = "Blue-tongued lizard", interpatch_distance = 10 ) resultlizard_barrier_shp <- example_barrier_shp() lizard_habitat_sf <- terra::as.polygons( example_habitat(), dissolve = TRUE ) |> sf::st_as_sf() result <- sf_habitat_connectivity( habitat = lizard_habitat_sf, barrier = lizard_barrier_shp, species = "Blue-tongued lizard", interpatch_distance = 10 ) result
Calculates a comprehensive set of habitat connectivity metrics including
effective mesh size, probability of connectedness, and patch statistics.
There are two methods, the default, and one that dispatches on objects of
class, patch_size_tbl, created by patch_size_tbl(), which is used mostly
internally inside of habitat_connectivity(). The default method requires
"connectivity" to be a vector of areas, and also requires scalar values
(values of length 1) of interpatch distance, resolution, and species. See
examples below.
summarise_connectivity(connectivity, ...) ## Default S3 method: summarise_connectivity( connectivity, interpatch_distance, data_resolution, species, ... )summarise_connectivity(connectivity, ...) ## Default S3 method: summarise_connectivity( connectivity, interpatch_distance, data_resolution, species, ... )
connectivity |
data.frame of class "patch_size_tbl", obtained via
|
... |
extra arguments to pass through for default method. |
interpatch_distance |
Numeric. The distance (in meters) where habitat
patches are considered connected. E.g., if set to 500, patches 498m apart
are connected, those 501m apart are not connected. This is passed
internally to a spatial operation known as "buffering", where this
distance is used as a radius from the edge of the habitat zone. This means
the specified |
data_resolution |
Numeric. Data resolution in meters. |
species |
Character. Name of species analysed. |
A tibble with connectivity metrics including number of patches, probability of connectedness, effective mesh size, mean and total patch areas.
# dispatch on `patch_size_tbl` class - `lizard_areas_connected` summarise_connectivity( connectivity = lizard_areas_connected ) # default method # manually passing area, interpatch distance, resolution, and species summarise_connectivity( connectivity = lizard_areas_connected$area, interpatch_distance = 10, data_resolution = 10, species = "Blue-tongued Lizard" )# dispatch on `patch_size_tbl` class - `lizard_areas_connected` summarise_connectivity( connectivity = lizard_areas_connected ) # default method # manually passing area, interpatch distance, resolution, and species summarise_connectivity( connectivity = lizard_areas_connected$area, interpatch_distance = 10, data_resolution = 10, species = "Blue-tongued Lizard" )
Calculate the total habitat area in hectars. A wrapper around summing the
area and multiplying by 0.0001 to give the units in hectares. Intended
for usage from objects created by habitat_connectivity(). See examples
below.
total_habitat_area(area)total_habitat_area(area)
area |
Numeric vector. Area of a connected patch. |
Numeric. Total habitat area in hectares.
total_habitat_area(lizard_areas_connected$area)total_habitat_area(lizard_areas_connected$area)
The palette the package's maps use, so a map downloaded from the Shiny app matches the one on screen. Based on scico's tofino palette.
urbio_colours()urbio_colours()
A named list of colours: habitat, interpatch_distance and
barrier.
urbio_colours()urbio_colours()
Shared by the downloadable PNGs and the report, so a map keeps its shape
across both. Width and heights are inches and dpi is dots per inch, as
ggplot2::ggsave() and Quarto's fig-width, fig-height and fig-dpi
expect. tall_height is for the faceted over-distance plot, which needs
more vertical room than a map.
urbio_figure_size()urbio_figure_size()
A named list: width, height, tall_height and dpi.
urbio_figure_size()urbio_figure_size()
Writes every map, table and GIS layer a planner needs into dir, laid out
in folders by interpatch distance, with a README explaining each file.
zip_connectivity_assets() wraps this into a single archive, which is
what the Shiny app's download button uses.
write_connectivity_assets(x, dir, reports = quarto_available())write_connectivity_assets(x, dir, reports = quarto_available())
x |
A |
dir |
Directory to write into. Created if it doesn't exist. |
reports |
Also render the HTML and PDF reports into |
The manifest, invisibly: a tibble of path, kind and
description, one row per file written, with paths relative to dir.
connectivity_report_data(), zip_connectivity_assets(), and
generate_connectivity_report() for one report on its own.
report_data <- connectivity_report_data( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) assets <- write_connectivity_assets( report_data, dir = tempfile(), reports = FALSE ) assets$pathreport_data <- connectivity_report_data( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) assets <- write_connectivity_assets( report_data, dir = tempfile(), reports = FALSE ) assets$path
Writes the report as a Quarto document you own a copy of, next to the data
it draws. Open it, change it, render it. render_connectivity_report()
renders it the way the package does, and
generate_connectivity_report() does both steps at once when the source
isn't wanted.
write_connectivity_report(x, path = NULL)write_connectivity_report(x, path = NULL)
x |
A |
path |
File to write, ending in |
Two files are written: path, and the analysis beside it as
<name>-data.rds. The document reads that file by name, so the two
travel together. Moving one without the other breaks the render.
The absolute path of the .qmd, invisibly.
render_connectivity_report() to render it,
generate_connectivity_report() to do both.
report_data <- connectivity_report_data( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) qmd <- write_connectivity_report( report_data, file.path(tempdir(), "lizard-report.qmd") ) readLines(qmd, n = 5)report_data <- connectivity_report_data( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) qmd <- write_connectivity_report( report_data, file.path(tempdir(), "lizard-report.qmd") ) readLines(qmd, n = 5)
Writes a connectivity_report_data object to an .rds file, and reads it
back. The summary, the habitat and barrier layers, and the patch raster
for each distance all travel together in the one file.
write_report_data(x, path) read_report_data(path)write_report_data(x, path) read_report_data(path)
x |
A |
path |
File to write to, or read from. |
base::saveRDS() on its own isn't enough. A SpatRaster points at
memory or at a file on disk rather than carrying its own values, so it
saves as a null pointer and comes back unusable. terra::wrap() packs
the values into the object on the way out, and terra::unwrap() restores
them on the way in.
This is also how an analysis reaches the separate R session that Quarto renders the report in.
write_report_data() returns path invisibly;
read_report_data() returns the connectivity_report_data.
report_data <- connectivity_report_data( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) path <- write_report_data(report_data, tempfile(fileext = ".rds")) read_report_data(path)$connectivityreport_data <- connectivity_report_data( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) path <- write_report_data(report_data, tempfile(fileext = ".rds")) read_report_data(path)$connectivity
write_connectivity_assets() into a folder named for the species and the
date, archived. This is what the Shiny app's download button returns.
zip_connectivity_assets(x, path, reports = quarto_available())zip_connectivity_assets(x, path, reports = quarto_available())
x |
A |
path |
Path to write the |
reports |
Also render the reports into the archive. See
|
The absolute path written, invisibly.
write_connectivity_assets() to write the files without
archiving, and zip_connectivity_reports() for the reports alone.
report_data <- connectivity_report_data( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) zip_path <- zip_connectivity_assets( report_data, tempfile(fileext = ".zip"), reports = FALSE ) zip::zip_list(zip_path)$filenamereport_data <- connectivity_report_data( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) zip_path <- zip_connectivity_assets( report_data, tempfile(fileext = ".zip"), reports = FALSE ) zip::zip_list(zip_path)$filename
The HTML and PDF reports, in a folder named for the species and the date, archived. This is the app's reports-only download, for someone who wants the write-up without the GIS layers. The analysis is packed once and rendered twice, so this costs less than two separate calls.
zip_connectivity_reports(x, path)zip_connectivity_reports(x, path)
x |
A |
path |
Path to write the |
The absolute path written, invisibly.
zip_connectivity_assets() for everything, and
generate_connectivity_report() for one report on its own.
report_data <- connectivity_report_data( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) # rendering needs the Quarto command line tool if (quarto_available()) { zip_path <- zip_connectivity_reports( report_data, tempfile(fileext = ".zip") ) zip::zip_list(zip_path)$filename }report_data <- connectivity_report_data( habitat = example_habitat(), barrier = example_barrier(), species = "Blue Tongue Lizard", interpatch_distance = 20, verbose = FALSE ) # rendering needs the Quarto command line tool if (quarto_available()) { zip_path <- zip_connectivity_reports( report_data, tempfile(fileext = ".zip") ) zip::zip_list(zip_path)$filename }