QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
Public Member Functions | List of all members
QgsJsonExporter Class Reference

Handles exporting QgsFeature features to GeoJSON features. More...

#include <qgsjsonutils.h>

Public Member Functions

 QgsJsonExporter (QgsVectorLayer *vectorLayer=nullptr, int precision=6)
 Constructor for QgsJsonExporter.
 
bool attributeDisplayName () const
 Returns whether original names of attributes or aliases are printed.
 
QgsAttributeList 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 excludedAttributes () const
 Returns a list of attributes which will be specifically excluded from the JSON exports.
 
QString exportFeature (const QgsFeature &feature, const QVariantMap &extraProperties=QVariantMap(), const QVariant &id=QVariant(), int indent=-1) const
 Returns a GeoJSON string representation of a feature.
 
QString exportFeatures (const QgsFeatureList &features, int indent=-1) const
 Returns a GeoJSON string representation of a list of features (feature collection).
 
json exportFeaturesToJsonObject (const QgsFeatureList &features) const
 Returns a JSON object representation of a list of features (feature collection).
 
json exportFeatureToJsonObject (const QgsFeature &feature, const QVariantMap &extraProperties=QVariantMap(), const QVariant &id=QVariant()) const
 Returns a QJsonObject representation of a feature.
 
bool includeAttributes () const
 Returns whether attributes will be included in the JSON exports.
 
bool includeGeometry () const
 Returns whether geometry will be included in the JSON exports.
 
bool includeRelated () const
 Returns whether attributes of related (child) features will be included in the JSON exports.
 
int precision () const
 Returns the maximum number of decimal places to use in geometry coordinates.
 
void setAttributeDisplayName (bool displayName)
 Sets whether to print original names of attributes or aliases if defined.
 
void setAttributes (const QgsAttributeList &attributes)
 Sets the list of attributes to include in the JSON exports.
 
void setDestinationCrs (const QgsCoordinateReferenceSystem &destinationCrs)
 Set the destination CRS for feature geometry transformation to destinationCrs, this defaults to EPSG:4326 and it is only effective when the automatic geometry transformation is active (it is by default).
 
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 setTransformGeometries (bool activate)
 Sets whether geometries should be transformed in EPSG 4326 (default behavior) or just keep as it is.
 
void setUseFieldFormatters (bool useFieldFormatters)
 Sets whether field formatters (of type KeyValue, List, ValueRelation, ValueMap) are used to export raw values as displayed values.
 
void setVectorLayer (QgsVectorLayer *vectorLayer)
 Sets the associated vector layer (required for related attribute export).
 
QgsCoordinateReferenceSystem sourceCrs () const
 Returns the source CRS for feature geometries.
 
bool useFieldFormatters () const
 Returned whether field formatters (of type KeyValue, List, ValueRelation, ValueMap) are used to export raw values as displayed values.
 
QgsVectorLayervectorLayer () const
 Returns the associated vector layer, if set.
 

Detailed Description

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.

Definition at line 44 of file qgsjsonutils.h.

Constructor & Destructor Documentation

◆ QgsJsonExporter()

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

Constructor for QgsJsonExporter.

Parameters
vectorLayerassociated vector layer (required for related attribute export)
precisionmaximum number of decimal places to use for geometry coordinates, the RFC 7946 GeoJSON specification recommends limiting coordinate precision to 6

Definition at line 42 of file qgsjsonutils.cpp.

Member Function Documentation

◆ attributeDisplayName()

bool QgsJsonExporter::attributeDisplayName ( ) const
inline

Returns whether original names of attributes or aliases are printed.

Since
QGIS 3.6

Definition at line 123 of file qgsjsonutils.h.

◆ attributes()

QgsAttributeList QgsJsonExporter::attributes ( ) const
inline

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()
excludedAttributes()
Note
Attributes excluded via excludedAttributes() take precedence over attributes returned by this method.

Definition at line 181 of file qgsjsonutils.h.

◆ excludedAttributes()

QgsAttributeList QgsJsonExporter::excludedAttributes ( ) const
inline

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

Definition at line 198 of file qgsjsonutils.h.

◆ exportFeature()

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

Returns a GeoJSON string representation of a feature.

Parameters
featurefeature to convert
extraPropertiesmap of extra attributes to include in feature's properties
idoptional ID to use as GeoJSON feature's ID instead of input feature's ID. If omitted, feature's ID is used.
indentnumber of indentation spaces for generated JSON (defaults to none)
Returns
GeoJSON string
See also
exportFeatures()
exportFeatureToJsonObject()

Definition at line 83 of file qgsjsonutils.cpp.

◆ exportFeatures()

QString QgsJsonExporter::exportFeatures ( const QgsFeatureList features,
int  indent = -1 
) const

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

Parameters
featuresfeatures to convert
indentnumber of indentation spaces for generated JSON (defaults to none)
Returns
GeoJSON string
See also
exportFeature()

Definition at line 237 of file qgsjsonutils.cpp.

◆ exportFeaturesToJsonObject()

json QgsJsonExporter::exportFeaturesToJsonObject ( const QgsFeatureList features) const

Returns a JSON object representation of a list of features (feature collection).

Parameters
featuresfeatures to convert
Returns
json object
See also
exportFeatures()
Since
QGIS 3.10

Definition at line 242 of file qgsjsonutils.cpp.

◆ exportFeatureToJsonObject()

json QgsJsonExporter::exportFeatureToJsonObject ( const QgsFeature feature,
const QVariantMap &  extraProperties = QVariantMap(),
const QVariant &  id = QVariant() 
) const

