
Build a rule-based fill style from filters and colours
Source:R/rfp_style_rules_build.R
rfp_style_rules_build.RdWrites a .qml carrying a RuleRenderer over polygon fill symbols: one rule
per entry, each with its own filter expression, legend label and colour. The
result is consumed by rfp_qgs_vector_add()'s qml = or by
rfp_qgs_style_set(), both of which take a QML path.
Arguments
- rules
A list of rules, each a list with
filter(a QGIS expression),label(the legend entry) andcolour("r,g,b", hex, or a namegrDevices::col2rgb()accepts).- path
Where to write the
.qml.- alpha
Symbol opacity, 0-1. Written to
<symbol alpha=>, which is where every QGIS-authored fill in the store carries it - not on the symbol layer, and not as a fourth colour component.- seed
A
.qmlto take the renderer structure from. Defaults to the shipped seed, whichdata-raw/styles/style_build-rules.Rlifts from two QGIS-authored donors. An argument rather than a constant so the generator is testable against a synthetic seed with no container.Three constraints on a caller-supplied one, because each is a way to get a plausible file that paints the wrong thing. Its renderer must be a
RuleRenderercarrying one prototype rule and one fill symbol; that symbol's layer must be aSimpleFillusing the modern<Option type="Map">encoding - aShapeburstFill, aGradientFillor a pre-3.x<prop k= v=>layer is refused rather than silently left at the seed's own colour. Only the firstSimpleFillin a multi-layer symbol is recoloured, and a nested rule tree is cloned per output rule; neither is reachable from the shipped seed, and neither is supported.- overwrite
Overwrite
pathif it exists.
Details
It exists because a categorized renderer matches exact values, so a
classification like "every transition that starts with Trees" - two LIKE
patterns over one column - is not expressible as one. Rule-based is, and rfp
shipped no polygon example to seed from: every RuleRenderer rfp ships - in
the store and in both templates - is the same point layer with marker
symbols.
What this does not check
Two filters that overlap are not detectable without the data, and a
feature matching both rules is drawn twice - at alpha < 1 that reads as a
different colour rather than as an error. A duplicate filter is refused
because it is decidable here; an overlap is a per-project count to run
against the layer, which is what rtj#337 prescribes.
Examples
out <- tempfile(fileext = ".qml")
rfp_style_rules_build(
rules = list(
list(filter = "\"transition\" LIKE 'Trees -> %'",
label = "trees from", colour = "223,65,204"),
list(filter = "\"transition\" LIKE '% -> Trees'",
label = "trees to", colour = "255,127,0")
),
path = out,
alpha = 0.75
)
#> ✔ Wrote /tmp/RtmpGuotGj/file1cb9fbfce58.qml: 2 rules, alpha 0.75.
doc <- xml2::read_xml(out)
xml2::xml_attr(xml2::xml_find_all(doc, ".//rule"), "label")
#> [1] "trees from" "trees to"
xml2::xml_attr(xml2::xml_find_first(doc, ".//renderer-v2"), "type")
#> [1] "RuleRenderer"