QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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"
23#include "qgsfields.h"
24
25#include <QPointer>
26#include <QJsonObject>
27
28class QTextCodec;
29
40class 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
253 void setDestinationCrs( const QgsCoordinateReferenceSystem &destinationCrs );
254
255 private:
256
258 int mPrecision;
259
264 QgsAttributeList mAttributeIndexes;
265
267 QgsAttributeList mExcludedAttributeIndexes;
268
270 bool mIncludeGeometry = true;
271
273 bool mIncludeAttributes = true;
274
276 bool mIncludeRelatedAttributes = false;
277
279 QPointer< QgsVectorLayer > mLayer;
280
282
283 QgsCoordinateTransform mTransform;
284
285 bool mAttributeDisplayName = false;
286
287 bool mTransformGeometries = true;
288
289 QgsCoordinateReferenceSystem mDestinationCrs;
290};
291
299class CORE_EXPORT QgsJsonUtils
300{
301 Q_GADGET
302
303 public:
304
313 static QgsFeatureList stringToFeatureList( const QString &string, const QgsFields &fields = QgsFields(), QTextCodec *encoding = nullptr );
314
322 static QgsFields stringToFields( const QString &string, QTextCodec *encoding = nullptr );
323
330 Q_INVOKABLE static QString encodeValue( const QVariant &value );
331
340 static QString exportAttributes( const QgsFeature &feature, QgsVectorLayer *layer = nullptr,
341 const QVector<QVariant> &attributeWidgetCaches = QVector<QVariant>() );
342
353 static json exportAttributesToJsonObject( const QgsFeature &feature, QgsVectorLayer *layer = nullptr,
354 const QVector<QVariant> &attributeWidgetCaches = QVector<QVariant>() ) SIP_SKIP;
355
364 Q_INVOKABLE static QVariantList parseArray( const QString &json, QVariant::Type type = QVariant::Invalid );
365
366
372 static json jsonFromVariant( const QVariant &v ) SIP_SKIP;
373
381 static QVariant parseJson( const std::string &jsonString ) SIP_SKIP;
382
390 static QVariant parseJson( const std::string &jsonString, QString &error ) SIP_SKIP;
391
397 static QVariant parseJson( const QString &jsonString ) SIP_SKIP;
398
399};
400
401#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:300
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:920
QList< int > QgsAttributeList
Definition: qgsfield.h:26
const QgsCoordinateReferenceSystem & crs
int precision