QGIS API Documentation 3.99.0-Master (8e76e220402)
Loading...
Searching...
No Matches
qgselevationshadingrenderer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgselevationshadingrenderer.cpp - QgsElevationShadingRenderer
3
4 ---------------------
5 begin : 4.12.2022
6 copyright : (C) 2022 by Vincent Cloarec
7 email : vcloarec at gmail dot com
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
17
18#include "qgselevationmap.h"
19#include "qgsrendercontext.h"
20#include "qgsunittypes.h"
21
22#include <QImage>
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
30
31void QgsElevationShadingRenderer::renderShading( const QgsElevationMap &elevation, QImage &image, const QgsRenderContext &context ) const
32{
33 if ( elevation.rawElevationImage().size() != image.size() )
34 return;
35
36 if ( mRenderEdl )
37 renderEdl( elevation, image, context );
38
39 if ( mRenderHillshading )
40 renderHillshading( elevation, image, context );
41}
42
44{
45 mIsActive = active;
46}
47
49{
50 return mIsActive;
51}
52
54{
55 mRenderEdl = active;
56}
57
59{
60 return mRenderEdl;
61}
62
64{
65 mRenderHillshading = active;
66}
67
69{
70 return mRenderHillshading;
71}
72
74{
75 return mHillshadingZFactor;
76}
77
79{
80 mHillshadingZFactor = zFactor;
81}
82
84{
85 return mHillshadingMultiDir;
86}
87
89{
90 mHillshadingMultiDir = multiDirectional;
91}
92
94{
95 return mLightAltitude;
96}
97
102
104{
105 return mLightAzimuth;
106}
107
109{
110 mLightAzimuth = lightAzimuth;
111}
112
113void QgsElevationShadingRenderer::writeXml( QDomElement &elem, const QgsReadWriteContext & ) const
114{
115 elem.setAttribute( u"is-active"_s, mIsActive ? 1 : 0 );
116
117 elem.setAttribute( u"combined-method"_s, static_cast<int>( mCombinedElevationMethod ) );
118
119 elem.setAttribute( u"edl-is-active"_s, mRenderEdl ? 1 : 0 );
120 elem.setAttribute( u"edl-strength"_s, qgsDoubleToString( mEyeDomeLightingStrength ) );
121 elem.setAttribute( u"edl-distance"_s, qgsDoubleToString( mEyeDomeLightingDistance ) );
122 elem.setAttribute( u"edl-distance-unit"_s, static_cast<int>( mEyeDomeLightingDistanceUnit ) );
123
124 elem.setAttribute( u"hillshading-is-active"_s, mRenderHillshading ? 1 : 0 );
125 elem.setAttribute( u"hillshading-z-factor"_s, qgsDoubleToString( mHillshadingZFactor ) );
126 elem.setAttribute( u"hillshading-is-multidirectional"_s, mHillshadingMultiDir ? 1 : 0 );
127
128 elem.setAttribute( u"light-altitude"_s, qgsDoubleToString( mLightAltitude ) );
129 elem.setAttribute( u"light-azimuth"_s, qgsDoubleToString( mLightAzimuth ) );
130}
131
132void QgsElevationShadingRenderer::readXml( const QDomElement &element, const QgsReadWriteContext & )
133{
134 if ( element.hasAttribute( u"is-active"_s ) )
135 mIsActive = element.attribute( u"is-active"_s ).toInt() == 1;
136
137 if ( element.hasAttribute( u"combined-method"_s ) )
138 mCombinedElevationMethod = static_cast<Qgis::ElevationMapCombineMethod>( element.attribute( u"combined-method"_s ).toInt() );
139
140 if ( element.hasAttribute( u"edl-is-active"_s ) )
141 mRenderEdl = element.attribute( u"edl-is-active"_s ).toInt() == 1;
142
143 if ( element.hasAttribute( u"edl-strength"_s ) )
144 mEyeDomeLightingStrength = element.attribute( u"edl-strength"_s ).toDouble();
145
146 if ( element.hasAttribute( u"edl-distance"_s ) )
147 mEyeDomeLightingDistance = element.attribute( u"edl-distance"_s ).toDouble();
148
149 if ( element.hasAttribute( u"edl-distance-unit"_s ) )
150 mEyeDomeLightingDistanceUnit = static_cast<Qgis::RenderUnit>( element.attribute( u"edl-distance-unit"_s ).toInt() );
151
152 if ( element.hasAttribute( u"hillshading-is-active"_s ) )
153 mRenderHillshading = element.attribute( u"hillshading-is-active"_s ).toInt() == 1;
154
155 if ( element.hasAttribute( u"hillshading-z-factor"_s ) )
156 mHillshadingZFactor = element.attribute( u"hillshading-z-factor"_s ).toDouble();
157
158 if ( element.hasAttribute( u"hillshading-is-multidirectional"_s ) )
159 mHillshadingMultiDir = element.attribute( u"hillshading-is-multidirectional"_s ).toInt() == 1;
160
161 if ( element.hasAttribute( u"light-altitude"_s ) )
162 mLightAltitude = element.attribute( u"light-altitude"_s ).toDouble();
163
164 if ( element.hasAttribute( u"light-azimuth"_s ) )
165 mLightAzimuth = element.attribute( u"light-azimuth"_s ).toDouble();
166}
167
172
174{
175 mCombinedElevationMethod = newCombinedElevationMethod;
176}
177
179{
180 return mEyeDomeLightingDistanceUnit;
181}
182
184{
185 mEyeDomeLightingDistanceUnit = newEyeDomeLightingDistanceUnit;
186}
187
189{
190 return mEyeDomeLightingDistance;
191}
192
194{
195 mEyeDomeLightingDistance = distance;
196}
197
199{
200 return mEyeDomeLightingStrength;
201}
202
204{
205 mEyeDomeLightingStrength = strength;
206}
207
208void QgsElevationShadingRenderer::renderEdl( const QgsElevationMap &elevation, QImage &image, const QgsRenderContext &context ) const
209{
210 double strength = mEyeDomeLightingStrength;
211 double distanceDouble = context.convertToPainterUnits( mEyeDomeLightingDistance, mEyeDomeLightingDistanceUnit );
212 int distance = static_cast<int>( std::round( distanceDouble ) );
213
214 elevation.applyEyeDomeLighting( image, distance, static_cast<float>( strength ), static_cast<float>( context.rendererScale() ) );
215}
216
217void QgsElevationShadingRenderer::renderHillshading( const QgsElevationMap &elevation, QImage &image, const QgsRenderContext &context ) const
218{
219 double pixelSize = context.mapToPixel().mapUnitsPerPixel();
220
221 // We suppose that the elevation are in meter, so we need to use meter for pixel size if possible
222 Qgis::DistanceUnit destinationUnit = context.distanceArea().lengthUnits();
223 double effPixelSize;
224 if ( destinationUnit != Qgis::DistanceUnit::Unknown )
225 effPixelSize = QgsUnitTypes::fromUnitToUnitFactor( destinationUnit, Qgis::DistanceUnit::Meters ) * pixelSize;
226 else
227 effPixelSize = pixelSize;
228
229 elevation.applyHillshading( image, mHillshadingMultiDir, mLightAltitude, mLightAzimuth, mHillshadingZFactor, effPixelSize, effPixelSize );
230}
DistanceUnit
Units of distance.
Definition qgis.h:5120
@ Meters
Meters.
Definition qgis.h:5121
@ Unknown
Unknown distance unit.
Definition qgis.h:5170
ElevationMapCombineMethod
Methods used to select the elevation when two elevation maps are combined.
Definition qgis.h:5022
RenderUnit
Rendering size units.
Definition qgis.h:5290
Qgis::DistanceUnit lengthUnits() const
Returns the units of distance for length calculations made by this object.
Stores a digital elevation model in a raster image which may get updated as a part of the map layer r...
void applyEyeDomeLighting(QImage &image, int distance, float strength, float rendererScale) const
Applies eye dome lighting effect to the given image.
QImage rawElevationImage() const
Returns raw elevation image with elevations encoded as color values.
void applyHillshading(QImage &image, bool multiDirectional, double altitude, double azimuth, double zFactor, double cellSizeX, double cellSizeY) const
Applies hill shading effect to the given image.
Qgis::ElevationMapCombineMethod combinedElevationMethod() const
Returns the method used when conbining different elevation sources.
bool isActiveHillshading() const
Returns whether the hillshading is active.
void setCombinedElevationMethod(Qgis::ElevationMapCombineMethod method)
Sets the method used when conbining different elevation sources.
bool isActive() const
Returns whether this shading renderer is active.
void setActive(bool active)
Sets whether this shading renderer is active.
double hillshadingZFactor() const
Returns the z factor used by the hill shading method.
double eyeDomeLightingStrength() const
Returns the strength of the eye dome lighting method.
void setLightAzimuth(double lightAzimuth)
Sets the azimuth of the light (degree) that can be used by some methods (e.g.
double lightAltitude() const
Returns the altitude of the light (degree) that can be used by some methods (e.g.
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const
Writes configuration on a DOM element.
void setHillshadingMultidirectional(bool multiDirectional)
Sets whether the hill shading method is multidirectional.
double eyeDomeLightingDistance() const
Returns the distance of the eye dome lighting method, that is the distance where the effect is apply ...
void setLightAltitude(double lightAltitude)
Sets the altitude of the light (degree) that can be used by some methods (e.g.
void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads configuration from a DOM element.
void renderShading(const QgsElevationMap &elevation, QImage &image, const QgsRenderContext &context) const
Render shading on image condidering the elevation map elevation and the renderer context context If e...
void setEyeDomeLightingStrength(double strength)
Sets the strength of the eye dome lighting method.
void setEyeDomeLightingDistanceUnit(Qgis::RenderUnit unit)
Sets the unit of the distance of the eye dome lighting method set by setEyeDomeLightingDistance().
void setActiveHillshading(bool active)
Sets active the hillshading.
bool isHillshadingMultidirectional() const
Returns whether the hill shading method is multidirectional.
void setHillshadingZFactor(double zFactor)
Sets the z factor used by the hill shading method.
bool isActiveEyeDomeLighting() const
Returns whether eye-dome lighting shading method is active.
void setEyeDomeLightingDistance(double distance)
Sets the distance of the eye dome lighting method, that is the distance where the effect is apply fro...
void setActiveEyeDomeLighting(bool active)
Sets active the eye-dome lighting shading method.
double lightAzimuth() const
Returns the azimuth of the light (degree) that can be used by some methods (e.g.
Qgis::RenderUnit eyeDomeLightingDistanceUnit() const
Returns the unit of the distance of the eye dome lighting method returned by eyeDomeLightingDistance(...
double mapUnitsPerPixel() const
Returns the current map units per pixel.
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
const QgsDistanceArea & distanceArea() const
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
double rendererScale() const
Returns the renderer map scale.
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
static Q_INVOKABLE double fromUnitToUnitFactor(Qgis::DistanceUnit fromUnit, Qgis::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6852