QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 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 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 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.

Since
QGIS 2.16

Definition at line 45 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 34 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 124 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 175 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 192 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 72 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 226 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
QJsonObject
See also
exportFeatures()

Definition at line 78 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 95 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 82 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 110 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 69 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 117 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 165 of file qgsjsonutils.h.

◆ 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 184 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 89 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 76 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 104 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 63 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 61 of file qgsjsonutils.cpp.

◆ 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 46 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 67 of file qgsjsonutils.cpp.

◆ vectorLayer()

QgsVectorLayer * QgsJsonExporter::vectorLayer ( ) const

Returns the associated vector layer, if set.

See also
setVectorLayer()

Definition at line 56 of file qgsjsonutils.cpp.


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