36#include <QJsonDocument>
40using namespace Qt::StringLiterals;
67 QString imageSaveFormat;
68 QString imageContentType;
71 imageContentType =
"image/png";
72 imageSaveFormat =
"PNG";
76 imageContentType =
"image/jpeg";
77 imageSaveFormat =
"JPEG";
88 imageContentType =
"image/png";
89 imageSaveFormat =
"PNG";
107#ifdef HAVE_SERVER_PYTHON_PLUGINS
110 if ( cacheManager && !imageSaveFormat.isEmpty() )
113 const QByteArray content = cacheManager->
getCachedImage( project, request, accessControl );
114 if ( !content.isEmpty() && image.loadFromData( content ) )
116 response.
setHeader( u
"Content-Type"_s, imageContentType );
117 image.save( response.
io(), qPrintable( imageSaveFormat ) );
127 const std::unique_ptr<QgsLayerTreeModel> model(
legendModel( context, *tree.get() ) );
141 if ( !parameters.
rule().isEmpty() )
146 throw QgsException( u
"Could not get a legend node for the requested RULE"_s );
156 const QJsonDocument doc( result );
157 response.
write( doc.toJson( QJsonDocument::Compact ) );
161 std::unique_ptr<QImage> result;
162 if ( !parameters.
rule().isEmpty() )
167 throw QgsException( u
"Could not get a legend node for the requested RULE"_s );
179#ifdef HAVE_SERVER_PYTHON_PLUGINS
182 const QByteArray content = response.
data();
183 if ( !content.isEmpty() )
184 cacheManager->
setCachedImage( &content, project, request, accessControl );
190 throw QgsException( u
"Failed to compute GetLegendGraphics image"_s );
207 if ( !parameters.
bbox().isEmpty() && !parameters.
rule().isEmpty() )
219 if ( !parameters.
bbox().isEmpty() )
223 const QString crs = parameters.
crs();
224 if ( crs.compare( u
"CRS:84"_s, Qt::CaseInsensitive ) == 0 )
233 const double ratio { bbox.
width() / bbox.
height() };
234 const int defaultHeight {
static_cast<int>( 800 / ratio ) };
235 if ( parameters.
width().isEmpty() && parameters.
srcWidth().isEmpty() )
239 if ( parameters.
height().isEmpty() && parameters.
srcHeight().isEmpty() )
249 auto model = std::make_unique<QgsLayerTreeModel>( &tree );
250 std::unique_ptr<QgsMapSettings> mapSettings;
258 if ( !parameters.
bbox().isEmpty() )
260 mapSettings = std::make_unique<QgsMapSettings>();
261 mapSettings->setOutputSize( context.
mapSize() );
264 const QString crs = parameters.
crs();
265 if ( crs.compare( u
"CRS:84"_s, Qt::CaseInsensitive ) == 0 )
274 mapSettings->setDestinationCrs( outputCrs );
275 mapSettings->setExtent( bbox );
283 model->setFilterSettings( &filterSettings );
287 if ( parameters.
rule().isEmpty() )
289 const QList<QgsLayerTreeNode *> children = tree.
children();
290 const QString ruleLabel = parameters.
ruleLabel();
309 else if ( ruleLabel.compare( u
"AUTO"_s, Qt::CaseInsensitive ) == 0 )
321 return model.release();
326 auto tree = std::make_unique<QgsLayerTree>();
328 QList<QgsVectorLayerFeatureCounter *> counters;
335 if ( !ml->serverProperties()->title().isEmpty() )
336 lt->
setName( ml->serverProperties()->title() );
340 const QString
property = u
"showFeatureCount"_s;
351 counters.append( counter );
356 counter->waitForFinished();
359 return tree.release();
369 auto tree = std::make_unique<QgsLayerTree>();
373 for (
int i = 0; i < layerNicknames.size(); ++i )
375 QString nickname = layerNicknames.at( i );
381 tree->addLayer( layer );
388 tree->insertChildNode( i, group->
clone() );
393 return tree.release();
402 if ( node->data( Qt::DisplayRole ).toString().compare( rule ) == 0 )
@ Hidden
Special style, item is hidden including margins around.
@ Subgroup
Legend subgroup title.
@ ShowRuleDetails
If set, the rule expression of a rule based renderer legend item will be added to the JSON.
QFlags< LegendJsonRenderFlag > LegendJsonRenderFlags
A helper class that centralizes restrictions given by all the access control filter plugins.
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 the axis order is inverted for the CRS compared to the order east/north (longitude/la...
Defines a QGIS exception class.
Contains settings relating to filtering the contents of QgsLayerTreeModel and views.
void setLayerFilterExpressionsFromLayerTree(QgsLayerTree *tree)
Sets layer filter expressions using a layer tree.
Layer tree group node serves as a container for layers and further groups.
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.
An abstract interface for legend items returned from QgsMapLayerLegend implementation.
A model representing the layer tree, including layers and groups of layers.
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.
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, Qgis::LegendComponent style)
Sets the style of a node.
Base class for all map layer types.
Describes 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.
void invert()
Swap x/y coordinates in the rectangle.
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.
Defines 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.
static bool wmsAddLegendGroupsLegendGraphic(const QgsProject &project)
Returns if legend groups should be in the legend graphic response if GetLegendGraphic is called on a ...
Defines the response interface passed to QgsService.
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 QgsFeedback * feedback() const
Returns the socket feedback if any.
virtual QIODevice * io()=0
Returns the underlying QIODevice.
Counts the features in a QgsVectorLayer in task.
Represents a vector layer which manages a vector based dataset.
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.
QImage * getLegendGraphics(QgsLayerTreeModel &model)
Returns the map legend as an image (or nullptr in case of error).
QJsonObject getLegendGraphicsAsJson(QgsLayerTreeModel &model, const Qgis::LegendJsonRenderFlags &jsonRenderFlags=Qgis::LegendJsonRenderFlags())
Returns the map legend as a JSON object.
@ 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 showRuleDetailsAsBool() const
Returns SHOWRULEDETAILS as a bool.
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.
void setSocketFeedback(QgsFeedback *feedback)
Sets the response feedback.
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.
Defines request interfaces passed to WMS service.
const QgsWmsParameters & wmsParameters() const
Returns the parameters interpreted for the WMS service.
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...