QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsrasterlayerelevationproperties.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrasterlayerelevationproperties.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 "qgsrasterlayer.h"
20 #include "qgslinesymbol.h"
21 #include "qgsfillsymbol.h"
22 #include "qgssymbollayerutils.h"
23 #include "qgslinesymbollayer.h"
24 #include "qgsfillsymbollayer.h"
25 #include "qgsapplication.h"
26 #include "qgscolorschemeregistry.h"
27 
30 {
32  setDefaultProfileLineSymbol( color );
33  setDefaultProfileFillSymbol( color );
34 }
35 
37 
39 {
40  return mEnabled;
41 }
42 
43 QDomElement QgsRasterLayerElevationProperties::writeXml( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context )
44 {
45  QDomElement element = document.createElement( QStringLiteral( "elevation" ) );
46  element.setAttribute( QStringLiteral( "enabled" ), mEnabled ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
47  element.setAttribute( QStringLiteral( "symbology" ), qgsEnumValueToKey( mSymbology ) );
48 
49  writeCommonProperties( element, document, context );
50  element.setAttribute( QStringLiteral( "band" ), mBandNumber );
51 
52  QDomElement profileLineSymbolElement = document.createElement( QStringLiteral( "profileLineSymbol" ) );
53  profileLineSymbolElement.appendChild( QgsSymbolLayerUtils::saveSymbol( QString(), mProfileLineSymbol.get(), document, context ) );
54  element.appendChild( profileLineSymbolElement );
55 
56  QDomElement profileFillSymbolElement = document.createElement( QStringLiteral( "profileFillSymbol" ) );
57  profileFillSymbolElement.appendChild( QgsSymbolLayerUtils::saveSymbol( QString(), mProfileFillSymbol.get(), document, context ) );
58  element.appendChild( profileFillSymbolElement );
59 
60  parentElement.appendChild( element );
61  return element;
62 }
63 
64 bool QgsRasterLayerElevationProperties::readXml( const QDomElement &element, const QgsReadWriteContext &context )
65 {
66  const QDomElement elevationElement = element.firstChildElement( QStringLiteral( "elevation" ) ).toElement();
67  mEnabled = elevationElement.attribute( QStringLiteral( "enabled" ), QStringLiteral( "0" ) ).toInt();
68  mSymbology = qgsEnumKeyToValue( elevationElement.attribute( QStringLiteral( "symbology" ) ), Qgis::ProfileSurfaceSymbology::Line );
69 
70  readCommonProperties( elevationElement, context );
71  mBandNumber = elevationElement.attribute( QStringLiteral( "band" ), QStringLiteral( "1" ) ).toInt();
72 
73  const QColor defaultColor = QgsApplication::colorSchemeRegistry()->fetchRandomStyleColor();
74 
75  const QDomElement profileLineSymbolElement = elevationElement.firstChildElement( QStringLiteral( "profileLineSymbol" ) ).firstChildElement( QStringLiteral( "symbol" ) );
76  mProfileLineSymbol.reset( QgsSymbolLayerUtils::loadSymbol< QgsLineSymbol >( profileLineSymbolElement, context ) );
77  if ( !mProfileLineSymbol )
78  setDefaultProfileLineSymbol( defaultColor );
79 
80  const QDomElement profileFillSymbolElement = elevationElement.firstChildElement( QStringLiteral( "profileFillSymbol" ) ).firstChildElement( QStringLiteral( "symbol" ) );
81  mProfileFillSymbol.reset( QgsSymbolLayerUtils::loadSymbol< QgsFillSymbol >( profileFillSymbolElement, context ) );
82  if ( !mProfileFillSymbol )
83  setDefaultProfileFillSymbol( defaultColor );
84 
85  return true;
86 }
87 
89 {
90  std::unique_ptr< QgsRasterLayerElevationProperties > res = std::make_unique< QgsRasterLayerElevationProperties >( nullptr );
91  res->setEnabled( mEnabled );
92  res->setProfileLineSymbol( mProfileLineSymbol->clone() );
93  res->setProfileFillSymbol( mProfileFillSymbol->clone() );
94  res->setProfileSymbology( mSymbology );
95  res->setBandNumber( mBandNumber );
96  res->copyCommonProperties( this );
97  return res.release();
98 }
99 
101 {
102  QStringList properties;
103  properties << tr( "Elevation band: %1" ).arg( mBandNumber );
104  properties << tr( "Scale: %1" ).arg( mZScale );
105  properties << tr( "Offset: %1" ).arg( mZOffset );
106  return QStringLiteral( "<li>%1</li>" ).arg( properties.join( QLatin1String( "</li><li>" ) ) );
107 }
108 
110 {
111  // TODO -- test actual raster z range
112  return true;
113 }
114 
116 {
117  // TODO -- determine actual z range from raster statistics
118  return QgsDoubleRange();
119 }
120 
122 {
123  return mEnabled;
124 }
125 
127 {
128  if ( enabled == mEnabled )
129  return;
130 
131  mEnabled = enabled;
132  emit changed();
134 }
135 
137 {
138  if ( mBandNumber == band )
139  return;
140 
141  mBandNumber = band;
142  emit changed();
144 }
145 
147 {
148  return mProfileLineSymbol.get();
149 }
150 
152 {
153  mProfileLineSymbol.reset( symbol );
154  emit changed();
156 }
157 
159 {
160  return mProfileFillSymbol.get();
161 }
162 
164 {
165  mProfileFillSymbol.reset( symbol );
166 }
167 
169 {
170  mSymbology = symbology;
171 }
172 
173 void QgsRasterLayerElevationProperties::setDefaultProfileLineSymbol( const QColor &color )
174 {
175  std::unique_ptr< QgsSimpleLineSymbolLayer > profileLineLayer = std::make_unique< QgsSimpleLineSymbolLayer >( color, 0.6 );
176  mProfileLineSymbol = std::make_unique< QgsLineSymbol>( QgsSymbolLayerList( { profileLineLayer.release() } ) );
177 }
178 
179 void QgsRasterLayerElevationProperties::setDefaultProfileFillSymbol( const QColor &color )
180 {
181  std::unique_ptr< QgsSimpleFillSymbolLayer > profileFillLayer = std::make_unique< QgsSimpleFillSymbolLayer >( color );
182  profileFillLayer->setStrokeStyle( Qt::NoPen );
183  mProfileFillSymbol = std::make_unique< QgsFillSymbol>( QgsSymbolLayerList( { profileFillLayer.release() } ) );
184 }
qgsEnumValueToKey
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition: qgis.h:2440
qgsrasterlayer.h
QgsColorSchemeRegistry::fetchRandomStyleColor
QColor fetchRandomStyleColor() const
Returns a random color for use with a new symbol style (e.g.
Definition: qgscolorschemeregistry.cpp:141
QgsRasterLayerElevationProperties::QgsRasterLayerElevationProperties
QgsRasterLayerElevationProperties(QObject *parent)
Constructor for QgsRasterLayerElevationProperties, with the specified parent object.
Definition: qgsrasterlayerelevationproperties.cpp:28
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsRasterLayerElevationProperties::htmlSummary
QString htmlSummary() const override
Returns a HTML formatted summary of the properties.
Definition: qgsrasterlayerelevationproperties.cpp:100
QgsMapLayerElevationProperties
Base class for storage of map layer elevation properties.
Definition: qgsmaplayerelevationproperties.h:41
Qgis::ProfileSurfaceSymbology
ProfileSurfaceSymbology
Surface symbology type for elevation profile plots.
Definition: qgis.h:1849
qgssymbollayerutils.h
QgsRasterLayerElevationProperties::showByDefaultInElevationProfilePlots
bool showByDefaultInElevationProfilePlots() const override
Returns true if the layer should be visible by default in newly created elevation profile plots.
Definition: qgsrasterlayerelevationproperties.cpp:121
QgsRasterLayerElevationProperties::setProfileFillSymbol
void setProfileFillSymbol(QgsFillSymbol *symbol)
Sets the fill symbol used to render the raster profile in elevation profile plots.
Definition: qgsrasterlayerelevationproperties.cpp:163
QgsApplication::colorSchemeRegistry
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes.
Definition: qgsapplication.cpp:2310
QgsRasterLayerElevationProperties::setBandNumber
void setBandNumber(int band)
Sets the band number from which the elevation should be taken.
Definition: qgsrasterlayerelevationproperties.cpp:136
QgsRasterLayerElevationProperties::profileFillSymbol
QgsFillSymbol * profileFillSymbol() const
Returns the fill symbol used to render the raster profile in elevation profile plots.
Definition: qgsrasterlayerelevationproperties.cpp:158
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
QgsRasterLayerElevationProperties::setProfileSymbology
void setProfileSymbology(Qgis::ProfileSurfaceSymbology symbology)
Sets the symbology option used to render the raster profile in elevation profile plots.
Definition: qgsrasterlayerelevationproperties.cpp:168
Qgis::ProfileSurfaceSymbology::Line
@ Line
The elevation surface will be rendered using a line symbol.
qgsapplication.h
QgsRasterLayerElevationProperties::setProfileLineSymbol
void setProfileLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the raster profile in elevation profile plots.
Definition: qgsrasterlayerelevationproperties.cpp:151
qgsrasterlayerelevationproperties.h
QgsRasterLayerElevationProperties
Raster layer specific subclass of QgsMapLayerElevationProperties.
Definition: qgsrasterlayerelevationproperties.h:34
QgsRasterLayerElevationProperties::readXml
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the elevation properties from a DOM element previously written by writeXml().
Definition: qgsrasterlayerelevationproperties.cpp:64
QgsRasterLayerElevationProperties::hasElevation
bool hasElevation() const override
Returns true if the layer has an elevation or z component.
Definition: qgsrasterlayerelevationproperties.cpp:38
QgsLineSymbol
A line symbol type, for rendering LineString and MultiLineString geometries.
Definition: qgslinesymbol.h:29
QgsMapLayerElevationProperties::mZOffset
double mZOffset
Z offset.
Definition: qgsmaplayerelevationproperties.h:299
QgsRasterLayerElevationProperties::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: qgsrasterlayerelevationproperties.cpp:43
QgsRasterLayerElevationProperties::clone
QgsRasterLayerElevationProperties * clone() const override
Creates a clone of the properties.
Definition: qgsrasterlayerelevationproperties.cpp:88
qgsfillsymbollayer.h
QgsRasterLayerElevationProperties::profileLineSymbol
QgsLineSymbol * profileLineSymbol() const
Returns the line symbol used to render the raster profile in elevation profile plots.
Definition: qgsrasterlayerelevationproperties.cpp:146
QgsMapLayerElevationProperties::profileGenerationPropertyChanged
void profileGenerationPropertyChanged()
Emitted when any of the elevation properties which relate solely to generation of elevation profiles ...
QgsRasterLayerElevationProperties::~QgsRasterLayerElevationProperties
~QgsRasterLayerElevationProperties() override
QgsRasterLayerElevationProperties::isVisibleInZRange
bool isVisibleInZRange(const QgsDoubleRange &range) const override
Returns true if the layer should be visible and rendered for the specified z range.
Definition: qgsrasterlayerelevationproperties.cpp:109
QgsMapLayerElevationProperties::profileRenderingPropertyChanged
void profileRenderingPropertyChanged()
Emitted when any of the elevation properties which relate solely to presentation of elevation results...
QgsDoubleRange
QgsRange which stores a range of double values.
Definition: qgsrange.h:202
qgslinesymbollayer.h
QgsMapLayerElevationProperties::mZScale
double mZScale
Z scale.
Definition: qgsmaplayerelevationproperties.h:297
QgsSymbolLayerList
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition: qgssymbol.h:27
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
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
QgsFillSymbol
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Definition: qgsfillsymbol.h:29
qgscolorschemeregistry.h
QgsMapLayerElevationProperties::changed
void changed()
Emitted when any of the elevation properties have changed.
QgsRasterLayerElevationProperties::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: qgsrasterlayerelevationproperties.cpp:115
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
QgsRasterLayerElevationProperties::setEnabled
void setEnabled(bool enabled)
Sets whether the elevation properties are enabled, i.e.
Definition: qgsrasterlayerelevationproperties.cpp:126
qgsfillsymbol.h
QgsSymbolLayerUtils::saveSymbol
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
Definition: qgssymbollayerutils.cpp:1397
qgslinesymbol.h