@@ -236,6 +236,136 @@ QString QgsJsonExporter::exportFeature( const QgsFeature &feature, const QVarian
236
236
return s;
237
237
}
238
238
239
+ QJsonObject QgsJsonExporter::exportFeatureV2 ( const QgsFeature &feature, const QVariantMap &extraProperties, const QVariant &id ) const
240
+ {
241
+ QJsonObject featureJson
242
+ {
243
+ { QStringLiteral ( " type" ), QStringLiteral ( " Feature" ) },
244
+ { QStringLiteral ( " id" ), ( ! id.isValid () ? QJsonValue ( feature.id () ) : QJsonValue::fromVariant ( id ) ) },
245
+ };
246
+
247
+ QgsGeometry geom = feature.geometry ();
248
+ if ( !geom.isNull () && mIncludeGeometry )
249
+ {
250
+ if ( mCrs .isValid () )
251
+ {
252
+ try
253
+ {
254
+ QgsGeometry transformed = geom;
255
+ if ( transformed.transform ( mTransform ) == 0 )
256
+ geom = transformed;
257
+ }
258
+ catch ( QgsCsException &cse )
259
+ {
260
+ Q_UNUSED ( cse );
261
+ }
262
+ }
263
+ QgsRectangle box = geom.boundingBox ();
264
+
265
+ if ( QgsWkbTypes::flatType ( geom.wkbType () ) != QgsWkbTypes::Point )
266
+ {
267
+ featureJson[ QStringLiteral ( " bbox" ) ] = QJsonArray ( { qgsDoubleToString ( box.xMinimum (), mPrecision ),
268
+ qgsDoubleToString ( box.yMinimum (), mPrecision ),
269
+ qgsDoubleToString ( box.xMaximum (), mPrecision ),
270
+ qgsDoubleToString ( box.yMaximum (), mPrecision ) } );
271
+ }
272
+ featureJson[ QStringLiteral ( " geometry" ) ] = QJsonDocument::fromJson ( geom.asJson ( mPrecision ).toLocal8Bit () ).object ();
273
+ }
274
+ else
275
+ {
276
+ featureJson[ QStringLiteral ( " geometry" ) ] = QJsonValue ();
277
+ }
278
+
279
+ // build up properties element
280
+ int attributeCounter { 0 };
281
+ QJsonObject properties;
282
+ if ( mIncludeAttributes || !extraProperties.isEmpty () )
283
+ {
284
+ // read all attribute values from the feature
285
+ if ( mIncludeAttributes )
286
+ {
287
+ QgsFields fields = mLayer ? mLayer ->fields () : feature.fields ();
288
+ // List of formatters through we want to pass the values
289
+ QStringList formattersWhiteList;
290
+ formattersWhiteList << QStringLiteral ( " KeyValue" )
291
+ << QStringLiteral ( " List" )
292
+ << QStringLiteral ( " ValueRelation" )
293
+ << QStringLiteral ( " ValueMap" );
294
+
295
+ for ( int i = 0 ; i < fields.count (); ++i )
296
+ {
297
+ if ( ( !mAttributeIndexes .isEmpty () && !mAttributeIndexes .contains ( i ) ) || mExcludedAttributeIndexes .contains ( i ) )
298
+ continue ;
299
+
300
+ QVariant val = feature.attributes ().at ( i );
301
+
302
+ if ( mLayer )
303
+ {
304
+ QgsEditorWidgetSetup setup = fields.at ( i ).editorWidgetSetup ();
305
+ QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry ()->fieldFormatter ( setup.type () );
306
+ if ( formattersWhiteList.contains ( fieldFormatter->id () ) )
307
+ val = fieldFormatter->representValue ( mLayer .data (), i, setup.config (), QVariant (), val );
308
+ }
309
+
310
+ QString name = fields.at ( i ).name ();
311
+ if ( mAttributeDisplayName )
312
+ {
313
+ name = mLayer ->attributeDisplayName ( i );
314
+ }
315
+ properties[ name ] = QJsonValue::fromVariant ( val );
316
+ attributeCounter++;
317
+ }
318
+ }
319
+
320
+ if ( !extraProperties.isEmpty () )
321
+ {
322
+ QVariantMap::const_iterator it = extraProperties.constBegin ();
323
+ for ( ; it != extraProperties.constEnd (); ++it )
324
+ {
325
+ properties[ it.key () ] = QJsonValue::fromVariant ( it.value () );
326
+ attributeCounter++;
327
+ }
328
+ }
329
+
330
+ // related attributes
331
+ if ( mLayer && mIncludeRelatedAttributes )
332
+ {
333
+ QList< QgsRelation > relations = QgsProject::instance ()->relationManager ()->referencedRelations ( mLayer .data () );
334
+ for ( const auto &relation : qgis::as_const ( relations ) )
335
+ {
336
+ QgsFeatureRequest req = relation.getRelatedFeaturesRequest ( feature );
337
+ req.setFlags ( QgsFeatureRequest::NoGeometry );
338
+ QgsVectorLayer *childLayer = relation.referencingLayer ();
339
+ QJsonArray relatedFeatureAttributes;
340
+ if ( childLayer )
341
+ {
342
+ QgsFeatureIterator it = childLayer->getFeatures ( req );
343
+ QVector<QVariant> attributeWidgetCaches;
344
+ int fieldIndex = 0 ;
345
+ const QgsFields fields { childLayer->fields () };
346
+ for ( const QgsField &field : fields )
347
+ {
348
+ QgsEditorWidgetSetup setup = field.editorWidgetSetup ();
349
+ QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry ()->fieldFormatter ( setup.type () );
350
+ attributeWidgetCaches.append ( fieldFormatter->createCache ( childLayer, fieldIndex, setup.config () ) );
351
+ fieldIndex++;
352
+ }
353
+ QgsFeature relatedFet;
354
+ while ( it.nextFeature ( relatedFet ) )
355
+ {
356
+ relatedFeatureAttributes += QgsJsonUtils::exportAttributes ( relatedFet, childLayer, attributeWidgetCaches );
357
+ }
358
+ }
359
+ properties[ relation.name () ] = relatedFeatureAttributes;
360
+ attributeCounter++;
361
+ }
362
+ }
363
+ }
364
+ // bool hasProperties = attributeCounter > 0;
365
+ featureJson[ QStringLiteral ( " properties" ) ] = properties;
366
+ return featureJson;
367
+ }
368
+
239
369
QString QgsJsonExporter::exportFeatures ( const QgsFeatureList &features ) const
240
370
{
241
371
QStringList featureJSON;
@@ -244,7 +374,6 @@ QString QgsJsonExporter::exportFeatures( const QgsFeatureList &features ) const
244
374
{
245
375
featureJSON << exportFeature ( feature );
246
376
}
247
-
248
377
return QStringLiteral ( " { \" type\" : \" FeatureCollection\" ,\n \" features\" :[\n %1\n ]}" ).arg ( featureJSON.join ( QStringLiteral ( " ,\n " ) ) );
249
378
}
250
379
@@ -351,3 +480,25 @@ QVariantList QgsJsonUtils::parseArray( const QString &json, QVariant::Type type
351
480
}
352
481
return result;
353
482
}
483
+
484
+
485
+ QJsonObject QgsJsonUtils::exportAttributesV2 ( const QgsFeature &feature, QgsVectorLayer *layer, const QVector<QVariant> &attributeWidgetCaches )
486
+ {
487
+ QgsFields fields = feature.fields ();
488
+ QJsonObject attrs;
489
+ for ( int i = 0 ; i < fields.count (); ++i )
490
+ {
491
+ QVariant val = feature.attributes ().at ( i );
492
+
493
+ if ( layer )
494
+ {
495
+ QgsEditorWidgetSetup setup = layer->fields ().at ( i ).editorWidgetSetup ();
496
+ QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry ()->fieldFormatter ( setup.type () );
497
+ if ( fieldFormatter != QgsApplication::fieldFormatterRegistry ()->fallbackFieldFormatter () )
498
+ val = fieldFormatter->representValue ( layer, i, setup.config (), attributeWidgetCaches.count () >= i ? attributeWidgetCaches.at ( i ) : QVariant (), val );
499
+ }
500
+
501
+ attrs.insert ( fields.at ( i ).name (), QJsonValue::fromVariant ( val ) );
502
+ }
503
+ return attrs;
504
+ }
0 commit comments