QGIS API Documentation
3.16.0-Hannover (43b64b13f3)
|
Go to the documentation of this file.
34 #include <QJsonObject>
35 #include <QJsonDocument>
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 ) );
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" ) );
135 QJsonDocument doc( result );
136 response.
write( doc.toJson( QJsonDocument::Compact ) );
140 std::unique_ptr<QImage> result;
141 if ( !parameters.
rule().isEmpty() )
146 throw QgsException( QStringLiteral(
"Could not get a legend node for the requested RULE" ) );
158 #ifdef HAVE_SERVER_PYTHON_PLUGINS
161 QByteArray content = response.
data();
162 if ( !content.isEmpty() )
163 cacheManager->
setCachedImage( &content, project, request, accessControl );
169 throw QgsException( QStringLiteral(
"Failed to compute GetLegendGraphics image" ) );
182 if ( parameters.
format() == QgsWmsParameters::Format::NONE )
188 if ( ! parameters.
bbox().isEmpty() && !parameters.
rule().isEmpty() )
191 QStringLiteral(
"BBOX parameter cannot be combined with RULE." ) );
202 if ( ! parameters.
bbox().isEmpty() )
206 QString
crs = parameters.
crs();
207 if (
crs.compare( QStringLiteral(
"CRS:84" ), Qt::CaseInsensitive ) == 0 )
217 const double ratio { bbox.width() / bbox.height() };
218 int defaultHeight {
static_cast<int>( 800 / ratio ) };
219 if ( parameters.
width().isEmpty() && parameters.
srcWidth().isEmpty() )
223 if ( parameters.
height().isEmpty() && parameters.
srcHeight().isEmpty() )
235 std::unique_ptr<QgsMapSettings> mapSettings;
243 if ( ! parameters.
bbox().isEmpty() )
245 mapSettings = qgis::make_unique<QgsMapSettings>();
246 mapSettings->setOutputSize( context.
mapSize() );
249 QString
crs = parameters.
crs();
250 if (
crs.compare( QStringLiteral(
"CRS:84" ), Qt::CaseInsensitive ) == 0 )
260 mapSettings->setDestinationCrs(
outputCrs );
261 mapSettings->setExtent( bbox );
266 model->setLegendFilterByMap( mapSettings.get() );
270 if ( parameters.
rule().isEmpty() )
272 QList<QgsLayerTreeNode *> children = tree.
children();
273 QString ruleLabel = parameters.
ruleLabel();
292 else if ( ruleLabel.compare( QStringLiteral(
"AUTO" ), Qt::CaseInsensitive ) == 0 )
304 return model.release();
309 std::unique_ptr<QgsLayerTree> tree(
new QgsLayerTree() );
311 QList<QgsVectorLayerFeatureCounter *> counters;
318 if ( !ml->title().isEmpty() )
323 const QString
property = QStringLiteral(
"showFeatureCount" );
334 counters.append( counter );
342 return tree.release();
351 if ( node->data( Qt::DisplayRole ).toString().compare( rule ) == 0 )
Counts the features in a QgsVectorLayer in task.
@ QGIS_InvalidParameterValue
ImageOutputFormat parseImageFormat(const QString &format)
Parse image format parameter.
QString srcHeight() const
Returns SRCHEIGHT parameter or an empty string if not defined.
Provides an interface to retrieve and manipulate WMS parameters received from the client.
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
void set(QgsWmsParameter::Name name, const QVariant &value)
Sets a parameter value thanks to its name.
static void setNodeLegendStyle(QgsLayerTreeNode *node, QgsLegendStyle::Style style)
Sets the style of a node.
Defines a QGIS exception class.
This class is a base class for nodes in a layer tree.
const QgsCoordinateReferenceSystem & outputCrs
virtual bool isEmbeddedInParent() const
bool showFeatureCountAsBool() const
Returns SHOWFEATURECOUNT as a bool.
QgsLayerTree * layerTree(const QgsWmsRenderContext &context)
const QgsCoordinateReferenceSystem & crs
static QgsCoordinateReferenceSystem fromOgcWmsCrs(const QString &ogcCrs)
Creates a CRS from a given OGC WMS-format Coordinate Reference System string.
QString crs() const
Returns CRS or an empty string if none is defined.
void setUseLayerName(bool use=true)
Uses the layer's name if use is true, or the name manually set if false.
Format
Output format for the response.
void setFlag(Flag flag, bool on=true)
Sets or unsets a rendering flag according to the on value.
QString bbox() const
Returns BBOX if defined or an empty string.
void setParameters(const QgsWmsParameters ¶meters)
Sets WMS parameters.
void writeImage(QgsServerResponse &response, QImage &img, const QString &formatStr, int imageQuality)
Write image response.
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
bool hasAxisInverted() const
Returns whether axis is inverted (e.g., for WMS 1.3) for the CRS.
Rendering context for the WMS renderer.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
int imageQuality() const
Returns the image quality to use for rendering according to the current configuration.
virtual QgsAccessControl * accessControls() const =0
Gets the registered access control filters.
A rectangle specified with double values.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
virtual void write(const QString &data)
Write string This is a convenient method that will write directly to the underlying I/O device.
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.
Namespace with helper functions for layer tree operations.
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...
QgsProjectVersion versionAsNumber() const
Returns VERSION parameter if defined or its default value.
A helper class that centralizes caches accesses given by all the server cache filter plugins.
void setName(const QString &n) override
Sets the layer's name.
Layer tree node points to a map layer.
bool ruleLabelAsBool() const
Returns RULELABEL as a bool.
QgsLayerTreeModel * legendModel(const QgsWmsRenderContext &context, QgsLayerTree &tree)
@ QGIS_MissingParameterValue
virtual QByteArray data() const =0
Gets the data written so far.
QgsRectangle bboxAsRectangle() const
Returns BBOX as a rectangle if defined and valid.
virtual void setEmbeddedInParent(bool embedded)
Format format() const
Returns format.
This class represents a coordinate reference system (CRS).
QgsLayerTree * rootGroup() const
Returns pointer to the root node of the layer tree. Always a non nullptr value.
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
A helper class that centralizes restrictions given by all the access control filter plugins.
Median cut implementation.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
double scaleDenominator() const
Returns the scale denominator to use for rendering according to the current configuration.
virtual void setUserLabel(const QString &userLabel)
Exception thrown in case of malformed request.
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.
bool layerTitleAsBool() const
Returns LAYERTITLE as a bool or its default value if not defined.
Represents a vector layer which manages a vector based data sets.
virtual QgsServerCacheManager * cacheManager() const =0
Gets the registered server cache filters.
QByteArray getCachedImage(const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Returns cached image (or 0 if image not in cache) like tiles.
Base class for all map layer types.
void writeGetLegendGraphics(QgsServerInterface *serverIface, const QgsProject *project, const QString &, const QgsServerRequest &request, QgsServerResponse &response)
Output GetLegendGRaphics response.
QString rule() const
Returns RULE parameter or an empty string if none is defined.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
virtual QIODevice * io()=0
Returns the underlying QIODevice.
QgsVectorLayerFeatureCounter * countSymbolFeatures(bool storeSymbolFids=false)
Count features for symbols.
bool setCachedImage(const QByteArray *img, const QgsProject *project, const QgsServerRequest &request, QgsAccessControl *accessControl) const
Updates or inserts the image in cache like tiles.
Map renderer for WMS requests.
QString formatAsString() const
Returns FORMAT parameter as a string.
@ Hidden
Special style, item is hidden including margins around.
QImage * getLegendGraphics(QgsLayerTreeModel &model)
Returns the map legend as an image (or nullptr in case of error).
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...
void configureLayers(QList< QgsMapLayer * > &layers, QgsMapSettings *settings=nullptr)
Configures layers for rendering optionally considering the map settings.
QList< QgsMapLayer * > layersToRender() const
Returns a list of all layers to actually render according to the current configuration.
bool isEmpty() const
Returns true if the rectangle is empty.
@ Subgroup
Legend subgroup title.
QJsonObject getLegendGraphicsAsJson(QgsLayerTreeModel &model)
Returns the map legend as a JSON object.
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins.
void checkParameters(QgsWmsParameters ¶meters)
checkParameters checks request parameters and sets SRCHEIGHT and SRCWIDTH to default values in case B...
QString width() const
Returns WIDTH parameter or an empty string if not defined.
A class to describe the version of a project.
bool waitForFinished(int timeout=30000)
Blocks the current thread until the task finishes or a maximum of timeout milliseconds.
QgsServerResponse Class defining response interface passed to services QgsService::executeRequest() m...
QStringList allLayersNickname() const
Returns nickname of layers found in LAYER and LAYERS parameters.
QgsWmsParameters parameters() const
Returns WMS parameters.
QString ruleLabel() const
Returns RULELABEL parameter or an empty string if none is defined.
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...
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...