34 #include <QJsonObject>
35 #include <QJsonDocument>
44 QgsWmsParameters parameters( QUrlQuery( request.
url() ) );
51 QgsWmsRenderContext context( project, serverIface );
54 context.setParameters( parameters );
63 QString imageSaveFormat;
64 QString imageContentType;
67 imageContentType =
"image/png";
68 imageSaveFormat =
"PNG";
70 else if ( format == QgsWmsParameters::Format::JPG )
72 imageContentType =
"image/jpeg";
73 imageSaveFormat =
"JPEG";
75 else if ( format == QgsWmsParameters::Format::NONE )
84 imageContentType =
"image/png";
85 imageSaveFormat =
"PNG";
92 if ( format == QgsWmsParameters::Format::NONE )
95 QStringLiteral(
"Output format '%1' is not supported in the GetLegendGraphic request" ).arg( parameters.formatAsString() ) );
99 #ifdef HAVE_SERVER_PYTHON_PLUGINS
102 if ( cacheManager && !imageSaveFormat.isEmpty() )
105 QByteArray content = cacheManager->
getCachedImage( project, request, accessControl );
106 if ( !content.isEmpty() && image.loadFromData( content ) )
108 response.
setHeader( QStringLiteral(
"Content-Type" ), imageContentType );
109 image.save( response.
io(), qPrintable( imageSaveFormat ) );
114 QgsRenderer renderer( context );
117 std::unique_ptr<QgsLayerTree> tree(
layerTree( context ) );
118 std::unique_ptr<QgsLayerTreeModel> model(
legendModel( context, *tree.get() ) );
121 if ( format == QgsWmsParameters::Format::JSON )
124 if ( !parameters.rule().isEmpty() )
127 QStringLiteral(
"RULE cannot be used with JSON format" ) );
131 result = renderer.getLegendGraphicsAsJson( *model.get() );
134 response.
setHeader( QStringLiteral(
"Content-Type" ), parameters.formatAsString() );
135 QJsonDocument doc( result );
136 response.
write( doc.toJson( QJsonDocument::Compact ) );
140 std::unique_ptr<QImage> result;
141 if ( !parameters.rule().isEmpty() )
144 result.reset( renderer.getLegendGraphics( *node ) );
148 result.reset( renderer.getLegendGraphics( *model.get() ) );
153 writeImage( response, *result, parameters.formatAsString(), context.imageQuality() );
154 #ifdef HAVE_SERVER_PYTHON_PLUGINS
157 QByteArray content = response.
data();
158 if ( !content.isEmpty() )
159 cacheManager->
setCachedImage( &content, project, request, accessControl );
165 throw QgsException( QStringLiteral(
"Failed to compute GetLegendGraphics image" ) );
172 if ( parameters.allLayersNickname().isEmpty() )
178 if ( parameters.format() == QgsWmsParameters::Format::NONE )
184 if ( ! parameters.bbox().isEmpty() && !parameters.rule().isEmpty() )
187 QStringLiteral(
"BBOX parameter cannot be combined with RULE." ) );
190 if ( ! parameters.bbox().isEmpty() && parameters.bboxAsRectangle().isEmpty() )
198 if ( ! parameters.bbox().isEmpty() )
202 QString
crs = parameters.crs();
203 if (
crs.compare( QStringLiteral(
"CRS:84" ), Qt::CaseInsensitive ) == 0 )
213 const double ratio { bbox.width() / bbox.height() };
214 int defaultHeight {
static_cast<int>( 800 / ratio ) };
215 if ( parameters.width().isEmpty() && parameters.srcWidth().isEmpty() )
219 if ( parameters.height().isEmpty() && parameters.srcHeight().isEmpty() )
229 const QgsWmsParameters parameters = context.parameters();
231 std::unique_ptr<QgsMapSettings> mapSettings;
233 if ( context.scaleDenominator() > 0 )
235 model->setLegendFilterByScale( context.scaleDenominator() );
239 if ( ! parameters.bbox().isEmpty() )
241 mapSettings = qgis::make_unique<QgsMapSettings>();
242 mapSettings->setOutputSize( context.mapSize() );
245 QString
crs = parameters.crs();
246 if (
crs.compare( QStringLiteral(
"CRS:84" ), Qt::CaseInsensitive ) == 0 )
256 mapSettings->setDestinationCrs(
outputCrs );
257 mapSettings->setExtent( bbox );
258 QgsRenderer renderer( context );
259 QList<QgsMapLayer *> layers = context.layersToRender();
260 renderer.configureLayers( layers, mapSettings.get() );
261 mapSettings->setLayers( context.layersToRender() );
262 model->setLegendFilterByMap( mapSettings.get() );
266 if ( parameters.rule().isEmpty() )
268 QList<QgsLayerTreeNode *> children = tree.
children();
269 QString ruleLabel = parameters.ruleLabel();
280 if ( !parameters.ruleLabelAsBool() )
288 else if ( ruleLabel.compare( QStringLiteral(
"AUTO" ), Qt::CaseInsensitive ) == 0 )
300 return model.release();
305 std::unique_ptr<QgsLayerTree> tree(
new QgsLayerTree() );
307 QList<QgsVectorLayerFeatureCounter *> counters;
314 if ( !ml->title().isEmpty() )
318 const bool showFeatureCount = context.parameters().showFeatureCountAsBool();
319 const QString
property = QStringLiteral(
"showFeatureCount" );
330 counters.append( counter );
338 return tree.release();
347 if ( node->
data( Qt::DisplayRole ).toString().compare( rule ) == 0 )