
Add Editable Tracking Fields to a QGIS Project
Source:R/rfp_tracking.R
rfp_qgs_tracking_fields_add.RdThe project half of rfp_tracking_fields_add(). A column with no widget is
invisible in the field app, so both are needed; run the GeoPackage half first.
Arguments
- qgs
Character. Path to the project
.qgs.- fields
NULL(default) for the shipped set, or a data frame in its shape.- force
Logical. Rewrite entries for fields that are already configured. Default
FALSE, which adds only what is missing. Needed when the shipped set changes for a field a project already has - new dropdown options, a new default, a changed label - because an add-only writer cannot express that. Scoped to the fields infields, so the four the app itself populates are never touched.- backup
Logical. Copy the
.qgsbeside itself first. DefaultTRUE.
Value
Invisibly, a character vector of the fields configured - empty when every one was already present.
Details
Writes <fieldConfiguration> (the widget), <aliases> (the label) and a
<default> for any field declaring one, onto the project's Position track
maplayer.
Three things about where the nodes go
They sit immediately before
<defaults>, and no<flags>is written. Measured againstForm PSCISin the shipped templates, QGIS orders thesefieldConfiguration, aliases, splitPolicies, defaults, .... A tracking maplayer has 16 children and no style block at all - the plugin writes one that ends atdefaults, constraints, constraintExpressions. The rule that a style block starts atflags(#171) governs the positional scan inrfp_qgs_style_export(), not how QGIS reads a maplayer, and the plugin's own working layer is the proof.No
<attributeEditorForm>and no<editorlayout>. Absent, QGIS uses the generated layout, which shows every field. A tab layout shows only what it lists, so adding one would hide the plugin's four (#159).indexon an alias is computed from the GeoPackage, not guessed, which is also what makes the orphan check free: a field with no column cannot be given an index. QGIS drops a form element naming an absent field silently, and this writer touches two artifacts that can drift apart.
See also
rfp_tracking_fields_add() for the GeoPackage half.
Other tracking:
rfp_qgs_tracking_add(),
rfp_qgs_tracking_get(),
rfp_qgs_tracking_set(),
rfp_tracking_fields_add(),
rfp_tracking_layer_create()
Examples
d <- file.path(tempdir(), "trkproj")
dir.create(d, showWarnings = FALSE)
qgs <- file.path(d, "p.qgs")
file.copy(system.file("templates", "bcrestoration_mobile.qgs", package = "rfp"),
qgs, overwrite = TRUE)
#> [1] TRUE
gpkg <- file.path(d, "tracking_layer.gpkg")
rfp_tracking_layer_create(gpkg, overwrite = TRUE)
rfp_tracking_fields_add(gpkg)
#> ✔ Added 4 fields to "tracking_layer": track_name, track_type, track_description, and named_by
rfp_qgs_tracking_add(qgs, backup = FALSE)
#> ✔ Added "Position track" to /tmp/RtmpfSNDuG/trkproj/p.qgs
rfp_qgs_tracking_fields_add(qgs, backup = FALSE)
#> ✔ Configured 4 tracking fields in /tmp/RtmpfSNDuG/trkproj/p.qgs