QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #include "qgspointcloudlayer.h"
20 #include "qgssymbollayerutils.h"
21 #include "qgsapplication.h"
22 #include "qgscolorschemeregistry.h"
23 
26 {
28 
29  if ( QgsPointCloudLayer *pcLayer = qobject_cast< QgsPointCloudLayer * >( parent ) )
30  {
31  connect( pcLayer, &QgsPointCloudLayer::rendererChanged, this, [this]
32  {
33  if ( mRespectLayerColors )
35  } );
36  }
37 }
38 
40 {
41  return true;
42 }
43 
44 QDomElement QgsPointCloudLayerElevationProperties::writeXml( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context )
45 {
46  QDomElement element = document.createElement( QStringLiteral( "elevation" ) );
47  writeCommonProperties( element, document, context );
48 
49  element.setAttribute( QStringLiteral( "max_screen_error" ), qgsDoubleToString( mMaximumScreenError ) );
50  element.setAttribute( QStringLiteral( "max_screen_error_unit" ), QgsUnitTypes::encodeUnit( mMaximumScreenErrorUnit ) );
51  element.setAttribute( QStringLiteral( "point_size" ), qgsDoubleToString( mPointSize ) );
52  element.setAttribute( QStringLiteral( "point_size_unit" ), QgsUnitTypes::encodeUnit( mPointSizeUnit ) );
53  element.setAttribute( QStringLiteral( "point_symbol" ), qgsEnumValueToKey( mPointSymbol ) );
54  element.setAttribute( QStringLiteral( "point_color" ), QgsSymbolLayerUtils::encodeColor( mPointColor ) );
55  element.setAttribute( QStringLiteral( "respect_layer_colors" ), mRespectLayerColors ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
56  element.setAttribute( QStringLiteral( "opacity_by_distance" ), mApplyOpacityByDistanceEffect ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
57 
58  parentElement.appendChild( element );
59  return element;
60 }
61 
62 bool QgsPointCloudLayerElevationProperties::readXml( const QDomElement &element, const QgsReadWriteContext &context )
63 {
64  const QDomElement elevationElement = element.firstChildElement( QStringLiteral( "elevation" ) ).toElement();
65  readCommonProperties( elevationElement, context );
66 
67  mMaximumScreenError = elevationElement.attribute( QStringLiteral( "max_screen_error" ), QStringLiteral( "0.3" ) ).toDouble();
68  bool ok = false;
69  mMaximumScreenErrorUnit = QgsUnitTypes::decodeRenderUnit( elevationElement.attribute( QStringLiteral( "max_screen_error_unit" ) ), &ok );
70  if ( !ok )
71  mMaximumScreenErrorUnit = QgsUnitTypes::RenderMillimeters;
72  mPointSize = elevationElement.attribute( QStringLiteral( "point_size" ), QStringLiteral( "0.6" ) ).toDouble();
73  mPointSizeUnit = QgsUnitTypes::decodeRenderUnit( elevationElement.attribute( QStringLiteral( "point_size_unit" ) ), &ok );
74  if ( !ok )
75  mPointSizeUnit = QgsUnitTypes::RenderMillimeters;
76  mPointSymbol = qgsEnumKeyToValue( elevationElement.attribute( QStringLiteral( "point_symbol" ) ), Qgis::PointCloudSymbol::Square );
77  const QString colorString = elevationElement.attribute( QStringLiteral( "point_color" ) );
78  if ( !colorString.isEmpty() )
79  {
80  mPointColor = QgsSymbolLayerUtils::decodeColor( elevationElement.attribute( QStringLiteral( "point_color" ) ) );
81  }
82  else
83  {
85  }
86  mRespectLayerColors = elevationElement.attribute( QStringLiteral( "respect_layer_colors" ), QStringLiteral( "1" ) ).toInt();
87  mApplyOpacityByDistanceEffect = elevationElement.attribute( QStringLiteral( "opacity_by_distance" ) ).toInt();
88 
89  return true;
90 }
91 
93 {
94  std::unique_ptr< QgsPointCloudLayerElevationProperties > res = std::make_unique< QgsPointCloudLayerElevationProperties >( nullptr );
95  res->copyCommonProperties( this );
96 
97  res->mMaximumScreenError = mMaximumScreenError;
98  res->mMaximumScreenErrorUnit = mMaximumScreenErrorUnit;
99  res->mPointSize = mPointSize;
100  res->mPointSizeUnit = mPointSizeUnit;
101  res->mPointSymbol = mPointSymbol;
102  res->mPointColor = mPointColor;
103  res->mRespectLayerColors = mRespectLayerColors;
104  res->mApplyOpacityByDistanceEffect = mApplyOpacityByDistanceEffect;
105 
106  return res.release();
107 }
108 
110 {
111  QStringList properties;
112  properties << tr( "Scale: %1" ).arg( mZScale );
113  properties << tr( "Offset: %1" ).arg( mZOffset );
114  return QStringLiteral( "<ul><li>%1</li></ul>" ).arg( properties.join( QLatin1String( "</li><li>" ) ) );
115 }
116 
118 {
119  // TODO -- test actual point cloud z range
120  return true;
121 }
122 
124 {
125  if ( QgsPointCloudLayer *pcLayer = qobject_cast< QgsPointCloudLayer * >( layer ) )
126  {
127  if ( pcLayer->dataProvider() )
128  {
129  const QgsPointCloudStatistics stats = pcLayer->statistics();
130 
131  // try to fetch z range from provider metadata
132  const double zMin = stats.minimum( QStringLiteral( "Z" ) );
133  const double zMax = stats.maximum( QStringLiteral( "Z" ) );
134  if ( !std::isnan( zMin ) && !std::isnan( zMax ) )
135  {
136  return QgsDoubleRange( zMin * mZScale + mZOffset, zMax * mZScale + mZOffset );
137  }
138  }
139  }
140 
141  return QgsDoubleRange();
142 }
143 
145 {
146  return true;
147 }
148 
150 {
151  if ( qgsDoubleNear( error, mMaximumScreenError ) )
152  return;
153 
154  mMaximumScreenError = error;
155  emit changed();
157 }
158 
160 {
161  if ( unit == mMaximumScreenErrorUnit )
162  return;
163 
164  mMaximumScreenErrorUnit = unit;
165  emit changed();
167 }
168 
170 {
171  return mPointSymbol;
172 }
173 
175 {
176  if ( mPointSymbol == symbol )
177  return;
178 
179  mPointSymbol = symbol;
180  emit changed();
182 }
183 
185 {
186  if ( color == mPointColor )
187  return;
188 
189  mPointColor = color;
190  emit changed();
192 }
193 
195 {
196  if ( apply == mApplyOpacityByDistanceEffect )
197  return;
198 
199  mApplyOpacityByDistanceEffect = apply;
200  emit changed();
201 
202  // turning ON opacity by distance requires a profile regeneration, turning it off does not.
203  if ( mApplyOpacityByDistanceEffect )
205  else
207 }
208 
210 {
211  if ( qgsDoubleNear( size, mPointSize ) )
212  return;
213 
214  mPointSize = size;
215  emit changed();
217 }
218 
220 {
221  if ( mPointSizeUnit == units )
222  return;
223 
224  mPointSizeUnit = units;
225  emit changed();
227 }
228 
230 {
231  if ( mRespectLayerColors == enabled )
232  return;
233 
234  mRespectLayerColors = enabled;
235  emit changed();
236 
237  // turning ON respect layer colors requires a profile regeneration, turning it off does not.
238  if ( mRespectLayerColors )
240  else
242 }
QgsSymbolLayerUtils::encodeColor
static QString encodeColor(const QColor &color)
Definition: qgssymbollayerutils.cpp:64
QgsPointCloudLayerElevationProperties::calculateZRange
QgsDoubleRange calculateZRange(QgsMapLayer *layer) const override
Attempts to calculate the overall elevation or z range for the specified layer, using the settings de...
Definition: qgspointcloudlayerelevationproperties.cpp:123
QgsPointCloudLayerElevationProperties::setPointSymbol
void setPointSymbol(Qgis::PointCloudSymbol symbol)
Sets the symbol used drawing points in elevation profile charts.
Definition: qgspointcloudlayerelevationproperties.cpp:174
qgsEnumValueToKey
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition: qgis.h:2440
QgsColorSchemeRegistry::fetchRandomStyleColor
QColor fetchRandomStyleColor() const
Returns a random color for use with a new symbol style (e.g.
Definition: qgscolorschemeregistry.cpp:141
QgsPointCloudLayerElevationProperties::setMaximumScreenErrorUnit
void setMaximumScreenErrorUnit(QgsUnitTypes::RenderUnit unit)
Sets the unit for the maximum screen error allowed when generating elevation profiles for the point c...
Definition: qgspointcloudlayerelevationproperties.cpp:159
QgsUnitTypes::RenderUnit
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:167
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsMapLayerElevationProperties
Base class for storage of map layer elevation properties.
Definition: qgsmaplayerelevationproperties.h:41
QgsPointCloudLayerElevationProperties
Point cloud layer specific subclass of QgsMapLayerElevationProperties.
Definition: qgspointcloudlayerelevationproperties.h:33
QgsPointCloudLayer
Represents a map layer supporting display of point clouds.
Definition: qgspointcloudlayer.h:45
qgssymbollayerutils.h
QgsApplication::colorSchemeRegistry
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes.
Definition: qgsapplication.cpp:2310
QgsPointCloudStatistics
Class used to store statistics of a point cloud dataset.
Definition: qgspointcloudstatistics.h:61
QgsUnitTypes::RenderMillimeters
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:169
QgsPointCloudLayerElevationProperties::showByDefaultInElevationProfilePlots
bool showByDefaultInElevationProfilePlots() const override
Returns true if the layer should be visible by default in newly created elevation profile plots.
Definition: qgspointcloudlayerelevationproperties.cpp:144
QgsSymbolLayerUtils::decodeColor
static QColor decodeColor(const QString &str)
Definition: qgssymbollayerutils.cpp:69
qgsDoubleToString
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:2204
QgsPointCloudStatistics::maximum
double maximum(const QString &attribute) const
Returns the maximum value for the attribute attribute If no matching statistic is available then NaN ...
Definition: qgspointcloudstatistics.cpp:110
QgsMapLayerElevationProperties::readCommonProperties
void readCommonProperties(const QDomElement &element, const QgsReadWriteContext &context)
Reads common class properties from a DOM element previously written by writeXml().
Definition: qgsmaplayerelevationproperties.cpp:54
qgsapplication.h
QgsUnitTypes::decodeRenderUnit
static Q_INVOKABLE QgsUnitTypes::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
Definition: qgsunittypes.cpp:2948
QgsUnitTypes::encodeUnit
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
Definition: qgsunittypes.cpp:122
qgspointcloudlayer.h
QgsPointCloudLayerElevationProperties::pointSymbol
Qgis::PointCloudSymbol pointSymbol() const
Returns the symbol used drawing points in elevation profile charts.
Definition: qgspointcloudlayerelevationproperties.cpp:169
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:2265
QgsPointCloudLayerElevationProperties::setRespectLayerColors
void setRespectLayerColors(bool enabled)
Sets whether layer coloring should be respected when rendering elevation profile plots.
Definition: qgspointcloudlayerelevationproperties.cpp:229
QgsPointCloudLayerElevationProperties::setPointSize
void setPointSize(double size)
Sets the point size used for drawing points in elevation profile charts.
Definition: qgspointcloudlayerelevationproperties.cpp:209
Qgis::PointCloudSymbol::Square
@ Square
Renders points as squares.
Qgis::PointCloudSymbol
PointCloudSymbol
Rendering symbols for point cloud points.
Definition: qgis.h:1888
QgsPointCloudLayerElevationProperties::htmlSummary
QString htmlSummary() const override
Returns a HTML formatted summary of the properties.
Definition: qgspointcloudlayerelevationproperties.cpp:109
QgsMapLayerElevationProperties::mZOffset
double mZOffset
Z offset.
Definition: qgsmaplayerelevationproperties.h:299
QgsPointCloudLayerElevationProperties::setPointColor
void setPointColor(const QColor &color)
Sets the color used drawing points in elevation profile charts.
Definition: qgspointcloudlayerelevationproperties.cpp:184
QgsPointCloudLayerElevationProperties::writeXml
QDomElement writeXml(QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context) override
Writes the properties to a DOM element, to be used later with readXml().
Definition: qgspointcloudlayerelevationproperties.cpp:44
QgsMapLayer::rendererChanged
void rendererChanged()
Signal emitted when renderer is changed.
QgsMapLayerElevationProperties::profileGenerationPropertyChanged
void profileGenerationPropertyChanged()
Emitted when any of the elevation properties which relate solely to generation of elevation profiles ...
QgsMapLayerElevationProperties::profileRenderingPropertyChanged
void profileRenderingPropertyChanged()
Emitted when any of the elevation properties which relate solely to presentation of elevation results...
QgsPointCloudLayerElevationProperties::setApplyOpacityByDistanceEffect
void setApplyOpacityByDistanceEffect(bool apply)
Sets whether a reduced opacity by distance from profile curve effect should be applied when drawing p...
Definition: qgspointcloudlayerelevationproperties.cpp:194
QgsPointCloudStatistics::minimum
double minimum(const QString &attribute) const
Returns the minimum value for the attribute attribute If no matching statistic is available then NaN ...
Definition: qgspointcloudstatistics.cpp:103
QgsDoubleRange
QgsRange which stores a range of double values.
Definition: qgsrange.h:202
QgsMapLayerElevationProperties::mZScale
double mZScale
Z scale.
Definition: qgsmaplayerelevationproperties.h:297
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsPointCloudLayerElevationProperties::hasElevation
bool hasElevation() const override
Returns true if the layer has an elevation or z component.
Definition: qgspointcloudlayerelevationproperties.cpp:39
qgsEnumKeyToValue
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:2459
qgscolorschemeregistry.h
QgsMapLayerElevationProperties::changed
void changed()
Emitted when any of the elevation properties have changed.
QgsPointCloudLayerElevationProperties::setMaximumScreenError
void setMaximumScreenError(double error)
Sets the maximum screen error allowed when generating elevation profiles for the point cloud.
Definition: qgspointcloudlayerelevationproperties.cpp:149
qgspointcloudlayerelevationproperties.h
QgsPointCloudLayerElevationProperties::setPointSizeUnit
void setPointSizeUnit(const QgsUnitTypes::RenderUnit units)
Sets the units used for the point size used for drawing points in elevation profile charts.
Definition: qgspointcloudlayerelevationproperties.cpp:219
QgsPointCloudLayerElevationProperties::isVisibleInZRange
bool isVisibleInZRange(const QgsDoubleRange &range) const override
Returns true if the layer should be visible and rendered for the specified z range.
Definition: qgspointcloudlayerelevationproperties.cpp:117
QgsMapLayerElevationProperties::writeCommonProperties
void writeCommonProperties(QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context)
Writes common class properties to a DOM element, to be used later with readXml().
Definition: qgsmaplayerelevationproperties.cpp:44
QgsPointCloudLayerElevationProperties::clone
QgsPointCloudLayerElevationProperties * clone() const override
Creates a clone of the properties.
Definition: qgspointcloudlayerelevationproperties.cpp:92
QgsPointCloudLayerElevationProperties::QgsPointCloudLayerElevationProperties
QgsPointCloudLayerElevationProperties(QObject *parent)
Constructor for QgsPointCloudLayerElevationProperties, with the specified parent object.
Definition: qgspointcloudlayerelevationproperties.cpp:24
QgsPointCloudLayerElevationProperties::readXml
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the elevation properties from a DOM element previously written by writeXml().
Definition: qgspointcloudlayerelevationproperties.cpp:62