Skip to content

Commit

Permalink
Add layers tag and their names in exported json
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Jan 28, 2019
1 parent e4cdd7c commit f9eba37
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/core/qgsjsonutils.cpp
Expand Up @@ -237,7 +237,11 @@ QString QgsJsonExporter::exportFeatures( const QgsFeatureList &features ) const
featureJSON << exportFeature( feature );
}

return QStringLiteral( "{ \"type\": \"FeatureCollection\",\n \"features\":[\n%1\n]}" ).arg( featureJSON.join( QStringLiteral( ",\n" ) ) );
QString layerName;
if ( mIncludeLayerName )
layerName.append( QStringLiteral( "\n \"name\": \"%1\",\n" ).arg( mLayer->name() ) );

return QStringLiteral( "{%1 \"type\": \"FeatureCollection\",\n \"features\":[\n%2\n]}" ).arg( layerName, featureJSON.join( QStringLiteral( ",\n" ) ) );
}


Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsjsonutils.h
Expand Up @@ -75,6 +75,10 @@ class CORE_EXPORT QgsJsonExporter
*/
bool includeGeometry() const { return mIncludeGeometry; }

void setIncludeLayerName( bool includeLayerName ) { mIncludeLayerName = includeLayerName; }

bool includeLayerName() const { return mIncludeLayerName; }

/**
* Sets whether to include attributes in the JSON exports.
* \param includeAttributes set to false to prevent attribute inclusion
Expand Down Expand Up @@ -223,6 +227,7 @@ class CORE_EXPORT QgsJsonExporter

QgsCoordinateTransform mTransform;

bool mIncludeLayerName = false;
};

/**
Expand Down
9 changes: 9 additions & 0 deletions src/server/services/wms/qgswmsrenderer.cpp
Expand Up @@ -2281,6 +2281,8 @@ namespace QgsWms
QByteArray QgsRenderer::convertFeatureInfoToJson( const QList<QgsMapLayer *> &layers, const QDomDocument &doc ) const
{
QString json;
json.append( "{" );
json.append( ( "\"layers\":[" ) );

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

Expand Down Expand Up @@ -2338,13 +2340,20 @@ namespace QgsWms
QgsJsonExporter exporter( vl );
exporter.setAttributes( attributes );
exporter.setIncludeGeometry( withGeometry );
exporter.setIncludeLayerName( true );

if ( i > 0 )
json.append( "," );

json.append( exporter.exportFeatures( features ) );
}
else // raster layer
{
}
}

json.append( ( "]}" ) );

return json.toUtf8();
}

Expand Down

0 comments on commit f9eba37

Please sign in to comment.