QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
Loading...
Searching...
No Matches
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
28#ifndef SIP_RUN
29#include "json_fwd.hpp"
30using namespace nlohmann;
31#endif
32
33class QTextCodec;
34
44class CORE_EXPORT QgsJsonExporter
45{
46 public:
47
54 QgsJsonExporter( QgsVectorLayer *vectorLayer = nullptr, int precision = 6 );
55
62 void setPrecision( int precision ) { mPrecision = precision; }
63
68 int precision() const { return mPrecision; }
69
75 void setIncludeGeometry( bool includeGeometry ) { mIncludeGeometry = includeGeometry; }
76
81 bool includeGeometry() const { return mIncludeGeometry; }
82
88 void setIncludeAttributes( bool includeAttributes ) { mIncludeAttributes = includeAttributes; }
89
94 bool includeAttributes() const { return mIncludeAttributes; }
95
103 void setIncludeRelated( bool includeRelated ) { mIncludeRelatedAttributes = includeRelated; }
104
109 bool includeRelated() const { return mIncludeRelatedAttributes; }
110
116 void setAttributeDisplayName( bool displayName ) { mAttributeDisplayName = displayName; }
117
123 bool attributeDisplayName() const { return mAttributeDisplayName; }
124
131 void setVectorLayer( QgsVectorLayer *vectorLayer );
132
137 QgsVectorLayer *vectorLayer() const;
138
146 void setSourceCrs( const QgsCoordinateReferenceSystem &crs );
147
153 QgsCoordinateReferenceSystem sourceCrs() const;
154
160 void setTransformGeometries( bool activate ) { mTransformGeometries = activate; }
161
171 void setAttributes( const QgsAttributeList &attributes ) { mAttributeIndexes = attributes; }
172
181 QgsAttributeList attributes() const { return mAttributeIndexes; }
182
190 void setExcludedAttributes( const QgsAttributeList &attributes ) { mExcludedAttributeIndexes = attributes; }
191
198 QgsAttributeList excludedAttributes() const { return mExcludedAttributeIndexes; }
199
211 QString exportFeature( const QgsFeature &feature,
212 const QVariantMap &extraProperties = QVariantMap(),
213 const QVariant &id = QVariant(),
214 int indent = -1 ) const;
215
225 json exportFeatureToJsonObject( const QgsFeature &feature,
226 const QVariantMap &extraProperties = QVariantMap(),
227 const QVariant &id = QVariant() ) const SIP_SKIP;
228
229
237 QString exportFeatures( const QgsFeatureList &features, int indent = -1 ) const;
238
246 json exportFeaturesToJsonObject( const QgsFeatureList &features ) const SIP_SKIP;
247
257 void setDestinationCrs( const QgsCoordinateReferenceSystem &destinationCrs );
258
259 private:
260
262 int mPrecision;
263
268 QgsAttributeList mAttributeIndexes;
269
271 QgsAttributeList mExcludedAttributeIndexes;
272
274 bool mIncludeGeometry = true;
275
277 bool mIncludeAttributes = true;
278
280 bool mIncludeRelatedAttributes = false;
281
283 QPointer< QgsVectorLayer > mLayer;
284
286
287 QgsCoordinateTransform mTransform;
288
289 bool mAttributeDisplayName = false;
290
291 bool mTransformGeometries = true;
292
293 QgsCoordinateReferenceSystem mDestinationCrs;
294};
295
302class CORE_EXPORT QgsJsonUtils
303{
304 Q_GADGET
305
306 public:
307
316 static QgsFeatureList stringToFeatureList( const QString &string, const QgsFields &fields = QgsFields(), QTextCodec *encoding SIP_PYARGREMOVE6 = nullptr );
317
325 static QgsFields stringToFields( const QString &string, QTextCodec *encoding SIP_PYARGREMOVE6 = nullptr );
326
333 Q_INVOKABLE static QString encodeValue( const QVariant &value );
334
343 static QString exportAttributes( const QgsFeature &feature, QgsVectorLayer *layer = nullptr,
344 const QVector<QVariant> &attributeWidgetCaches = QVector<QVariant>() );
345
356 static json exportAttributesToJsonObject( const QgsFeature &feature, QgsVectorLayer *layer = nullptr,
357 const QVector<QVariant> &attributeWidgetCaches = QVector<QVariant>() ) SIP_SKIP;
358
366 Q_INVOKABLE static QVariantList parseArray( const QString &json, QVariant::Type type = QVariant::Invalid );
367
376 static QgsGeometry geometryFromGeoJson( const json &geometry ) SIP_SKIP;
377
385 static QgsGeometry geometryFromGeoJson( const QString &geometry );
386
392 static json jsonFromVariant( const QVariant &v ) SIP_SKIP;
393
401 static QVariant parseJson( const std::string &jsonString ) SIP_SKIP;
402
410 static QVariant parseJson( const std::string &jsonString, QString &error ) SIP_SKIP;
411
417 static QVariant parseJson( const QString &jsonString ) SIP_SKIP;
418
424 static QVariant jsonToVariant( const json &value ) SIP_SKIP;
425
426};
427
428#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
A geometry is the spatial representation of a feature.
Handles exporting QgsFeature features to GeoJSON features.
void setIncludeGeometry(bool includeGeometry)
Sets whether to include geometry in the JSON exports.
void setIncludeRelated(bool includeRelated)
Sets whether to include attributes of features linked via references in the JSON exports.
void setPrecision(int precision)
Sets the maximum number of decimal places to use in geometry coordinates.
void setAttributes(const QgsAttributeList &attributes)
Sets the list of attributes to include in the JSON exports.
bool attributeDisplayName() const
Returns whether original names of attributes or aliases are printed.
QgsAttributeList excludedAttributes() const
Returns a list of attributes which will be specifically excluded from the JSON exports.
void setIncludeAttributes(bool includeAttributes)
Sets whether to include attributes in the JSON exports.
void setTransformGeometries(bool activate)
Sets whether geometries should be transformed in EPSG 4326 (default behavior) or just keep as it is.
void setAttributeDisplayName(bool displayName)
Sets whether to print original names of attributes or aliases if defined.
QgsAttributeList attributes() const
Returns the list of attributes which will be included in the JSON exports, or an empty list if all at...
void setExcludedAttributes(const QgsAttributeList &attributes)
Sets a list of attributes to specifically exclude from the JSON exports.
bool includeAttributes() const
Returns whether attributes will be included in the JSON exports.
int precision() const
Returns the maximum number of decimal places to use in geometry coordinates.
bool includeRelated() const
Returns whether attributes of related (child) features will be included in the JSON exports.
bool includeGeometry() const
Returns whether geometry will be included in the JSON exports.
Helper utilities for working with JSON and GeoJSON conversions.
Represents a vector layer which manages a vector based data sets.
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_PYARGREMOVE6
Definition qgis_sip.h:156
QList< QgsFeature > QgsFeatureList
Definition qgsfeature.h:917
QList< int > QgsAttributeList
Definition qgsfield.h:27
const QgsCoordinateReferenceSystem & crs
int precision