Add a new vector layer to QGIS.

iface.addVectorLayer(x, baseName = NULL, ...)

# S3 method for geo_json
iface.addVectorLayer(x, baseName = NULL, crs = 4326, ...)

# S3 method for sf
iface.addVectorLayer(x, baseName = NULL, proj4 = FALSE, ...)

# S3 method for list
iface.addVectorLayer(x = list(url = NULL, layers = NULL, styles
  = NULL, format = "image/svg", crs = "EPSG:4326"), baseName = NULL,
  providerKey = "wfs", ...)

# S3 method for default
iface.addVectorLayer(x, baseName = NULL,
  providerKey = "ogr", proj4 = FALSE, ...)

Arguments

x

an object of class geo_json, sf, one of sp's Spatial* classes or else a QGIS provider string or named list of strings from which a provider string can be constructed (see below)

baseName

name for the new layer to be displayed in the QGIS layers panel (will be filled by an appropriate default if missing)

...

low-level parameters (such as host and auth) to be passed on to qgisremote()

crs

QGIS-understandable EPSG id or specification string identifying the coordinate reference system of the data (see qgisCRS()). This is passed on to QGIS to ensure the data is added correctly, since the GeoJSON format itself does not contain any projection information.

proj4

logical (passed on to qgisCRS()): when custom projections are extracted from spatial data objects to be passed on to QGIS, setting proj4 = TRUE will keep proj4 strings from being converted to (usually safer) WKT specifications.

providerKey

QGIS provider key (only applicable when x is a provider string)

Value

A representation of the layer just added (of list of class qgislayer()).

Methods (by class)

  • geo_json: If given an object of class geo_json, adds it to QGIS through a HTTP POST request. GeoJSON does not contain information on the coordinate reference system of the data, which must be passed explicitly using the crs argument (defaults to EPSG 4326).

  • sf: If x is an object of class sf or one of sp's Spatial* classes, converts the spatial data to GeoJSON and adds it to QGIS through a HTTP POST request. Many other geospatial formats can be added through this function by first converting them to sf objects using sf::st_as_sf().

  • list: When passed a named list, compiles a WFS service provider string based on the named options found in the list by concatenating their entries in the format name1=value1&name2=value2&..., then tries to add this as a vector layer.

  • default: When x is a simple character string, it is assumed to be a QGIS provider string denoting a local or external resource, and instructs QGIS to add the resource from that provider url using the engine specified by the given providerKey. It is the user's responsibility to make sure that the machine on which the QGIS Network API is running is able to access and read the resource.

See also

qgislayer(), mapLayer(), mapLayers(), iface.addRasterLayer(), iface.addTileLayer()

Examples

botswana <- raster::getData('GADM', country = 'BWA', level = 2) iface.addVectorLayer(botswana, baseName = 'Botswana admin boundaries')
#> Transferring data...
#> Converting 30 features to GeoJSON...
#> QGIS map layer with the following properties: #> crs: CRS arguments: #> +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 #> valid: [1] TRUE #> name: [1] "Botswana admin boundaries OGRGeoJSON Polygon" #> extent: [1] 19.99953 -26.90738 29.36831 -17.78081 #> publicSource: [1] "/tmp/tmptxTYv5" #> type: [1] 0 #> id: [1] "Botswana_admin_boundaries20170831104311027" #> isEditable: [1] FALSE