|
19 | 19 |
|
20 | 20 |
|
21 | 21 | #include "qgswmsutils.h"
|
| 22 | +#include "qgsjsonutils.h" |
22 | 23 | #include "qgswmsrenderer.h"
|
23 | 24 | #include "qgsfilterrestorer.h"
|
24 | 25 | #include "qgsexception.h"
|
@@ -1119,6 +1120,8 @@ namespace QgsWms
|
1119 | 1120 | ba = convertFeatureInfoToText( result );
|
1120 | 1121 | else if ( infoFormat == QgsWmsParameters::Format::HTML )
|
1121 | 1122 | ba = convertFeatureInfoToHtml( result );
|
| 1123 | + else if ( infoFormat == QgsWmsParameters::Format::JSON ) |
| 1124 | + ba = convertFeatureInfoToJson( layers, result ); |
1122 | 1125 | else
|
1123 | 1126 | ba = result.toByteArray();
|
1124 | 1127 |
|
@@ -2275,6 +2278,72 @@ namespace QgsWms
|
2275 | 2278 | return featureInfoString.toUtf8();
|
2276 | 2279 | }
|
2277 | 2280 |
|
| 2281 | + QByteArray QgsRenderer::convertFeatureInfoToJson( const QList<QgsMapLayer *> &layers, const QDomDocument &doc ) const |
| 2282 | + { |
| 2283 | + QString json; |
| 2284 | + |
| 2285 | + const QDomNodeList layerList = doc.elementsByTagName( QStringLiteral( "Layer" ) ); |
| 2286 | + for ( int i = 0; i < layerList.size(); ++i ) |
| 2287 | + { |
| 2288 | + const QDomElement layerElem = layerList.at( i ).toElement(); |
| 2289 | + const QString layerName = layerElem.attribute( QStringLiteral( "name" ) ); |
| 2290 | + |
| 2291 | + QgsMapLayer *layer; |
| 2292 | + for ( QgsMapLayer *l : layers ) |
| 2293 | + { |
| 2294 | + if ( layerNickname( *l ).compare( layerName ) == 0 ) |
| 2295 | + { |
| 2296 | + layer = l; |
| 2297 | + } |
| 2298 | + } |
| 2299 | + |
| 2300 | + if ( ! layer ) |
| 2301 | + continue; |
| 2302 | + |
| 2303 | + if ( layer->type() == QgsMapLayer::VectorLayer ) |
| 2304 | + { |
| 2305 | + QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ); |
| 2306 | + |
| 2307 | + // search features to export |
| 2308 | + QgsFeatureList features; |
| 2309 | + QgsAttributeList attributes; |
| 2310 | + const QDomNodeList featuresNode = layerElem.elementsByTagName( QStringLiteral( "Feature" ) ); |
| 2311 | + if ( !featuresNode.isEmpty() ) |
| 2312 | + { |
| 2313 | + for ( int j = 0; j < featuresNode.size(); ++j ) |
| 2314 | + { |
| 2315 | + const QDomElement featureNode = featuresNode.at( j ).toElement(); |
| 2316 | + const qint64 fid = featureNode.attribute( QStringLiteral( "id" ) ).toLongLong(); |
| 2317 | + const QgsFeature feature = vl->getFeature( fid ); |
| 2318 | + features.append( feature ); |
| 2319 | + |
| 2320 | + // search attributes to export |
| 2321 | + if ( not attributes.isEmpty() ) |
| 2322 | + continue; |
| 2323 | + |
| 2324 | + const QDomNodeList attributesNode = featureNode.elementsByTagName( QStringLiteral( "Attribute" ) ); |
| 2325 | + for ( int k = 0; k < attributesNode.size(); ++k ) |
| 2326 | + { |
| 2327 | + const QDomElement attributeElement = attributesNode.at( k ).toElement(); |
| 2328 | + const QString fieldName = attributeElement.attribute( QStringLiteral( "name" ) ); |
| 2329 | + |
| 2330 | + attributes << feature.fieldNameIndex( fieldName ); |
| 2331 | + } |
| 2332 | + } |
| 2333 | + } |
| 2334 | + |
| 2335 | + // export |
| 2336 | + QgsJsonExporter exporter( vl ); |
| 2337 | + json.append( exporter.exportFeatures( features ) ); |
| 2338 | + } |
| 2339 | + else // raster layer |
| 2340 | + { |
| 2341 | + } |
| 2342 | + } |
| 2343 | + |
| 2344 | + return json.toUtf8(); |
| 2345 | + } |
| 2346 | + |
2278 | 2347 | QDomElement QgsRenderer::createFeatureGML(
|
2279 | 2348 | QgsFeature *feat,
|
2280 | 2349 | QgsVectorLayer *layer,
|
|
0 commit comments