
Export Per-Layer QGIS Styles from a Project as QML
Source:R/rfp_qgs_style_export.R
rfp_qgs_style_export.RdLift each vector layer's style out of a .qgs project into a standalone
.qml file. This is an XML copy, not a translation - the QML QGIS writes
for a layer is that layer's <maplayer> node minus its source binding, so
the exported style is byte-equivalent to one saved from QGIS Desktop.
Arguments
- path
Character. Path to a source
.qgsproject file.- layers
Character vector of QGIS layer names to export.
NULL(default) exports every vector layer that resolves to a table.- path_out
Character. Directory to write the
.qmlfiles into. Default is<project>_styles/alongside the source. Created if missing.- xref_layers
Named character vector mapping QGIS layer names to target table names, for layers whose datasource does not name the table you want. Names are QGIS layer names, values are table names.
Value
A tibble with one row per exported style: layer (the QGIS layer
name), table (the resolved target table) and path_qml.
Details
Everything QGIS considers style comes across: symbology, labelling, diagrams,
field configuration, aliases, form layout, and the rest. Contrast
rfp_styles_apply() with registry =, which rebuilds symbols from the gq
registry and can only carry what that cross-backend abstraction models.
The project file is read only. Nothing here re-saves a .qgs, so it is
safe to run against a project saved by a newer QGIS than any available
headless build.
Pass the result to rfp_styles_apply() to write the styles into a
GeoPackage's layer_styles table.
See also
Other qgs:
rfp_qgs_form_add(),
rfp_qgs_layer_rm(),
rfp_qgs_preview_set(),
rfp_qgs_raster_add(),
rfp_qgs_service_add(),
rfp_qgs_service_rm(),
rfp_qgs_services(),
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
qgs <- system.file("testdata", "styles_fixture.qgs", package = "rfp")
out <- file.path(tempdir(), "styles")
styles <- rfp_qgs_style_export(qgs, path_out = out)
#> ✔ Exported 2 styles to: /tmp/RtmpfSNDuG/styles
styles
#> # A tibble: 2 × 3
#> layer table path_qml
#> <chr> <chr> <chr>
#> 1 form_exceedance form_exceedance /tmp/RtmpfSNDuG/styles/form_exceedance.qml
#> 2 roads_dra roads_dra /tmp/RtmpfSNDuG/styles/roads_dra.qml
# Each row is a standalone QML holding that layer's full symbology
qml <- xml2::read_xml(styles$path_qml[1])
xml2::xml_name(xml2::xml_children(qml))[1:5]
#> [1] "flags" "temporal" "elevation" "renderer-v2" "selection"
# The renderer comes across verbatim - nothing is re-derived
xml2::xml_attr(xml2::xml_find_first(qml, ".//renderer-v2"), "type")
#> [1] "singleSymbol"