QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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. More...
 
bool attributeDisplayName () const
 Returns whether original names of attributes or aliases are printed. More...
 
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. More...
 
QgsAttributeList excludedAttributes () const
 Returns a list of attributes which will be specifically excluded from the JSON exports. More...
 
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. More...
 
QString exportFeatures (const QgsFeatureList &features, int indent=-1) const
 Returns a GeoJSON string representation of a list of features (feature collection). More...
 
json exportFeaturesToJsonObject (const QgsFeatureList &features) const
 Returns a JSON object representation of a list of features (feature collection). More...
 
json exportFeatureToJsonObject (const QgsFeature &feature, const QVariantMap &extraProperties=QVariantMap(), const QVariant &id=QVariant()) const
 Returns a QJsonObject representation of a feature. More...
 
bool includeAttributes () const
 Returns whether attributes will be included in the JSON exports. More...
 
bool includeGeometry () const
 Returns whether geometry will be included in the JSON exports. More...
 
bool includeRelated () const
 Returns whether attributes of related (child) features will be included in the JSON exports. More...
 
int precision () const
 Returns the maximum number of decimal places to use in geometry coordinates. More...
 
void setAttributeDisplayName (bool displayName)
 Sets whether to print original names of attributes or aliases if defined. More...
 
void setAttributes (const QgsAttributeList &attributes)
 Sets the list of attributes to include in the JSON exports. More...
 
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). More...
 
void setExcludedAttributes (const QgsAttributeList &attributes)
 Sets a list of attributes to specifically exclude from the JSON exports. More...
 
void setIncludeAttributes (bool includeAttributes)
 Sets whether to include attributes in the JSON exports. More...
 
void setIncludeGeometry (bool includeGeometry)
 Sets whether to include geometry in the JSON exports. More...
 
void setIncludeRelated (bool includeRelated)
 Sets whether to include attributes of features linked via references in the JSON exports. More...
 
void setPrecision (int precision)
 Sets the maximum number of decimal places to use in geometry coordinates. More...
 
void setSourceCrs (const QgsCoordinateReferenceSystem &crs)
 Sets the source CRS for feature geometries. More...
 
void setTransformGeometries (bool activate)
 Sets whether geometries should be transformed in EPSG 4326 (default behavior) or just keep as it is. More...
 
void setVectorLayer (QgsVectorLayer *vectorLayer)
 Sets the associated vector layer (required for related attribute export). More...
 
QgsCoordinateReferenceSystem sourceCrs () const
 Returns the source CRS for feature geometries. More...
 
QgsVectorLayervectorLayer () const
 Returns the associated vector layer, if set. More...
 

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 41 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 82 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 236 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 241 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 88 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 255 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 71 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.

◆ 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 56 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 77 of file qgsjsonutils.cpp.

◆ vectorLayer()

QgsVectorLayer * QgsJsonExporter::vectorLayer ( ) const

Returns the associated vector layer, if set.

See also
setVectorLayer()

Definition at line 66 of file qgsjsonutils.cpp.


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