18#include <nlohmann/json.hpp>
40#include <QJsonDocument>
43#include "moc_qgsjsonutils.cpp"
52 mTransform.setSourceCrs( mCrs );
57 mTransform.setDestinationCrs( mDestinationCrs );
66 mTransform.setSourceCrs( mCrs );
78 mTransform.setSourceCrs( mCrs );
87 const QVariant &
id,
int indent )
const
93 catch ( json::type_error &ex )
98 catch ( json::other_error &ex )
109 {
"type",
"Feature" },
114 auto intId =
id.toLongLong( &ok );
117 featureJson[
"id"] = intId;
121 featureJson[
"id"] =
id.toString().toStdString();
126 featureJson[
"id"] =
nullptr;
130 featureJson[
"id"] = feature.
id();
134 if ( !geom.
isNull() && mIncludeGeometry )
136 if ( mCrs.isValid() )
153 featureJson[
"bbox" ] =
161 featureJson[
"geometry" ] = geom.
asJsonObject( mPrecision );
165 featureJson[
"geometry" ] =
nullptr;
170 if ( mIncludeAttributes || !extraProperties.isEmpty() )
173 if ( mIncludeAttributes )
177 QStringList formattersAllowList;
178 formattersAllowList << u
"KeyValue"_s
180 << u
"ValueRelation"_s
183 for (
int i = 0; i < fields.
count(); ++i )
185 if ( ( !mAttributeIndexes.isEmpty() && !mAttributeIndexes.contains( i ) ) || mExcludedAttributeIndexes.contains( i ) )
190 if ( mUseFieldFormatters && mLayer )
194 if ( formattersAllowList.contains( fieldFormatter->
id() ) )
198 QString name = fields.
at( i ).
name();
199 if ( mAttributeDisplayName )
201 name = mLayer->attributeDisplayName( i );
207 if ( !extraProperties.isEmpty() )
209 QVariantMap::const_iterator it = extraProperties.constBegin();
210 for ( ; it != extraProperties.constEnd(); ++it )
217 if ( mLayer && mIncludeRelatedAttributes )
220 for (
const auto &relation : std::as_const( relations ) )
225 json relatedFeatureAttributes;
229 QVector<QVariant> attributeWidgetCaches;
232 for (
const QgsField &field : fields )
236 attributeWidgetCaches.append( fieldFormatter->
createCache( childLayer, fieldIndex, setup.
config() ) );
245 properties[ relation.name().toStdString() ] = relatedFeatureAttributes;
249 featureJson[
"properties" ] = properties;
262 {
"type",
"FeatureCollection" },
263 {
"features", json::array() }
268 for (
const QgsFeature &feature : std::as_const( features ) )
277 mDestinationCrs = destinationCrs;
278 mTransform.setDestinationCrs( mDestinationCrs );
288 encoding = QTextCodec::codecForName(
"UTF-8" );
296 encoding = QTextCodec::codecForName(
"UTF-8" );
306 switch ( value.userType() )
308 case QMetaType::Type::Int:
309 case QMetaType::Type::UInt:
310 case QMetaType::Type::LongLong:
311 case QMetaType::Type::ULongLong:
312 case QMetaType::Type::Double:
313 return value.toString();
315 case QMetaType::Type::Bool:
316 return value.toBool() ?
"true" :
"false";
318 case QMetaType::Type::QStringList:
319 case QMetaType::Type::QVariantList:
320 case QMetaType::Type::QVariantMap:
321 return QString::fromUtf8( QJsonDocument::fromVariant( value ).toJson( QJsonDocument::Compact ) );
324 case QMetaType::Type::QString:
325 QString v = value.toString()
326 .replace(
'\\',
"\\\\"_L1 )
327 .replace(
'"',
"\\\""_L1 )
328 .replace(
'\r',
"\\r"_L1 )
329 .replace(
'\b',
"\\b"_L1 )
330 .replace(
'\t',
"\\t"_L1 )
331 .replace(
'/',
"\\/"_L1 )
332 .replace(
'\n',
"\\n"_L1 );
334 return v.prepend(
'"' ).append(
'"' );
342 for (
int i = 0; i < fields.
count(); ++i )
354 val = fieldFormatter->
representValue( layer, i, setup.
config(), attributeWidgetCaches.count() >= i ? attributeWidgetCaches.at( i ) : QVariant(), val );
359 return attrs.prepend(
'{' ).
append(
'}' );
364 QString errorMessage;
368 const auto jObj( json::parse( json.toStdString() ) );
369 if ( ! jObj.is_array() )
371 throw json::parse_error::create( 0, 0, u
"JSON value must be an array"_s.toStdString(), &jObj );
373 for (
const auto &item : jObj )
377 if ( item.is_number_integer() )
381 else if ( item.is_number_unsigned() )
383 v = item.get<
unsigned>();
385 else if ( item.is_number_float() )
388 v = item.get<
double>();
390 else if ( item.is_string() )
392 v = QString::fromStdString( item.get<std::string>() );
394 else if ( item.is_boolean() )
396 v = item.get<
bool>();
398 else if ( item.is_null() )
405 if ( type != QMetaType::Type::UnknownType )
407 if ( ! v.convert(
static_cast<int>( type ) ) )
409 QgsLogger::warning( u
"Cannot convert json array element to specified type, ignoring: %1"_s.arg( v.toString() ) );
413 result.push_back( v );
418 result.push_back( v );
422 catch ( json::parse_error &ex )
424 errorMessage = ex.what();
438 if ( !coords.is_array() || coords.size() < 2 || coords.size() > 3 )
440 QgsDebugError( u
"JSON Point geometry coordinates must be an array of two or three numbers"_s );
444 const double x = coords[0].get<
double >();
445 const double y = coords[1].get<
double >();
446 if ( coords.size() == 2 )
448 return std::make_unique< QgsPoint >( x, y );
452 const double z = coords[2].get<
double >();
453 return std::make_unique< QgsPoint >( x, y, z );
459 if ( !coords.is_array() || coords.size() < 2 )
461 QgsDebugError( u
"JSON LineString geometry coordinates must be an array of at least two points"_s );
465 const std::size_t coordsSize = coords.size();
470 x.resize( coordsSize );
471 y.resize( coordsSize );
472 z.resize( coordsSize );
474 double *xOut = x.data();
475 double *yOut = y.data();
476 double *zOut = z.data();
478 for (
const auto &coord : coords )
480 if ( !coord.is_array() || coord.size() < 2 || coord.size() > 3 )
482 QgsDebugError( u
"JSON LineString geometry coordinates must be an array of two or three numbers"_s );
486 *xOut++ = coord[0].get<
double >();
487 *yOut++ = coord[1].get<
double >();
488 if ( coord.size() == 3 )
490 *zOut++ = coord[2].get<
double >();
495 *zOut++ = std::numeric_limits< double >::quiet_NaN();
499 return std::make_unique< QgsLineString >( x, y, hasZ ? z : QVector<double>() );
504 if ( !coords.is_array() || coords.size() < 1 )
506 QgsDebugError( u
"JSON Polygon geometry coordinates must be an array"_s );
510 const std::size_t coordsSize = coords.size();
517 auto polygon = std::make_unique< QgsPolygon >( exterior.release() );
518 for ( std::size_t i = 1; i < coordsSize; ++i )
525 polygon->addInteriorRing( ring.release() );
532 if ( !geometry.is_object() )
538 if ( !geometry.contains(
"type" ) )
544 const QString type = QString::fromStdString( geometry[
"type"].get< std::string >() );
545 if ( type.compare(
"Point"_L1, Qt::CaseInsensitive ) == 0 )
547 if ( !geometry.contains(
"coordinates" ) )
549 QgsDebugError( u
"JSON Point geometry must contain 'coordinates'"_s );
553 const json &coords = geometry[
"coordinates"];
556 else if ( type.compare(
"MultiPoint"_L1, Qt::CaseInsensitive ) == 0 )
558 if ( !geometry.contains(
"coordinates" ) )
560 QgsDebugError( u
"JSON MultiPoint geometry must contain 'coordinates'"_s );
564 const json &coords = geometry[
"coordinates"];
566 if ( !coords.is_array() )
568 QgsDebugError( u
"JSON MultiPoint geometry coordinates must be an array"_s );
572 auto multiPoint = std::make_unique< QgsMultiPoint >();
573 multiPoint->reserve(
static_cast< int >( coords.size() ) );
574 for (
const auto &pointCoords : coords )
581 multiPoint->addGeometry( point.release() );
586 else if ( type.compare(
"LineString"_L1, Qt::CaseInsensitive ) == 0 )
588 if ( !geometry.contains(
"coordinates" ) )
590 QgsDebugError( u
"JSON LineString geometry must contain 'coordinates'"_s );
594 const json &coords = geometry[
"coordinates"];
597 else if ( type.compare(
"MultiLineString"_L1, Qt::CaseInsensitive ) == 0 )
599 if ( !geometry.contains(
"coordinates" ) )
601 QgsDebugError( u
"JSON MultiLineString geometry must contain 'coordinates'"_s );
605 const json &coords = geometry[
"coordinates"];
607 if ( !coords.is_array() )
609 QgsDebugError( u
"JSON MultiLineString geometry coordinates must be an array"_s );
613 auto multiLineString = std::make_unique< QgsMultiLineString >();
614 multiLineString->reserve(
static_cast< int >( coords.size() ) );
615 for (
const auto &lineCoords : coords )
622 multiLineString->addGeometry( line.release() );
625 return multiLineString;
627 else if ( type.compare(
"Polygon"_L1, Qt::CaseInsensitive ) == 0 )
629 if ( !geometry.contains(
"coordinates" ) )
631 QgsDebugError( u
"JSON Polygon geometry must contain 'coordinates'"_s );
635 const json &coords = geometry[
"coordinates"];
636 if ( !coords.is_array() || coords.size() < 1 )
638 QgsDebugError( u
"JSON Polygon geometry coordinates must be an array of at least one ring"_s );
644 else if ( type.compare(
"MultiPolygon"_L1, Qt::CaseInsensitive ) == 0 )
646 if ( !geometry.contains(
"coordinates" ) )
648 QgsDebugError( u
"JSON MultiPolygon geometry must contain 'coordinates'"_s );
652 const json &coords = geometry[
"coordinates"];
654 if ( !coords.is_array() )
656 QgsDebugError( u
"JSON MultiPolygon geometry coordinates must be an array"_s );
660 auto multiPolygon = std::make_unique< QgsMultiPolygon >();
661 multiPolygon->reserve(
static_cast< int >( coords.size() ) );
662 for (
const auto &polygonCoords : coords )
669 multiPolygon->addGeometry( polygon.release() );
674 else if ( type.compare(
"GeometryCollection"_L1, Qt::CaseInsensitive ) == 0 )
676 if ( !geometry.contains(
"geometries" ) )
678 QgsDebugError( u
"JSON GeometryCollection geometry must contain 'geometries'"_s );
682 const json &geometries = geometry[
"geometries"];
684 if ( !geometries.is_array() )
686 QgsDebugError( u
"JSON GeometryCollection geometries must be an array"_s );
690 auto collection = std::make_unique< QgsGeometryCollection >();
691 collection->reserve(
static_cast< int >( geometries.size() ) );
692 for (
const auto &geometry : geometries )
699 collection->addGeometry(
object.release() );
705 QgsDebugError( u
"Unhandled GeoJSON geometry type: %1"_s.arg( type ) );
711 if ( !geometry.is_object() )
724 const auto jObj( json::parse( geometry.toStdString() ) );
727 catch ( json::parse_error &ex )
729 QgsDebugError( u
"Cannot parse json (%1): %2"_s.arg( geometry, ex.what() ) );
741 if ( val.userType() == QMetaType::Type::QVariantMap )
743 const QVariantMap &vMap = val.toMap();
744 json jMap = json::object();
745 for (
auto it = vMap.constBegin(); it != vMap.constEnd(); it++ )
751 else if ( val.userType() == QMetaType::Type::QVariantList || val.userType() == QMetaType::Type::QStringList )
753 const QVariantList &vList = val.toList();
754 json jList = json::array();
755 for (
const auto &v : vList )
763 switch ( val.userType() )
766 case QMetaType::UInt:
767 case QMetaType::LongLong:
768 case QMetaType::ULongLong:
769 j = val.toLongLong();
771 case QMetaType::Double:
772 case QMetaType::Float:
775 case QMetaType::Bool:
778 case QMetaType::QByteArray:
779 j = val.toByteArray().toBase64().toStdString();
782 j = val.toString().toStdString();
792 const QVariant res =
parseJson( jsonString, error );
794 if ( !error.isEmpty() )
797 QString::fromStdString( jsonString ) ) );
807 const json j = json::parse( jsonString );
810 catch ( json::parse_error &ex )
812 error = QString::fromStdString( ex.what() );
814 catch ( json::type_error &ex )
816 error = QString::fromStdString( ex.what() );
824 bool isPrimitive =
true;
826 std::function<QVariant( json )> _parser { [ & ]( json jObj ) -> QVariant {
828 if ( jObj.is_array() )
831 QVariantList results;
832 results.reserve( jObj.size() );
833 for (
const auto &item : jObj )
835 results.push_back( _parser( item ) );
839 else if ( jObj.is_object() )
843 for (
const auto &item : jObj.items() )
845 const auto key { QString::fromStdString( item.key() ) };
846 const auto value { _parser( item.value() ) };
847 results[ key ] = value;
853 if ( jObj.is_number_unsigned() )
857 const qulonglong num { jObj.get<qulonglong>() };
858 if ( num <= std::numeric_limits<int>::max() )
860 result =
static_cast<int>( num );
862 else if ( num <= std::numeric_limits<qlonglong>::max() )
864 result =
static_cast<qlonglong
>( num );
871 else if ( jObj.is_number_integer() )
873 const qlonglong num { jObj.get<qlonglong>() };
874 if ( num <= std::numeric_limits<int>::max() && num >= std::numeric_limits<int>::lowest() )
876 result =
static_cast<int>( num );
883 else if ( jObj.is_boolean() )
885 result = jObj.get<
bool>();
887 else if ( jObj.is_number_float() )
890 result = jObj.get<
double>();
892 else if ( jObj.is_string() )
894 if ( isPrimitive && jObj.get<std::string>().length() == 0 )
896 result = QString::fromStdString( jObj.get<std::string>() ).append(
"\"" ).insert( 0,
"\"" );
900 result = QString::fromStdString( jObj.get<std::string>() );
903 else if ( jObj.is_null() )
912 return _parser( value );
917 return jsonString.isEmpty() ? QVariant() :
parseJson( jsonString.toStdString() );
924 for (
int i = 0; i < fields.
count(); ++i )
928 if ( layer && useFieldFormatters )
933 val = fieldFormatter->
representValue( layer, i, setup.
config(), attributeWidgetCaches.count() >= i ? attributeWidgetCaches.at( i ) : QVariant(), val );
944 if ( crs.
authid() ==
"OGC:CRS84" || crs.
authid() ==
"EPSG:4326" )
947 value[
"crs"][
"type"] =
"name";
948 value[
"crs"][
"properties"][
"name"] = crs.
toOgcUrn().toStdString();
@ Success
Operation succeeded.
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
static QgsFieldFormatterRegistry * fieldFormatterRegistry()
Gets the registry of available field formatters.
Represents a coordinate reference system (CRS).
QString toOgcUrn() const
Returns the crs as OGC URN (format: urn:ogc:def:crs:OGC:1.3:CRS84) Returns an empty string on failure...
Custom exception class for Coordinate Reference System related exceptions.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Encapsulate a field in an attribute table or data source.
QgsEditorWidgetSetup editorWidgetSetup() const
Gets the editor widget setup for the field.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
A geometry is the spatial representation of a feature.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
virtual json asJsonObject(int precision=17) const
Exports the geometry to a json object.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Qgis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.).
json exportFeatureToJsonObject(const QgsFeature &feature, const QVariantMap &extraProperties=QVariantMap(), const QVariant &id=QVariant()) const
Returns a QJsonObject representation of a feature.
json exportFeaturesToJsonObject(const QgsFeatureList &features) const
Returns a JSON object representation of a list of features (feature collection).
void setSourceCrs(const QgsCoordinateReferenceSystem &crs)
Sets the source CRS for feature geometries.
void setDestinationCrs(const QgsCoordinateReferenceSystem &destinationCrs)
Set the destination CRS for feature geometry transformation to destinationCrs, this defaults to EPSG:...
QgsVectorLayer * vectorLayer() const
Returns the associated vector layer, if set.
QString exportFeature(const QgsFeature &feature, const QVariantMap &extraProperties=QVariantMap(), const QVariant &id=QVariant(), int indent=-1) const
Returns a GeoJSON string representation of a feature.
int precision() const
Returns the maximum number of decimal places to use in geometry coordinates.
QString exportFeatures(const QgsFeatureList &features, int indent=-1) const
Returns a GeoJSON string representation of a list of features (feature collection).
void setVectorLayer(QgsVectorLayer *vectorLayer)
Sets the associated vector layer (required for related attribute export).
QgsCoordinateReferenceSystem sourceCrs() const
Returns the source CRS for feature geometries.
QgsJsonExporter(QgsVectorLayer *vectorLayer=nullptr, int precision=6)
Constructor for QgsJsonExporter.
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 void addCrsInfo(json &value, const QgsCoordinateReferenceSystem &crs)
Add crs information entry in json object regarding old GeoJSON specification format if it differs fro...
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.
static void warning(const QString &msg)
Goes to qWarning.
static QgsFeatureList stringToFeatureList(const QString &string, const QgsFields &fields, QTextCodec *encoding)
Attempts to parse a string representing a collection of features using OGR.
static QgsFields stringToFields(const QString &string, QTextCodec *encoding)
Attempts to retrieve the fields from a string representing a collection of features using OGR.
QgsRelationManager * relationManager
static QgsProject * instance()
Returns the QgsProject singleton instance.
A rectangle specified with double values.
QList< QgsRelation > referencedRelations(const QgsVectorLayer *layer=nullptr) const
Gets all relations where this layer is the referenced part (i.e.
static QMetaType::Type variantTypeToMetaType(QVariant::Type variantType)
Converts a QVariant::Type to a QMetaType::Type.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.
Represents a vector layer which manages a vector based dataset.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const final
Queries the layer for features specified in request.
static Qgis::WkbType flatType(Qgis::WkbType type)
Returns the flat type for a WKB type.
double qgsRound(double number, int places)
Returns a double number, rounded (as close as possible) to the specified number of places.
QList< QgsFeature > QgsFeatureList
std::unique_ptr< QgsPoint > parsePointFromGeoJson(const json &coords)
std::unique_ptr< QgsPolygon > parsePolygonFromGeoJson(const json &coords)
std::unique_ptr< QgsAbstractGeometry > parseGeometryFromGeoJson(const json &geometry)
std::unique_ptr< QgsLineString > parseLineStringFromGeoJson(const json &coords)
#define QgsDebugError(str)