
Set a Layer's Preview Expression in a QGIS Project
Source:R/rfp_qgs_preview_set.R
rfp_qgs_preview_set.RdThe 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.
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.NULLremoves the expression, restoring QGIS's default preview.- backup
Logical. Write
<qgs>.bakbefore mutating. DefaultTRUE.
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.
See also
Other qgs:
rfp_qgs_form_add(),
rfp_qgs_layer_rm(),
rfp_qgs_raster_add(),
rfp_qgs_service_add(),
rfp_qgs_service_rm(),
rfp_qgs_services(),
rfp_qgs_style_export(),
rfp_qgs_style_set(),
rfp_qgs_subset_get(),
rfp_qgs_subset_set(),
rfp_qgs_theme_add(),
rfp_qgs_theme_lookup(),
rfp_qgs_theme_names(),
rfp_qgs_theme_rm(),
rfp_qgs_theme_set(),
rfp_qgs_theme_unbury(),
rfp_qgs_themes(),
rfp_qgs_vector_add(),
rfp_qwc_config_create(),
rfp_raster_style_path(),
rfp_raster_styles()
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)