@@ -71,169 +71,7 @@ QgsCoordinateReferenceSystem QgsJsonExporter::sourceCrs() const
7171QString QgsJsonExporter::exportFeature ( const QgsFeature &feature, const QVariantMap &extraProperties,
7272 const QVariant &id ) const
7373{
74-
75- QString s = QStringLiteral ( " {\n \" type\" :\" Feature\" ,\n " );
76-
77- // ID
78- s += QStringLiteral ( " \" id\" :%1,\n " ).arg ( !id.isValid () ? QString::number ( feature.id () ) : QgsJsonUtils::encodeValue ( id ) );
79-
80- QgsGeometry geom = feature.geometry ();
81- if ( !geom.isNull () && mIncludeGeometry )
82- {
83- if ( mCrs .isValid () )
84- {
85- try
86- {
87- QgsGeometry transformed = geom;
88- if ( transformed.transform ( mTransform ) == 0 )
89- geom = transformed;
90- }
91- catch ( QgsCsException &cse )
92- {
93- Q_UNUSED ( cse );
94- }
95- }
96- QgsRectangle box = geom.boundingBox ();
97-
98- if ( QgsWkbTypes::flatType ( geom.wkbType () ) != QgsWkbTypes::Point )
99- {
100- s += QStringLiteral ( " \" bbox\" :[%1, %2, %3, %4],\n " ).arg ( qgsDoubleToString ( box.xMinimum (), mPrecision ),
101- qgsDoubleToString ( box.yMinimum (), mPrecision ),
102- qgsDoubleToString ( box.xMaximum (), mPrecision ),
103- qgsDoubleToString ( box.yMaximum (), mPrecision ) );
104- }
105- s += QLatin1String ( " \" geometry\" :\n " );
106- s += geom.asJson ( mPrecision );
107- s += QLatin1String ( " ,\n " );
108- }
109- else
110- {
111- s += QLatin1String ( " \" geometry\" :null,\n " );
112- }
113-
114- // build up properties element
115- QString properties;
116- int attributeCounter = 0 ;
117- if ( mIncludeAttributes || !extraProperties.isEmpty () )
118- {
119- // read all attribute values from the feature
120-
121- if ( mIncludeAttributes )
122- {
123- QgsFields fields = mLayer ? mLayer ->fields () : feature.fields ();
124- // List of formatters through we want to pass the values
125- QStringList formattersWhiteList;
126- formattersWhiteList << QStringLiteral ( " KeyValue" )
127- << QStringLiteral ( " List" )
128- << QStringLiteral ( " ValueRelation" )
129- << QStringLiteral ( " ValueMap" );
130-
131- for ( int i = 0 ; i < fields.count (); ++i )
132- {
133- if ( ( !mAttributeIndexes .isEmpty () && !mAttributeIndexes .contains ( i ) ) || mExcludedAttributeIndexes .contains ( i ) )
134- continue ;
135-
136- if ( attributeCounter > 0 )
137- properties += QLatin1String ( " ,\n " );
138- QVariant val = feature.attributes ().at ( i );
139-
140- if ( mLayer )
141- {
142- QgsEditorWidgetSetup setup = fields.at ( i ).editorWidgetSetup ();
143- QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry ()->fieldFormatter ( setup.type () );
144- if ( formattersWhiteList.contains ( fieldFormatter->id () ) )
145- val = fieldFormatter->representValue ( mLayer .data (), i, setup.config (), QVariant (), val );
146- }
147-
148- QString name = fields.at ( i ).name ();
149- if ( mAttributeDisplayName )
150- {
151- name = mLayer ->attributeDisplayName ( i );
152- }
153-
154- properties += QStringLiteral ( " \" %1\" :%2" ).arg ( name, QgsJsonUtils::encodeValue ( val ) );
155-
156- ++attributeCounter;
157- }
158- }
159-
160- if ( !extraProperties.isEmpty () )
161- {
162- QVariantMap::const_iterator it = extraProperties.constBegin ();
163- for ( ; it != extraProperties.constEnd (); ++it )
164- {
165- if ( attributeCounter > 0 )
166- properties += QLatin1String ( " ,\n " );
167-
168- properties += QStringLiteral ( " \" %1\" :%2" ).arg ( it.key (), QgsJsonUtils::encodeValue ( it.value () ) );
169-
170- ++attributeCounter;
171- }
172- }
173-
174- // related attributes
175- if ( mLayer && mIncludeRelatedAttributes )
176- {
177- QList< QgsRelation > relations = QgsProject::instance ()->relationManager ()->referencedRelations ( mLayer .data () );
178- const auto constRelations = relations;
179- for ( const QgsRelation &relation : constRelations )
180- {
181- if ( attributeCounter > 0 )
182- properties += QLatin1String ( " ,\n " );
183-
184- QgsFeatureRequest req = relation.getRelatedFeaturesRequest ( feature );
185- req.setFlags ( QgsFeatureRequest::NoGeometry );
186- QgsVectorLayer *childLayer = relation.referencingLayer ();
187- QString relatedFeatureAttributes;
188- if ( childLayer )
189- {
190- QgsFeatureIterator it = childLayer->getFeatures ( req );
191- QVector<QVariant> attributeWidgetCaches;
192- int fieldIndex = 0 ;
193- const QgsFields fields = childLayer->fields ();
194- for ( const QgsField &field : fields )
195- {
196- QgsEditorWidgetSetup setup = field.editorWidgetSetup ();
197- QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry ()->fieldFormatter ( setup.type () );
198- attributeWidgetCaches.append ( fieldFormatter->createCache ( childLayer, fieldIndex, setup.config () ) );
199- fieldIndex++;
200- }
201-
202- QgsFeature relatedFet;
203- int relationFeatures = 0 ;
204- while ( it.nextFeature ( relatedFet ) )
205- {
206- if ( relationFeatures > 0 )
207- relatedFeatureAttributes += QLatin1String ( " ,\n " );
208-
209- relatedFeatureAttributes += QgsJsonUtils::exportAttributes ( relatedFet, childLayer, attributeWidgetCaches );
210- relationFeatures++;
211- }
212- }
213- relatedFeatureAttributes.prepend ( ' [' ).append ( ' ]' );
214-
215- properties += QStringLiteral ( " \" %1\" :%2" ).arg ( relation.name (), relatedFeatureAttributes );
216- attributeCounter++;
217- }
218- }
219- }
220-
221- bool hasProperties = attributeCounter > 0 ;
222-
223- s += QLatin1String ( " \" properties\" :" );
224- if ( hasProperties )
225- {
226- // read all attribute values from the feature
227- s += " {\n " + properties + " \n }\n " ;
228- }
229- else
230- {
231- s += QLatin1String ( " null\n " );
232- }
233-
234- s += ' }' ;
235-
236- return s;
74+ return QString::fromStdString ( exportFeatureToJsonObject ( feature, extraProperties, id ).dump () );
23775}
23876
23977json QgsJsonExporter::exportFeatureToJsonObject ( const QgsFeature &feature, const QVariantMap &extraProperties, const QVariant &id ) const
0 commit comments