Skip to contents

Lift 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.

Usage

rfp_qgs_style_export(path, layers = NULL, path_out = NULL, xref_layers = NULL)

Arguments

path

Character. Path to a source .qgs project 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 .qml files 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.

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"