QgsJsonExporter class

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.

Constructors, destructors, conversion operators

QgsJsonExporter(QgsVectorLayer* vectorLayer = nullptr, int precision = 6)
Constructor for QgsJsonExporter.

Public functions

auto attributes() const -> QgsAttributeList
Returns the list of attributes which will be included in the JSON exports, or an empty list if all attributes will be included.
auto excludedAttributes() const -> QgsAttributeList
Returns a list of attributes which will be specifically excluded from the JSON exports.
auto exportFeature(const QgsFeature& feature, const QVariantMap& extraProperties = QVariantMap(), const QVariant& id = QVariant()) const -> QString
Returns a GeoJSON string representation of a feature.
auto exportFeatures(const QgsFeatureList& features) const -> QString
Returns a GeoJSON string representation of a list of features (feature collection).
auto includeAttributes() const -> bool
Returns whether attributes will be included in the JSON exports.
auto includeGeometry() const -> bool
Returns whether geometry will be included in the JSON exports.
auto includeRelated() const -> bool
Returns whether attributes of related (child) features will be included in the JSON exports.
auto precision() const -> int
Returns the maximum number of decimal places to use in geometry coordinates.
void setAttributes(const QgsAttributeList& attributes)
Sets the list of attributes to include in the JSON exports.
void setExcludedAttributes(const QgsAttributeList& attributes)
Sets a list of attributes to specifically exclude from the JSON exports.
void setIncludeAttributes(bool includeAttributes)
Sets whether to include attributes in the JSON exports.
void setIncludeGeometry(bool includeGeometry)
Sets whether to include geometry in the JSON exports.
void setIncludeRelated(bool includeRelated)
Sets whether to include attributes of features linked via references in the JSON exports.
void setPrecision(int precision)
Sets the maximum number of decimal places to use in geometry coordinates.
void setSourceCrs(const QgsCoordinateReferenceSystem& crs)
Sets the source CRS for feature geometries.
void setVectorLayer(QgsVectorLayer* vectorLayer)
Sets the associated vector layer (required for related attribute export).
auto sourceCrs() const -> QgsCoordinateReferenceSystem
Returns the source CRS for feature geometries.
auto vectorLayer() const -> QgsVectorLayer*
Returns the associated vector layer, if set.

Function documentation

QgsJsonExporter::QgsJsonExporter(QgsVectorLayer* vectorLayer = nullptr, int precision = 6)

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

QgsAttributeList QgsJsonExporter::attributes() const

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

QgsAttributeList QgsJsonExporter::excludedAttributes() const

Returns a list of attributes which will be specifically excluded from the JSON exports.

Excluded attributes take precedence over attributes included via attributes().

QString QgsJsonExporter::exportFeature(const QgsFeature& feature, const QVariantMap& extraProperties = QVariantMap(), const QVariant& id = QVariant()) const

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

QString QgsJsonExporter::exportFeatures(const QgsFeatureList& features) const

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

Parameters
features features to convert
Returns GeoJSON string

bool QgsJsonExporter::includeAttributes() const

Returns whether attributes will be included in the JSON exports.

bool QgsJsonExporter::includeGeometry() const

Returns whether geometry will be included in the JSON exports.

bool QgsJsonExporter::includeRelated() const

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

int QgsJsonExporter::precision() const

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

void QgsJsonExporter::setAttributes(const QgsAttributeList& attributes)

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

void QgsJsonExporter::setExcludedAttributes(const QgsAttributeList& attributes)

Sets a list of attributes to specifically exclude from the JSON exports.

Parameters
attributes list of attribute indexes to exclude

Excluded attributes take precedence over attributes included via setAttributes().

void QgsJsonExporter::setIncludeAttributes(bool includeAttributes)

Sets whether to include attributes in the JSON exports.

Parameters
includeAttributes set to false to prevent attribute inclusion

void QgsJsonExporter::setIncludeGeometry(bool includeGeometry)

Sets whether to include geometry in the JSON exports.

Parameters
includeGeometry set to false to prevent geometry inclusion

void QgsJsonExporter::setIncludeRelated(bool includeRelated)

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.

void QgsJsonExporter::setPrecision(int precision)

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

Parameters
precision number of decimal places

The RFC 7946 GeoJSON specification recommends limiting coordinate precision to 6

void QgsJsonExporter::setSourceCrs(const QgsCoordinateReferenceSystem& crs)

Sets the source CRS for feature geometries.

Parameters
crs source CRS for input feature geometries

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

void QgsJsonExporter::setVectorLayer(QgsVectorLayer* vectorLayer)

Sets the associated vector layer (required for related attribute export).

Parameters
vectorLayer vector layer

This will automatically update the sourceCrs() to match.

QgsCoordinateReferenceSystem QgsJsonExporter::sourceCrs() const

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.

QgsVectorLayer* QgsJsonExporter::vectorLayer() const

Returns the associated vector layer, if set.