35 #include <QJsonObject>
36 #include <QJsonDocument>
41 const QgsWmsRequest &request,
45 QgsWmsParameters parameters = request.wmsParameters();
52 QgsWmsRenderContext context( project, serverIface );
55 context.setParameters( parameters );
64 QString imageSaveFormat;
65 QString imageContentType;
68 imageContentType =
"image/png";
69 imageSaveFormat =
"PNG";
71 else if ( format == QgsWmsParameters::Format::JPG )
73 imageContentType =
"image/jpeg";
74 imageSaveFormat =
"JPEG";
76 else if ( format == QgsWmsParameters::Format::NONE )
85 imageContentType =
"image/png";
86 imageSaveFormat =
"PNG";
93 if ( format == QgsWmsParameters::Format::NONE )
96 QStringLiteral(
"Output format '%1' is not supported in the GetLegendGraphic request" ).arg( parameters.formatAsString() ) );
100 #ifdef HAVE_SERVER_PYTHON_PLUGINS
103 if ( cacheManager && !imageSaveFormat.isEmpty() )
106 const QByteArray content = cacheManager->
getCachedImage( project, request, accessControl );
107 if ( !content.isEmpty() && image.loadFromData( content ) )
109 response.
setHeader( QStringLiteral(
"Content-Type" ), imageContentType );
110 image.save( response.
io(), qPrintable( imageSaveFormat ) );
115 QgsRenderer renderer( context );
118 std::unique_ptr<QgsLayerTree> tree(
layerTree( context ) );
119 const std::unique_ptr<QgsLayerTreeModel> model(
legendModel( context, *tree.get() ) );
122 if ( format == QgsWmsParameters::Format::JSON )
125 if ( !parameters.rule().isEmpty() )
128 QStringLiteral(
"RULE cannot be used with JSON format" ) );
132 result = renderer.getLegendGraphicsAsJson( *model.get() );
135 response.
setHeader( QStringLiteral(
"Content-Type" ), parameters.formatAsString() );
136 const QJsonDocument doc( result );
137 response.
write( doc.toJson( QJsonDocument::Compact ) );
141 std::unique_ptr<QImage> result;
142 if ( !parameters.rule().isEmpty() )
147 throw QgsException( QStringLiteral(
"Could not get a legend node for the requested RULE" ) );
149 result.reset( renderer.getLegendGraphics( *node ) );
153 result.reset( renderer.getLegendGraphics( *model.get() ) );
158 writeImage( response, *result, parameters.formatAsString(), context.imageQuality() );
159 #ifdef HAVE_SERVER_PYTHON_PLUGINS
162 const QByteArray content = response.
data();
163 if ( !content.isEmpty() )
164 cacheManager->
setCachedImage( &content, project, request, accessControl );
170 throw QgsException( QStringLiteral(
"Failed to compute GetLegendGraphics image" ) );
177 if ( parameters.allLayersNickname().isEmpty() )
183 if ( parameters.format() == QgsWmsParameters::Format::NONE )
189 if ( ! parameters.bbox().isEmpty() && !parameters.rule().isEmpty() )
192 QStringLiteral(
"BBOX parameter cannot be combined with RULE." ) );
195 if ( ! parameters.bbox().isEmpty() && parameters.bboxAsRectangle().isEmpty() )
203 if ( ! parameters.bbox().isEmpty() )
207 const QString
crs = parameters.crs();
208 if (
crs.compare( QStringLiteral(
"CRS:84" ), Qt::CaseInsensitive ) == 0 )
218 const double ratio { bbox.width() / bbox.height() };
219 const int defaultHeight {
static_cast<int>( 800 / ratio ) };
220 if ( parameters.width().isEmpty() && parameters.srcWidth().isEmpty() )
224 if ( parameters.height().isEmpty() && parameters.srcHeight().isEmpty() )
234 const QgsWmsParameters parameters = context.parameters();
236 std::unique_ptr<QgsMapSettings> mapSettings;
238 if ( context.scaleDenominator() > 0 )
240 model->setLegendFilterByScale( context.scaleDenominator() );
244 if ( ! parameters.bbox().isEmpty() )
246 mapSettings = std::make_unique<QgsMapSettings>();
247 mapSettings->setOutputSize( context.mapSize() );
250 const QString
crs = parameters.crs();
251 if (
crs.compare( QStringLiteral(
"CRS:84" ), Qt::CaseInsensitive ) == 0 )
261 mapSettings->setDestinationCrs(
outputCrs );
262 mapSettings->setExtent( bbox );
263 QgsRenderer renderer( context );
264 QList<QgsMapLayer *> layers = context.layersToRender();
265 renderer.configureLayers( layers, mapSettings.get() );
266 mapSettings->setLayers( context.layersToRender() );
267 model->setLegendFilterByMap( mapSettings.get() );
271 if ( parameters.rule().isEmpty() )
273 const QList<QgsLayerTreeNode *> children = tree.
children();
274 const QString ruleLabel = parameters.ruleLabel();
285 if ( !parameters.ruleLabelAsBool() )
293 else if ( ruleLabel.compare( QStringLiteral(
"AUTO" ), Qt::CaseInsensitive ) == 0 )
305 return model.release();
310 std::unique_ptr<QgsLayerTree> tree(
new QgsLayerTree() );
312 QList<QgsVectorLayerFeatureCounter *> counters;
319 if ( !ml->title().isEmpty() )
323 const bool showFeatureCount = context.parameters().showFeatureCountAsBool();
324 const QString
property = QStringLiteral(
"showFeatureCount" );
335 counters.append( counter );
343 return tree.release();
352 if ( node->
data( Qt::DisplayRole ).toString().compare( rule ) == 0 )