Skip to contents

Writes 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>.gpkg holds <schema>.<table>).

name

Character vector. Display name in the layer tree. NULL (default) uses table.

qml

Character vector. Style to apply. NULL (default) looks for a .qml beside the GeoPackage.

geometry

Character vector, one of "Point", "Line", "Polygon". NULL (default) reads it from the GeoPackage - from gpkg_geometry_columns when that declares a concrete type, and otherwise from a feature, since most rfp-written layers declare GEOMETRY.

subset

Character vector. A provider filter - the SQL WHERE clause the OGR provider applies, written into the datasource as |subset=<expr>. NULL (default) or NA means 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 own name: 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 themes names (#225). FALSE with themes = "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 default Base - lidar - it stays off.

themes

Character. Map themes to join, or "all" - which themes the layer is PRESENT in, while visible decides 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. A visible = FALSE join ticks nothing.

above

Character. Insert directly above this layer id in the ordering lists - see rfp_qgs_raster_add().

Value

Invisibly, a character vector of the layer ids written (NA for a layer already present).

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.

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")
)
} # }