QGIS API Documentation 3.41.0-Master (88383c3d16f)
Loading...
Searching...
No Matches
qgsvectorlayerelevationproperties.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectorlayerelevationproperties.cpp
3 ---------------
4 begin : February 2022
5 copyright : (C) 2022 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 "moc_qgsvectorlayerelevationproperties.cpp"
20#include "qgslinesymbol.h"
21#include "qgsfillsymbol.h"
22#include "qgsmarkersymbol.h"
23#include "qgssymbollayerutils.h"
24#include "qgslinesymbollayer.h"
25#include "qgsfillsymbollayer.h"
27#include "qgsapplication.h"
29#include "qgsvectorlayer.h"
30
33{
35 setDefaultProfileLineSymbol( color );
36 setDefaultProfileFillSymbol( color );
37 setDefaultProfileMarkerSymbol( color );
38}
39
41
43{
44 // layer is always considered as having elevation -- even if no z values are present or any
45 // offset/extrusion etc is set, then we are still considering the features as sitting on the terrain
46 // height
47 return true;
48}
49
50QDomElement QgsVectorLayerElevationProperties::writeXml( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context )
51{
52 QDomElement element = document.createElement( QStringLiteral( "elevation" ) );
53 writeCommonProperties( element, document, context );
54
55 element.setAttribute( QStringLiteral( "extrusionEnabled" ), mEnableExtrusion ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
56 element.setAttribute( QStringLiteral( "extrusion" ), qgsDoubleToString( mExtrusionHeight ) );
57 element.setAttribute( QStringLiteral( "customToleranceEnabled" ), mEnableCustomTolerance ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
58 if ( mCustomTolerance != 0 )
59 element.setAttribute( QStringLiteral( "customTolerance" ), qgsDoubleToString( mCustomTolerance ) );
60 element.setAttribute( QStringLiteral( "clamping" ), qgsEnumValueToKey( mClamping ) );
61 element.setAttribute( QStringLiteral( "binding" ), qgsEnumValueToKey( mBinding ) );
62 element.setAttribute( QStringLiteral( "type" ), qgsEnumValueToKey( mType ) );
63 element.setAttribute( QStringLiteral( "symbology" ), qgsEnumValueToKey( mSymbology ) );
64 if ( !std::isnan( mElevationLimit ) )
65 element.setAttribute( QStringLiteral( "elevationLimit" ), qgsDoubleToString( mElevationLimit ) );
66
67 element.setAttribute( QStringLiteral( "respectLayerSymbol" ), mRespectLayerSymbology ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
68 element.setAttribute( QStringLiteral( "showMarkerSymbolInSurfacePlots" ), mShowMarkerSymbolInSurfacePlots ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
69
70 QDomElement profileLineSymbolElement = document.createElement( QStringLiteral( "profileLineSymbol" ) );
71 profileLineSymbolElement.appendChild( QgsSymbolLayerUtils::saveSymbol( QString(), mProfileLineSymbol.get(), document, context ) );
72 element.appendChild( profileLineSymbolElement );
73
74 QDomElement profileFillSymbolElement = document.createElement( QStringLiteral( "profileFillSymbol" ) );
75 profileFillSymbolElement.appendChild( QgsSymbolLayerUtils::saveSymbol( QString(), mProfileFillSymbol.get(), document, context ) );
76 element.appendChild( profileFillSymbolElement );
77
78 QDomElement profileMarkerSymbolElement = document.createElement( QStringLiteral( "profileMarkerSymbol" ) );
79 profileMarkerSymbolElement.appendChild( QgsSymbolLayerUtils::saveSymbol( QString(), mProfileMarkerSymbol.get(), document, context ) );
80 element.appendChild( profileMarkerSymbolElement );
81
82 parentElement.appendChild( element );
83 return element;
84}
85
86bool QgsVectorLayerElevationProperties::readXml( const QDomElement &element, const QgsReadWriteContext &context )
87{
88 const QDomElement elevationElement = element.firstChildElement( QStringLiteral( "elevation" ) ).toElement();
89 if ( elevationElement.isNull() )
90 return false;
91
92 readCommonProperties( elevationElement, context );
93
94 mClamping = qgsEnumKeyToValue( elevationElement.attribute( QStringLiteral( "clamping" ) ), Qgis::AltitudeClamping::Terrain );
95 mBinding = qgsEnumKeyToValue( elevationElement.attribute( QStringLiteral( "binding" ) ), Qgis::AltitudeBinding::Centroid );
96 mType = qgsEnumKeyToValue( elevationElement.attribute( QStringLiteral( "type" ) ), Qgis::VectorProfileType::IndividualFeatures );
97 mEnableExtrusion = elevationElement.attribute( QStringLiteral( "extrusionEnabled" ), QStringLiteral( "0" ) ).toInt();
98 mExtrusionHeight = elevationElement.attribute( QStringLiteral( "extrusion" ), QStringLiteral( "0" ) ).toDouble();
99 mEnableCustomTolerance = elevationElement.attribute( QStringLiteral( "customToleranceEnabled" ), QStringLiteral( "0" ) ).toInt();
100 mCustomTolerance = elevationElement.attribute( QStringLiteral( "customTolerance" ), QStringLiteral( "0" ) ).toDouble();
101 mSymbology = qgsEnumKeyToValue( elevationElement.attribute( QStringLiteral( "symbology" ) ), Qgis::ProfileSurfaceSymbology::Line );
102 if ( elevationElement.hasAttribute( QStringLiteral( "elevationLimit" ) ) )
103 mElevationLimit = elevationElement.attribute( QStringLiteral( "elevationLimit" ) ).toDouble();
104 else
105 mElevationLimit = std::numeric_limits< double >::quiet_NaN();
106
107 mShowMarkerSymbolInSurfacePlots = elevationElement.attribute( QStringLiteral( "showMarkerSymbolInSurfacePlots" ), QStringLiteral( "0" ) ).toInt();
108
109 mRespectLayerSymbology = elevationElement.attribute( QStringLiteral( "respectLayerSymbol" ), QStringLiteral( "1" ) ).toInt();
110
112
113 const QDomElement profileLineSymbolElement = elevationElement.firstChildElement( QStringLiteral( "profileLineSymbol" ) ).firstChildElement( QStringLiteral( "symbol" ) );
114 mProfileLineSymbol.reset( QgsSymbolLayerUtils::loadSymbol< QgsLineSymbol >( profileLineSymbolElement, context ) );
115 if ( !mProfileLineSymbol )
116 setDefaultProfileLineSymbol( color );
117
118 const QDomElement profileFillSymbolElement = elevationElement.firstChildElement( QStringLiteral( "profileFillSymbol" ) ).firstChildElement( QStringLiteral( "symbol" ) );
119 mProfileFillSymbol.reset( QgsSymbolLayerUtils::loadSymbol< QgsFillSymbol >( profileFillSymbolElement, context ) );
120 if ( !mProfileFillSymbol )
121 setDefaultProfileFillSymbol( color );
122
123 const QDomElement profileMarkerSymbolElement = elevationElement.firstChildElement( QStringLiteral( "profileMarkerSymbol" ) ).firstChildElement( QStringLiteral( "symbol" ) );
124 mProfileMarkerSymbol.reset( QgsSymbolLayerUtils::loadSymbol< QgsMarkerSymbol >( profileMarkerSymbolElement, context ) );
125 if ( !mProfileMarkerSymbol )
126 setDefaultProfileMarkerSymbol( color );
127
128 return true;
129}
130
132{
133 QgsVectorLayer *vlayer = qobject_cast< QgsVectorLayer * >( layer );
134 if ( !vlayer )
135 return;
136
137 mZOffset = 0;
138 mZScale = 1;
139
140 mEnableExtrusion = false;
141 mExtrusionHeight = 0;
142
143 // By default override default tolerance for Polygon and Line
144 // to avoid unexpected behaviors.
145 // For example, see: https://github.com/qgis/QGIS/issues/58016
146 mEnableCustomTolerance = vlayer->geometryType() != Qgis::GeometryType::Point;
147 mCustomTolerance = 0;
148
150
152
153 if ( QgsWkbTypes::hasZ( vlayer->wkbType() ) )
154 {
156 }
157 else
158 {
160 }
161}
162
164{
165 auto res = std::make_unique< QgsVectorLayerElevationProperties >( nullptr );
166 res->setClamping( mClamping );
167 res->setBinding( mBinding );
168 res->setType( mType );
169 res->setExtrusionEnabled( mEnableExtrusion );
170 res->setExtrusionHeight( mExtrusionHeight );
171 res->setCustomToleranceEnabled( mEnableCustomTolerance );
172 res->setCustomTolerance( mCustomTolerance );
173 res->setProfileLineSymbol( mProfileLineSymbol->clone() );
174 res->setProfileFillSymbol( mProfileFillSymbol->clone() );
175 res->setProfileMarkerSymbol( mProfileMarkerSymbol->clone() );
176 res->setRespectLayerSymbology( mRespectLayerSymbology );
177 res->setProfileSymbology( mSymbology );
178 res->setElevationLimit( mElevationLimit );
179 res->setShowMarkerSymbolInSurfacePlots( mShowMarkerSymbolInSurfacePlots );
180 res->copyCommonProperties( this );
181 return res.release();
182}
183
185{
186 QStringList properties;
187
188 switch ( mClamping )
189 {
191 properties << tr( "Clamped to Terrain" );
192 break;
194 properties << tr( "Relative to Terrain" );
195 break;
197 properties << tr( "Absolute" );
198 break;
199 }
200
202 {
204 {
207 break;
209 properties << tr( "Offset: %1" ).arg( mDataDefinedProperties.property( Property::ZOffset ).field() );
210 break;
212 properties << tr( "Offset: %1" ).arg( mDataDefinedProperties.property( Property::ZOffset ).expressionString() );
213 break;
214 }
215 }
216 else
217 {
218 properties << tr( "Offset: %1" ).arg( mZOffset );
219 }
220
221 if ( mEnableExtrusion )
222 {
224 {
226 {
229 break;
231 properties << tr( "Extrusion: %1" ).arg( mDataDefinedProperties.property( Property::ExtrusionHeight ).field() );
232 break;
234 properties << tr( "Extrusion: %1" ).arg( mDataDefinedProperties.property( Property::ExtrusionHeight ).expressionString() );
235 break;
236 }
237 }
238 else
239 {
240 properties << tr( "Extrusion: %1" ).arg( mExtrusionHeight );
241 }
242 }
243
244 if ( mEnableCustomTolerance )
245 {
246 properties << tr( "CustomTolerance: %1" ).arg( mCustomTolerance );
247 }
248
249 properties << tr( "Scale: %1" ).arg( mZScale );
250
251 return QStringLiteral( "<li>%1</li>" ).arg( properties.join( QLatin1String( "</li><li>" ) ) );
252}
253
255{
256 // TODO -- test actual layer z range
257 return true;
258}
259
261{
262 // TODO -- determine actual z range from layer statistics
263 return QgsDoubleRange();
264}
265
267{
268 // show by default if the features aren't just directly clamped onto the terrain with
269 // no other changes
270 return !qgsDoubleNear( mZOffset, 0 )
271 || !qgsDoubleNear( mZScale, 1 )
272 || mEnableExtrusion
273 || mClamping != Qgis::AltitudeClamping::Terrain;
274}
275
277{
278 if ( mClamping == clamping )
279 return;
280
281 mClamping = clamping;
282 emit changed();
284}
285
287{
288 if ( mBinding == binding )
289 return;
290
291 mBinding = binding;
292 emit changed();
294}
295
297{
298 if ( type == mType )
299 return;
300
301 mType = type;
302 emit changed();
304}
305
307{
308 if ( mEnableExtrusion == enabled )
309 return;
310
311 mEnableExtrusion = enabled;
312 emit changed();
314}
315
317{
318 if ( mExtrusionHeight == height )
319 return;
320
321 mExtrusionHeight = height;
322 emit changed();
324}
325
327{
328 if ( mCustomTolerance == tolerance )
329 return;
330
331 mCustomTolerance = tolerance;
332 emit changed();
334}
335
337{
338 if ( mEnableCustomTolerance == enabled )
339 return;
340
341 mEnableCustomTolerance = enabled;
342 emit changed();
344}
345
347{
348 if ( mRespectLayerSymbology == enabled )
349 return;
350
351 mRespectLayerSymbology = enabled;
352 emit changed();
354}
355
357{
358 return mProfileLineSymbol.get();
359}
360
362{
363 mProfileLineSymbol.reset( symbol );
364 emit changed();
366}
367
369{
370 return mProfileFillSymbol.get();
371}
372
374{
375 mProfileFillSymbol.reset( symbol );
376 emit changed();
378}
379
381{
382 return mProfileMarkerSymbol.get();
383}
384
386{
387 mProfileMarkerSymbol.reset( symbol );
388 emit changed();
390}
391
393{
394 if ( mSymbology == symbology )
395 return;
396
397 mSymbology = symbology;
398 emit changed();
400}
401
403{
404 return mElevationLimit;
405}
406
408{
409 if ( qgsDoubleNear( mElevationLimit, limit ) )
410 return;
411
412 mElevationLimit = limit;
413 emit changed();
415}
416
418{
419 if ( show == mShowMarkerSymbolInSurfacePlots )
420 return;
421
422 mShowMarkerSymbolInSurfacePlots = show;
423 emit changed();
425}
426
427void QgsVectorLayerElevationProperties::setDefaultProfileLineSymbol( const QColor &color )
428{
429 auto profileLineLayer = std::make_unique< QgsSimpleLineSymbolLayer >( color, 0.6 );
430 mProfileLineSymbol = std::make_unique< QgsLineSymbol>( QgsSymbolLayerList( { profileLineLayer.release() } ) );
431}
432
433void QgsVectorLayerElevationProperties::setDefaultProfileMarkerSymbol( const QColor &color )
434{
435 auto profileMarkerLayer = std::make_unique< QgsSimpleMarkerSymbolLayer >( Qgis::MarkerShape::Diamond, 3 );
436 profileMarkerLayer->setColor( color );
437 profileMarkerLayer->setStrokeWidth( 0.2 );
438 profileMarkerLayer->setStrokeColor( color.darker( 140 ) );
439 mProfileMarkerSymbol = std::make_unique< QgsMarkerSymbol>( QgsSymbolLayerList( { profileMarkerLayer.release() } ) );
440}
441
442void QgsVectorLayerElevationProperties::setDefaultProfileFillSymbol( const QColor &color )
443{
444 auto profileFillLayer = std::make_unique< QgsSimpleFillSymbolLayer >( color );
445 profileFillLayer->setStrokeWidth( 0.2 );
446 profileFillLayer->setStrokeColor( color.darker( 140 ) );
447 mProfileFillSymbol = std::make_unique< QgsFillSymbol>( QgsSymbolLayerList( { profileFillLayer.release() } ) );
448}
AltitudeClamping
Altitude clamping.
Definition qgis.h:3782
@ Relative
Elevation is relative to terrain height (final elevation = terrain elevation + feature elevation)
@ Terrain
Elevation is clamped to terrain (final elevation = terrain elevation)
@ Absolute
Elevation is taken directly from feature and is independent of terrain height (final elevation = feat...
@ Invalid
Invalid (not set) property.
@ Field
Field based property.
@ Static
Static property.
@ Expression
Expression based property.
AltitudeBinding
Altitude binding.
Definition qgis.h:3795
@ Centroid
Clamp just centroid of feature.
VectorProfileType
Types of elevation profiles to generate for vector sources.
Definition qgis.h:3993
@ IndividualFeatures
Treat each feature as an individual object (eg buildings)
ProfileSurfaceSymbology
Surface symbology type for elevation profile plots.
Definition qgis.h:3980
@ Line
The elevation surface will be rendered using a line symbol.
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.
QgsRange which stores a range of double values.
Definition qgsrange.h:233
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
A line symbol type, for rendering LineString and MultiLineString geometries.
Base class for storage of map layer elevation properties.
QgsPropertyCollection mDataDefinedProperties
Property collection for data defined elevation settings.
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 ...
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:76
A marker symbol type, for rendering Point and MultiPoint geometries.
bool isActive(int key) const final
Returns true if the collection contains an active property with the specified key.
void clear() final
Removes all properties from the collection.
QgsProperty property(int key) const final
Returns a matching property from the collection, if one exists.
QString expressionString() const
Returns the expression used for the property value.
Qgis::PropertyType propertyType() const
Returns the property type.
QString field() const
Returns the current field name the property references.
The class is used as a container of context for various read/write operations on other objects.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
Vector layer specific subclass of QgsMapLayerElevationProperties.
QString htmlSummary() const override
Returns a HTML formatted summary of the properties.
void setCustomToleranceEnabled(bool enabled)
Sets whether custom tolerance is enabled.
void setDefaultsFromLayer(QgsMapLayer *layer) override
Sets default properties based on sensible choices for the given map layer.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the elevation properties from a DOM element previously written by writeXml().
void setCustomTolerance(double tolerance)
Sets the feature custom tolerance.
QgsFillSymbol * profileFillSymbol() const
Returns the symbol used to render polygons for the layer in elevation profile plots.
void setProfileFillSymbol(QgsFillSymbol *symbol)
Sets the fill symbol used to render polygons for the layer in elevation profile plots.
void setExtrusionHeight(double height)
Sets the feature extrusion height.
void setShowMarkerSymbolInSurfacePlots(bool show)
Sets whether the marker symbol should also be shown in continuous surface plots.
void setBinding(Qgis::AltitudeBinding binding)
Sets the altitude binding method, which determines how altitude is bound to individual vertices in fe...
QDomElement writeXml(QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context) override
Writes the properties to a DOM element, to be used later with readXml().
Qgis::VectorProfileType type() const
Returns the type of profile the layer represents.
double elevationLimit() const
Returns the elevation limit, which is used when profileSymbology() is Qgis::ProfileSurfaceSymbology::...
QgsDoubleRange calculateZRange(QgsMapLayer *layer) const override
Attempts to calculate the overall elevation or z range for the specified layer, using the settings de...
QgsLineSymbol * profileLineSymbol() const
Returns the symbol used to render lines for the layer in elevation profile plots.
void setProfileSymbology(Qgis::ProfileSurfaceSymbology symbology)
Sets the symbology option used to render the vector profile in elevation profile plots.
void setProfileMarkerSymbol(QgsMarkerSymbol *symbol)
Sets the marker symbol used to render points for the layer in elevation profile plots.
void setExtrusionEnabled(bool enabled)
Sets whether extrusion is enabled.
void setType(Qgis::VectorProfileType type)
Sets the type of profile the layer represents.
QgsVectorLayerElevationProperties * clone() const override
Creates a clone of the properties.
Qgis::AltitudeClamping clamping() const
Returns the altitude clamping method, which dictates how feature heights are interpreted with respect...
QgsVectorLayerElevationProperties(QObject *parent)
Constructor for QgsVectorLayerElevationProperties, with the specified parent object.
bool showByDefaultInElevationProfilePlots() const override
Returns true if the layer should be visible by default in newly created elevation profile plots.
QgsMarkerSymbol * profileMarkerSymbol() const
Returns the symbol used to render points for the layer in elevation profile plots.
Qgis::AltitudeBinding binding() const
Returns the altitude binding method, which determines how altitude is bound to individual vertices in...
void setProfileLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render lines for the layer in elevation profile plots.
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.
bool hasElevation() const override
Returns true if the layer has an elevation or z component.
void setRespectLayerSymbology(bool enabled)
Sets whether layer symbology should be respected when rendering elevation profile plots.
void setElevationLimit(double limit)
Sets the elevation limit, which is used when profileSymbology() is Qgis::ProfileSurfaceSymbology::Fil...
void setClamping(Qgis::AltitudeClamping clamping)
Sets the altitude clamping method, which dictates how feature heights are interpreted with respect to...
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE Qgis::WkbType wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.
static bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
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:6335
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6042
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:6316
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:6125
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition qgssymbol.h:30