QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsjsonutils.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsjsonutils.h
3  -------------
4  Date : May 206
5  Copyright : (C) 2016 Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSJSONUTILS_H
17 #define QGSJSONUTILS_H
18 
19 #include "qgis_core.h"
20 #include "qgsfeature.h"
22 #include "qgscoordinatetransform.h"
23 #include "qgsfields.h"
24 
25 #ifndef SIP_RUN
26 #include <json_fwd.hpp>
27 using namespace nlohmann;
28 #endif
29 
30 #include <QPointer>
31 #include <QJsonObject>
32 
33 class QTextCodec;
34 
45 class CORE_EXPORT QgsJsonExporter
46 {
47  public:
48 
55  QgsJsonExporter( QgsVectorLayer *vectorLayer = nullptr, int precision = 6 );
56 
63  void setPrecision( int precision ) { mPrecision = precision; }
64 
69  int precision() const { return mPrecision; }
70 
76  void setIncludeGeometry( bool includeGeometry ) { mIncludeGeometry = includeGeometry; }
77 
82  bool includeGeometry() const { return mIncludeGeometry; }
83 
89  void setIncludeAttributes( bool includeAttributes ) { mIncludeAttributes = includeAttributes; }
90 
95  bool includeAttributes() const { return mIncludeAttributes; }
96 
104  void setIncludeRelated( bool includeRelated ) { mIncludeRelatedAttributes = includeRelated; }
105 
110  bool includeRelated() const { return mIncludeRelatedAttributes; }
111 
117  void setAttributeDisplayName( bool displayName ) { mAttributeDisplayName = displayName; }
118 
124  bool attributeDisplayName() const { return mAttributeDisplayName; }
125 
132  void setVectorLayer( QgsVectorLayer *vectorLayer );
133 
138  QgsVectorLayer *vectorLayer() const;
139 
147  void setSourceCrs( const QgsCoordinateReferenceSystem &crs );
148 
154  QgsCoordinateReferenceSystem sourceCrs() const;
155 
161  void setTransformGeometries( bool activate ) { mTransformGeometries = activate; }
162 
172  void setAttributes( const QgsAttributeList &attributes ) { mAttributeIndexes = attributes; }
173 
182  QgsAttributeList attributes() const { return mAttributeIndexes; }
183 
191  void setExcludedAttributes( const QgsAttributeList &attributes ) { mExcludedAttributeIndexes = attributes; }
192 
199  QgsAttributeList excludedAttributes() const { return mExcludedAttributeIndexes; }
200 
212  QString exportFeature( const QgsFeature &feature,
213  const QVariantMap &extraProperties = QVariantMap(),
214  const QVariant &id = QVariant(),
215  int indent = -1 ) const;
216 
226  json exportFeatureToJsonObject( const QgsFeature &feature,
227  const QVariantMap &extraProperties = QVariantMap(),
228  const QVariant &id = QVariant() ) const SIP_SKIP;
229 
230 
238  QString exportFeatures( const QgsFeatureList &features, int indent = -1 ) const;
239 
247  json exportFeaturesToJsonObject( const QgsFeatureList &features ) const SIP_SKIP;
248 
249  private:
250 
252  int mPrecision;
253 
258  QgsAttributeList mAttributeIndexes;
259 
261  QgsAttributeList mExcludedAttributeIndexes;
262 
264  bool mIncludeGeometry = true;
265 
267  bool mIncludeAttributes = true;
268 
270  bool mIncludeRelatedAttributes = false;
271 
273  QPointer< QgsVectorLayer > mLayer;
274 
276 
277  QgsCoordinateTransform mTransform;
278 
279  bool mAttributeDisplayName = false;
280 
281  bool mTransformGeometries = true;
282 };
283 
291 class CORE_EXPORT QgsJsonUtils
292 {
293  public:
294 
303  static QgsFeatureList stringToFeatureList( const QString &string, const QgsFields &fields = QgsFields(), QTextCodec *encoding = nullptr );
304 
312  static QgsFields stringToFields( const QString &string, QTextCodec *encoding = nullptr );
313 
320  static QString encodeValue( const QVariant &value );
321 
330  static QString exportAttributes( const QgsFeature &feature, QgsVectorLayer *layer = nullptr,
331  const QVector<QVariant> &attributeWidgetCaches = QVector<QVariant>() );
332 
343  static json exportAttributesToJsonObject( const QgsFeature &feature, QgsVectorLayer *layer = nullptr,
344  const QVector<QVariant> &attributeWidgetCaches = QVector<QVariant>() ) SIP_SKIP;
345 
354  static QVariantList parseArray( const QString &json, QVariant::Type type = QVariant::Invalid );
355 
356 
362  static json jsonFromVariant( const QVariant &v ) SIP_SKIP;
363 
369  static QVariant parseJson( const std::string &jsonString ) SIP_SKIP;
370 
376  static QVariant parseJson( const QString &jsonString ) SIP_SKIP;
377 
378 };
379 
380 #endif // QGSJSONUTILS_H
qgsfields.h
QgsJsonExporter::includeAttributes
bool includeAttributes() const
Returns whether attributes will be included in the JSON exports.
Definition: qgsjsonutils.h:95
QgsJsonExporter::includeRelated
bool includeRelated() const
Returns whether attributes of related (child) features will be included in the JSON exports.
Definition: qgsjsonutils.h:110
QgsJsonUtils
Helper utilities for working with JSON and GeoJSON conversions.
Definition: qgsjsonutils.h:292
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:51
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:45
QgsJsonExporter::setIncludeGeometry
void setIncludeGeometry(bool includeGeometry)
Sets whether to include geometry in the JSON exports.
Definition: qgsjsonutils.h:76
qgsfeature.h
QgsJsonExporter::precision
int precision() const
Returns the maximum number of decimal places to use in geometry coordinates.
Definition: qgsjsonutils.h:69
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
QgsJsonExporter::setExcludedAttributes
void setExcludedAttributes(const QgsAttributeList &attributes)
Sets a list of attributes to specifically exclude from the JSON exports.
Definition: qgsjsonutils.h:191
QgsJsonExporter::setTransformGeometries
void setTransformGeometries(bool activate)
Sets whether geometries should be transformed in EPSG 4326 (default behavior) or just keep as it is.
Definition: qgsjsonutils.h:161
precision
int precision
Definition: qgswfsgetfeature.cpp:49
QgsJsonExporter::setIncludeAttributes
void setIncludeAttributes(bool includeAttributes)
Sets whether to include attributes in the JSON exports.
Definition: qgsjsonutils.h:89
QgsJsonExporter::setAttributeDisplayName
void setAttributeDisplayName(bool displayName)
Sets whether to print original names of attributes or aliases if defined.
Definition: qgsjsonutils.h:117
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsJsonExporter::includeGeometry
bool includeGeometry() const
Returns whether geometry will be included in the JSON exports.
Definition: qgsjsonutils.h:82
QgsJsonExporter::setPrecision
void setPrecision(int precision)
Sets the maximum number of decimal places to use in geometry coordinates.
Definition: qgsjsonutils.h:63
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:583
qgscoordinatetransform.h
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:206
QgsJsonExporter
Handles exporting QgsFeature features to GeoJSON features.
Definition: qgsjsonutils.h:46
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsJsonExporter::excludedAttributes
QgsAttributeList excludedAttributes() const
Returns a list of attributes which will be specifically excluded from the JSON exports.
Definition: qgsjsonutils.h:199
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsJsonExporter::attributes
QgsAttributeList attributes() const
Returns the list of attributes which will be included in the JSON exports, or an empty list if all at...
Definition: qgsjsonutils.h:182
QgsJsonExporter::attributeDisplayName
bool attributeDisplayName() const
Returns whether original names of attributes or aliases are printed.
Definition: qgsjsonutils.h:124
QgsJsonExporter::setIncludeRelated
void setIncludeRelated(bool includeRelated)
Sets whether to include attributes of features linked via references in the JSON exports.
Definition: qgsjsonutils.h:104
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:53
qgscoordinatereferencesystem.h
QgsJsonExporter::setAttributes
void setAttributes(const QgsAttributeList &attributes)
Sets the list of attributes to include in the JSON exports.
Definition: qgsjsonutils.h:172