QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgselevationutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgselevationutils.cpp
3 ------------------
4 Date : November 2020
5 Copyright : (C) 2020 by Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgselevationutils.h"
17#include "qgsproject.h"
20
22{
23 const QMap<QString, QgsMapLayer *> &mapLayers = project->mapLayers();
24 QgsMapLayer *currentLayer = nullptr;
25
26 double min = std::numeric_limits<double>::quiet_NaN();
27 double max = std::numeric_limits<double>::quiet_NaN();
28
29 for ( QMap<QString, QgsMapLayer *>::const_iterator it = mapLayers.constBegin(); it != mapLayers.constEnd(); ++it )
30 {
31 currentLayer = it.value();
32
33 if ( !currentLayer->elevationProperties() || !currentLayer->elevationProperties()->hasElevation() )
34 continue;
35
36 const QgsDoubleRange layerRange = currentLayer->elevationProperties()->calculateZRange( currentLayer );
37 if ( layerRange.isInfinite() )
38 continue;
39
40 if ( layerRange.lower() > std::numeric_limits< double >::lowest() )
41 {
42 if ( std::isnan( min ) || layerRange.lower() < min )
43 min = layerRange.lower();
44 }
45
46 if ( layerRange.upper() < std::numeric_limits< double >::max() )
47 {
48 if ( std::isnan( max ) || layerRange.upper() > max )
49 max = layerRange.upper();
50 }
51 }
52
53 return QgsDoubleRange( std::isnan( min ) ? std::numeric_limits< double >::lowest() : min,
54 std::isnan( max ) ? std::numeric_limits< double >::max() : max );
55}
56
58{
59 const QMap<QString, QgsMapLayer *> &mapLayers = project->mapLayers();
60 QList< QgsMapLayer * > layers;
61 for ( QMap<QString, QgsMapLayer *>::const_iterator it = mapLayers.constBegin(); it != mapLayers.constEnd(); ++it )
62 {
63 if ( it.value() )
64 layers << it.value();
65 }
66
67 return significantZValuesForLayers( layers );
68}
69
70QList<double> QgsElevationUtils::significantZValuesForLayers( const QList<QgsMapLayer *> &layers )
71{
72 QSet< double > values;
73
74 for ( QgsMapLayer *currentLayer : layers )
75 {
76 if ( !currentLayer->elevationProperties() || !currentLayer->elevationProperties()->hasElevation() )
77 continue;
78
79 const QList< double > layerValues = currentLayer->elevationProperties()->significantZValues( currentLayer );
80 for ( double value : layerValues )
81 {
82 values.insert( value );
83 }
84 }
85
86 QList< double > res = qgis::setToList( values );
87 std::sort( res.begin(), res.end() );
88 return res;
89}
90
92{
93 return static_cast< bool >( layer->elevationProperties() );
94}
95
97{
98 switch ( layer->type() )
99 {
101 {
102 if ( QgsRasterLayerElevationProperties *properties = qobject_cast<QgsRasterLayerElevationProperties * >( layer->elevationProperties() ) )
103 {
104 properties->setEnabled( true );
106 // This could potentially be made smarter, eg by checking the data type of bands. But that's likely overkill..!
107 properties->setBandNumber( 1 );
108 return true;
109 }
110 break;
111 }
112
113 // can't automatically enable elevation for these layer types
122 break;
123 }
124 return false;
125}
126
@ RepresentsElevationSurface
Pixel values represent an elevation surface.
@ Group
Composite group layer. Added in QGIS 3.24.
@ Plugin
Plugin based layer.
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ Vector
Vector layer.
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
@ Mesh
Mesh layer. Added in QGIS 3.2.
@ Raster
Raster layer.
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
QgsRange which stores a range of double values.
Definition qgsrange.h:231
bool isInfinite() const
Returns true if the range consists of all possible values.
Definition qgsrange.h:285
static QList< double > significantZValuesForLayers(const QList< QgsMapLayer * > &layers)
Returns a list of significant elevation/z-values for the specified layers.
static QgsDoubleRange calculateZRangeForProject(QgsProject *project)
Calculates the elevation range for a project.
static bool enableElevationForLayer(QgsMapLayer *layer)
Automatically enables elevation for a map layer, using reasonable defaults.
static QList< double > significantZValuesForProject(QgsProject *project)
Returns a list of significant elevation/z-values for the specified project, using the values from lay...
static bool canEnableElevationForLayer(QgsMapLayer *layer)
Returns true if elevation can be enabled for a map layer.
virtual QgsDoubleRange calculateZRange(QgsMapLayer *layer) const
Attempts to calculate the overall elevation or z range for the specified layer, using the settings de...
virtual bool hasElevation() const
Returns true if the layer has an elevation or z component.
Base class for all map layer types.
Definition qgsmaplayer.h:76
Qgis::LayerType type
Definition qgsmaplayer.h:86
virtual QgsMapLayerElevationProperties * elevationProperties()
Returns the layer's elevation properties.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
T lower() const
Returns the lower bound of the range.
Definition qgsrange.h:78
T upper() const
Returns the upper bound of the range.
Definition qgsrange.h:85
Raster layer specific subclass of QgsMapLayerElevationProperties.