Skip to contents

The preview expression is what QGIS and Mergin Maps show when a feature is identified or tapped - the line at the top of the attribute form, and the label in the identify results. It is a property of the layer's data, so a style carrying one from the layer it was authored against will name a column the new layer may not have, and QGIS answers that by falling back to the raw feature id. Silently: no error, and nothing in the project that looks wrong until someone taps a polygon in the field and reads WHSE_BASEMAPPING.CWB_FLOODPLAINS_BC_AREA_SVW.fid--65332c11_1a040979f81_5f1b.

Usage

rfp_qgs_preview_set(qgs, layer, expr, backup = TRUE)

Arguments

qgs

Character. Path to the project .qgs.

layer

Character. The layer's display name, as it appears in the layer tree. An unknown name, or one shared by more than one layer, is an error naming the candidates - never a guess.

expr

Character, a QGIS expression. Quote a column reference ("floodplain_name"), which is what QGIS itself writes. NULL removes the expression, restoring QGIS's default preview.

backup

Logical. Write <qgs>.bak before mutating. Default TRUE.

Value

Invisibly, the expression that was there before - NA_character_ if the layer had none.

Details

expr is therefore checked against the layer's real columns before it is written, and a reference that does not resolve is an error. That is stricter than the same check during rfp_qgs_vector_add(), which warns and drops - adding a layer is a bulk operation that must not abort, while setting an expression by name is someone asking for this one.

A layer whose datasource rfp cannot resolve to a readable GeoPackage table - a WMS or ArcGIS service, another OGR driver, a file that is absent - is written unchecked, with a message saying so. Absence has to be proven (see ?rfp-layout); refusing on a table rfp simply could not read would make the function unusable on exactly the layers it cannot judge.

Examples

project <- file.path(tempdir(), "preview_example")
dir.create(project)
file.copy(
  list.files(
    system.file("testdata", "subset_fixture", "parent", package = "rfp"),
    full.names = TRUE),
  project
)
#> [1] TRUE TRUE TRUE
qgs <- file.path(project, "parent.qgs")

rfp_qgs_preview_set(qgs, "streams", "\"name\"", backup = FALSE)
#>  "streams" previews as "\"name\""

# A column the layer does not have is refused rather than written.
try(rfp_qgs_preview_set(qgs, "streams", "\"gnis_name\"", backup = FALSE))
#> Error : Expression names field(s) 'streams' does not have: gnis_name
#> Available: fid, geom, id, name

rfp_qgs_preview_set(qgs, "streams", NULL, backup = FALSE)
#>  Removed the preview expression from "streams"
unlink(project, recursive = TRUE)