1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
/** \ingroup core
* \class QgsJSONExporter
* \brief Handles exporting QgsFeature features to GeoJSON features.
*
* Note that geometries will be automatically reprojected to WGS84 to match GeoJSON spec
* if either the source vector layer or source CRS is set.
* \note Added in version 2.16
*/
class QgsJSONExporter
{
%TypeHeaderCode
#include <qgsjsonutils.h>
%End
public:
/** Constructor for QgsJSONExporter.
* @param vectorLayer associated vector layer (required for related attribute export)
* @param precision maximum number of decimal places to use for geometry coordinates
*/
QgsJSONExporter( const QgsVectorLayer* vectorLayer = nullptr, int precision = 17 );
/** Sets the maximum number of decimal places to use in geometry coordinates.
* @param precision number of decimal places
* @see precision()
*/
void setPrecision( int precision );
/** Returns the maximum number of decimal places to use in geometry coordinates.
* @see setPrecision()
*/
int precision() const;
/** Sets whether to include geometry in the JSON exports.
* @param includeGeometry set to false to prevent geometry inclusion
* @see includeGeometry()
*/
void setIncludeGeometry( bool includeGeometry );
/** Returns whether geometry will be included in the JSON exports.
* @see setIncludeGeometry()
*/
bool includeGeometry() const;
/** Sets whether to include attributes in the JSON exports.
* @param includeAttributes set to false to prevent attribute inclusion
* @see includeAttributes()
*/
void setIncludeAttributes( bool includeAttributes );
/** Returns whether attributes will be included in the JSON exports.
* @see setIncludeAttributes()
*/
bool includeAttributes() const;
/** Sets whether to include attributes of features linked via references in the JSON exports.
* @param includeRelated set to true to include attributes for any related child features
* within the exported properties element.
* @note associated vector layer must be set with setVectorLayer()
* @see includeRelated()
*/
void setIncludeRelated( bool includeRelated );
/** Returns whether attributes of related (child) features will be included in the JSON exports.
* @see setIncludeRelated()
*/
bool includeRelated() const;
/** Sets the associated vector layer (required for related attribute export). This will automatically
* update the sourceCrs() to match.
* @param vectorLayer vector layer
* @see vectorLayer()
*/
void setVectorLayer( const QgsVectorLayer* vectorLayer );
/** Returns the associated vector layer, if set.
* @see setVectorLayer()
*/
QgsVectorLayer* vectorLayer() const;
/** Sets the source CRS for feature geometries. The source CRS must be set if geometries are to be
* correctly automatically reprojected to WGS 84, to match GeoJSON specifications.
* @param crs source CRS for input feature geometries
* @note the source CRS will be overwritten when a vector layer is specified via setVectorLayer()
* @see sourceCrs()
*/
void setSourceCrs( const QgsCoordinateReferenceSystem& crs );
/** Returns the source CRS for feature geometries. The source CRS must be set if geometries are to be
* correctly automatically reprojected to WGS 84, to match GeoJSON specifications.
* @see setSourceCrs()
*/
const QgsCoordinateReferenceSystem& sourceCrs() const;
/** Sets the list of attributes to include in the JSON exports.
* @param attributes list of attribute indexes, or an empty list to include all
* attributes
* @see attributes()
* @see setExcludedAttributes()
* @note Attributes excluded via setExcludedAttributes() take precedence over
* attributes specified by this method.
*/
void setAttributes( const QgsAttributeList& attributes );
/** Returns the list of attributes which will be included in the JSON exports, or
* an empty list if all attributes will be included.
* @see setAttributes()
* @see excludedAttributes()
* @note Attributes excluded via excludedAttributes() take precedence over
* attributes returned by this method.
*/
QgsAttributeList attributes() const;
/** Sets a list of attributes to specifically exclude from the JSON exports. Excluded attributes
* take precedence over attributes included via setAttributes().
* @param attributes list of attribute indexes to exclude
* @see excludedAttributes()
* @see setAttributes()
*/
void setExcludedAttributes( const QgsAttributeList& attributes );
/** Returns a list of attributes which will be specifically excluded from the JSON exports. Excluded attributes
* take precedence over attributes included via attributes().
* @see setExcludedAttributes()
* @see attributes()
*/
QgsAttributeList excludedAttributes() const;
/** Returns a GeoJSON string representation of a feature.
* @param feature feature to convert
* @param extraProperties map of extra attributes to include in feature's properties
* @param id optional ID to use as GeoJSON feature's ID instead of input feature's ID. If omitted, feature's
* ID is used.
* @returns GeoJSON string
* @see exportFeatures()
*/
QString exportFeature( const QgsFeature& feature,
const QVariantMap& extraProperties = QVariantMap(),
const QVariant& id = QVariant() ) const;
/** Returns a GeoJSON string representation of a list of features (feature collection).
* @param features features to convert
* @returns GeoJSON string
* @see exportFeature()
*/
QString exportFeatures( const QgsFeatureList& features ) const;
private:
QgsJSONExporter( const QgsJSONExporter& );
};
/** \ingroup core
* \class QgsJSONUtils
* \brief Helper utilities for working with JSON and GeoJSON conversions.
* \note Added in version 2.16
*/
class QgsJSONUtils
{
%TypeHeaderCode
#include <qgsjsonutils.h>
%End
public:
/** Attempts to parse a GeoJSON string to a collection of features.
* @param string GeoJSON string to parse
* @param fields fields collection to use for parsed features
* @param encoding text encoding
* @returns list of parsed features, or an empty list if no features could be parsed
* @see stringToFields()
* @note this function is a wrapper around QgsOgrUtils::stringToFeatureList()
*/
static QgsFeatureList stringToFeatureList( const QString& string, const QgsFields& fields, QTextCodec* encoding );
/** Attempts to retrieve the fields from a GeoJSON string representing a collection of features.
* @param string GeoJSON string to parse
* @param encoding text encoding
* @returns retrieved fields collection, or an empty list if no fields could be determined from the string
* @see stringToFeatureList()
* @note this function is a wrapper around QgsOgrUtils::stringToFields()
*/
static QgsFields stringToFields( const QString& string, QTextCodec* encoding );
/** Encodes a value to a JSON string representation, adding appropriate quotations and escaping
* where required.
* @param value value to encode
* @returns encoded value
*/
static QString encodeValue( const QVariant& value );
/** Exports all attributes from a QgsFeature as a JSON map type.
* @param feature feature to export
*/
static QString exportAttributes( const QgsFeature& feature );
};
|