QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 #include <QPointer>
26 #include <QJsonObject>
27 
28 class QTextCodec;
29 
40 class CORE_EXPORT QgsJsonExporter
41 {
42  public:
43 
50  QgsJsonExporter( QgsVectorLayer *vectorLayer = nullptr, int precision = 6 );
51 
58  void setPrecision( int precision ) { mPrecision = precision; }
59 
64  int precision() const { return mPrecision; }
65 
71  void setIncludeGeometry( bool includeGeometry ) { mIncludeGeometry = includeGeometry; }
72 
77  bool includeGeometry() const { return mIncludeGeometry; }
78 
84  void setIncludeAttributes( bool includeAttributes ) { mIncludeAttributes = includeAttributes; }
85 
90  bool includeAttributes() const { return mIncludeAttributes; }
91 
99  void setIncludeRelated( bool includeRelated ) { mIncludeRelatedAttributes = includeRelated; }
100 
105  bool includeRelated() const { return mIncludeRelatedAttributes; }
106 
112  void setAttributeDisplayName( bool displayName ) { mAttributeDisplayName = displayName; }
113 
119  bool attributeDisplayName() const { return mAttributeDisplayName; }
120 
127  void setVectorLayer( QgsVectorLayer *vectorLayer );
128 
133  QgsVectorLayer *vectorLayer() const;
134 
142  void setSourceCrs( const QgsCoordinateReferenceSystem &crs );
143 
149  QgsCoordinateReferenceSystem sourceCrs() const;
150 
156  void setTransformGeometries( bool activate ) { mTransformGeometries = activate; }
157 
167  void setAttributes( const QgsAttributeList &attributes ) { mAttributeIndexes = attributes; }
168 
177  QgsAttributeList attributes() const { return mAttributeIndexes; }
178 
186  void setExcludedAttributes( const QgsAttributeList &attributes ) { mExcludedAttributeIndexes = attributes; }
187 
194  QgsAttributeList excludedAttributes() const { return mExcludedAttributeIndexes; }
195 
207  QString exportFeature( const QgsFeature &feature,
208  const QVariantMap &extraProperties = QVariantMap(),
209  const QVariant &id = QVariant(),
210  int indent = -1 ) const;
211 
221  json exportFeatureToJsonObject( const QgsFeature &feature,
222  const QVariantMap &extraProperties = QVariantMap(),
223  const QVariant &id = QVariant() ) const SIP_SKIP;
224 
225 
233  QString exportFeatures( const QgsFeatureList &features, int indent = -1 ) const;
234 
242  json exportFeaturesToJsonObject( const QgsFeatureList &features ) const SIP_SKIP;
243 
244  private:
245 
247  int mPrecision;
248 
253  QgsAttributeList mAttributeIndexes;
254 
256  QgsAttributeList mExcludedAttributeIndexes;
257 
259  bool mIncludeGeometry = true;
260 
262  bool mIncludeAttributes = true;
263 
265  bool mIncludeRelatedAttributes = false;
266 
268  QPointer< QgsVectorLayer > mLayer;
269 
271 
272  QgsCoordinateTransform mTransform;
273 
274  bool mAttributeDisplayName = false;
275 
276  bool mTransformGeometries = true;
277 };
278 
286 class CORE_EXPORT QgsJsonUtils
287 {
288  Q_GADGET
289 
290  public:
291 
300  static QgsFeatureList stringToFeatureList( const QString &string, const QgsFields &fields = QgsFields(), QTextCodec *encoding = nullptr );
301 
309  static QgsFields stringToFields( const QString &string, QTextCodec *encoding = nullptr );
310 
317  Q_INVOKABLE static QString encodeValue( const QVariant &value );
318 
327  static QString exportAttributes( const QgsFeature &feature, QgsVectorLayer *layer = nullptr,
328  const QVector<QVariant> &attributeWidgetCaches = QVector<QVariant>() );
329 
340  static json exportAttributesToJsonObject( const QgsFeature &feature, QgsVectorLayer *layer = nullptr,
341  const QVector<QVariant> &attributeWidgetCaches = QVector<QVariant>() ) SIP_SKIP;
342 
351  Q_INVOKABLE static QVariantList parseArray( const QString &json, QVariant::Type type = QVariant::Invalid );
352 
353 
359  static json jsonFromVariant( const QVariant &v ) SIP_SKIP;
360 
366  static QVariant parseJson( const std::string &jsonString ) SIP_SKIP;
367 
373  static QVariant parseJson( const QString &jsonString ) SIP_SKIP;
374 
375 };
376 
377 #endif // QGSJSONUTILS_H
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Container of fields for a vector layer.
Definition: qgsfields.h:45
Handles exporting QgsFeature features to GeoJSON features.
Definition: qgsjsonutils.h:41
void setIncludeGeometry(bool includeGeometry)
Sets whether to include geometry in the JSON exports.
Definition: qgsjsonutils.h:71
void setIncludeRelated(bool includeRelated)
Sets whether to include attributes of features linked via references in the JSON exports.
Definition: qgsjsonutils.h:99
void setPrecision(int precision)
Sets the maximum number of decimal places to use in geometry coordinates.
Definition: qgsjsonutils.h:58
void setAttributes(const QgsAttributeList &attributes)
Sets the list of attributes to include in the JSON exports.
Definition: qgsjsonutils.h:167
bool attributeDisplayName() const
Returns whether original names of attributes or aliases are printed.
Definition: qgsjsonutils.h:119
QgsAttributeList excludedAttributes() const
Returns a list of attributes which will be specifically excluded from the JSON exports.
Definition: qgsjsonutils.h:194
void setIncludeAttributes(bool includeAttributes)
Sets whether to include attributes in the JSON exports.
Definition: qgsjsonutils.h:84
void setTransformGeometries(bool activate)
Sets whether geometries should be transformed in EPSG 4326 (default behavior) or just keep as it is.
Definition: qgsjsonutils.h:156
void setAttributeDisplayName(bool displayName)
Sets whether to print original names of attributes or aliases if defined.
Definition: qgsjsonutils.h:112
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:177
void setExcludedAttributes(const QgsAttributeList &attributes)
Sets a list of attributes to specifically exclude from the JSON exports.
Definition: qgsjsonutils.h:186
bool includeAttributes() const
Returns whether attributes will be included in the JSON exports.
Definition: qgsjsonutils.h:90
int precision() const
Returns the maximum number of decimal places to use in geometry coordinates.
Definition: qgsjsonutils.h:64
bool includeRelated() const
Returns whether attributes of related (child) features will be included in the JSON exports.
Definition: qgsjsonutils.h:105
bool includeGeometry() const
Returns whether geometry will be included in the JSON exports.
Definition: qgsjsonutils.h:77
Helper utilities for working with JSON and GeoJSON conversions.
Definition: qgsjsonutils.h:287
Represents a vector layer which manages a vector based data sets.
#define SIP_SKIP
Definition: qgis_sip.h:126
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QList< int > QgsAttributeList
Definition: qgsfield.h:26
const QgsCoordinateReferenceSystem & crs
int precision