Skip to contents

Drops a layer's <maplayer> and every reference to it - the layer tree, each map theme that lists it, the ordering lists - then prunes any group the removal left empty.

Usage

rfp_qgs_layer_rm(qgs, layer, backup = TRUE)

Arguments

qgs

Character. Path to the project .qgs.

layer

Character vector of layer display names, as they appear in the layer tree. An unknown name is an error listing what the project has - removing five of six and reporting success is the failure worth preventing. A name shared by more than one layer is an error naming the candidates rather than a guess.

backup

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

Value

Invisibly, the number of layers removed.

Details

Two cases need this, and the second is the one that is easy to argue away:

  • A dead reference. rfp_project_create() drops a layer that is empty in the project's AOI, and nothing repairs the project afterwards, so a .qgs can point at a GeoPackage that was never written. QGIS then raises Handle Unavailable Layers on every open - on a phone, the first thing a field user meets. rfp_project_audit()'s dead element finds these.

  • A live layer, when the data model changes underneath it. Collapsing six healthy per-watershed layers into two means deleting six working layers, which by hand is four elements each and a theme count that is easy to under-count.

No file is deleted. Only the reference goes, so the operation is reversible by re-adding, and it is safe to run on a live project. Removing a form's data as well is NewGraphEnvironment/rfp#129.

Examples

project <- file.path(tempdir(), "layer_rm_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_layer_rm(qgs, "streams", backup = FALSE)
#>  Removed 1 layer: "streams"
#>  Removed from 1 map theme: "ThemeA (1)". A preset survives under its own name with the entry swept out, so nothing else reports this.

# An unknown name is an error rather than a silent skip.
try(rfp_qgs_layer_rm(qgs, "not a layer", backup = FALSE))
#> Error : No layer named 'not a layer' in this project.
#> Present: lakes, roads, dtm, external, Annotations
unlink(project, recursive = TRUE)