35#include <QJsonDocument>
63 QString imageSaveFormat;
64 QString imageContentType;
65 if ( format == QgsWmsParameters::Format::PNG )
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 )
83 format = QgsWmsParameters::Format::PNG;
84 imageContentType =
"image/png";
85 imageSaveFormat =
"PNG";
97 if ( format == QgsWmsParameters::Format::NONE )
100 QStringLiteral(
"Output format '%1' is not supported in the GetLegendGraphic request" ).arg( parameters.
formatAsString() ) );
104#ifdef HAVE_SERVER_PYTHON_PLUGINS
107 if ( cacheManager && !imageSaveFormat.isEmpty() )
110 const QByteArray content = cacheManager->
getCachedImage( project, request, accessControl );
111 if ( !content.isEmpty() && image.loadFromData( content ) )
113 response.
setHeader( QStringLiteral(
"Content-Type" ), imageContentType );
114 image.save( response.
io(), qPrintable( imageSaveFormat ) );
124 const std::unique_ptr<QgsLayerTreeModel> model(
legendModel( context, *tree.get() ) );
127 if ( format == QgsWmsParameters::Format::JSON )
130 if ( !parameters.
rule().isEmpty() )
133 QStringLiteral(
"RULE cannot be used with JSON format" ) );
141 const QJsonDocument doc( result );
142 response.
write( doc.toJson( QJsonDocument::Compact ) );
146 std::unique_ptr<QImage> result;
147 if ( !parameters.
rule().isEmpty() )
152 throw QgsException( QStringLiteral(
"Could not get a legend node for the requested RULE" ) );
164#ifdef HAVE_SERVER_PYTHON_PLUGINS
167 const QByteArray content = response.
data();
168 if ( !content.isEmpty() )
169 cacheManager->
setCachedImage( &content, project, request, accessControl );
175 throw QgsException( QStringLiteral(
"Failed to compute GetLegendGraphics image" ) );
188 if ( parameters.
format() == QgsWmsParameters::Format::NONE )
194 if ( ! parameters.
bbox().isEmpty() && !parameters.
rule().isEmpty() )
197 QStringLiteral(
"BBOX parameter cannot be combined with RULE." ) );
208 if ( ! parameters.
bbox().isEmpty() )
212 const QString
crs = parameters.
crs();
213 if (
crs.compare( QStringLiteral(
"CRS:84" ), Qt::CaseInsensitive ) == 0 )
223 const double ratio { bbox.width() / bbox.height() };
224 const int defaultHeight {
static_cast<int>( 800 / ratio ) };
225 if ( parameters.
width().isEmpty() && parameters.
srcWidth().isEmpty() )
229 if ( parameters.
height().isEmpty() && parameters.
srcHeight().isEmpty() )
241 std::unique_ptr<QgsMapSettings> mapSettings;
249 if ( ! parameters.
bbox().isEmpty() )
251 mapSettings = std::make_unique<QgsMapSettings>();
252 mapSettings->setOutputSize( context.
mapSize() );
255 const QString
crs = parameters.
crs();
256 if (
crs.compare( QStringLiteral(
"CRS:84" ), Qt::CaseInsensitive ) == 0 )
266 mapSettings->setDestinationCrs(
outputCrs );
267 mapSettings->setExtent( bbox );
272 model->setLegendFilterByMap( mapSettings.get() );
276 if ( parameters.
rule().isEmpty() )
278 const QList<QgsLayerTreeNode *> children = tree.
children();
279 const QString ruleLabel = parameters.
ruleLabel();
298 else if ( ruleLabel.compare( QStringLiteral(
"AUTO" ), Qt::CaseInsensitive ) == 0 )
310 return model.release();
315 std::unique_ptr<QgsLayerTree> tree(
new QgsLayerTree() );
317 QList<QgsVectorLayerFeatureCounter *> counters;
324 if ( !ml->title().isEmpty() )
329 const QString
property = QStringLiteral(
"showFeatureCount" );
332 if ( ml->type() != Qgis::LayerType::Vector || !showFeatureCount )
340 counters.append( counter );
348 return tree.release();
358 std::unique_ptr<QgsLayerTree> tree(
new QgsLayerTree() );
362 for (
int i = 0; i < layerNicknames.size(); ++i )
364 QString nickname = layerNicknames.at( i );
370 tree->addLayer( layer );
382 return tree.release();
391 if ( node->data( Qt::DisplayRole ).toString().compare( rule ) == 0 )
A helper class that centralizes restrictions given by all the access control filter plugins.
This class represents a coordinate reference system (CRS).
static QgsCoordinateReferenceSystem fromOgcWmsCrs(const QString &ogcCrs)
Creates a CRS from a given OGC WMS-format Coordinate Reference System string.
bool hasAxisInverted() const
Returns whether axis is inverted (e.g., for WMS 1.3) for the CRS.
Defines a QGIS exception class.
Layer tree group node serves as a container for layers and further groups.
void insertChildNode(int index, QgsLayerTreeNode *node)
Insert existing node at specified position.
QgsLayerTreeGroup * findGroup(const QString &name)
Find group node with specified name.
QgsLayerTreeGroup * clone() const override
Returns a clone of the group.
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
Layer tree node points to a map layer.
void setName(const QString &n) override
Sets the layer's name.
void setUseLayerName(bool use=true)
Uses the layer's name if use is true, or the name manually set if false.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
virtual bool isEmbeddedInParent() const
virtual void setEmbeddedInParent(bool embedded)
virtual void setUserLabel(const QString &userLabel)
The QgsLayerTreeModel class is model implementation for Qt item views framework.
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...
QgsLayerTree * rootGroup() const
Returns pointer to the root node of the layer tree. Always a non nullptr value.
This class is a base class for nodes in a layer tree.
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.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
Namespace with helper functions for layer tree operations.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
static void setNodeLegendStyle(QgsLayerTreeNode *node, QgsLegendStyle::Style style)
Sets the style of a node.
@ Subgroup
Legend subgroup title.
@ Hidden
Special style, item is hidden including margins around.
Base class for all map layer types.
A class to describe the version of a project.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
static QgsProject * instance()
Returns the QgsProject singleton instance.
A rectangle specified with double values.
bool isEmpty() const
Returns true if the rectangle is empty.
A helper class that centralizes caches accesses given by all the server cache filter plugins.
bool setCachedImage(const QByteArray *img, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Updates or inserts the image in cache like tiles.
QByteArray getCachedImage(const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Returns cached image (or 0 if image not in cache) like tiles.
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins.
virtual QgsServerCacheManager * cacheManager() const =0
Gets the registered server cache filters.
virtual QgsAccessControl * accessControls() const =0
Gets the registered access control filters.
QgsServerResponse Class defining response interface passed to services QgsService::executeRequest() m...
virtual void write(const QString &data)
Write string This is a convenient method that will write directly to the underlying I/O device.
virtual QByteArray data() const =0
Gets the data written so far.
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...
virtual QIODevice * io()=0
Returns the underlying QIODevice.
bool waitForFinished(int timeout=30000)
Blocks the current thread until the task finishes or a maximum of timeout milliseconds.
Counts the features in a QgsVectorLayer in task.
Represents a vector layer which manages a vector based data sets.
QgsVectorLayerFeatureCounter * countSymbolFeatures(bool storeSymbolFids=false)
Count features for symbols.
Exception thrown in case of malformed request.
Map renderer for WMS requests.
void configureLayers(QList< QgsMapLayer * > &layers, QgsMapSettings *settings=nullptr)
Configures layers for rendering optionally considering the map settings.
QJsonObject getLegendGraphicsAsJson(QgsLayerTreeModel &model)
Returns the map legend as a JSON object.
QImage * getLegendGraphics(QgsLayerTreeModel &model)
Returns the map legend as an image (or nullptr in case of error).
@ QGIS_InvalidParameterValue
@ QGIS_MissingParameterValue
Provides an interface to retrieve and manipulate WMS parameters received from the client.
QString rule() const
Returns RULE parameter or an empty string if none is defined.
QStringList allLayersNickname() const
Returns nickname of layers found in LAYER and LAYERS parameters.
QString formatAsString() const
Returns FORMAT parameter as a string.
QgsProjectVersion versionAsNumber() const
Returns VERSION parameter if defined or its default value.
QString ruleLabel() const
Returns RULELABEL parameter or an empty string if none is defined.
QgsRectangle bboxAsRectangle() const
Returns BBOX as a rectangle if defined and valid.
void set(QgsWmsParameter::Name name, const QVariant &value)
Sets a parameter value thanks to its name.
QString srcHeight() const
Returns SRCHEIGHT parameter or an empty string if not defined.
bool showFeatureCountAsBool() const
Returns SHOWFEATURECOUNT as a bool.
QString bbox() const
Returns BBOX if defined or an empty string.
Format format() const
Returns format.
bool ruleLabelAsBool() const
Returns RULELABEL as a bool.
QString srcWidth() const
Returns SRCWIDTH parameter or an empty string if not defined.
QString height() const
Returns HEIGHT parameter or an empty string if not defined.
QString crs() const
Returns CRS or an empty string if none is defined.
bool layerTitleAsBool() const
Returns LAYERTITLE as a bool or its default value if not defined.
bool addLayerGroups() const
Returns true if layer groups shall be added to GetLegendGraphic results.
Format
Output format for the response.
QString width() const
Returns WIDTH parameter or an empty string if not defined.
Rendering context for the WMS renderer.
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 setParameters(const QgsWmsParameters ¶meters)
Sets WMS parameters.
QList< QgsMapLayer * > layersToRender() const
Returns a list of all layers to actually render according to the current configuration.
QgsMapLayer * layer(const QString &nickname) const
Returns the layer corresponding to the nickname, or a nullptr if not found or if the layer do not nee...
void setFlag(Flag flag, bool on=true)
Sets or unsets a rendering flag according to the on value.
QgsWmsParameters parameters() const
Returns WMS parameters.
double scaleDenominator() const
Returns the scale denominator to use for rendering according to the current configuration.
int imageQuality() const
Returns the image quality to use for rendering according to the current configuration.
Class defining request interface passed to WMS service.
const QgsWmsParameters & wmsParameters() const
Returns the parameters interpreted for the WMS service.
SERVER_EXPORT bool wmsAddLegendGroupsLegendGraphic(const QgsProject &project)
Returns if legend groups should be in the legend graphic response if GetLegendGraphic is called on a ...
Median cut implementation.
void writeImage(QgsServerResponse &response, QImage &img, const QString &formatStr, int imageQuality)
Write image response.
void writeGetLegendGraphics(QgsServerInterface *serverIface, const QgsProject *project, const QgsWmsRequest &request, QgsServerResponse &response)
Output GetLegendGRaphics response.
QgsLayerTree * layerTree(const QgsWmsRenderContext &context)
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
QgsLayerTreeModel * legendModel(const QgsWmsRenderContext &context, QgsLayerTree &tree)
QgsLayerTree * layerTreeWithGroups(const QgsWmsRenderContext &context, QgsLayerTree *projectRoot)
@ Unknown
Unknown/invalid format.
ImageOutputFormat parseImageFormat(const QString &format)
Parse image format parameter.
void checkParameters(QgsWmsParameters ¶meters)
checkParameters checks request parameters and sets SRCHEIGHT and SRCWIDTH to default values in case B...
const QgsCoordinateReferenceSystem & outputCrs
const QgsCoordinateReferenceSystem & crs