29 , mInterface( interface )
38 initRestrictedLayers();
41 searchLayersToRender();
42 removeUnwantedLayers();
43 checkLayerReadPermissions();
45 std::reverse( mLayersToRender.begin(), mLayersToRender.end() );
62 return mFlags.testFlag( flag );
85 if ( mSlds.contains( nickname ) )
87 sld = mSlds[ nickname ];
98 if ( mStyles.contains( nickname ) )
100 style = mStyles[ nickname ];
165 if ( !mParameters.
dpi().isEmpty() )
176 std::function <QStringList( const QString &name )> findLeaves = [ & ](
const QString & name ) -> QStringList
179 if ( mLayerGroups.contains( name ) )
181 const auto &
layers { mLayerGroups[ name ] };
182 for (
const auto &l :
layers )
186 if ( mLayerGroups.contains( nick ) )
188 _result.append( name );
192 _result.append( findLeaves( nick ) );
198 _result.append( name );
203 for (
const auto &name : constNicks )
205 result.append( findLeaves( name ) );
212 return mLayersToRender;
217 return mNicknameLayers.values();
222 double denominator = -1;
224 if ( mScaleDenominator >= 0 )
226 denominator = mScaleDenominator;
239 removeUnwantedLayers();
261 else if ( name.isEmpty() )
273 for (
auto layer : mLayersToRender )
287 return layer( nickname ) !=
nullptr;
292 return mLayerGroups.contains( name );
295 void QgsWmsRenderContext::initNicknameLayers()
306 initLayerGroupsRecursive( root, rootName.isEmpty() ? mProject->
title() : rootName );
309 void QgsWmsRenderContext::initLayerGroupsRecursive(
const QgsLayerTreeGroup *group,
const QString &groupName )
311 if ( !groupName.isEmpty() )
313 mLayerGroups[groupName] = QList<QgsMapLayer *>();
314 const auto projectLayerTreeRoot { mProject->
layerTreeRoot() };
315 const auto treeGroupLayers { group->
findLayers() };
318 if ( ! projectLayerTreeRoot->hasCustomLayerOrder() )
320 for (
const auto &tl : treeGroupLayers )
322 mLayerGroups[groupName].push_back( tl->layer() );
327 const auto projectLayerOrder { projectLayerTreeRoot->layerOrder() };
329 QList<QgsMapLayer *> groupLayersList;
330 for (
const auto &tl : treeGroupLayers )
332 groupLayersList << tl->layer();
334 for (
const auto &l : projectLayerOrder )
336 if ( groupLayersList.contains( l ) )
338 mLayerGroups[groupName].push_back( l );
348 QString name = child->customProperty( QStringLiteral(
"wmsShortName" ) ).toString();
350 if ( name.isEmpty() )
351 name = child->name();
353 initLayerGroupsRecursive( static_cast<const QgsLayerTreeGroup *>( child ), name );
359 void QgsWmsRenderContext::initRestrictedLayers()
361 mRestrictedLayers.clear();
367 QStringList restrictedLayersNames;
370 for (
const QString &l : qgis::as_const( restricted ) )
375 const QList<QgsLayerTreeLayer *> groupLayers = group->
findLayers();
378 restrictedLayersNames.append( treeLayer->name() );
383 restrictedLayersNames.append( l );
391 if ( restrictedLayersNames.contains(
layer->
name() ) )
398 void QgsWmsRenderContext::searchLayersToRender()
400 mLayersToRender.clear();
404 if ( ! mParameters.
sldBody().isEmpty() )
406 searchLayersToRenderSld();
410 searchLayersToRenderStyle();
416 for (
const QString &
layer : constLayers )
418 if ( mNicknameLayers.contains(
layer )
419 && !mLayersToRender.contains( mNicknameLayers[
layer] ) )
421 mLayersToRender.append( mNicknameLayers[layer] );
427 void QgsWmsRenderContext::searchLayersToRenderSld()
437 ( void )doc.setContent( sld,
true );
438 QDomElement docEl = doc.documentElement();
440 QDomElement root = doc.firstChildElement(
"StyledLayerDescriptor" );
441 QDomElement namedElem = root.firstChildElement(
"NamedLayer" );
443 if ( docEl.isNull() )
448 QDomNodeList named = docEl.elementsByTagName(
"NamedLayer" );
449 for (
int i = 0; i < named.size(); ++i )
451 QDomNodeList names = named.item( i ).toElement().elementsByTagName(
"Name" );
452 if ( !names.isEmpty() )
454 QString lname = names.item( 0 ).toElement().text();
456 if ( mNicknameLayers.contains( lname ) )
458 mSlds[lname] = namedElem;
459 mLayersToRender.append( mNicknameLayers[ lname ] );
461 else if ( mLayerGroups.contains( lname ) )
466 mSlds[name] = namedElem;
467 mLayersToRender.insert( 0,
layer );
481 void QgsWmsRenderContext::searchLayersToRenderStyle()
485 const QString nickname = param.mNickname;
486 const QString
style = param.mStyle;
488 if ( mNicknameLayers.contains( nickname ) )
490 if ( !style.isEmpty() )
492 mStyles[nickname] =
style;
495 mLayersToRender.append( mNicknameLayers[ nickname ] );
497 else if ( mLayerGroups.contains( nickname ) )
500 QList<QString> layersFromGroup;
504 if ( !style.isEmpty() )
506 mStyles[ nickname ] =
style;
508 layersFromGroup.push_front( nickname );
511 for (
const auto &name : layersFromGroup )
513 mLayersToRender.append( mNicknameLayers[ name ] );
526 bool QgsWmsRenderContext::layerScaleVisibility(
const QString &name )
const 528 bool visible =
false;
530 if ( ! mNicknameLayers.contains( name ) )
537 bool useScaleConstraint = (
scaleDenominator() > 0 && scaleBasedVisibility );
586 if ( wmsMaxWidthEnv != -1 && wmsMaxWidthProj != -1 )
589 wmsMaxWidth = std::min( wmsMaxWidthProj, wmsMaxWidthEnv );
594 wmsMaxWidth = std::max( wmsMaxWidthProj, wmsMaxWidthEnv );
597 if ( wmsMaxWidth != -1 &&
mapWidth() > wmsMaxWidth )
606 if ( wmsMaxHeightEnv != -1 && wmsMaxHeightProj != -1 )
609 wmsMaxHeight = std::min( wmsMaxHeightProj, wmsMaxHeightEnv );
614 wmsMaxHeight = std::max( wmsMaxHeightProj, wmsMaxHeightEnv );
617 if ( wmsMaxHeight != -1 &&
mapHeight() > wmsMaxHeight )
630 switch ( mParameters.
format() )
632 case QgsWmsParameters::Format::JPG:
638 const int bytes_per_line = ( (
mapWidth() * depth + 31 ) >> 5 ) << 2;
640 if ( std::numeric_limits<int>::max() / depth < static_cast<uint>(
mapWidth() )
641 || bytes_per_line <= 0
643 || std::numeric_limits<int>::max() /
static_cast<uint
>( bytes_per_line ) < static_cast<uint>(
mapHeight() )
644 || std::numeric_limits<int>::max() /
sizeof( uchar * ) < static_cast<uint>(
mapHeight() ) )
658 if ( !mParameters.
bbox().isEmpty() && extent.
isEmpty() )
683 if ( !mParameters.
bbox().isEmpty() && extent.
isEmpty() )
689 QString
crs = mParameters.
crs();
690 if ( crs.compare(
"CRS:84", Qt::CaseInsensitive ) == 0 )
692 crs = QString(
"EPSG:4326" );
702 if ( !extent.
isEmpty() && height > 0 && width > 0 )
704 const double mapRatio = extent.
width() / extent.
height();
705 const double imageRatio =
static_cast<double>( width ) / static_cast<double>( height );
709 const double cellsize = ( extent.
width() /
static_cast<double>( width ) ) * 0.5 + ( extent.
height() /
static_cast<double>( height ) ) * 0.5;
710 width = extent.
width() / cellsize;
711 height = extent.
height() / cellsize;
721 else if ( height <= 0 )
727 return QSize( width, height );
730 void QgsWmsRenderContext::removeUnwantedLayers()
732 QList<QgsMapLayer *>
layers;
738 if ( !layerScaleVisibility( nickname ) )
741 if ( mRestrictedLayers.contains( nickname ) )
752 if ( ! wfsLayers.contains(
layer->
id() ) )
758 layers.append(
layer );
764 void QgsWmsRenderContext::checkLayerReadPermissions()
766 #ifdef HAVE_SERVER_PYTHON_PLUGINS 767 for (
const auto layer : mLayersToRender )
769 if ( !accessControl()->layerReadPermission(
layer ) )
777 #ifdef HAVE_SERVER_PYTHON_PLUGINS Layer tree group node serves as a container for layers and further groups.
int srcHeightAsInt() const
Returns SRCHEIGHT parameter as an int or its default value if not defined.
qreal dotsPerMm() const
Returns default dots per mm according to the current configuration.
A rectangle specified with double values.
Base class for all map layer types.
QList< QgsMapLayer * > layersToRender() const
Returns a list of all layers to actually render according to the current configuration.
int imageQualityAsInt() const
Returns IMAGE_QUALITY parameter as an integer.
SERVER_EXPORT int wmsMaxWidth(const QgsProject &project)
Returns the maximum width for WMS images defined in a QGIS project.
SERVER_EXPORT int wmsTileBuffer(const QgsProject &project)
Returns the tile buffer in pixels for WMS images defined in a QGIS project.
QgsMapLayerType type() const
Returns the type of the layer.
QList< QgsMapLayer * > layers() const
Returns a list of all layers read from the project.
virtual QgsServerSettings * serverSettings()=0
Returns the server settings.
QgsWmsParameters parameters() const
Returns WMS parameters.
QString shortName() const
Returns the short name of the layer used by QGIS Server to identify the layer.
QgsRectangle bboxAsRectangle() const
Returns BBOX as a rectangle if defined and valid.
QString scale() const
Returns SCALE parameter or an empty string if none is defined.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
QString sldBody() const
Returns SLD_body if defined or an empty string.
int wmsMaxWidth() const
Returns the server-wide max width of a WMS GetMap request.
double dpiAsDouble() const
Returns DPI parameter as an int or its default value if not defined.
bool isInScaleRange(double scale) const
Tests whether the layer should be visible at the specified scale.
bool updateExtent() const
Returns true if the extent has to be updated before the rendering, false otherwise.
int widthAsInt() const
Returns WIDTH parameter as an int or its default value if not defined.
Provides a way to retrieve settings by prioritizing according to environment variables, ini file and default values.
QMap< QString, QList< QgsMapLayer * > > layerGroups() const
Returns a map having layer group names as keys and a list of layers as values.
Exception thrown in case of malformed request.
const QgsCoordinateReferenceSystem & crs
QStringList flattenedQueryLayers() const
Returns a list of query layer names where group names are replaced by the names of their layer compon...
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.
QgsWmsRenderContext()=default
Default constructor for QgsWmsRenderContext.
bool isValidWidthHeight() const
Returns true if width and height are valid according to the maximum values defined within the project...
bool isValidLayer(const QString &nickname) const
Returns true if the layer has to be rendered, false otherwise.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
void setScaleDenominator(double scaleDenominator)
Sets a custom scale denominator.
int tileBuffer() const
Returns the tile buffer value to use for rendering according to the current configuration.
QString crs() const
Returns CRS or an empty string if none is defined.
bool isEmpty() const
Returns true if the rectangle is empty.
A class to describe the version of a project.
double width() const
Returns the width of the rectangle.
Provides an interface to retrieve and manipulate WMS parameters received from the client...
bool tiledAsBool() const
Returns TILED parameter as a boolean.
QString imageQuality() const
Returns IMAGE_QUALITY parameter or an empty string if not defined.
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.
Flag
Available rendering options.
QStringList queryLayersNickname() const
Returns nickname of layers found in QUERY_LAYERS parameter.
Format format() const
Returns format.
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
bool testFlag(Flag flag) const
Returns the status of a rendering flag.
const QgsServerSettings & settings() const
Returns settings of the server.
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...
int wmsMaxHeight() const
Returns the server-wide max height of a WMS GetMap request.
const QgsProject * project() const
Returns the project.
Median cut implementation.
double scaleDenominator() const
Returns the scale denominator to use for rendering according to the current configuration.
QgsLayerTree * layerTreeRoot() const
Returns pointer to the root (invisible) node of the project's layer tree.
QString layerNickname(const QgsMapLayer &layer) const
Returns the nickname (short name, id or name) of the layer according to the current configuration...
int heightAsInt() const
Returns HEIGHT parameter as an int or its default value if not defined.
int wmsPrecisionAsInt() const
Returns WMS_PRECISION parameter as an int or its default value if not defined.
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins...
double scaleAsDouble() const
Returns SCALE as a double.
static QgsCoordinateReferenceSystem fromOgcWmsCrs(const QString &ogcCrs)
Creates a CRS from a given OGC WMS-format Coordinate Reference System string.
bool isValidGroup(const QString &name) const
Returns true if name is a group.
int precision() const
Returns the precision to use according to the current configuration.
double mapTileBuffer(int mapWidth) const
Returns the tile buffer in geographical units for the given map width in pixels.
bool hasScaleBasedVisibility() const
Returns whether scale based visibility is enabled for the layer.
QDomElement sld(const QgsMapLayer &layer) const
Returns a SLD document for a specific layer.
SERVER_EXPORT int wmsFeatureInfoPrecision(const QgsProject &project)
Returns the geometry precision for GetFeatureInfo request.
QgsLayerTreeGroup * findGroup(const QString &name)
Find group node with specified name.
Exception thrown when data access violates access controls.
bool hasAxisInverted() const
Returns whether axis is inverted (e.g., for WMS 1.3) for the CRS.
SERVER_EXPORT QStringList wfsLayerIds(const QgsProject &project)
Returns the Layer ids list defined in a QGIS project as published in WFS.
This class represents a coordinate reference system (CRS).
int mapHeight() const
Returns HEIGHT or SRCHEIGHT according to UseSrcWidthHeight flag.
const QgsCoordinateReferenceSystem & outputCrs
A helper class that centralizes restrictions given by all the access control filter plugins...
int srcWidthAsInt() const
Returns SRCWIDTH parameter as an int or its default value if not defined.
virtual QgsAccessControl * accessControls() const =0
Gets the registered access control filters.
QString title() const
Returns the project's title.
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
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...
SERVER_EXPORT QStringList wmsRestrictedLayers(const QgsProject &project)
Returns the restricted layer name list.
Container of other groups and layers.
SERVER_EXPORT QString wmsRootName(const QgsProject &project)
Returns the WMS root layer name defined in a QGIS project.
QString dpi() const
Returns DPI parameter or an empty string if not defined.
QList< QgsWmsParametersLayer > layersParameters() const
Returns parameters for each layer found in LAYER/LAYERS.
WMS parameter received from the client.
QString style(const QgsMapLayer &layer) const
Returns a style's name for a specific layer.
SERVER_EXPORT int wmsImageQuality(const QgsProject &project)
Returns the quality for WMS images defined in a QGIS project.
void invert()
Swap x/y coordinates in the rectangle.
double height() const
Returns the height of the rectangle.
Layer tree node points to a map layer.
SERVER_EXPORT int wmsMaxHeight(const QgsProject &project)
Returns the maximum height for WMS images defined in a QGIS project.
SERVER_EXPORT bool wmsUseLayerIds(const QgsProject &project)
Returns if layer ids are used as name in WMS.
int mapWidth() const
Returns WIDTH or SRCWIDTH according to UseSrcWidthHeight flag.