QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 (const QgsVectorLayer *vectorLayer=nullptr, int precision=6)
 Constructor for QgsJSONExporter. 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()) const
 Returns a GeoJSON string representation of a feature. More...
 
QString exportFeatures (const QgsFeatureList &features) const
 Returns a GeoJSON string representation of a list of features (feature collection). 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 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 (const QgsVectorLayer *vectorLayer)
 Sets the associated vector layer (required for related attribute export). More...
 
const QgsCoordinateReferenceSystemsourceCrs () 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.

Note
Added in version 2.16

Definition at line 35 of file qgsjsonutils.h.

Constructor & Destructor Documentation

◆ QgsJSONExporter()

QgsJSONExporter::QgsJSONExporter ( const 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 25 of file qgsjsonutils.cpp.

Member Function Documentation

◆ 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 136 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 151 of file qgsjsonutils.h.

◆ exportFeature()

QString QgsJSONExporter::exportFeature ( const QgsFeature feature,
const QVariantMap &  extraProperties = QVariantMap(),
const QVariant id = QVariant() 
) 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.
Returns
GeoJSON string
See also
exportFeatures()

Definition at line 66 of file qgsjsonutils.cpp.

◆ exportFeatures()

QString QgsJSONExporter::exportFeatures ( const QgsFeatureList features) const

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

Parameters
featuresfeatures to convert
Returns
GeoJSON string
See also
exportFeature()

Definition at line 209 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 78 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 67 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 91 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 56 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 127 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 144 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 73 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 62 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 86 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 51 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 55 of file qgsjsonutils.cpp.

◆ setVectorLayer()

void QgsJSONExporter::setVectorLayer ( const 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 40 of file qgsjsonutils.cpp.

◆ sourceCrs()

const 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 61 of file qgsjsonutils.cpp.

◆ vectorLayer()

QgsVectorLayer * QgsJSONExporter::vectorLayer ( ) const

Returns the associated vector layer, if set.

See also
setVectorLayer()

Definition at line 50 of file qgsjsonutils.cpp.


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