Returns a QJsonObject representation of a feature.

Parameters
featurefeature to convert
extraPropertiesmap of extra attributes to include in feature's properties
idoptional ID to use as GeoJSON feature's ID instead of input feature's ID. If omitted, feature's ID is used.
Returns
json object
See also
exportFeatures()

Definition at line 89 of file qgsjsonutils.cpp.

◆ includeAttributes()

bool QgsJsonExporter::includeAttributes ( ) const
inline

Returns whether attributes will be included in the JSON exports.

See also
setIncludeAttributes()

Definition at line 94 of file qgsjsonutils.h.

◆ includeGeometry()

bool QgsJsonExporter::includeGeometry ( ) const
inline

Returns whether geometry will be included in the JSON exports.

See also
setIncludeGeometry()

Definition at line 81 of file qgsjsonutils.h.

◆ includeRelated()

bool QgsJsonExporter::includeRelated ( ) const
inline

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

See also
setIncludeRelated()

Definition at line 109 of file qgsjsonutils.h.

◆ precision()

int QgsJsonExporter::precision ( ) const
inline

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

See also
setPrecision()

Definition at line 68 of file qgsjsonutils.h.

◆ setAttributeDisplayName()

void QgsJsonExporter::setAttributeDisplayName ( bool  displayName)
inline

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

Since
QGIS 3.6

Definition at line 116 of file qgsjsonutils.h.

◆ setAttributes()

void QgsJsonExporter::setAttributes ( const QgsAttributeList attributes)
inline

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

Parameters
attributeslist of attribute indexes, or an empty list to include all attributes
See also
attributes()
setExcludedAttributes()
Note
Attributes excluded via setExcludedAttributes() take precedence over attributes specified by this method.

Definition at line 171 of file qgsjsonutils.h.

◆ setDestinationCrs()

void QgsJsonExporter::setDestinationCrs ( const QgsCoordinateReferenceSystem destinationCrs)

Set the destination CRS for feature geometry transformation to destinationCrs, this defaults to EPSG:4326 and it is only effective when the automatic geometry transformation is active (it is by default).

See also
setTransformGeometries()
setSourceCrs()
Since
QGIS 3.30

Definition at line 259 of file qgsjsonutils.cpp.

◆ setExcludedAttributes()

void QgsJsonExporter::setExcludedAttributes ( const QgsAttributeList attributes)
inline

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

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

Parameters
attributeslist of attribute indexes to exclude
See also
excludedAttributes()
setAttributes()

Definition at line 190 of file qgsjsonutils.h.

◆ setIncludeAttributes()

void QgsJsonExporter::setIncludeAttributes ( bool  includeAttributes)
inline

Sets whether to include attributes in the JSON exports.

Parameters
includeAttributesset to false to prevent attribute inclusion
See also
includeAttributes()

Definition at line 88 of file qgsjsonutils.h.

◆ setIncludeGeometry()

void QgsJsonExporter::setIncludeGeometry ( bool  includeGeometry)
inline

Sets whether to include geometry in the JSON exports.

Parameters
includeGeometryset to false to prevent geometry inclusion
See also
includeGeometry()

Definition at line 75 of file qgsjsonutils.h.

◆ setIncludeRelated()

void QgsJsonExporter::setIncludeRelated ( bool  includeRelated)
inline

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

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

Definition at line 103 of file qgsjsonutils.h.

◆ setPrecision()

void QgsJsonExporter::setPrecision ( int  precision)
inline

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

The RFC 7946 GeoJSON specification recommends limiting coordinate precision to 6

Parameters
precisionnumber of decimal places
See also
precision()

Definition at line 62 of file qgsjsonutils.h.

◆ setSourceCrs()

void QgsJsonExporter::setSourceCrs ( const QgsCoordinateReferenceSystem crs)

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
crssource CRS for input feature geometries
Note
the source CRS will be overwritten when a vector layer is specified via setVectorLayer()
See also
sourceCrs()

Definition at line 72 of file qgsjsonutils.cpp.

◆ setTransformGeometries()

void QgsJsonExporter::setTransformGeometries ( bool  activate)
inline

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

Since
QGIS 3.12

Definition at line 160 of file qgsjsonutils.h.

◆ setUseFieldFormatters()

void QgsJsonExporter::setUseFieldFormatters ( bool  useFieldFormatters)
inline

Sets whether field formatters (of type KeyValue, List, ValueRelation, ValueMap) are used to export raw values as displayed values.

The default is true.

Since
QGIS 3.40

Definition at line 206 of file qgsjsonutils.h.

◆ setVectorLayer()

void QgsJsonExporter::setVectorLayer ( QgsVectorLayer vectorLayer)

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

This will automatically update the sourceCrs() to match.

Parameters
vectorLayervector layer
See also
vectorLayer()

Definition at line 57 of file qgsjsonutils.cpp.

◆ sourceCrs()

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.

See also
setSourceCrs()

Definition at line 78 of file qgsjsonutils.cpp.

◆ useFieldFormatters()

bool QgsJsonExporter::useFieldFormatters ( ) const
inline

Returned whether field formatters (of type KeyValue, List, ValueRelation, ValueMap) are used to export raw values as displayed values.

Since
QGIS 3.40

Definition at line 214 of file qgsjsonutils.h.

◆ vectorLayer()

QgsVectorLayer * QgsJsonExporter::vectorLayer ( ) const

Returns the associated vector layer, if set.

See also
setVectorLayer()

Definition at line 67 of file qgsjsonutils.cpp.


The documentation for this class was generated from the following files: