QGIS API Documentation 4.3.0-Master (d3b565c628d)
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 QList< QgsMapLayer * > layers;
26 layers.reserve( mapLayers.size() );
27 for ( QMap<QString, QgsMapLayer *>::const_iterator it = mapLayers.constBegin(); it != mapLayers.constEnd(); ++it )
28 {
29 if ( it.value() )
30 layers << it.value();
31 }
32
33 return calculateZRangeForLayers( layers );
34}
35
36QgsDoubleRange QgsElevationUtils::calculateZRangeForLayers( const QList< QgsMapLayer * > &layers )
37{
38 double min = std::numeric_limits<double>::quiet_NaN();
39 double max = std::numeric_limits<double>::quiet_NaN();
40
41 for ( QgsMapLayer *currentLayer : layers )
42 {
43 if ( !currentLayer->elevationProperties() || !currentLayer->elevationProperties()->hasElevation() )
44 continue;
45
46 const QgsDoubleRange layerRange = currentLayer->elevationProperties()->calculateZRange( currentLayer );
47 if ( layerRange.isInfinite() )
48 continue;
49
50 if ( layerRange.lower() > std::numeric_limits< double >::lowest() )
51 {
52 if ( std::isnan( min ) || layerRange.lower() < min )
53 min = layerRange.lower();
54 }
55
56 if ( layerRange.upper() < std::numeric_limits< double >::max() )
57 {
58 if ( std::isnan( max ) || layerRange.upper() > max )
59 max = layerRange.upper();
60 }
61 }
62
63 return QgsDoubleRange( std::isnan( min ) ? std::numeric_limits< double >::lowest() : min, std::isnan( max ) ? std::numeric_limits< double >::max() : max );
64}
65
67{
68 const QMap<QString, QgsMapLayer *> &mapLayers = project->mapLayers();
69 QList< QgsMapLayer * > layers;
70 for ( QMap<QString, QgsMapLayer *>::const_iterator it = mapLayers.constBegin(); it != mapLayers.constEnd(); ++it )
71 {
72 if ( it.value() )
73 layers << it.value();
74 }
75
76 return significantZValuesForLayers( layers );
77}
78
79QList<double> QgsElevationUtils::significantZValuesForLayers( const QList<QgsMapLayer *> &layers )
80{
81 QSet< double > values;
82
83 for ( QgsMapLayer *currentLayer : layers )
84 {
85 if ( !currentLayer->elevationProperties() || !currentLayer->elevationProperties()->hasElevation() )
86 continue;
87
88 const QList< double > layerValues = currentLayer->elevationProperties()->significantZValues( currentLayer );
89 for ( double value : layerValues )
90 {
91 values.insert( value );
92 }
93 }
94
95 QList< double > res = qgis::setToList( values );
96 std::sort( res.begin(), res.end() );
97 return res;
98}
99
101{
102 return static_cast< bool >( layer->elevationProperties() );
103}
104
106{
107 switch ( layer->type() )
108 {
110 {
111 if ( QgsRasterLayerElevationProperties *properties = qobject_cast<QgsRasterLayerElevationProperties * >( layer->elevationProperties() ) )
112 {
113 properties->setEnabled( true );
115 // This could potentially be made smarter, eg by checking the data type of bands. But that's likely overkill..!
116 properties->setBandNumber( 1 );
117 return true;
118 }
119 break;
120 }
121
122 // can't automatically enable elevation for these layer types
131 break;
132 }
133 return false;
134}
@ RepresentsElevationSurface
Pixel values represent an elevation surface.
Definition qgis.h:4242
@ Group
Composite group layer. Added in QGIS 3.24.
Definition qgis.h:214
@ Plugin
Plugin based layer.
Definition qgis.h:209
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
Definition qgis.h:215
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
Definition qgis.h:212
@ Vector
Vector layer.
Definition qgis.h:207
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
Definition qgis.h:211
@ Mesh
Mesh layer. Added in QGIS 3.2.
Definition qgis.h:210
@ Raster
Raster layer.
Definition qgis.h:208
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
Definition qgis.h:213
QgsRange which stores a range of double values.
Definition qgsrange.h:217
bool isInfinite() const
Returns true if the range consists of all possible values.
Definition qgsrange.h:266
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 QgsDoubleRange calculateZRangeForLayers(const QList< QgsMapLayer * > &layers)
Calculates the elevation range for the specified layers.
static bool canEnableElevationForLayer(QgsMapLayer *layer)
Returns true if elevation can be enabled for a map layer.
Base class for all map layer types.
Definition qgsmaplayer.h:83
Qgis::LayerType type
Definition qgsmaplayer.h:93
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:114
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:79
T upper() const
Returns the upper bound of the range.
Definition qgsrange.h:86
Raster layer specific subclass of QgsMapLayerElevationProperties.