QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
|
Go to the documentation of this file.
31 #include <QJsonDocument>
34 #include <nlohmann/json.hpp>
38 , mLayer( vectorLayer )
75 const QVariant &
id,
int indent )
const
84 {
"type",
"Feature" },
89 auto intId =
id.toLongLong( &ok );
92 featureJson[
"id"] = intId;
96 featureJson[
"id"] =
id.toString().toStdString();
101 featureJson[
"id"] =
nullptr;
105 featureJson[
"id"] = feature.
id();
109 if ( !geom.
isNull() && mIncludeGeometry )
128 featureJson[
"bbox" ] =
136 featureJson[
"geometry" ] = geom.
asJsonObject( mPrecision );
140 featureJson[
"geometry" ] =
nullptr;
144 int attributeCounter { 0 };
146 if ( mIncludeAttributes || !extraProperties.isEmpty() )
149 if ( mIncludeAttributes )
153 QStringList formattersAllowList;
154 formattersAllowList << QStringLiteral(
"KeyValue" )
155 << QStringLiteral(
"List" )
156 << QStringLiteral(
"ValueRelation" )
157 << QStringLiteral(
"ValueMap" );
159 for (
int i = 0; i < fields.
count(); ++i )
161 if ( ( !mAttributeIndexes.isEmpty() && !mAttributeIndexes.contains( i ) ) || mExcludedAttributeIndexes.contains( i ) )
170 if ( formattersAllowList.contains( fieldFormatter->
id() ) )
174 QString name = fields.
at( i ).
name();
175 if ( mAttributeDisplayName )
177 name = mLayer->attributeDisplayName( i );
184 if ( !extraProperties.isEmpty() )
186 QVariantMap::const_iterator it = extraProperties.constBegin();
187 for ( ; it != extraProperties.constEnd(); ++it )
195 if ( mLayer && mIncludeRelatedAttributes )
198 for (
const auto &relation : std::as_const( relations ) )
203 json relatedFeatureAttributes;
207 QVector<QVariant> attributeWidgetCaches;
214 attributeWidgetCaches.append( fieldFormatter->
createCache( childLayer, fieldIndex, setup.
config() ) );
223 properties[ relation.name().toStdString() ] = relatedFeatureAttributes;
228 featureJson[
"properties" ] = properties;
241 {
"type",
"FeatureCollection" },
242 {
"features", json::array() }
244 for (
const QgsFeature &feature : std::as_const( features ) )
258 encoding = QTextCodec::codecForName(
"UTF-8" );
266 encoding = QTextCodec::codecForName(
"UTF-8" );
273 if ( value.isNull() )
274 return QStringLiteral(
"null" );
276 switch ( value.type() )
280 case QVariant::LongLong:
281 case QVariant::ULongLong:
282 case QVariant::Double:
283 return value.toString();
286 return value.toBool() ?
"true" :
"false";
288 case QVariant::StringList:
291 return QString::fromUtf8( QJsonDocument::fromVariant( value ).toJson( QJsonDocument::Compact ) );
294 case QVariant::String:
295 QString v = value.toString()
296 .replace(
'\\', QLatin1String(
"\\\\" ) )
297 .replace(
'"', QLatin1String(
"\\\"" ) )
298 .replace(
'\r', QLatin1String(
"\\r" ) )
299 .replace(
'\b', QLatin1String(
"\\b" ) )
300 .replace(
'\t', QLatin1String(
"\\t" ) )
301 .replace(
'/', QLatin1String(
"\\/" ) )
302 .replace(
'\n', QLatin1String(
"\\n" ) );
304 return v.prepend(
'"' ).append(
'"' );
312 for (
int i = 0; i < fields.
count(); ++i )
315 attrs += QLatin1String(
",\n" );
324 val = fieldFormatter->
representValue( layer, i, setup.
config(), attributeWidgetCaches.count() >= i ? attributeWidgetCaches.at( i ) : QVariant(), val );
329 return attrs.prepend(
'{' ).append(
'}' );
334 QString errorMessage;
338 const auto jObj(
json::parse( json.toStdString() ) );
339 if ( ! jObj.is_array() )
341 throw json::parse_error::create( 0, 0, QStringLiteral(
"JSON value must be an array" ).toStdString() );
343 for (
const auto &item : jObj )
347 if ( item.is_number_integer() )
351 else if ( item.is_number_unsigned() )
353 v = item.get<
unsigned>();
355 else if ( item.is_number_float() )
358 v = item.get<
double>();
360 else if ( item.is_string() )
362 v = QString::fromStdString( item.get<std::string>() );
364 else if ( item.is_boolean() )
366 v = item.get<
bool>();
368 else if ( item.is_null() )
371 v = QVariant( type == QVariant::Type::Invalid ? QVariant::Type::Int : type );
375 if ( type != QVariant::Invalid )
377 if ( ! v.convert(
static_cast<int>( type ) ) )
379 QgsLogger::warning( QStringLiteral(
"Cannot convert json array element to specified type, ignoring: %1" ).arg( v.toString() ) );
383 result.push_back( v );
388 result.push_back( v );
392 catch ( json::parse_error &ex )
394 errorMessage = ex.what();
395 QgsLogger::warning( QStringLiteral(
"Cannot parse json (%1): %2" ).arg( ex.what(), json ) );
403 if ( val.isNull() || ! val.isValid() )
408 if ( val.type() == QVariant::Type::Map )
410 const QVariantMap &vMap = val.toMap();
411 json jMap = json::object();
412 for (
auto it = vMap.constBegin(); it != vMap.constEnd(); it++ )
418 else if ( val.type() == QVariant::Type::List || val.type() == QVariant::Type::StringList )
420 const QVariantList &vList = val.toList();
421 json jList = json::array();
422 for (
const auto &v : vList )
430 switch ( val.userType() )
433 case QMetaType::UInt:
434 case QMetaType::LongLong:
435 case QMetaType::ULongLong:
436 j = val.toLongLong();
438 case QMetaType::Double:
439 case QMetaType::Float:
442 case QMetaType::Bool:
445 case QMetaType::QByteArray:
446 j = val.toByteArray().toBase64().toStdString();
449 j = val.toString().toStdString();
459 const QVariant res =
parseJson( jsonString, error );
461 if ( !error.isEmpty() )
464 QString::fromStdString( jsonString ) ) );
472 bool isPrimitive =
true;
474 std::function<QVariant( json )> _parser { [ & ]( json jObj ) -> QVariant {
476 if ( jObj.is_array() )
479 QVariantList results;
480 results.reserve( jObj.size() );
481 for (
const auto &item : jObj )
483 results.push_back( _parser( item ) );
487 else if ( jObj.is_object() )
491 for (
const auto &item : jObj.items() )
493 const auto key { QString::fromStdString( item.key() ) };
494 const auto value { _parser( item.value() ) };
495 results[ key ] = value;
501 if ( jObj.is_number_integer() )
503 result = jObj.get<
int>();
505 else if ( jObj.is_number_unsigned() )
507 result = jObj.get<
unsigned>();
509 else if ( jObj.is_boolean() )
511 result = jObj.get<
bool>();
513 else if ( jObj.is_number_float() )
516 result = jObj.get<
double>();
518 else if ( jObj.is_string() )
520 if ( isPrimitive && jObj.get<std::string>().length() == 0 )
522 result = QString::fromStdString( jObj.get<std::string>() ).append(
"\"" ).insert( 0,
"\"" );
526 result = QString::fromStdString( jObj.get<std::string>() );
529 else if ( jObj.is_null() )
544 catch ( json::parse_error &ex )
546 error = QString::fromStdString( ex.what() );
553 return parseJson( jsonString.toStdString() );
560 for (
int i = 0; i < fields.
count(); ++i )
569 val = fieldFormatter->
representValue( layer, i, setup.
config(), attributeWidgetCaches.count() >= i ? attributeWidgetCaches.at( i ) : QVariant(), val );
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QgsRelationManager relationManager
QgsCoordinateReferenceSystem crs
json exportFeaturesToJsonObject(const QgsFeatureList &features) const
Returns a JSON object representation of a list of features (feature collection).
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
QgsVectorLayer * vectorLayer() const
Returns the associated vector layer, if set.
static QgsFields stringToFields(const QString &string, QTextCodec *encoding=nullptr)
Attempts to retrieve the fields from a GeoJSON string representing a collection of features.
const QgsCoordinateReferenceSystem & crs
static Type flatType(Type type) SIP_HOLDGIL
Returns the flat type for a WKB type.
int count() const
Returns number of items.
Container of fields for a vector layer.
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
QString exportFeatures(const QgsFeatureList &features, int indent=-1) const
Returns a GeoJSON string representation of a list of features (feature collection).
json exportFeatureToJsonObject(const QgsFeature &feature, const QVariantMap &extraProperties=QVariantMap(), const QVariant &id=QVariant()) const
Returns a QJsonObject representation of a feature.
static QgsProject * instance()
Returns the QgsProject singleton instance.
static QgsFeatureList stringToFeatureList(const QString &string, const QgsFields &fields, QTextCodec *encoding)
Attempts to parse a string representing a collection of features using OGR.
A rectangle specified with double values.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
This class wraps a request for features to a vector layer (or directly its vector data provider).
static QgsFields stringToFields(const QString &string, QTextCodec *encoding)
Attempts to retrieve the fields from a string representing a collection of features using OGR.
Custom exception class for Coordinate Reference System related exceptions.
static json exportAttributesToJsonObject(const QgsFeature &feature, QgsVectorLayer *layer=nullptr, const QVector< QVariant > &attributeWidgetCaches=QVector< QVariant >())
Exports all attributes from a QgsFeature as a json object.
QList< QgsRelation > referencedRelations(const QgsVectorLayer *layer=nullptr) const
Gets all relations where this layer is the referenced part (i.e.
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.
double qgsRound(double number, int places)
Returns a double number, rounded (as close as possible) to the specified number of places.
QgsCoordinateReferenceSystem sourceCrs() const
Returns the source CRS for feature geometries.
QList< QgsFeature > QgsFeatureList
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
static void warning(const QString &msg)
Goes to qWarning.
static Q_INVOKABLE QString encodeValue(const QVariant &value)
Encodes a value to a JSON string representation, adding appropriate quotations and escaping where req...
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
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.
This class represents a coordinate reference system (CRS).
static QgsFieldFormatterRegistry * fieldFormatterRegistry()
Gets the registry of available field formatters.
virtual json asJsonObject(int precision=17) const
Exports the geometry to a json object.
void setVectorLayer(QgsVectorLayer *vectorLayer)
Sets the associated vector layer (required for related attribute export).
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
void setSourceCrs(const QgsCoordinateReferenceSystem &crs)
Sets the source CRS for feature geometries.
bool nextFeature(QgsFeature &f)
A geometry is the spatial representation of a feature.
Represents a vector layer which manages a vector based data sets.
@ Success
Operation succeeded.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
QgsJsonExporter(QgsVectorLayer *vectorLayer=nullptr, int precision=6)
Constructor for QgsJsonExporter.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
QgsEditorWidgetSetup editorWidgetSetup() const
Gets the editor widget setup for the field.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Wrapper for iterator of features from vector data provider or vector layer.
static json jsonFromVariant(const QVariant &v)
Converts a QVariant v to a json object.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
static QgsFeatureList stringToFeatureList(const QString &string, const QgsFields &fields=QgsFields(), QTextCodec *encoding=nullptr)
Attempts to parse a GeoJSON string to a collection of features.
static Q_INVOKABLE QVariantList parseArray(const QString &json, QVariant::Type type=QVariant::Invalid)
Parse a simple array (depth=1)
QgsSQLStatement::Node * parse(const QString &str, QString &parserErrorMsg, bool allowFragments)
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
static QVariant parseJson(const std::string &jsonString)
Converts JSON jsonString to a QVariant, in case of parsing error an invalid QVariant is returned and ...
Encapsulate a field in an attribute table or data source.