Subgroup: Json

Class: QgsJsonExporter

class qgis.core.QgsJsonExporter(vectorLayer: QgsVectorLayer = None, precision: int = 6)

Bases: sip.wrapper

Constructor for QgsJsonExporter.

Parameters:
  • vectorLayer – associated vector layer (required for related attribute export)
  • precision – maximum number of decimal places to use for geometry coordinates,

the RFC 7946 GeoJSON specification recommends limiting coordinate precision to 6

QgsJsonExporter(QgsJsonExporter)

Handles exporting QgsFeature features to GeoJSON features.

Note that geometries will be automatically reprojected to WGS84 to match GeoJSON spec if either the source vector layer or source CRS is set.

New in version 2.16: Methods

attributes Returns the list of attributes which will be included in the JSON exports, or an empty list if all attributes will be included.
excludedAttributes Returns a list of attributes which will be specifically excluded from the JSON exports.
exportFeature Returns a GeoJSON string representation of a feature.
exportFeatures Returns a GeoJSON string representation of a list of features (feature collection).
includeAttributes Returns whether attributes will be included in the JSON exports.
includeGeometry Returns whether geometry will be included in the JSON exports.
includeRelated Returns whether attributes of related (child) features will be included in the JSON exports.
precision Returns the maximum number of decimal places to use in geometry coordinates.
setAttributes Sets the list of attributes to include in the JSON exports.
setExcludedAttributes Sets a list of attributes to specifically exclude from the JSON exports.
setIncludeAttributes Sets whether to include attributes in the JSON exports.
setIncludeGeometry Sets whether to include geometry in the JSON exports.
setIncludeRelated Sets whether to include attributes of features linked via references in the JSON exports.
setPrecision Sets the maximum number of decimal places to use in geometry coordinates.
setSourceCrs Sets the source CRS for feature geometries.
setVectorLayer Sets the associated vector layer (required for related attribute export).
sourceCrs Returns the source CRS for feature geometries.
vectorLayer Returns the associated vector layer, if set.

Signals

Attributes

attributes(self) → List[int]

Returns the list of attributes which will be included in the JSON exports, or an empty list if all attributes will be included.

See also

setAttributes()

Note

Attributes excluded via excludedAttributes() take precedence over attributes returned by this method.

excludedAttributes(self) → List[int]

Returns a list of attributes which will be specifically excluded from the JSON exports. Excluded attributes take precedence over attributes included via attributes().

See also

attributes()

exportFeature(self, feature: QgsFeature, extraProperties: Dict[str, Any] = QVariantMap(), id: Any = None) → str

Returns a GeoJSON string representation of a feature.

Parameters:
  • feature – feature to convert
  • extraProperties – map of extra attributes to include in feature’s properties
  • id – optional ID to use as GeoJSON feature’s ID instead of input feature’s ID. If omitted, feature’s

ID is used.

Returns:GeoJSON string

See also

exportFeatures()

exportFeatures(self, features: object) → str

Returns a GeoJSON string representation of a list of features (feature collection).

Parameters:features – features to convert
Returns:GeoJSON string

See also

exportFeature()

includeAttributes(self) → bool

Returns whether attributes will be included in the JSON exports.

includeGeometry(self) → bool

Returns whether geometry will be included in the JSON exports.

includeRelated(self) → bool

Returns whether attributes of related (child) features will be included in the JSON exports.

precision(self) → int

Returns the maximum number of decimal places to use in geometry coordinates.

See also

setPrecision()

setAttributes(self, attributes: Iterable[int])

Sets the list of attributes to include in the JSON exports.

Parameters:attributes – list of attribute indexes, or an empty list to include all

attributes

See also

attributes()

Note

Attributes excluded via setExcludedAttributes() take precedence over attributes specified by this method.

setExcludedAttributes(self, attributes: Iterable[int])

Sets a list of attributes to specifically exclude from the JSON exports. Excluded attributes take precedence over attributes included via setAttributes().

Parameters:attributes – list of attribute indexes to exclude

See also

setAttributes()

setIncludeAttributes(self, includeAttributes: bool)

Sets whether to include attributes in the JSON exports.

Parameters:includeAttributes – set to false to prevent attribute inclusion
setIncludeGeometry(self, includeGeometry: bool)

Sets whether to include geometry in the JSON exports.

Parameters:includeGeometry – set to false to prevent geometry inclusion
setIncludeRelated(self, includeRelated: bool)

Sets whether to include attributes of features linked via references in the JSON exports.

Parameters:includeRelated – set to true to include attributes for any related child features

within the exported properties element.

Note

associated vector layer must be set with setVectorLayer()

See also

includeRelated()

setPrecision(self, precision: int)

Sets the maximum number of decimal places to use in geometry coordinates. The RFC 7946 GeoJSON specification recommends limiting coordinate precision to 6

Parameters:precision – number of decimal places

See also

precision()

setSourceCrs(self, crs: QgsCoordinateReferenceSystem)

Sets the source CRS for feature geometries. The source CRS must be set if geometries are to be correctly automatically reprojected to WGS 84, to match GeoJSON specifications.

Parameters:crs – source CRS for input feature geometries

Note

the source CRS will be overwritten when a vector layer is specified via setVectorLayer()

See also

sourceCrs()

setVectorLayer(self, vectorLayer: QgsVectorLayer)

Sets the associated vector layer (required for related attribute export). This will automatically update the sourceCrs() to match.

Parameters:vectorLayer – vector layer

See also

vectorLayer()

sourceCrs(self) → QgsCoordinateReferenceSystem

Returns the source CRS for feature geometries. The source CRS must be set if geometries are to be correctly automatically reprojected to WGS 84, to match GeoJSON specifications.

See also

setSourceCrs()

vectorLayer(self) → QgsVectorLayer

Returns the associated vector layer, if set.

See also

setVectorLayer()