53 const QString format = request.
parameters().value( QStringLiteral(
"FORMAT" ), QStringLiteral(
"PNG" ) );
58 switch ( outputFormat )
64 contentType =
"image/png";
68 contentType =
"image/jpeg";
73 QStringLiteral(
"Output format '%1' is not supported in the GetLegendGraphic request" ).arg( format ) );
78 #ifdef HAVE_SERVER_PYTHON_PLUGINS 84 QByteArray content = cacheManager->
getCachedImage( project, request, accessControl );
85 if ( !content.isEmpty() && image.loadFromData( content ) )
87 response.
setHeader( QStringLiteral(
"Content-Type" ), contentType );
88 image.save( response.
io(), qPrintable( saveFormat ) );
96 std::unique_ptr<QgsLayerTree> tree(
layerTree( context ) );
97 std::unique_ptr<QgsLayerTreeModel> model(
legendModel( context, *tree.get() ) );
100 std::unique_ptr<QImage> result;
101 if ( !parameters.
rule().isEmpty() )
116 #ifdef HAVE_SERVER_PYTHON_PLUGINS 119 QByteArray content = response.
data();
120 if ( !content.isEmpty() )
121 cacheManager->
setCachedImage( &content, project, request, accessControl );
127 throw QgsException( QStringLiteral(
"Failed to compute GetLegendGraphics image" ) );
139 if ( parameters.
format() == QgsWmsParameters::Format::NONE )
145 if ( ! parameters.
bbox().isEmpty() && !parameters.
rule().isEmpty() )
148 QStringLiteral(
"BBOX parameter cannot be combined with RULE." ) );
159 if ( ! parameters.
bbox().isEmpty() )
163 QString
crs = parameters.
crs();
164 if ( crs.compare( QStringLiteral(
"CRS:84" ), Qt::CaseInsensitive ) == 0 )
174 const double ratio { bbox.width() / bbox.height() };
175 int defaultHeight {
static_cast<int>( 800 / ratio ) };
176 if ( parameters.
width().isEmpty() && parameters.
srcWidth().isEmpty() )
180 if ( parameters.
height().isEmpty() && parameters.
srcHeight().isEmpty() )
192 std::unique_ptr<QgsMapSettings> mapSettings;
200 if ( ! parameters.
bbox().isEmpty() )
202 mapSettings = qgis::make_unique<QgsMapSettings>();
203 mapSettings->setOutputSize( context.
mapSize() );
206 QString
crs = parameters.
crs();
207 if ( crs.compare( QStringLiteral(
"CRS:84" ), Qt::CaseInsensitive ) == 0 )
217 mapSettings->setDestinationCrs( outputCrs );
218 mapSettings->setExtent( bbox );
223 model->setLegendFilterByMap( mapSettings.get() );
227 if ( parameters.
rule().isEmpty() )
229 QList<QgsLayerTreeNode *> children = tree.
children();
260 return model.release();
265 std::unique_ptr<QgsLayerTree> tree(
new QgsLayerTree() );
267 QList<QgsVectorLayerFeatureCounter *> counters;
274 if ( !ml->title().isEmpty() )
279 const QString
property = QStringLiteral(
"showFeatureCount" );
290 counters.append( counter );
298 return tree.release();
307 if ( node->data( Qt::DisplayRole ).toString().compare( rule ) == 0 )
static void setNodeLegendStyle(QgsLayerTreeNode *node, QgsLegendStyle::Style style)
Sets the style of a node.
virtual void setHeader(const QString &key, const QString &value)=0
Set Header entry Add Header entry to the response Note that it is usually an error to set Header afte...
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
A rectangle specified with double values.
Base class for all map layer types.
virtual bool isEmbeddedInParent() const
QList< QgsMapLayer * > layersToRender() const
Returns a list of all layers to actually render according to the current configuration.
QgsVectorLayerFeatureCounter * countSymbolFeatures()
Count features for symbols.
void setUseLayerName(bool use=true)
Uses the layer's name if use is true, or the name manually set if false.
QgsWmsParameters parameters() const
Returns WMS parameters.
QgsRectangle bboxAsRectangle() const
Returns BBOX as a rectangle if defined and valid.
bool setCachedImage(const QByteArray *img, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Updates or inserts the image in cache like tiles.
QString srcWidth() const
Returns SRCWIDTH parameter or an empty string if not defined.
Counts the features in a QgsVectorLayer in task.
QgsLayerTree * layerTree(const QgsWmsRenderContext &context)
bool waitForFinished(int timeout=30000)
Blocks the current thread until the task finishes or a maximum of timeout milliseconds.
Exception thrown in case of malformed request.
const QgsCoordinateReferenceSystem & crs
int imageQuality() const
Returns the image quality to use for rendering according to the current configuration.
void setFlag(Flag flag, bool on=true)
Sets or unsets a rendering flag according to the on value.
void setParameters(const QgsWmsParameters ¶meters)
Sets WMS parameters.
QString bbox() const
Returns BBOX if defined or an empty string.
bool ruleLabelAsBool() const
Returns RULELABEL as a bool.
Exception class for WMS service exceptions.
bool showFeatureCountAsBool() const
Returns SHOWFEATURECOUNT as a bool.
void writeImage(QgsServerResponse &response, QImage &img, const QString &formatStr, int imageQuality)
Write image response.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
QStringList allLayersNickname() const
Returns nickname of layers found in LAYER and LAYERS parameters.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
Namespace with helper functions for layer tree operations.
QString crs() const
Returns CRS or an empty string if none is defined.
bool isEmpty() const
Returns true if the rectangle is empty.
QgsServerRequest::Parameters parameters() const
Returns a map of query parameters with keys converted to uppercase.
A class to describe the version of a project.
Provides an interface to retrieve and manipulate WMS parameters received from the client...
void configureLayers(QList< QgsMapLayer *> &layers, QgsMapSettings *settings=nullptr)
Configures layers for rendering optionally considering the map settings.
A helper class that centralizes caches accesses given by all the server cache filter plugins...
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
This class is a base class for nodes in a layer tree.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
QgsProjectVersion versionAsNumber() const
Returns VERSION parameter if defined or its default value.
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
Format format() const
Returns format.
void writeGetLegendGraphics(QgsServerInterface *serverIface, const QgsProject *project, const QString &, const QgsServerRequest &request, QgsServerResponse &response)
Output GetLegendGRaphics response.
void set(QgsWmsParameter::Name name, const QVariant &value)
Sets a parameter value thanks to its name.
QSize mapSize(bool aspectRatio=true) const
Returns the size (in pixels) of the map to render, according to width and height WMS parameters as we...
void setName(const QString &n) override
Sets the layer's name.
bool layerTitleAsBool() const
Returns LAYERTITLE as a bool or its default value if not defined.
Median cut implementation.
double scaleDenominator() const
Returns the scale denominator to use for rendering according to the current configuration.
virtual QByteArray data() const =0
Gets the data written so far.
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins...
Special style, item is hidden including margins around.
virtual void setEmbeddedInParent(bool embedded)
Map renderer for WMS requests.
static QgsCoordinateReferenceSystem fromOgcWmsCrs(const QString &ogcCrs)
Creates a CRS from a given OGC WMS-format Coordinate Reference System string.
QgsLayerTreeModel * legendModel(const QgsWmsRenderContext &context, QgsLayerTree &tree)
QgsLayerTree * rootGroup() const
Returns pointer to the root node of the layer tree. Always a non nullptr value.
virtual QIODevice * io()=0
Returns the underlying QIODevice.
Rendering context for the WMS renderer.
QString width() const
Returns WIDTH parameter or an empty string if not defined.
bool hasAxisInverted() const
Returns whether axis is inverted (e.g., for WMS 1.3) for the CRS.
This class represents a coordinate reference system (CRS).
virtual QgsServerCacheManager * cacheManager() const =0
Gets the registered server cache filters.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
const QgsCoordinateReferenceSystem & outputCrs
QList< QgsLayerTreeModelLegendNode * > layerLegendNodes(QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent=false)
Returns filtered list of active legend nodes attached to a particular layer node (by default it retur...
A helper class that centralizes restrictions given by all the access control filter plugins...
QString height() const
Returns HEIGHT parameter or an empty string if not defined.
ImageOutputFormat
Supported image output format.
QString rule() const
Returns RULE parameter or an empty string if none is defined.
QgsServerResponse Class defining response interface passed to services QgsService::executeRequest() m...
virtual QgsAccessControl * accessControls() const =0
Gets the registered access control filters.
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
void checkParameters(QgsWmsParameters ¶meters)
checkParameters checks request parameters and sets SRCHEIGHT and SRCWIDTH to default values in case B...
QString srcHeight() const
Returns SRCHEIGHT parameter or an empty string if not defined.
Represents a vector layer which manages a vector based data sets.
Defines a QGIS exception class.
ImageOutputFormat parseImageFormat(const QString &format)
Parse image format parameter.
virtual void setUserLabel(const QString &userLabel)
void setCustomProperty(const QString &key, const QVariant &value)
Sets a custom property for the node. Properties are stored in a map and saved in project file...
Layer tree node points to a map layer.
QImage * getLegendGraphics(QgsLayerTreeModel &model)
Returns the map legend as an image (or nullptr in case of error).
QByteArray getCachedImage(const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Returns cached image (or 0 if image not in cache) like tiles.