30 , mInterface( interface )
37 qDeleteAll( mExternalLayers );
38 mExternalLayers.clear();
45 initRestrictedLayers();
48 searchLayersToRender();
49 removeUnwantedLayers();
51 std::reverse( mLayersToRender.begin(), mLayersToRender.end() );
54bool QgsWmsRenderContext::addLayerToRender(
QgsMapLayer *layer )
56 const bool allowed = checkLayerReadPermissions(
layer );
59 mLayersToRender.append(
layer );
79 return mFlags.testFlag( flag );
102 if ( mSlds.contains( nickname ) )
104 sld = mSlds[ nickname ];
115 if ( mStyles.contains( nickname ) )
117 style = mStyles[ nickname ];
127 const QList<QgsWmsParametersLayer> cLayerParams { mParameters.
layersParameters() };
129 for (
const auto ¶ms : std::as_const( cLayerParams ) )
189 if ( !mParameters.
dpi().isEmpty() )
201 std::function <QStringList(
const QString &name )> findLeaves = [ & ](
const QString & name ) -> QStringList
204 if ( mLayerGroups.contains( name ) )
206 const auto &
layers { mLayerGroups[ name ] };
207 for (
const auto &l :
layers )
211 if ( mLayerGroups.contains( nick ) )
213 _result.append( name );
217 _result.append( findLeaves( nick ) );
223 _result.append( name );
228 for (
const auto &name : std::as_const( layerNames ) )
230 result.append( findLeaves( name ) );
237 return mLayersToRender;
242 return mNicknameLayers.values();
247 double denominator = -1;
249 if ( mScaleDenominator >= 0 )
251 denominator = mScaleDenominator;
264 removeUnwantedLayers();
284 std::find_if( mExternalLayers.cbegin(), mExternalLayers.cend(),
287 return l->id() == layer.id();
288 } ) == mExternalLayers.cend() )
292 else if ( name.isEmpty() )
304 for (
auto layer : mLayersToRender )
318 return layer( nickname );
323 return mLayerGroups.value( nickname );
328 return mLayerGroups.contains( name );
331void QgsWmsRenderContext::initNicknameLayers()
342 initLayerGroupsRecursive( root, rootName.isEmpty() ? mProject->
title() : rootName );
345void QgsWmsRenderContext::initLayerGroupsRecursive(
const QgsLayerTreeGroup *group,
const QString &groupName )
347 if ( !groupName.isEmpty() )
349 QList<QgsMapLayer *> layerGroup;
350 const auto projectLayerTreeRoot { mProject->
layerTreeRoot() };
351 const auto treeGroupLayers { group->
findLayers() };
354 if ( ! projectLayerTreeRoot->hasCustomLayerOrder() )
356 for (
const auto &tl : treeGroupLayers )
358 auto layer = tl->layer();
359 if ( checkLayerReadPermissions(
layer ) )
361 layerGroup.push_back(
layer );
367 const auto projectLayerOrder { projectLayerTreeRoot->layerOrder() };
369 QList<QgsMapLayer *> groupLayersList;
370 for (
const auto &tl : treeGroupLayers )
372 auto layer = tl->layer();
373 if ( checkLayerReadPermissions(
layer ) )
375 groupLayersList <<
layer;
378 for (
const auto &l : projectLayerOrder )
380 if ( groupLayersList.contains( l ) )
382 layerGroup.push_back( l );
387 if ( !layerGroup.empty() )
389 mLayerGroups[groupName] = layerGroup;
397 QString name = child->customProperty( QStringLiteral(
"wmsShortName" ) ).toString();
399 if ( name.isEmpty() )
400 name = child->name();
402 initLayerGroupsRecursive(
static_cast<const QgsLayerTreeGroup *
>( child ), name );
408void QgsWmsRenderContext::initRestrictedLayers()
410 mRestrictedLayers.clear();
416 QStringList restrictedLayersNames;
419 for (
const QString &l : std::as_const( restricted ) )
424 const QList<QgsLayerTreeLayer *> groupLayers = group->
findLayers();
427 restrictedLayersNames.append( treeLayer->name() );
432 restrictedLayersNames.append( l );
440 if ( restrictedLayersNames.contains(
layer->
name() ) )
447void QgsWmsRenderContext::searchLayersToRender()
449 mLayersToRender.clear();
453 if ( ! mParameters.
sldBody().isEmpty() )
455 searchLayersToRenderSld();
459 searchLayersToRenderStyle();
465 for (
const QString &layerName : queryLayerNames )
467 const QList<QgsMapLayer *>
layers = mNicknameLayers.values( layerName );
470 if ( !mLayersToRender.contains( lyr ) )
472 if ( !addLayerToRender( lyr ) )
474 throw QgsSecurityException( QStringLiteral(
"Your are not allowed to access the layer %1" ).arg( lyr->name() ) );
485 for (
const QString &layerName : queryLayerNames )
487 const QList<QgsMapLayer *>
layers = mNicknameLayers.values( layerName );
490 if ( !mLayersToRender.contains( lyr ) )
492 if ( !addLayerToRender( lyr ) )
494 throw QgsSecurityException( QStringLiteral(
"Your are not allowed to access the layer %1" ).arg( lyr->name() ) );
503void QgsWmsRenderContext::searchLayersToRenderSld()
513 ( void )doc.setContent(
sld,
true );
514 QDomElement docEl = doc.documentElement();
516 QDomElement root = doc.firstChildElement(
"StyledLayerDescriptor" );
517 QDomElement namedElem = root.firstChildElement(
"NamedLayer" );
519 if ( docEl.isNull() )
524 QDomNodeList named = docEl.elementsByTagName(
"NamedLayer" );
525 for (
int i = 0; i < named.size(); ++i )
527 QDomNodeList names = named.item( i ).toElement().elementsByTagName(
"Name" );
528 if ( !names.isEmpty() )
530 QString lname = names.item( 0 ).toElement().text();
531 if ( mNicknameLayers.contains( lname ) )
533 mSlds[lname] = namedElem;
534 for (
const auto layer : mNicknameLayers.values( lname ) )
536 if ( !addLayerToRender(
layer ) )
542 else if ( mLayerGroups.contains( lname ) )
547 param.mValue = lname;
554 mSlds[name] = namedElem;
555 mLayersToRender.insert( 0,
layer );
561 param.mValue = lname;
569void QgsWmsRenderContext::searchLayersToRenderStyle()
573 const QString nickname = param.mNickname;
574 const QString
style = param.mStyle;
579 std::unique_ptr<QgsMapLayer>
layer = std::make_unique< QgsRasterLayer >( param.mExternalUri, param.mNickname, QStringLiteral(
"wms" ) );
584 mExternalLayers.append(
layer.release() );
585 auto lyr = mExternalLayers.last();
586 if ( !addLayerToRender( lyr ) )
588 throw QgsSecurityException( QStringLiteral(
"Your are not allowed to access the layer %1" ).arg( lyr->name() ) );
593 else if ( mNicknameLayers.contains( nickname ) )
595 if ( !
style.isEmpty() )
597 mStyles[nickname] =
style;
600 for (
const auto layer : mNicknameLayers.values( nickname ) )
602 if ( !addLayerToRender(
layer ) )
608 else if ( mLayerGroups.contains( nickname ) )
613 param.mValue = nickname;
622 if ( !
style.isEmpty() )
624 mStyles[ nickname ] =
style;
631 for (
const auto layer : mNicknameLayers.values( name ) )
633 addLayerToRender(
layer );
640 param.mValue = nickname;
647bool QgsWmsRenderContext::layerScaleVisibility(
const QString &name )
const
649 bool visible =
false;
651 if ( ! mNicknameLayers.contains( name ) )
656 const QList<QgsMapLayer *>
layers = mNicknameLayers.values( name );
660 bool useScaleConstraint = (
scaleDenominator() > 0 && scaleBasedVisibility );
709 if ( width <= 0 || height <= 0 )
718 if ( wmsMaxWidthEnv != -1 && wmsMaxWidthProj != -1 )
721 wmsMaxWidth = std::min( wmsMaxWidthProj, wmsMaxWidthEnv );
726 wmsMaxWidth = std::max( wmsMaxWidthProj, wmsMaxWidthEnv );
729 if ( wmsMaxWidth != -1 && width > wmsMaxWidth )
738 if ( wmsMaxHeightEnv != -1 && wmsMaxHeightProj != -1 )
741 wmsMaxHeight = std::min( wmsMaxHeightProj, wmsMaxHeightEnv );
746 wmsMaxHeight = std::max( wmsMaxHeightProj, wmsMaxHeightEnv );
749 if ( wmsMaxHeight != -1 && height > wmsMaxHeight )
763 switch ( mParameters.
format() )
771 if ( width > ( std::numeric_limits<int>::max() - 31 ) / depth )
774 const int bytes_per_line = ( ( width * depth + 31 ) >> 5 ) << 2;
776 if ( std::numeric_limits<int>::max() / bytes_per_line < height
777 || std::numeric_limits<int>::max() /
sizeof( uchar * ) <
static_cast<uint
>( height ) )
791 if ( !mParameters.
bbox().isEmpty() && extent.
isEmpty() )
816 if ( !mParameters.
bbox().isEmpty() && extent.
isEmpty() )
822 QString
crs = mParameters.
crs();
823 if (
crs.compare(
"CRS:84", Qt::CaseInsensitive ) == 0 )
825 crs = QString(
"EPSG:4326" );
835 if ( !extent.
isEmpty() && height > 0 && width > 0 )
837 const double mapRatio = extent.
width() / extent.
height();
838 const double imageRatio =
static_cast<double>( width ) /
static_cast<double>( height );
842 const double cellsize = ( extent.
width() /
static_cast<double>( width ) ) * 0.5 + ( extent.
height() /
static_cast<double>( height ) ) * 0.5;
843 width = extent.
width() / cellsize;
844 height = extent.
height() / cellsize;
854 else if ( height <= 0 )
860 return QSize( width, height );
863void QgsWmsRenderContext::removeUnwantedLayers()
865 QList<QgsMapLayer *>
layers;
873 if ( !layerScaleVisibility( nickname ) )
876 if ( mRestrictedLayers.contains( nickname ) )
887 if ( ! wfsLayers.contains(
layer->
id() ) )
902 for (
const auto &
layer : mExternalLayers )
911bool QgsWmsRenderContext::checkLayerReadPermissions(
QgsMapLayer *layer )
913#ifdef HAVE_SERVER_PYTHON_PLUGINS
914 if ( !accessControl()->layerReadPermission(
layer ) )
916 QString msg = QStringLiteral(
"Checking forbidden access for layer: %1" ).arg(
layer->
name() );
925#ifdef HAVE_SERVER_PYTHON_PLUGINS
934 mSocketFeedback = feedback;
939 return mSocketFeedback;
@ Info
Information message.
A helper class that centralizes restrictions given by all the access control filter plugins.
Exception thrown in case of malformed request.
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 the axis order is inverted for the CRS compared to the order east/north (longitude/la...
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Layer tree group node serves as a container for layers and further groups.
QgsLayerTreeGroup * findGroup(const QString &name)
Find group node with specified name.
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
Layer tree node points to a map layer.
This class is a base class for nodes in a layer tree.
@ NodeGroup
Container of other groups and layers.
QString shortName() const
Returns the short name of the layer used by QGIS Server to identify the layer.
Base class for all map layer types.
bool isInScaleRange(double scale) const
Tests whether the layer should be visible at the specified scale.
QgsMapLayerServerProperties * serverProperties()
Returns QGIS Server Properties for the map layer.
bool hasScaleBasedVisibility() const
Returns whether scale based visibility is enabled for the layer.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
A class to describe the version of a project.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
QString title() const
Returns the project's title.
QgsLayerTree * layerTreeRoot() const
Returns pointer to the root (invisible) node of the project's layer tree.
A rectangle specified with double values.
double width() const
Returns the width of the rectangle.
bool isEmpty() const
Returns true if the rectangle has no area.
double height() const
Returns the height of the rectangle.
void invert()
Swap x/y coordinates in the rectangle.
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins.
virtual QgsAccessControl * accessControls() const =0
Gets the registered access control filters.
virtual QgsServerSettings * serverSettings()=0
Returns the server settings.
Provides a way to retrieve settings by prioritizing according to environment variables,...
int wmsMaxWidth() const
Returns the server-wide max width of a WMS GetMap request.
int wmsMaxHeight() const
Returns the server-wide max height of a WMS GetMap request.
Exception thrown in case of malformed request.
Exception thrown when data access violates access controls.
@ QGIS_InvalidParameterValue
WMS parameter received from the client.
Provides an interface to retrieve and manipulate WMS parameters received from the client.
int wmsPrecisionAsInt() const
Returns WMS_PRECISION parameter as an int or its default value if not defined.
QStringList allLayersNickname() const
Returns nickname of layers found in LAYER and LAYERS parameters.
QgsProjectVersion versionAsNumber() const
Returns VERSION parameter if defined or its default value.
QString scale() const
Returns SCALE parameter or an empty string if none is defined.
double scaleAsDouble() const
Returns SCALE as a double.
QgsRectangle bboxAsRectangle() const
Returns BBOX as a rectangle if defined and valid.
double dpiAsDouble() const
Returns DPI parameter as an int or its default value if not defined.
QList< QgsWmsParametersLayer > layersParameters() const
Returns parameters for each layer found in LAYER/LAYERS.
QString bbox() const
Returns BBOX if defined or an empty string.
int heightAsInt() const
Returns HEIGHT parameter as an int or its default value if not defined.
Format format() const
Returns format.
int widthAsInt() const
Returns WIDTH parameter as an int or its default value if not defined.
QString sldBody() const
Returns SLD_body if defined or an empty string.
int imageQualityAsInt() const
Returns IMAGE_QUALITY parameter as an integer.
int srcHeightAsInt() const
Returns SRCHEIGHT parameter as an int or its default value if not defined.
QString imageQuality() const
Returns IMAGE_QUALITY parameter or an empty string if not defined.
QString crs() const
Returns CRS or an empty string if none is defined.
bool tiledAsBool() const
Returns TILED parameter as a boolean.
QString dpi() const
Returns DPI parameter or an empty string if not defined.
QStringList queryLayersNickname() const
Returns nickname of layers found in QUERY_LAYERS parameter.
int srcWidthAsInt() const
Returns SRCWIDTH parameter as an int or its default value if not defined.
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...
bool isExternalLayer(const QString &name) const
Returns true if the layer is an external layer, false otherwise.
bool isValidGroup(const QString &name) const
Returns true if name is a group.
QStringList flattenedQueryLayers(const QStringList &layerNames) const
Returns a list of query layer names where group names are replaced by the names of their layer compon...
QgsFeedback * socketFeedback() const
Returns the response feedback if any.
QgsWmsRenderContext(const QgsProject *project, QgsServerInterface *interface)
Constructor for QgsWmsRenderContext.
QList< QgsMapLayer * > layers() const
Returns a list of all layers read from the project.
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.
int mapWidth() const
Returns WIDTH or SRCWIDTH according to UseSrcWidthHeight flag.
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...
int tileBuffer() const
Returns the tile buffer value to use for rendering according to the current configuration.
bool updateExtent() const
Returns true if the extent has to be updated before the rendering, false otherwise.
const QgsServerSettings & settings() const
Returns settings of the server.
void setFlag(Flag flag, bool on=true)
Sets or unsets a rendering flag according to the on value.
bool isValidWidthHeight() const
Returns true if width and height are valid according to the maximum values defined within the project...
QList< QgsMapLayer * > layersFromGroup(const QString &nickname) const
Returns the group's layers list corresponding to the nickname, or an empty list if not found.
QgsWmsParameters parameters() const
Returns WMS parameters.
void setScaleDenominator(double scaleDenominator)
Sets a custom scale denominator.
QString style(const QgsMapLayer &layer) const
Returns a style's name for a specific layer.
QMap< QString, QList< QgsMapLayer * > > layerGroups() const
Returns a map having layer group names as keys and a list of layers as values.
double mapTileBuffer(int mapWidth) const
Returns the tile buffer in geographical units for the given map width in pixels.
QString layerNickname(const QgsMapLayer &layer) const
Returns the nickname (short name, id or name) of the layer according to the current configuration.
void setSocketFeedback(QgsFeedback *feedback)
Sets the response feedback.
double scaleDenominator() const
Returns the scale denominator to use for rendering according to the current configuration.
qreal dotsPerMm() const
Returns default dots per mm according to the current configuration.
bool testFlag(Flag flag) const
Returns the status of a rendering flag.
QDomElement sld(const QgsMapLayer &layer) const
Returns a SLD document for a specific layer.
bool isValidLayer(const QString &nickname) const
Returns true if the layer has to be rendered, false otherwise.
const QgsProject * project() const
Returns the project.
int precision() const
Returns the precision to use according to the current configuration.
int imageQuality() const
Returns the image quality to use for rendering according to the current configuration.
int mapHeight() const
Returns HEIGHT or SRCHEIGHT according to UseSrcWidthHeight flag.
bool renderMapTiles() const
Returns true if WMS requests should use the QgsMapSettings::RenderMapTile flag, so that no visible ar...
Flag
Available rendering options.
@ AddAllLayers
For GetPrint: add layers from LAYER(S) parameter.
SERVER_EXPORT int wmsTileBuffer(const QgsProject &project)
Returns the tile buffer in pixels for WMS images defined in a QGIS project.
SERVER_EXPORT QString wmsRootName(const QgsProject &project)
Returns the WMS root layer name defined in a QGIS project.
SERVER_EXPORT bool wmsSkipNameForGroup(const QgsProject &project)
Returns if name attribute should be skipped for groups in WMS capabilities document.
SERVER_EXPORT int wmsFeatureInfoPrecision(const QgsProject &project)
Returns the geometry precision for GetFeatureInfo request.
SERVER_EXPORT bool wmsUseLayerIds(const QgsProject &project)
Returns if layer ids are used as name in WMS.
SERVER_EXPORT QStringList wfsLayerIds(const QgsProject &project)
Returns the Layer ids list defined in a QGIS project as published in WFS.
SERVER_EXPORT bool wmsRenderMapTiles(const QgsProject &project)
Returns true if WMS requests should use the QgsMapSettings::RenderMapTile flag, so that no visible ar...
SERVER_EXPORT QStringList wmsRestrictedLayers(const QgsProject &project)
Returns the restricted layer name list.
SERVER_EXPORT int wmsImageQuality(const QgsProject &project)
Returns the quality for WMS images defined in a QGIS project.
SERVER_EXPORT int wmsMaxWidth(const QgsProject &project)
Returns the maximum width for WMS images defined in a QGIS project.
SERVER_EXPORT int wmsMaxHeight(const QgsProject &project)
Returns the maximum height for WMS images defined in a QGIS project.
Median cut implementation.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
const QgsCoordinateReferenceSystem & outputCrs
const QgsCoordinateReferenceSystem & crs