QGIS API Documentation
3.16.0-Hannover (43b64b13f3)
|
Go to the documentation of this file.
30 , mInterface( interface )
37 qDeleteAll( mExternalLayers );
38 mExternalLayers.clear();
45 initRestrictedLayers();
48 searchLayersToRender();
49 removeUnwantedLayers();
50 checkLayerReadPermissions();
52 std::reverse( mLayersToRender.begin(), mLayersToRender.end() );
69 return mFlags.testFlag( flag );
92 if ( mSlds.contains( nickname ) )
94 sld = mSlds[ nickname ];
105 if ( mStyles.contains( nickname ) )
107 style = mStyles[ nickname ];
172 if ( !mParameters.
dpi().isEmpty() )
183 std::function <QStringList(
const QString &name )> findLeaves = [ & ](
const QString & name ) -> QStringList
186 if ( mLayerGroups.contains( name ) )
188 const auto &
layers { mLayerGroups[ name ] };
189 for (
const auto &l :
layers )
193 if ( mLayerGroups.contains( nick ) )
195 _result.append( name );
199 _result.append( findLeaves( nick ) );
205 _result.append( name );
210 for (
const auto &name : constNicks )
212 result.append( findLeaves( name ) );
219 return mLayersToRender;
224 return mNicknameLayers.values();
229 double denominator = -1;
231 if ( mScaleDenominator >= 0 )
233 denominator = mScaleDenominator;
246 removeUnwantedLayers();
268 else if ( name.isEmpty() )
280 for (
auto layer : mLayersToRender )
294 return layer( nickname ) !=
nullptr;
299 return mLayerGroups.value( nickname );
304 return mLayerGroups.contains( name );
307 void QgsWmsRenderContext::initNicknameLayers()
318 initLayerGroupsRecursive( root, rootName.isEmpty() ? mProject->
title() : rootName );
321 void QgsWmsRenderContext::initLayerGroupsRecursive(
const QgsLayerTreeGroup *group,
const QString &groupName )
323 if ( !groupName.isEmpty() )
325 mLayerGroups[groupName] = QList<QgsMapLayer *>();
326 const auto projectLayerTreeRoot { mProject->
layerTreeRoot() };
327 const auto treeGroupLayers { group->
findLayers() };
330 if ( ! projectLayerTreeRoot->hasCustomLayerOrder() )
332 for (
const auto &tl : treeGroupLayers )
334 mLayerGroups[groupName].push_back( tl->layer() );
339 const auto projectLayerOrder { projectLayerTreeRoot->layerOrder() };
341 QList<QgsMapLayer *> groupLayersList;
342 for (
const auto &tl : treeGroupLayers )
344 groupLayersList << tl->layer();
346 for (
const auto &l : projectLayerOrder )
348 if ( groupLayersList.contains( l ) )
350 mLayerGroups[groupName].push_back( l );
360 QString name = child->customProperty( QStringLiteral(
"wmsShortName" ) ).toString();
362 if ( name.isEmpty() )
363 name = child->name();
365 initLayerGroupsRecursive(
static_cast<const QgsLayerTreeGroup *
>( child ), name );
371 void QgsWmsRenderContext::initRestrictedLayers()
373 mRestrictedLayers.clear();
379 QStringList restrictedLayersNames;
382 for (
const QString &l : qgis::as_const( restricted ) )
387 const QList<QgsLayerTreeLayer *> groupLayers = group->
findLayers();
390 restrictedLayersNames.append( treeLayer->name() );
395 restrictedLayersNames.append( l );
403 if ( restrictedLayersNames.contains(
layer->
name() ) )
410 void QgsWmsRenderContext::searchLayersToRender()
412 mLayersToRender.clear();
416 if ( ! mParameters.
sldBody().isEmpty() )
418 searchLayersToRenderSld();
422 searchLayersToRenderStyle();
428 for (
const QString &layerName : queryLayerNames )
430 const QList<QgsMapLayer *>
layers = mNicknameLayers.values( layerName );
432 if ( !mLayersToRender.contains( lyr ) )
434 mLayersToRender.append( lyr );
440 void QgsWmsRenderContext::searchLayersToRenderSld()
450 ( void )doc.setContent(
sld,
true );
451 QDomElement docEl = doc.documentElement();
453 QDomElement root = doc.firstChildElement(
"StyledLayerDescriptor" );
454 QDomElement namedElem = root.firstChildElement(
"NamedLayer" );
456 if ( docEl.isNull() )
461 QDomNodeList named = docEl.elementsByTagName(
"NamedLayer" );
462 for (
int i = 0; i < named.size(); ++i )
464 QDomNodeList names = named.item( i ).toElement().elementsByTagName(
"Name" );
465 if ( !names.isEmpty() )
467 QString lname = names.item( 0 ).toElement().text();
468 if ( mNicknameLayers.contains( lname ) )
470 mSlds[lname] = namedElem;
471 mLayersToRender.append( mNicknameLayers.values( lname ) );
473 else if ( mLayerGroups.contains( lname ) )
478 mSlds[name] = namedElem;
479 mLayersToRender.insert( 0,
layer );
485 param.mValue = lname;
493 void QgsWmsRenderContext::searchLayersToRenderStyle()
497 const QString nickname = param.mNickname;
498 const QString
style = param.mStyle;
502 std::unique_ptr<QgsMapLayer>
layer = qgis::make_unique< QgsRasterLayer >( param.mExternalUri, param.mNickname, QStringLiteral(
"wms" ) );
507 mExternalLayers.append(
layer.release() );
508 mLayersToRender.append( mExternalLayers.last() );
511 else if ( mNicknameLayers.contains( nickname ) )
513 if ( !
style.isEmpty() )
515 mStyles[nickname] =
style;
518 mLayersToRender.append( mNicknameLayers.values( nickname ) );
520 else if ( mLayerGroups.contains( nickname ) )
527 if ( !
style.isEmpty() )
529 mStyles[ nickname ] =
style;
536 mLayersToRender.append( mNicknameLayers.values( name ) );
542 param.mValue = nickname;
549 bool QgsWmsRenderContext::layerScaleVisibility(
const QString &name )
const
551 bool visible =
false;
553 if ( ! mNicknameLayers.contains( name ) )
558 const QList<QgsMapLayer *>
layers = mNicknameLayers.values( name );
562 bool useScaleConstraint = (
scaleDenominator() > 0 && scaleBasedVisibility );
612 if ( wmsMaxWidthEnv != -1 && wmsMaxWidthProj != -1 )
615 wmsMaxWidth = std::min( wmsMaxWidthProj, wmsMaxWidthEnv );
620 wmsMaxWidth = std::max( wmsMaxWidthProj, wmsMaxWidthEnv );
632 if ( wmsMaxHeightEnv != -1 && wmsMaxHeightProj != -1 )
635 wmsMaxHeight = std::min( wmsMaxHeightProj, wmsMaxHeightEnv );
640 wmsMaxHeight = std::max( wmsMaxHeightProj, wmsMaxHeightEnv );
656 switch ( mParameters.
format() )
658 case QgsWmsParameters::Format::JPG:
664 const int bytes_per_line = ( (
mapWidth() * depth + 31 ) >> 5 ) << 2;
666 if ( std::numeric_limits<int>::max() / depth < static_cast<uint>(
mapWidth() )
667 || bytes_per_line <= 0
669 || std::numeric_limits<int>::max() /
static_cast<uint
>( bytes_per_line ) <
static_cast<uint
>(
mapHeight() )
670 || std::numeric_limits<int>::max() /
sizeof( uchar * ) <
static_cast<uint
>(
mapHeight() ) )
684 if ( !mParameters.
bbox().isEmpty() && extent.
isEmpty() )
709 if ( !mParameters.
bbox().isEmpty() && extent.
isEmpty() )
715 QString
crs = mParameters.
crs();
716 if (
crs.compare(
"CRS:84", Qt::CaseInsensitive ) == 0 )
718 crs = QString(
"EPSG:4326" );
728 if ( !extent.
isEmpty() && height > 0 && width > 0 )
730 const double mapRatio = extent.
width() / extent.
height();
731 const double imageRatio =
static_cast<double>( width ) /
static_cast<double>( height );
735 const double cellsize = ( extent.
width() /
static_cast<double>( width ) ) * 0.5 + ( extent.
height() /
static_cast<double>( height ) ) * 0.5;
736 width = extent.
width() / cellsize;
737 height = extent.
height() / cellsize;
747 else if ( height <= 0 )
753 return QSize( width, height );
756 void QgsWmsRenderContext::removeUnwantedLayers()
758 QList<QgsMapLayer *>
layers;
766 if ( !layerScaleVisibility( nickname ) )
769 if ( mRestrictedLayers.contains( nickname ) )
780 if ( ! wfsLayers.contains(
layer->
id() ) )
795 for (
const auto &
layer : mExternalLayers )
797 if (
layer->
name().compare( name ) == 0 )
804 void QgsWmsRenderContext::checkLayerReadPermissions()
806 #ifdef HAVE_SERVER_PYTHON_PLUGINS
807 for (
const auto layer : mLayersToRender )
809 if ( !accessControl()->layerReadPermission(
layer ) )
817 #ifdef HAVE_SERVER_PYTHON_PLUGINS
Exception thrown when data access violates access controls.
@ NodeGroup
Container of other groups and layers.
@ QGIS_InvalidParameterValue
Provides an interface to retrieve and manipulate WMS parameters received from the client.
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
QString title() const
Returns the project's title.
double height() const SIP_HOLDGIL
Returns the height of the rectangle.
int mapWidth() const
Returns WIDTH or SRCWIDTH according to UseSrcWidthHeight flag.
QList< QgsMapLayer * > layersFromGroup(const QString &nickname) const
Returns the group's layers list corresponding to the nickname, or an empty list if not found.
This class is a base class for nodes in a layer tree.
const QgsCoordinateReferenceSystem & outputCrs
int wmsMaxWidth() const
Returns the server-wide max width of a WMS GetMap request.
int wmsPrecisionAsInt() const
Returns WMS_PRECISION parameter as an int or its default value if not defined.
bool isExternalLayer(const QString &name) const
Returns true if the layer is an external layer, false otherwise.
int heightAsInt() const
Returns HEIGHT parameter as an int or its default value if not defined.
QString style(const QgsMapLayer &layer) const
Returns a style's name for a specific layer.
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
void invert()
Swap x/y coordinates in the rectangle.
int srcHeightAsInt() const
Returns SRCHEIGHT parameter as an int or its default value if not defined.
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 setFlag(Flag flag, bool on=true)
Sets or unsets a rendering flag according to the on value.
QString shortName() const
Returns the short name of the layer used by QGIS Server to identify the layer.
QString bbox() const
Returns BBOX if defined or an empty string.
void setParameters(const QgsWmsParameters ¶meters)
Sets WMS parameters.
bool updateExtent() const
Returns true if the extent has to be updated before the rendering, false otherwise.
QMap< QString, QList< QgsMapLayer * > > layerGroups() const
Returns a map having layer group names as keys and a list of layers as values.
bool isValidWidthHeight() const
Returns true if width and height are valid according to the maximum values defined within the project...
bool hasAxisInverted() const
Returns whether axis is inverted (e.g., for WMS 1.3) for the CRS.
int imageQuality() const
Returns the image quality to use for rendering according to the current configuration.
QString dpi() const
Returns DPI parameter or an empty string if not defined.
virtual QgsAccessControl * accessControls() const =0
Gets the registered access control filters.
A rectangle specified with double values.
QgsWmsRenderContext(const QgsProject *project, QgsServerInterface *interface)
Constructor for QgsWmsRenderContext.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
QString imageQuality() const
Returns IMAGE_QUALITY parameter or an empty string if not defined.
Provides a way to retrieve settings by prioritizing according to environment variables,...
SERVER_EXPORT bool wmsUseLayerIds(const QgsProject &project)
Returns if layer ids are used as name in WMS.
int tileBuffer() const
Returns the tile buffer value to use for rendering according to the current configuration.
QStringList queryLayersNickname() const
Returns nickname of layers found in QUERY_LAYERS parameter.
bool isInScaleRange(double scale) const
Tests whether the layer should be visible at the specified scale.
QString sldBody() const
Returns SLD_body if defined or an empty string.
SERVER_EXPORT int wmsFeatureInfoPrecision(const QgsProject &project)
Returns the geometry precision for GetFeatureInfo request.
QgsLayerTree * layerTreeRoot() const
Returns pointer to the root (invisible) node of the project's layer tree.
int widthAsInt() const
Returns WIDTH parameter as an int or its default value if not defined.
SERVER_EXPORT int wmsMaxHeight(const QgsProject &project)
Returns the maximum height for WMS images defined in a QGIS project.
SERVER_EXPORT QStringList wfsLayerIds(const QgsProject &project)
Returns the Layer ids list defined in a QGIS project as published in WFS.
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...
QStringList flattenedQueryLayers() const
Returns a list of query layer names where group names are replaced by the names of their layer compon...
QgsProjectVersion versionAsNumber() const
Returns VERSION parameter if defined or its default value.
SERVER_EXPORT int wmsTileBuffer(const QgsProject &project)
Returns the tile buffer in pixels for WMS images defined in a QGIS project.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
void setScaleDenominator(double scaleDenominator)
Sets a custom scale denominator.
Layer tree node points to a map layer.
SERVER_EXPORT QStringList wmsRestrictedLayers(const QgsProject &project)
Returns the restricted layer name list.
Layer tree group node serves as a container for layers and further groups.
double dpiAsDouble() const
Returns DPI parameter as an int or its default value if not defined.
const QgsServerSettings & settings() const
Returns settings of the server.
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< QgsWmsParametersLayer > layersParameters() const
Returns parameters for each layer found in LAYER/LAYERS.
int precision() const
Returns the precision to use according to the current configuration.
virtual QgsServerSettings * serverSettings()=0
Returns the server settings.
bool hasScaleBasedVisibility() const
Returns whether scale based visibility is enabled for the layer.
QgsRectangle bboxAsRectangle() const
Returns BBOX as a rectangle if defined and valid.
QDomElement sld(const QgsMapLayer &layer) const
Returns a SLD document for a specific layer.
Flag
Available rendering options.
QString layerNickname(const QgsMapLayer &layer) const
Returns the nickname (short name, id or name) of the layer according to the current configuration.
Format format() const
Returns format.
This class represents a coordinate reference system (CRS).
SERVER_EXPORT int wmsMaxWidth(const QgsProject &project)
Returns the maximum width for WMS images defined in a QGIS project.
double scaleAsDouble() const
Returns SCALE as a double.
A helper class that centralizes restrictions given by all the access control filter plugins.
Median cut implementation.
bool isValidGroup(const QString &name) const
Returns true if name is a group.
double scaleDenominator() const
Returns the scale denominator to use for rendering according to the current configuration.
SERVER_EXPORT QString wmsRootName(const QgsProject &project)
Returns the WMS root layer name defined in a QGIS project.
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...
Exception thrown in case of malformed request.
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
Base class for all map layer types.
bool testFlag(Flag flag) const
Returns the status of a rendering flag.
QString scale() const
Returns SCALE parameter or an empty string if none is defined.
int wmsMaxHeight() const
Returns the server-wide max height of a WMS GetMap request.
const QgsProject * project() const
Returns the project.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
double mapTileBuffer(int mapWidth) const
Returns the tile buffer in geographical units for the given map width in pixels.
SERVER_EXPORT int wmsImageQuality(const QgsProject &project)
Returns the quality for WMS images defined in a QGIS project.
bool tiledAsBool() const
Returns TILED parameter as a boolean.
WMS parameter received from the client.
~QgsWmsRenderContext()
Destructor for QgsWmsRenderContext.
qreal dotsPerMm() const
Returns default dots per mm according to the current configuration.
int imageQualityAsInt() const
Returns IMAGE_QUALITY parameter as an integer.
QList< QgsMapLayer * > layersToRender() const
Returns a list of all layers to actually render according to the current configuration.
QgsLayerTreeGroup * findGroup(const QString &name)
Find group node with specified name.
bool isEmpty() const
Returns true if the rectangle is empty.
int mapHeight() const
Returns HEIGHT or SRCHEIGHT according to UseSrcWidthHeight flag.
QList< QgsMapLayer * > layers() const
Returns a list of all layers read from the project.
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins.
A class to describe the version of a project.
int srcWidthAsInt() const
Returns SRCWIDTH parameter as an int or its default value if not defined.
QgsWmsParameters parameters() const
Returns WMS parameters.