
Add a PostGIS Layer to a QGIS Project by pg Service Name
Source:R/rfp_qgs_postgres_add.R
rfp_qgs_postgres_add.RdWrites a <maplayer> reading a PostGIS table through QGIS's postgres
provider, and its layer-tree entry, into an existing .qgs. The layer is read
live from the database rather than copied into the project, which suits a
large, shared layer such as the FWA stream network.
Usage
rfp_qgs_postgres_add(
qgs,
service,
schema,
table,
key,
wkb_type,
geometry_column = "geom",
crs = "EPSG:3005",
name = NULL,
subset = NULL,
qml = NULL,
group,
position = c("bottom", "top"),
visible = TRUE,
themes = "all",
above = NULL
)Arguments
- qgs
Character. Path to the project
.qgs.- service
Character. The pg service name, as declared in
pg_service.conf. Letters, digits,_,.and-only.- schema, table
Character. The table to read. Letters, digits and
_.- key
Character. A unique integer column, usually the primary key. Lower-case letters, digits and
_.- wkb_type
Character. The layer's WKB type as the table declares it, e.g.
"MultiLineString"or"LineStringZM": the single orMultiform ofPoint,LineStringorPolygon, optionally withZ,MorZM. There is no file to probe, so it is stated, and QGIS writes it verbatim intowkbType. Named apart fromrfp_qgs_vector_add()'sgeometry, which is the dimension ("Line"), not the WKB name.- geometry_column
Character. Defaults to
"geom".- crs
Character.
"EPSG:<code>", defaulting to"EPSG:3005". The project must already use it, or rfp must ship its<srs>block (seerfp_qgs_raster_add()).- name
Character. Display name in the layer tree.
NULL(default) usestable.- subset
Character. A provider filter: an SQL
WHEREclause PostgreSQL runs, written assql=<expr>at the end of the datasource.NULL(default) orNAmeans no filter. Unlike SQLite, PostgreSQL never reads a double-quoted token as a string, so"KO"is a column and a wrong one makes the layer unavailable rather than silently matching. Quote values with single quotes.- qml
Character. Style to apply.
NULL(default) leaves the layer on a default symbol, with a warning.- group
Character. Layer-tree group to insert into. Required; see
rfp_qgs_vector_add()for why there is no root-level default.- position
"bottom"(default) or"top"within the group.- visible
Logical. Checked in the layer tree, and the state its entry is written with in every theme
themesnames.- themes
Character. Map themes to join, or
"all"(default). Seerfp_qgs_vector_add().- above
Character. Insert directly above this layer id in the ordering lists - see
rfp_qgs_raster_add().
Details
A pg service name is the only connection this accepts. The host, port,
database and credentials live in each machine's pg_service.conf (found
through PGSERVICEFILE, or ~/.pg_service.conf), so the project pushed to
Mergin carries none of them. A datasource that would carry host,
hostaddr, port, dbname, user, password or authcfg is refused. A
device without that service shows the layer as unavailable, and the rest of
the project is unaffected.
The node follows what QGIS 4.2.1 writes for the same layer
(inst/testdata/nodes/vector_postgres_service.xml), including
checkPrimaryKeyUnicity='0': key is trusted to be unique, so QGIS does not
scan the table to check it on every load. The extent is not written, because
rfp does not query the database; QGIS computes it from the provider.
Which machines can draw it. A project reads the layer wherever QGIS has
the service: QGIS Desktop, or QGIS Server with a service file. Mergin Maps on
a phone reads no database, so there the layer is unavailable while the rest
of the project works. Add one to a field project only where a desktop
reviewer is the consumer, or pass visible = FALSE.
One layer per call, unlike the vectorised rfp_qgs_vector_add(). Out of
scope: composite keys, quoted or mixed-case key and geometry columns, a
table with several geometry columns read as one layer, and non-EPSG CRSs.
Nothing about the table is checked here: rfp does not connect to the
database. A wrong name, or a missing column in subset, shows up when QGIS
opens the project, as an unavailable layer. A wrong wkb_type does not:
QGIS reads the table filtered to the declared type, so a line table declared
"Point" loads valid and shows no features, with nothing reporting it
(measured in QGIS 4.2.1). Take the type from the table's own
geometry_columns row. Either way the project keeps the layer:
rfp_project_create()'s trim and the audit's dead check leave it alone,
since neither can judge a database (#108).
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_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_qgs_vector_add(),
rfp_qwc_config_create(),
rfp_qwc_probe(),
rfp_raster_style_path(),
rfp_raster_styles()
Examples
qgs <- file.path(tempdir(), "postgres_add_example.qgs")
file.copy(system.file("testdata", "raster_add_fixture.qgs", package = "rfp"),
qgs, overwrite = TRUE)
#> [1] TRUE
id <- rfp_qgs_postgres_add(
qgs,
service = "fwapg",
schema = "whse_basemapping",
table = "fwa_stream_networks_sp",
key = "linear_feature_id",
wkb_type = "LineStringZM",
name = "Streams (fwapg)",
subset = "stream_order >= 4",
group = "Streams"
)
#> ! "Streams (fwapg)": no style - the layer will draw with a default symbol
#> ✔ Added "Streams (fwapg)" (LineStringZM, service "fwapg")
#> ℹ Checked "bcrestoration Mobile " and "bcrestoration Mobile /Streams" in 1 theme so "Streams (fwapg)" draws there
# The project names the service and nothing else about the database.
doc <- xml2::read_xml(qgs)
xml2::xml_text(xml2::xml_find_first(
doc, sprintf("//maplayer[id='%s']/datasource", id)))
#> [1] "service='fwapg' key='linear_feature_id' srid=3005 type=LineStringZM checkPrimaryKeyUnicity='0' table=\"whse_basemapping\".\"fwa_stream_networks_sp\" (geom) sql=stream_order >= 4"