Skip to content

Commit f9eba37

Browse files
committedJan 28, 2019
Add layers tag and their names in exported json
1 parent e4cdd7c commit f9eba37

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed
 

‎src/core/qgsjsonutils.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,11 @@ QString QgsJsonExporter::exportFeatures( const QgsFeatureList &features ) const
237237
featureJSON << exportFeature( feature );
238238
}
239239

240-
return QStringLiteral( "{ \"type\": \"FeatureCollection\",\n \"features\":[\n%1\n]}" ).arg( featureJSON.join( QStringLiteral( ",\n" ) ) );
240+
QString layerName;
241+
if ( mIncludeLayerName )
242+
layerName.append( QStringLiteral( "\n \"name\": \"%1\",\n" ).arg( mLayer->name() ) );
243+
244+
return QStringLiteral( "{%1 \"type\": \"FeatureCollection\",\n \"features\":[\n%2\n]}" ).arg( layerName, featureJSON.join( QStringLiteral( ",\n" ) ) );
241245
}
242246

243247

‎src/core/qgsjsonutils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ class CORE_EXPORT QgsJsonExporter
7575
*/
7676
bool includeGeometry() const { return mIncludeGeometry; }
7777

78+
void setIncludeLayerName( bool includeLayerName ) { mIncludeLayerName = includeLayerName; }
79+
80+
bool includeLayerName() const { return mIncludeLayerName; }
81+
7882
/**
7983
* Sets whether to include attributes in the JSON exports.
8084
* \param includeAttributes set to false to prevent attribute inclusion
@@ -223,6 +227,7 @@ class CORE_EXPORT QgsJsonExporter
223227

224228
QgsCoordinateTransform mTransform;
225229

230+
bool mIncludeLayerName = false;
226231
};
227232

228233
/**

‎src/server/services/wms/qgswmsrenderer.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,6 +2281,8 @@ namespace QgsWms
22812281
QByteArray QgsRenderer::convertFeatureInfoToJson( const QList<QgsMapLayer *> &layers, const QDomDocument &doc ) const
22822282
{
22832283
QString json;
2284+
json.append( "{" );
2285+
json.append( ( "\"layers\":[" ) );
22842286

22852287
const bool withGeometry = ( QgsServerProjectUtils::wmsFeatureInfoAddWktGeometry( *mProject ) && mWmsParameters.withGeometry() );
22862288

@@ -2338,13 +2340,20 @@ namespace QgsWms
23382340
QgsJsonExporter exporter( vl );
23392341
exporter.setAttributes( attributes );
23402342
exporter.setIncludeGeometry( withGeometry );
2343+
exporter.setIncludeLayerName( true );
2344+
2345+
if ( i > 0 )
2346+
json.append( "," );
2347+
23412348
json.append( exporter.exportFeatures( features ) );
23422349
}
23432350
else // raster layer
23442351
{
23452352
}
23462353
}
23472354

2355+
json.append( ( "]}" ) );
2356+
23482357
return json.toUtf8();
23492358
}
23502359

0 commit comments

Comments
 (0)
Please sign in to comment.