Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
[FEATURE] New class QgsJSONUtils with utilities for converting
features to and from GeoJSON strings Sponsored by Kanton of Zug, Switzerland
- Loading branch information
1 parent
8d70a51
commit 24309df
Showing
8 changed files
with
498 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** \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 ); | ||
|
||
/** Returns a GeoJSON string representation of a feature. | ||
* @param feature feature to convert | ||
* @param precision maximum number of decimal places to use for geometry coordinates | ||
* @param attrIndexes list of attribute indexes to include in GeoJSON, or an empty list to include | ||
* all attributes | ||
* @param includeGeom set to false to avoid including the geometry representation in the JSON output | ||
* @param includeAttributes set to false to avoid including any attribute values in the JSON output | ||
* @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 | ||
*/ | ||
static QString featureToGeoJSON( const QgsFeature& feature, | ||
int precision = 17, | ||
const QgsAttributeList& attrIndexes = QgsAttributeList(), | ||
bool includeGeom = true, | ||
bool includeAttributes = true, | ||
const QVariant& id = QVariant() ); | ||
|
||
/** 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 ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/*************************************************************************** | ||
qgsjsonutils.h | ||
------------- | ||
Date : May 206 | ||
Copyright : (C) 2016 Nyall Dawson | ||
Email : nyall dot dawson at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsjsonutils.h" | ||
#include "qgsogrutils.h" | ||
#include "qgsgeometry.h" | ||
|
||
QgsFeatureList QgsJSONUtils::stringToFeatureList( const QString &string, const QgsFields &fields, QTextCodec *encoding ) | ||
{ | ||
return QgsOgrUtils::stringToFeatureList( string, fields, encoding ); | ||
} | ||
|
||
QgsFields QgsJSONUtils::stringToFields( const QString &string, QTextCodec *encoding ) | ||
{ | ||
return QgsOgrUtils::stringToFields( string, encoding ); | ||
} | ||
|
||
QString QgsJSONUtils::featureToGeoJSON( const QgsFeature& feature, | ||
int precision, | ||
const QgsAttributeList& attrIndexes, | ||
bool includeGeom, | ||
bool includeAttributes, | ||
const QVariant& id ) | ||
{ | ||
QString s = "{\n \"type\":\"Feature\",\n"; | ||
|
||
// ID | ||
s += QString( " \"id\":%1" ).arg( !id.isValid() ? QString::number( feature.id() ) : encodeValue( id ) ); | ||
|
||
if ( includeAttributes || includeGeom ) | ||
s += ",\n"; | ||
else | ||
s += '\n'; | ||
|
||
const QgsGeometry* geom = feature.constGeometry(); | ||
if ( geom && !geom->isEmpty() && includeGeom ) | ||
{ | ||
QgsRectangle box = geom->boundingBox(); | ||
|
||
if ( QgsWKBTypes::flatType( geom->geometry()->wkbType() ) != QgsWKBTypes::Point ) | ||
{ | ||
s += QString( " \"bbox\":[%1, %2, %3, %4],\n" ).arg( qgsDoubleToString( box.xMinimum(), precision ), | ||
qgsDoubleToString( box.yMinimum(), precision ), | ||
qgsDoubleToString( box.xMaximum(), precision ), | ||
qgsDoubleToString( box.yMaximum(), precision ) ); | ||
} | ||
s += " \"geometry\":\n "; | ||
s += geom->exportToGeoJSON( precision ); | ||
if ( includeAttributes ) | ||
s += ",\n"; | ||
else | ||
s += '\n'; | ||
} | ||
|
||
if ( includeAttributes ) | ||
{ | ||
//read all attribute values from the feature | ||
s += " \"properties\":{\n"; | ||
|
||
const QgsFields* fields = feature.fields(); | ||
int attributeCounter = 0; | ||
|
||
for ( int i = 0; i < fields->count(); ++i ) | ||
{ | ||
if ( !attrIndexes.isEmpty() && !attrIndexes.contains( i ) ) | ||
continue; | ||
|
||
if ( attributeCounter > 0 ) | ||
s += ",\n"; | ||
QVariant val = feature.attributes().at( i ); | ||
|
||
s += QString( " \"%1\":%2" ).arg( fields->at( i ).name(), encodeValue( val ) ); | ||
|
||
++attributeCounter; | ||
} | ||
|
||
s += "\n }\n"; | ||
} | ||
|
||
s += "}"; | ||
|
||
return s; | ||
} | ||
|
||
QString QgsJSONUtils::encodeValue( const QVariant &value ) | ||
{ | ||
if ( value.isNull() ) | ||
return "null"; | ||
|
||
switch ( value.type() ) | ||
{ | ||
case QVariant::Int: | ||
case QVariant::UInt: | ||
case QVariant::LongLong: | ||
case QVariant::ULongLong: | ||
case QVariant::Double: | ||
return value.toString(); | ||
|
||
case QVariant::Bool: | ||
return value.toBool() ? "true" : "false"; | ||
|
||
default: | ||
case QVariant::String: | ||
QString v = value.toString().replace( '"', "\\\"" ) | ||
.replace( '\r', "\\r" ) | ||
.replace( '\n', "\\n" ); | ||
return v.prepend( '"' ).append( '"' ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/*************************************************************************** | ||
qgsjsonutils.h | ||
------------- | ||
Date : May 206 | ||
Copyright : (C) 2016 Nyall Dawson | ||
Email : nyall dot dawson at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSJSONUTILS_H | ||
#define QGSJSONUTILS_H | ||
|
||
#include "qgsfeature.h" | ||
|
||
class QTextCodec; | ||
|
||
/** \ingroup core | ||
* \class QgsJSONUtils | ||
* \brief Helper utilities for working with JSON and GeoJSON conversions. | ||
* \note Added in version 2.16 | ||
*/ | ||
|
||
class CORE_EXPORT QgsJSONUtils | ||
{ | ||
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 ); | ||
|
||
/** Returns a GeoJSON string representation of a feature. | ||
* @param feature feature to convert | ||
* @param precision maximum number of decimal places to use for geometry coordinates | ||
* @param attrIndexes list of attribute indexes to include in GeoJSON, or an empty list to include | ||
* all attributes | ||
* @param includeGeom set to false to avoid including the geometry representation in the JSON output | ||
* @param includeAttributes set to false to avoid including any attribute values in the JSON output | ||
* @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 | ||
*/ | ||
static QString featureToGeoJSON( const QgsFeature& feature, | ||
int precision = 17, | ||
const QgsAttributeList& attrIndexes = QgsAttributeList(), | ||
bool includeGeom = true, | ||
bool includeAttributes = true, | ||
const QVariant& id = QVariant() ); | ||
|
||
/** 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 ); | ||
|
||
}; | ||
|
||
#endif // QGSJSONUTILS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.