
Add a Background Vector Layer to a QGIS Project
Source:R/rfp_qgs_vector_add.R
rfp_qgs_vector_add.RdWrites a <maplayer> and its layer-tree entry into an existing .qgs, so a
vector layer added to a project after creation becomes visible without a
QGIS Desktop round trip. The counterpart of rfp_qgs_raster_add() for
vectors, and of rfp_qgs_form_add() for layers that are not forms.
Usage
rfp_qgs_vector_add(
qgs,
gpkg,
table = NULL,
name = NULL,
qml = NULL,
geometry = NULL,
subset = NULL,
group,
position = c("bottom", "top"),
visible = TRUE,
themes = "all",
above = NULL
)Arguments
- qgs
Character. Path to the project
.qgs.- gpkg
Character vector. Path(s) to the GeoPackage(s), absolute or relative to the project directory.
- table
Character vector. Table inside each GeoPackage.
NULL(default) uses the file stem, which is the rfp split-layout convention (<schema>.<table>.gpkgholds<schema>.<table>).- name
Character vector. Display name in the layer tree.
NULL(default) usestable.- qml
Character vector. Style to apply.
NULL(default) looks for a.qmlbeside the GeoPackage.- geometry
Character vector, one of
"Point","Line","Polygon".NULL(default) reads it from the GeoPackage - fromgpkg_geometry_columnswhen that declares a concrete type, and otherwise from a feature, since most rfp-written layers declareGEOMETRY.- subset
Character vector. A provider filter - the SQL
WHEREclause the OGR provider applies, written into the datasource as|subset=<expr>.NULL(default) orNAmeans no filter. Quote string literals with single quotes: a double-quoted token is an SQL identifier, and one that names no column is refused (#318).Several filtered views of one table are several layers, so repeat the GeoPackage path - the call is vectorized over
gpkg. Each needs its ownname: a layer id is a digest of the name and the path, not the subset.- group
Character. Layer-tree group to insert into. Required. It used to default to
NULL, documented as inserting at the root; measured, it created an unnamed group at the END of the tree - after every named group and therefore under the basemaps, where nothing draws (#213). There is no correct root-level placement either, so the argument is required and an unknown project is answered with the groups it does have.- position
"bottom"(default) or"top"within the group. Background layers belong under the data they provide context for, which is the opposite of a form.- visible
Logical. The layer's state: checked in the layer tree, and the state its entry is written with in every theme
themesnames (#225).FALSEwiththemes = "all"is present-and-off everywhere, and a crew can turn it on for one lookup. One QGIS desktop case undoes it: where a theme checks the layer's group, switching to that theme in the Layers panel turns the layer on and rewrites the entry to on, which a save persists. In a group the theme leaves unchecked -rfp_qgs_raster_add()'s defaultBase - lidar- it stays off.- themes
Character. Map themes to join, or
"all"- which themes the layer is PRESENT in, whilevisibledecides whether it is on there. A visible join also makes the layer draw there: each group on its path that the theme leaves unchecked is ticked in that theme (#358), or the layer is listed, visible and hidden by its group (#216). Only themes recording group state are touched, and nothing is ever unchecked. Avisible = FALSEjoin ticks nothing.- above
Character. Insert directly above this layer id in the ordering lists - see
rfp_qgs_raster_add().
Details
Vectorized over gpkg: several layers can be added in one call, and each is
inserted in the order given.
A layer with no style draws as a default thin line or dot. QGIS stores a
style in the project when a layer is added and never re-queries the data, so
a layer_styles table in the GeoPackage does not help here (see the styling
notes in the package README). Pass qml =, or put a .qml beside the
GeoPackage and it is picked up automatically.
A style is checked before it is copied (#403). Every part of a .qml is
written into the project and synced to every device that opens it, so a
style is refused when it would carry another layer's connection: a
<datasource>, a relation or value-relation widget naming a source outside
the project, attribute actions, a namespace or entity declaration, or
connection text such as host=, PG: or user:pass@ anywhere in it.
Comments are dropped. A <relation> to a layer inside the project, as QGIS
writes for a parent and child form, is kept. A value-relation or
relation-reference widget is refused wherever it points, since QGIS writes a
provider name beside its source. Nothing is written when any style in the
call is refused, and the message names where, never the value.
See also
Other qgs:
rfp_qgs_form_add(),
rfp_qgs_layer_rm(),
rfp_qgs_postgres_add(),
rfp_qgs_preview_set(),
rfp_qgs_raster_add(),
rfp_qgs_service_add(),
rfp_qgs_service_rm(),
rfp_qgs_services(),
rfp_qgs_style_export(),
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_rename(),
rfp_qgs_theme_rm(),
rfp_qgs_theme_set(),
rfp_qgs_theme_unbury(),
rfp_qgs_themes(),
rfp_qwc_config_create(),
rfp_qwc_probe(),
rfp_raster_style_path(),
rfp_raster_styles()
Examples
if (FALSE) { # \dontrun{
rfp_qgs_vector_add(
"project.qgs",
"background_layers/osm.trail.gpkg",
name = "Trails",
group = "Roads,Railways,Pipelines",
qml = system.file("extdata", "styles", "vector", "osm.trail.qml",
package = "rfp")
)
} # }