QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
19#include "qgsproject.h"
21
23{
24 const QMap<QString, QgsMapLayer *> &mapLayers = project->mapLayers();
25 QgsMapLayer *currentLayer = nullptr;
26
27 double min = std::numeric_limits<double>::quiet_NaN();
28 double max = std::numeric_limits<double>::quiet_NaN();
29
30 for ( QMap<QString, QgsMapLayer *>::const_iterator it = mapLayers.constBegin(); it != mapLayers.constEnd(); ++it )
31 {
32 currentLayer = it.value();
33
34 if ( !currentLayer->elevationProperties() || !currentLayer->elevationProperties()->hasElevation() )
35 continue;
36
37 const QgsDoubleRange layerRange = currentLayer->elevationProperties()->calculateZRange( currentLayer );
38 if ( layerRange.isInfinite() )
39 continue;
40
41 if ( layerRange.lower() > std::numeric_limits< double >::lowest() )
42 {
43 if ( std::isnan( min ) || layerRange.lower() < min )
44 min = layerRange.lower();
45 }
46
47 if ( layerRange.upper() < std::numeric_limits< double >::max() )
48 {
49 if ( std::isnan( max ) || layerRange.upper() > max )
50 max = layerRange.upper();
51 }
52 }
53
54 return QgsDoubleRange( std::isnan( min ) ? std::numeric_limits< double >::lowest() : min,
55 std::isnan( max ) ? std::numeric_limits< double >::max() : max );
56}
57
59{
60 const QMap<QString, QgsMapLayer *> &mapLayers = project->mapLayers();
61 QList< QgsMapLayer * > layers;
62 for ( QMap<QString, QgsMapLayer *>::const_iterator it = mapLayers.constBegin(); it != mapLayers.constEnd(); ++it )
63 {
64 if ( it.value() )
65 layers << it.value();
66 }
67
68 return significantZValuesForLayers( layers );
69}
70
71QList<double> QgsElevationUtils::significantZValuesForLayers( const QList<QgsMapLayer *> &layers )
72{
73 QSet< double > values;
74
75 for ( QgsMapLayer *currentLayer : layers )
76 {
77 if ( !currentLayer->elevationProperties() || !currentLayer->elevationProperties()->hasElevation() )
78 continue;
79
80 const QList< double > layerValues = currentLayer->elevationProperties()->significantZValues( currentLayer );
81 for ( double value : layerValues )
82 {
83 values.insert( value );
84 }
85 }
86
87 QList< double > res = qgis::setToList( values );
88 std::sort( res.begin(), res.end() );
89 return res;
90}
91
93{
94 return static_cast< bool >( layer->elevationProperties() );
95}
96
98{
99 switch ( layer->type() )
100 {
102 {
103 if ( QgsRasterLayerElevationProperties *properties = qobject_cast<QgsRasterLayerElevationProperties * >( layer->elevationProperties() ) )
104 {
105 properties->setEnabled( true );
107 // This could potentially be made smarter, eg by checking the data type of bands. But that's likely overkill..!
108 properties->setBandNumber( 1 );
109 return true;
110 }
111 break;
112 }
113
114 // can't automatically enable elevation for these layer types
123 break;
124 }
125 return false;
126}
127
@ RepresentsElevationSurface
Pixel values represent an elevation surface.
Definition qgis.h:4023
@ Group
Composite group layer. Added in QGIS 3.24.
Definition qgis.h:198
@ Plugin
Plugin based layer.
Definition qgis.h:193
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
Definition qgis.h:199
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
Definition qgis.h:196
@ Vector
Vector layer.
Definition qgis.h:191
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
Definition qgis.h:195
@ Mesh
Mesh layer. Added in QGIS 3.2.
Definition qgis.h:194
@ Raster
Raster layer.
Definition qgis.h:192
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
Definition qgis.h:197
QgsRange which stores a range of double values.
Definition qgsrange.h:233
bool isInfinite() const
Returns true if the range consists of all possible values.
Definition qgsrange.h:287
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:80
Qgis::LayerType type
Definition qgsmaplayer.h:90
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:109
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.