QGIS API Documentation 4.3.0-Master (bf28115e945)
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"
22#include "qgsfeature.h"
23#include "qgsfields.h"
24
25#include <QJsonObject>
26#include <QPointer>
27
28#ifndef SIP_RUN
29#include <nlohmann/json_fwd.hpp>
30using namespace nlohmann;
31#endif
32
33class QTextCodec;
34
43
44class CORE_EXPORT QgsJsonExporter
45{
46 public:
54
61 void setPrecision( int precision ) { mPrecision = precision; }
62
67 int precision() const { return mPrecision; }
68
74 void setIncludeGeometry( bool includeGeometry ) { mIncludeGeometry = includeGeometry; }
75
80 bool includeGeometry() const { return mIncludeGeometry; }
81
87 void setIncludeAttributes( bool includeAttributes ) { mIncludeAttributes = includeAttributes; }
88
93 bool includeAttributes() const { return mIncludeAttributes; }
94
102 void setIncludeRelated( bool includeRelated ) { mIncludeRelatedAttributes = includeRelated; }
103
108 bool includeRelated() const { return mIncludeRelatedAttributes; }
109
115 void setAttributeDisplayName( bool displayName ) { mAttributeDisplayName = displayName; }
116
121
122 bool attributeDisplayName() const { return mAttributeDisplayName; }
123
130 void setVectorLayer( QgsVectorLayer *vectorLayer );
131
136 QgsVectorLayer *vectorLayer() const;
137
145 void setSourceCrs( const QgsCoordinateReferenceSystem &crs );
146
152 QgsCoordinateReferenceSystem sourceCrs() const;
153
159 void setTransformGeometries( bool activate ) { mTransformGeometries = activate; }
160
170 void setAttributes( const QgsAttributeList &attributes ) { mAttributeIndexes = attributes; }
171
180 QgsAttributeList attributes() const { return mAttributeIndexes; }
181
189 void setExcludedAttributes( const QgsAttributeList &attributes ) { mExcludedAttributeIndexes = attributes; }
190
197 QgsAttributeList excludedAttributes() const { return mExcludedAttributeIndexes; }
198
205 void setUseFieldFormatters( bool useFieldFormatters ) { mUseFieldFormatters = useFieldFormatters; }
206
213 bool useFieldFormatters() const { return mUseFieldFormatters; }
214
227 QString exportFeature(
228 const QgsFeature &feature, const QVariantMap &extraProperties = QVariantMap(), const QVariant &id = QVariant(), int indent = -1, const QVariantMap &extraMembers = QVariantMap()
229 ) const;
230
241 json exportFeatureToJsonObject( const QgsFeature &feature, const QVariantMap &extraProperties = QVariantMap(), const QVariant &id = QVariant(), const QVariantMap &extraMembers = QVariantMap() ) const
242 SIP_SKIP;
243
251 QString exportFeatures( const QgsFeatureList &features, int indent = -1 ) const;
252
260 json exportFeaturesToJsonObject( const QgsFeatureList &features ) const SIP_SKIP;
261
271 void setDestinationCrs( const QgsCoordinateReferenceSystem &destinationCrs );
272
277 Qgis::GeoJsonProfile geoJsonProfile() const;
278
283 void setGeoJsonProfile( Qgis::GeoJsonProfile profile );
284
285 private:
287 int mPrecision;
288
293 QgsAttributeList mAttributeIndexes;
294
296 QgsAttributeList mExcludedAttributeIndexes;
297
299 bool mIncludeGeometry = true;
300
302 bool mIncludeAttributes = true;
303
305 bool mIncludeRelatedAttributes = false;
306
308 QPointer< QgsVectorLayer > mLayer;
309
311
312 QgsCoordinateTransform mTransform;
313
314 bool mAttributeDisplayName = false;
315
316 bool mTransformGeometries = true;
317
318 QgsCoordinateReferenceSystem mDestinationCrs;
319
320 bool mUseFieldFormatters = true;
321
323 mutable bool mOmitCollectionLevelInformation = false;
324
325 Qgis::GeoJsonProfile mGeoJsonProfile = Qgis::GeoJsonProfile::Legacy;
326};
327
333
334class CORE_EXPORT QgsJsonUtils
335{
336 Q_GADGET
337
338 public:
347 static QgsFeatureList stringToFeatureList( const QString &string, const QgsFields &fields = QgsFields(), QTextCodec *encoding SIP_PYARGREMOVE6 = nullptr );
348
356 static QgsFields stringToFields( const QString &string, QTextCodec *encoding SIP_PYARGREMOVE6 = nullptr );
357
364 Q_INVOKABLE static QString encodeValue( const QVariant &value );
365
374 static QString exportAttributes( const QgsFeature &feature, QgsVectorLayer *layer = nullptr, const QVector<QVariant> &attributeWidgetCaches = QVector<QVariant>() );
375
387 static json exportAttributesToJsonObject( const QgsFeature &feature, QgsVectorLayer *layer = nullptr, const QVector<QVariant> &attributeWidgetCaches = QVector<QVariant>(), bool useFieldFormatters = true )
388 SIP_SKIP;
389
397 Q_INVOKABLE static QVariantList parseArray( const QString &json, QMetaType::Type type = QMetaType::Type::UnknownType );
398
407 Q_INVOKABLE Q_DECL_DEPRECATED static QVariantList parseArray( const QString &json, QVariant::Type type ) SIP_DEPRECATED;
408
417 static QgsGeometry geometryFromGeoJson( const json &geometry ) SIP_SKIP;
418
426 static QgsGeometry geometryFromGeoJson( const QString &geometry );
427
433 static QVariant geometryToGeoJsonVariant( const QgsGeometry &geometry );
434
440 static json jsonFromVariant( const QVariant &v ) SIP_SKIP;
441
449 static QVariant parseJson( const std::string &jsonString ) SIP_SKIP;
450
458 static QVariant parseJson( const std::string &jsonString, QString &error ) SIP_SKIP;
459
465 static QVariant parseJson( const QString &jsonString ) SIP_SKIP;
466
472 static QVariant jsonToVariant( const json &value ) SIP_SKIP;
473
482};
483
484#endif // QGSJSONUTILS_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
GeoJsonProfile
GeoJson export Profile according to OGC Features and Geometries JSON - Part 1: Core https://docs....
Definition qgis.h:5045
@ Legacy
Legacy GeoJson profile used in QGIS prior to 4.2, which included some non-standard extensions and dev...
Definition qgis.h:5046
Represents a coordinate reference system (CRS).
Handles coordinate transforms between two coordinate systems.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
Container of fields for a vector layer.
Definition qgsfields.h:46
A geometry is the spatial representation of a feature.
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 setUseFieldFormatters(bool useFieldFormatters)
Sets whether field formatters (of type KeyValue, List, ValueRelation, ValueMap) are used to export ra...
void setIncludeAttributes(bool includeAttributes)
Sets whether to include attributes in the JSON exports.
bool useFieldFormatters() const
Returned whether field formatters (of type KeyValue, List, ValueRelation, ValueMap) are used to expor...
QgsVectorLayer * vectorLayer() const
Returns the associated vector layer, if set.
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.
QgsJsonExporter(QgsVectorLayer *vectorLayer=nullptr, int precision=6)
Constructor for QgsJsonExporter.
bool includeGeometry() const
Returns whether geometry will be included in the JSON exports.
Helper utilities for working with JSON and GeoJSON conversions.
static void addCrsInfo(json &value, const QgsCoordinateReferenceSystem &crs, Qgis::GeoJsonProfile profile=Qgis::GeoJsonProfile::Legacy)
Add crs information entry in json object regarding old GeoJSON specification format if it differs fro...
static QgsGeometry geometryFromGeoJson(const json &geometry)
Parses a GeoJSON "geometry" value to a QgsGeometry object.
static QString exportAttributes(const QgsFeature &feature, QgsVectorLayer *layer=nullptr, const QVector< QVariant > &attributeWidgetCaches=QVector< QVariant >())
Exports all attributes from a QgsFeature as a JSON map type.
static QgsFeatureList stringToFeatureList(const QString &string, const QgsFields &fields=QgsFields(), QTextCodec *encoding SIP_PYARGREMOVE6=nullptr)
Attempts to parse a GeoJSON string to a collection of features.
static Q_INVOKABLE QString encodeValue(const QVariant &value)
Encodes a value to a JSON string representation, adding appropriate quotations and escaping where req...
static QVariant parseJson(const std::string &jsonString)
Converts JSON jsonString to a QVariant, in case of parsing error an invalid QVariant is returned and ...
static QVariant geometryToGeoJsonVariant(const QgsGeometry &geometry)
Converts a geometry to a GeoJSON compatible variant.
static Q_INVOKABLE QVariantList parseArray(const QString &json, QMetaType::Type type=QMetaType::Type::UnknownType)
Parse a simple array (depth=1).
static json exportAttributesToJsonObject(const QgsFeature &feature, QgsVectorLayer *layer=nullptr, const QVector< QVariant > &attributeWidgetCaches=QVector< QVariant >(), bool useFieldFormatters=true)
Exports all attributes from a QgsFeature as a json object.
static QVariant jsonToVariant(const json &value)
Converts a JSON value to a QVariant, in case of parsing error an invalid QVariant is returned.
static QgsFields stringToFields(const QString &string, QTextCodec *encoding SIP_PYARGREMOVE6=nullptr)
Attempts to retrieve the fields from a GeoJSON string representing a collection of features.
static json jsonFromVariant(const QVariant &v)
Converts a QVariant v to a json object.
Represents a vector layer which manages a vector based dataset.
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_PYARGREMOVE6
Definition qgis_sip.h:163
QList< QgsFeature > QgsFeatureList
QList< int > QgsAttributeList
Definition qgsfield.h:30