QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
qgspointcloudlayerelevationproperties.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspointcloudlayerelevationproperties.cpp
3 ---------------
4 begin : November 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall dot dawson dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgsapplication.h"
22#include "qgscolorutils.h"
23#include "qgspointcloudlayer.h"
24#include "qgsvirtualpointcloudprovider.h"
25
26#include "moc_qgspointcloudlayerelevationproperties.cpp"
27
30{
32
33 if ( QgsPointCloudLayer *pcLayer = qobject_cast< QgsPointCloudLayer * >( parent ) )
34 {
35 connect( pcLayer, &QgsPointCloudLayer::rendererChanged, this, [this]
36 {
37 if ( mRespectLayerColors )
39 } );
40 }
41}
42
44{
45 return true;
46}
47
48QDomElement QgsPointCloudLayerElevationProperties::writeXml( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context )
49{
50 QDomElement element = document.createElement( QStringLiteral( "elevation" ) );
51 writeCommonProperties( element, document, context );
52
53 element.setAttribute( QStringLiteral( "max_screen_error" ), qgsDoubleToString( mMaximumScreenError ) );
54 element.setAttribute( QStringLiteral( "max_screen_error_unit" ), QgsUnitTypes::encodeUnit( mMaximumScreenErrorUnit ) );
55 element.setAttribute( QStringLiteral( "point_size" ), qgsDoubleToString( mPointSize ) );
56 element.setAttribute( QStringLiteral( "point_size_unit" ), QgsUnitTypes::encodeUnit( mPointSizeUnit ) );
57 element.setAttribute( QStringLiteral( "point_symbol" ), qgsEnumValueToKey( mPointSymbol ) );
58 element.setAttribute( QStringLiteral( "point_color" ), QgsColorUtils::colorToString( mPointColor ) );
59 element.setAttribute( QStringLiteral( "respect_layer_colors" ), mRespectLayerColors ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
60 element.setAttribute( QStringLiteral( "opacity_by_distance" ), mApplyOpacityByDistanceEffect ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
61
62 parentElement.appendChild( element );
63 return element;
64}
65
66bool QgsPointCloudLayerElevationProperties::readXml( const QDomElement &element, const QgsReadWriteContext &context )
67{
68 const QDomElement elevationElement = element.firstChildElement( QStringLiteral( "elevation" ) ).toElement();
69 readCommonProperties( elevationElement, context );
70
71 mMaximumScreenError = elevationElement.attribute( QStringLiteral( "max_screen_error" ), QStringLiteral( "0.3" ) ).toDouble();
72 bool ok = false;
73 mMaximumScreenErrorUnit = QgsUnitTypes::decodeRenderUnit( elevationElement.attribute( QStringLiteral( "max_screen_error_unit" ) ), &ok );
74 if ( !ok )
75 mMaximumScreenErrorUnit = Qgis::RenderUnit::Millimeters;
76 mPointSize = elevationElement.attribute( QStringLiteral( "point_size" ), QStringLiteral( "0.6" ) ).toDouble();
77 mPointSizeUnit = QgsUnitTypes::decodeRenderUnit( elevationElement.attribute( QStringLiteral( "point_size_unit" ) ), &ok );
78 if ( !ok )
79 mPointSizeUnit = Qgis::RenderUnit::Millimeters;
80 mPointSymbol = qgsEnumKeyToValue( elevationElement.attribute( QStringLiteral( "point_symbol" ) ), Qgis::PointCloudSymbol::Square );
81 const QString colorString = elevationElement.attribute( QStringLiteral( "point_color" ) );
82 if ( !colorString.isEmpty() )
83 {
84 mPointColor = QgsColorUtils::colorFromString( elevationElement.attribute( QStringLiteral( "point_color" ) ) );
85 }
86 else
87 {
89 }
90 mRespectLayerColors = elevationElement.attribute( QStringLiteral( "respect_layer_colors" ), QStringLiteral( "1" ) ).toInt();
91 mApplyOpacityByDistanceEffect = elevationElement.attribute( QStringLiteral( "opacity_by_distance" ) ).toInt();
92
93 return true;
94}
95
97{
98 auto res = std::make_unique< QgsPointCloudLayerElevationProperties >( nullptr );
99 res->copyCommonProperties( this );
100
101 res->mMaximumScreenError = mMaximumScreenError;
102 res->mMaximumScreenErrorUnit = mMaximumScreenErrorUnit;
103 res->mPointSize = mPointSize;
104 res->mPointSizeUnit = mPointSizeUnit;
105 res->mPointSymbol = mPointSymbol;
106 res->mPointColor = mPointColor;
107 res->mRespectLayerColors = mRespectLayerColors;
108 res->mApplyOpacityByDistanceEffect = mApplyOpacityByDistanceEffect;
109
110 return res.release();
111}
112
114{
115 QStringList properties;
116 properties << tr( "Scale: %1" ).arg( mZScale );
117 properties << tr( "Offset: %1" ).arg( mZOffset );
118 return QStringLiteral( "<ul><li>%1</li></ul>" ).arg( properties.join( QLatin1String( "</li><li>" ) ) );
119}
120
122{
123 // TODO -- test actual point cloud z range
124 return true;
125}
126
128{
129 if ( QgsPointCloudLayer *pcLayer = qobject_cast< QgsPointCloudLayer * >( layer ) )
130 {
131 if ( pcLayer->dataProvider() )
132 {
133 double zMin = std::numeric_limits<double>::quiet_NaN();
134 double zMax = std::numeric_limits<double>::quiet_NaN();
135 const QgsPointCloudStatistics stats = pcLayer->statistics();
136 if ( !stats.statisticsMap().isEmpty() )
137 {
138 // try to fetch z range from provider metadata
139 zMin = stats.minimum( QStringLiteral( "Z" ) );
140 zMax = stats.maximum( QStringLiteral( "Z" ) );
141 }
142 // try to fetch the elevation properties from virtual point cloud metadata
143 else if ( QgsVirtualPointCloudProvider *virtualProvider = dynamic_cast< QgsVirtualPointCloudProvider * >( pcLayer->dataProvider() ) )
144 {
145 for ( QgsPointCloudSubIndex subIndex : virtualProvider->subIndexes() )
146 {
147 const QgsDoubleRange newRange = subIndex.zRange();
148 if ( newRange.isInfinite() ) continue;
149 zMin = std::isnan( zMin ) ? newRange.lower() : std::min( zMin, newRange.lower() );
150 zMax = std::isnan( zMax ) ? newRange.upper() : std::max( zMax, newRange.upper() );
151 }
152 }
153
154 if ( !std::isnan( zMin ) && !std::isnan( zMax ) )
155 {
156 return QgsDoubleRange( zMin * mZScale + mZOffset, zMax * mZScale + mZOffset );
157 }
158 }
159 }
160
161 return QgsDoubleRange();
162}
163
165{
166 const QgsDoubleRange range = calculateZRange( layer );
167 if ( !range.isInfinite() && range.lower() != range.upper() )
168 return {range.lower(), range.upper() };
169 else if ( !range.isInfinite() )
170 return {range.lower() };
171 else
172 return {};
173}
174
179
181{
182 if ( qgsDoubleNear( error, mMaximumScreenError ) )
183 return;
184
185 mMaximumScreenError = error;
186 emit changed();
188}
189
191{
192 if ( unit == mMaximumScreenErrorUnit )
193 return;
194
195 mMaximumScreenErrorUnit = unit;
196 emit changed();
198}
199
204
206{
207 if ( mPointSymbol == symbol )
208 return;
209
210 mPointSymbol = symbol;
211 emit changed();
213}
214
216{
217 if ( color == mPointColor )
218 return;
219
220 mPointColor = color;
221 emit changed();
223}
224
226{
227 if ( apply == mApplyOpacityByDistanceEffect )
228 return;
229
230 mApplyOpacityByDistanceEffect = apply;
231 emit changed();
232
233 // turning ON opacity by distance requires a profile regeneration, turning it off does not.
234 if ( mApplyOpacityByDistanceEffect )
236 else
238}
239
241{
242 if ( qgsDoubleNear( size, mPointSize ) )
243 return;
244
245 mPointSize = size;
246 emit changed();
248}
249
251{
252 if ( mPointSizeUnit == units )
253 return;
254
255 mPointSizeUnit = units;
256 emit changed();
258}
259
261{
262 if ( mRespectLayerColors == enabled )
263 return;
264
265 mRespectLayerColors = enabled;
266 emit changed();
267
268 // turning ON respect layer colors requires a profile regeneration, turning it off does not.
269 if ( mRespectLayerColors )
271 else
273}
PointCloudSymbol
Rendering symbols for point cloud points.
Definition qgis.h:4246
@ Square
Renders points as squares.
Definition qgis.h:4247
RenderUnit
Rendering size units.
Definition qgis.h:5183
@ Millimeters
Millimeters.
Definition qgis.h:5184
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes.
QColor fetchRandomStyleColor() const
Returns a random color for use with a new symbol style (e.g.
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
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
void writeCommonProperties(QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context)
Writes common class properties to a DOM element, to be used later with readXml().
void profileGenerationPropertyChanged()
Emitted when any of the elevation properties which relate solely to generation of elevation profiles ...
QgsMapLayerElevationProperties(QObject *parent)
Constructor for QgsMapLayerElevationProperties, with the specified parent object.
void readCommonProperties(const QDomElement &element, const QgsReadWriteContext &context)
Reads common class properties from a DOM element previously written by writeXml().
void changed()
Emitted when any of the elevation properties have changed.
void profileRenderingPropertyChanged()
Emitted when any of the elevation properties which relate solely to presentation of elevation results...
Base class for all map layer types.
Definition qgsmaplayer.h:80
void rendererChanged()
Signal emitted when renderer is changed.
void setRespectLayerColors(bool enabled)
Sets whether layer coloring should be respected when rendering elevation profile plots.
QString htmlSummary() const override
Returns a HTML formatted summary of the properties.
void setPointColor(const QColor &color)
Sets the color used drawing points in elevation profile charts.
bool showByDefaultInElevationProfilePlots() const override
Returns true if the layer should be visible by default in newly created elevation profile plots.
bool hasElevation() const override
Returns true if the layer has an elevation or z component.
void setApplyOpacityByDistanceEffect(bool apply)
Sets whether a reduced opacity by distance from profile curve effect should be applied when drawing p...
QDomElement writeXml(QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context) override
Writes the properties to a DOM element, to be used later with readXml().
void setPointSize(double size)
Sets the point size used for drawing points in elevation profile charts.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the elevation properties from a DOM element previously written by writeXml().
void setPointSymbol(Qgis::PointCloudSymbol symbol)
Sets the symbol used drawing points in elevation profile charts.
QgsDoubleRange calculateZRange(QgsMapLayer *layer) const override
Attempts to calculate the overall elevation or z range for the specified layer, using the settings de...
void setMaximumScreenError(double error)
Sets the maximum screen error allowed when generating elevation profiles for the point cloud.
bool isVisibleInZRange(const QgsDoubleRange &range, QgsMapLayer *layer=nullptr) const override
Returns true if the layer should be visible and rendered for the specified z range.
void setPointSizeUnit(const Qgis::RenderUnit units)
Sets the units used for the point size used for drawing points in elevation profile charts.
QgsPointCloudLayerElevationProperties * clone() const override
Creates a clone of the properties.
void setMaximumScreenErrorUnit(Qgis::RenderUnit unit)
Sets the unit for the maximum screen error allowed when generating elevation profiles for the point c...
QList< double > significantZValues(QgsMapLayer *layer) const override
Returns a list of significant elevation/z-values for the specified layer, using the settings defined ...
Qgis::PointCloudSymbol pointSymbol() const
Returns the symbol used drawing points in elevation profile charts.
QgsPointCloudLayerElevationProperties(QObject *parent)
Constructor for QgsPointCloudLayerElevationProperties, with the specified parent object.
Represents a map layer supporting display of point clouds.
Used to store statistics of a point cloud dataset.
double maximum(const QString &attribute) const
Returns the maximum value for the attribute attribute If no matching statistic is available then NaN ...
double minimum(const QString &attribute) const
Returns the minimum value for the attribute attribute If no matching statistic is available then NaN ...
QMap< QString, QgsPointCloudAttributeStatistics > statisticsMap() const
Returns a map object containing all the statistics.
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
A container for the context for various read/write operations on objects.
static Q_INVOKABLE Qgis::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
Definition qgis.h:6817
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6524
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:6798
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6607