Class: QgsJsonExporter

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

Bases: sip.wrapper

Constructor for QgsJsonExporter.

Parameters
  • vectorLayer (QgsVectorLayer = None) – associated vector layer (required for related attribute export)

  • precision (int = 6) – 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.

Methods

attributeDisplayName

Returns whether original names of attributes or aliases are printed.

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.

setAttributeDisplayName

Sets whether to print original names of attributes or aliases if defined.

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.

setTransformGeometries

Sets whether geometries should be transformed in EPSG 4326 (default behavior) or just keep as it is.

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.

attributeDisplayName(self) → bool

Returns whether original names of attributes or aliases are printed.

New in version 3.6.

Return type

bool

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.

Return type

List[int]

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()

Return type

List[int]

exportFeature(self, feature: QgsFeature, extraProperties: Dict[str, Any] = {}, id: Any = None, indent: int = - 1) → str

Returns a GeoJSON string representation of a feature.

Parameters
  • feature (QgsFeature) – feature to convert

  • extraProperties (Dict[str) – map of extra attributes to include in feature’s properties

  • id (Any = None) – optional ID to use as GeoJSON feature’s ID instead of input feature’s ID. If omitted, feature’s ID is used.

  • indent (int = -1) – number of indentation spaces for generated JSON (defaults to none)

Return type

str

Returns

GeoJSON string

See also

exportFeatures()

See also

exportFeatureToJsonObject()

exportFeatures(self, features: Iterable[QgsFeature], indent: int = - 1) → str

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

Parameters
  • features (Iterable[QgsFeature]) – features to convert

  • indent (int = -1) – number of indentation spaces for generated JSON (defaults to none)

Return type

str

Returns

GeoJSON string

See also

exportFeature()

includeAttributes(self) → bool

Returns whether attributes will be included in the JSON exports.

Return type

bool

includeGeometry(self) → bool

Returns whether geometry will be included in the JSON exports.

Return type

bool

includeRelated(self) → bool

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

Return type

bool

precision(self) → int

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

See also

setPrecision()

Return type

int

setAttributeDisplayName(self, displayName: bool)

Sets whether to print original names of attributes or aliases if defined.

New in version 3.6.

Parameters

displayName (bool) –

setAttributes(self, attributes: Iterable[int])

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

Parameters

attributes (Iterable[int]) – 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 (Iterable[int]) – list of attribute indexes to exclude

See also

setAttributes()

setIncludeAttributes(self, includeAttributes: bool)

Sets whether to include attributes in the JSON exports.

Parameters

includeAttributes (bool) – set to False to prevent attribute inclusion

setIncludeGeometry(self, includeGeometry: bool)

Sets whether to include geometry in the JSON exports.

Parameters

includeGeometry (bool) – 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 (bool) – 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 (int) – 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 (QgsCoordinateReferenceSystem) – source CRS for input feature geometries

Note

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

See also

sourceCrs()

setTransformGeometries(self, activate: bool)

Sets whether geometries should be transformed in EPSG 4326 (default behavior) or just keep as it is.

New in version 3.10.

Parameters

activate (bool) –

setVectorLayer(self, vectorLayer: QgsVectorLayer)

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

Parameters

vectorLayer (QgsVectorLayer) – 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()

Return type

QgsCoordinateReferenceSystem

vectorLayer(self)QgsVectorLayer

Returns the associated vector layer, if set.

See also

setVectorLayer()

Return type

QgsVectorLayer