
Rename Duplicate and Invalid Names in a QGIS Project File
Source:R/rfp_qgs_rename.R
rfp_qgs_rename.RdProgrammatic rename of duplicate layer names, duplicate group names, and
invalid characters (commas) in .qgs XML files. Updates all three name
locations in sync: <layername>, layer-tree-layer name, and
legendlayer name, 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
.qgsproject file.- dry_run
Logical. If
TRUE(default), preview changes without modifying the file. Set toFALSEto apply changes.- fix_duplicates
Logical. Append context from datasource path to make duplicate layer names unique (e.g.,
odm_orthophotobecomesodm_orthophoto_197640_buck_buc192). DefaultTRUE.- 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.bakbackup before modifying. DefaultTRUE. Ignored whendry_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
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"))
} # }