QGIS API Documentation  3.6.0-Noosa (5873452)
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 
27 class QTextCodec;
28 
39 class CORE_EXPORT QgsJsonExporter
40 {
41  public:
42 
49  QgsJsonExporter( QgsVectorLayer *vectorLayer = nullptr, int precision = 6 );
50 
57  void setPrecision( int precision ) { mPrecision = precision; }
58 
63  int precision() const { return mPrecision; }
64 
70  void setIncludeGeometry( bool includeGeometry ) { mIncludeGeometry = includeGeometry; }
71 
76  bool includeGeometry() const { return mIncludeGeometry; }
77 
83  void setIncludeAttributes( bool includeAttributes ) { mIncludeAttributes = includeAttributes; }
84 
89  bool includeAttributes() const { return mIncludeAttributes; }
90 
98  void setIncludeRelated( bool includeRelated ) { mIncludeRelatedAttributes = includeRelated; }
99 
104  bool includeRelated() const { return mIncludeRelatedAttributes; }
105 
111  void setAttributeDisplayName( bool displayName ) { mAttributeDisplayName = displayName; };
112 
118  bool attributeDisplayName() const { return mAttributeDisplayName; }
119 
126  void setVectorLayer( QgsVectorLayer *vectorLayer );
127 
132  QgsVectorLayer *vectorLayer() const;
133 
141  void setSourceCrs( const QgsCoordinateReferenceSystem &crs );
142 
148  QgsCoordinateReferenceSystem sourceCrs() const;
149 
159  void setAttributes( const QgsAttributeList &attributes ) { mAttributeIndexes = attributes; }
160 
169  QgsAttributeList attributes() const { return mAttributeIndexes; }
170 
178  void setExcludedAttributes( const QgsAttributeList &attributes ) { mExcludedAttributeIndexes = attributes; }
179 
186  QgsAttributeList excludedAttributes() const { return mExcludedAttributeIndexes; }
187 
197  QString exportFeature( const QgsFeature &feature,
198  const QVariantMap &extraProperties = QVariantMap(),
199  const QVariant &id = QVariant() ) const;
200 
201 
208  QString exportFeatures( const QgsFeatureList &features ) const;
209 
210  private:
211 
213  int mPrecision;
214 
219  QgsAttributeList mAttributeIndexes;
220 
222  QgsAttributeList mExcludedAttributeIndexes;
223 
225  bool mIncludeGeometry = true;
226 
228  bool mIncludeAttributes = true;
229 
231  bool mIncludeRelatedAttributes = false;
232 
234  QPointer< QgsVectorLayer > mLayer;
235 
237 
238  QgsCoordinateTransform mTransform;
239 
240  bool mAttributeDisplayName = false;
241 };
242 
250 class CORE_EXPORT QgsJsonUtils
251 {
252  public:
253 
263  static QgsFeatureList stringToFeatureList( const QString &string, const QgsFields &fields, QTextCodec *encoding );
264 
273  static QgsFields stringToFields( const QString &string, QTextCodec *encoding );
274 
281  static QString encodeValue( const QVariant &value );
282 
291  static QString exportAttributes( const QgsFeature &feature, QgsVectorLayer *layer = nullptr,
292  const QVector<QVariant> &attributeWidgetCaches = QVector<QVariant>() );
293 
300  static QVariantList parseArray( const QString &json, QVariant::Type type );
301 };
302 
303 #endif // QGSJSONUTILS_H
QgsAttributeList excludedAttributes() const
Returns a list of attributes which will be specifically excluded from the JSON exports.
Definition: qgsjsonutils.h:186
int precision
bool includeRelated() const
Returns whether attributes of related (child) features will be included in the JSON exports...
Definition: qgsjsonutils.h:104
void setAttributeDisplayName(bool displayName)
Sets whether to print original names of attributes or aliases if defined.
Definition: qgsjsonutils.h:111
void setIncludeAttributes(bool includeAttributes)
Sets whether to include attributes in the JSON exports.
Definition: qgsjsonutils.h:83
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:571
void setIncludeGeometry(bool includeGeometry)
Sets whether to include geometry in the JSON exports.
Definition: qgsjsonutils.h:70
Helper utilities for working with JSON and GeoJSON conversions.
Definition: qgsjsonutils.h:250
Container of fields for a vector layer.
Definition: qgsfields.h:42
bool includeAttributes() const
Returns whether attributes will be included in the JSON exports.
Definition: qgsjsonutils.h:89
int precision() const
Returns the maximum number of decimal places to use in geometry coordinates.
Definition: qgsjsonutils.h:63
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
const QgsCoordinateReferenceSystem & crs
bool attributeDisplayName() const
Returns whether original names of attributes or aliases are printed.
Definition: qgsjsonutils.h:118
void setAttributes(const QgsAttributeList &attributes)
Sets the list of attributes to include in the JSON exports.
Definition: qgsjsonutils.h:159
void setExcludedAttributes(const QgsAttributeList &attributes)
Sets a list of attributes to specifically exclude from the JSON exports.
Definition: qgsjsonutils.h:178
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:169
void setIncludeRelated(bool includeRelated)
Sets whether to include attributes of features linked via references in the JSON exports.
Definition: qgsjsonutils.h:98
Handles exporting QgsFeature features to GeoJSON features.
Definition: qgsjsonutils.h:39
bool includeGeometry() const
Returns whether geometry will be included in the JSON exports.
Definition: qgsjsonutils.h:76
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
QList< int > QgsAttributeList
Definition: qgsfield.h:27
void setPrecision(int precision)
Sets the maximum number of decimal places to use in geometry coordinates.
Definition: qgsjsonutils.h:57
Represents a vector layer which manages a vector based data sets.