QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsfeature.h"
21 #include "qgscoordinatetransform.h"
22 
23 class QTextCodec;
24 class QgsVectorLayer;
25 
35 class CORE_EXPORT QgsJSONExporter
36 {
37  public:
38 
44  QgsJSONExporter( const QgsVectorLayer* vectorLayer = nullptr, int precision = 6 );
45 
51  void setPrecision( int precision ) { mPrecision = precision; }
52 
56  int precision() const { return mPrecision; }
57 
62  void setIncludeGeometry( bool includeGeometry ) { mIncludeGeometry = includeGeometry; }
63 
67  bool includeGeometry() const { return mIncludeGeometry; }
68 
73  void setIncludeAttributes( bool includeAttributes ) { mIncludeAttributes = includeAttributes; }
74 
78  bool includeAttributes() const { return mIncludeAttributes; }
79 
86  void setIncludeRelated( bool includeRelated ) { mIncludeRelatedAttributes = includeRelated; }
87 
91  bool includeRelated() const { return mIncludeRelatedAttributes; }
92 
98  void setVectorLayer( const QgsVectorLayer* vectorLayer );
99 
103  QgsVectorLayer* vectorLayer() const;
104 
111  void setSourceCrs( const QgsCoordinateReferenceSystem& crs );
112 
117  const QgsCoordinateReferenceSystem& sourceCrs() const;
118 
127  void setAttributes( const QgsAttributeList& attributes ) { mAttributeIndexes = attributes; }
128 
136  QgsAttributeList attributes() const { return mAttributeIndexes; }
137 
144  void setExcludedAttributes( const QgsAttributeList& attributes ) { mExcludedAttributeIndexes = attributes; }
145 
151  QgsAttributeList excludedAttributes() const { return mExcludedAttributeIndexes; }
152 
161  QString exportFeature( const QgsFeature& feature,
162  const QVariantMap& extraProperties = QVariantMap(),
163  const QVariant& id = QVariant() ) const;
164 
165 
171  QString exportFeatures( const QgsFeatureList& features ) const;
172 
173  private:
174 
176  int mPrecision;
177 
180  QgsAttributeList mAttributeIndexes;
181 
183  QgsAttributeList mExcludedAttributeIndexes;
184 
186  bool mIncludeGeometry;
187 
189  bool mIncludeAttributes;
190 
192  bool mIncludeRelatedAttributes;
193 
195  QString mLayerId;
196 
198 
199  QgsCoordinateTransform mTransform;
200 
201 };
202 
209 class CORE_EXPORT QgsJSONUtils
210 {
211  public:
212 
221  static QgsFeatureList stringToFeatureList( const QString& string, const QgsFields& fields, QTextCodec* encoding );
222 
230  static QgsFields stringToFields( const QString& string, QTextCodec* encoding );
231 
237  static QString encodeValue( const QVariant& value );
238 
242  static QString exportAttributes( const QgsFeature& feature );
243 
244 };
245 
246 #endif // QGSJSONUTILS_H
Container of fields for a vector layer.
Definition: qgsfield.h:252
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
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:136
void setIncludeRelated(bool includeRelated)
Sets whether to include attributes of features linked via references in the JSON exports.
Definition: qgsjsonutils.h:86
Handles exporting QgsFeature features to GeoJSON features.
Definition: qgsjsonutils.h:35
void setIncludeAttributes(bool includeAttributes)
Sets whether to include attributes in the JSON exports.
Definition: qgsjsonutils.h:73
int precision() const
Returns the maximum number of decimal places to use in geometry coordinates.
Definition: qgsjsonutils.h:56
void setAttributes(const QgsAttributeList &attributes)
Sets the list of attributes to include in the JSON exports.
Definition: qgsjsonutils.h:127
void setIncludeGeometry(bool includeGeometry)
Sets whether to include geometry in the JSON exports.
Definition: qgsjsonutils.h:62
void setPrecision(int precision)
Sets the maximum number of decimal places to use in geometry coordinates.
Definition: qgsjsonutils.h:51
bool includeAttributes() const
Returns whether attributes will be included in the JSON exports.
Definition: qgsjsonutils.h:78
Class for storing a coordinate reference system (CRS)
void setExcludedAttributes(const QgsAttributeList &attributes)
Sets a list of attributes to specifically exclude from the JSON exports.
Definition: qgsjsonutils.h:144
Class for doing transforms between two map coordinate systems.
bool includeRelated() const
Returns whether attributes of related (child) features will be included in the JSON exports...
Definition: qgsjsonutils.h:91
bool includeGeometry() const
Returns whether geometry will be included in the JSON exports.
Definition: qgsjsonutils.h:67
Represents a vector layer which manages a vector based data sets.
QgsAttributeList excludedAttributes() const
Returns a list of attributes which will be specifically excluded from the JSON exports.
Definition: qgsjsonutils.h:151
Helper utilities for working with JSON and GeoJSON conversions.
Definition: qgsjsonutils.h:209