Skip to contents

Programmatic rename of duplicate layer names, duplicate group names, and invalid characters (commas) in .qgs XML files. A layer has three name locations - <layername>, layer-tree-layer name and legendlayer name - and all three are kept in sync using the layer ID as the cross-reference anchor.

Usage

rfp_qgs_rename(
  path,
  dry_run = TRUE,
  fix_duplicates = TRUE,
  fix_commas = TRUE,
  fix_groups = TRUE,
  xref_names = NULL,
  backup = TRUE
)

Arguments

path

Character. Path to a .qgs project file.

dry_run

Logical. If TRUE (default), preview changes without modifying the file. Set to FALSE to apply changes.

fix_duplicates

Logical. Append context from datasource path to make duplicate layer names unique (e.g., odm_orthophoto becomes odm_orthophoto_197640_buck_buc192). Default TRUE.

fix_commas

Logical. Remove commas from all names (WMS spec forbids commas). Default TRUE.

fix_groups

Logical. Append suffixes to duplicate group names within the same parent. Default TRUE.

xref_names

Named character vector. Explicit renames to apply. Names are current layer names, values are new names (e.g., c("lakes" = "Lakes Layer")).

backup

Logical. Create a .qgs.bak backup before modifying. Default TRUE. Ignored when dry_run = TRUE.

Value

A tibble with columns element, old_name, new_name, id, reason describing all changes made (or that would be made in dry-run).

Details

A group is not like that, and saying otherwise was this function's standing defect (#329). A group has a fourth reference, in every map theme that records its check state, and that one is keyed by path rather than by id - so the id anchor cannot reach it. Renaming a group therefore left every theme naming a group the tree no longer had: the reference dangled, the group read as unchecked, and every layer inside it was present, listed, visible="1" and drew nothing (#216).

Nothing is repointed. Group state is derived from where a theme's visible layers sit, so any preset a group rename buries is re-derived in place, with no old-to-new map to get byte-exact. That reconciliation runs only where a group was actually renamed - a call that renames nothing leaves the themes alone, even a project that arrived carrying buried entries, because repairing those is its own operation with its own front door, rfp_qgs_theme_unbury().

The write is then checked differentially and refuses if the operation buried anything. Real projects arrive carrying buried entries from unrelated causes, so an absolute check would refuse to operate on them and blame this call for inherited state (#134/#213).

By default runs in dry-run mode - previews changes without modifying the file. Set dry_run = FALSE to apply changes.

Examples

if (FALSE) { # \dontrun{
# Preview changes
rfp_qgs_rename("~/Projects/gis/restoration_wedzin_kwa/restoration_wedzin_kwa.qgs")

# Apply changes
rfp_qgs_rename("my_project.qgs", dry_run = FALSE)

# Explicit rename
rfp_qgs_rename("my_project.qgs", dry_run = FALSE,
               xref_names = c("odm_orthophoto" = "ortho_site_a"))
} # }