QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
29
30void QgsElevationShadingRenderer::renderShading( const QgsElevationMap &elevation, QImage &image, const QgsRenderContext &context ) const
31{
32 if ( elevation.rawElevationImage().size() != image.size() )
33 return;
34
35 if ( mRenderEdl )
36 renderEdl( elevation, image, context );
37
38 if ( mRenderHillshading )
39 renderHillshading( elevation, image, context );
40}
41
43{
44 mIsActive = active;
45}
46
48{
49 return mIsActive;
50}
51
53{
54 mRenderEdl = active;
55}
56
58{
59 return mRenderEdl;
60}
61
63{
64 mRenderHillshading = active;
65}
66
68{
69 return mRenderHillshading;
70}
71
73{
74 return mHillshadingZFactor;
75}
76
78{
79 mHillshadingZFactor = zFactor;
80}
81
83{
84 return mHillshadingMultiDir;
85}
86
88{
89 mHillshadingMultiDir = multiDirectional;
90}
91
93{
94 return mLightAltitude;
95}
96
101
103{
104 return mLightAzimuth;
105}
106
108{
109 mLightAzimuth = lightAzimuth;
110}
111
112void QgsElevationShadingRenderer::writeXml( QDomElement &elem, const QgsReadWriteContext & ) const
113{
114 elem.setAttribute( u"is-active"_s, mIsActive ? 1 : 0 );
115
116 elem.setAttribute( u"combined-method"_s, static_cast<int>( mCombinedElevationMethod ) );
117
118 elem.setAttribute( u"edl-is-active"_s, mRenderEdl ? 1 : 0 );
119 elem.setAttribute( u"edl-strength"_s, qgsDoubleToString( mEyeDomeLightingStrength ) );
120 elem.setAttribute( u"edl-distance"_s, qgsDoubleToString( mEyeDomeLightingDistance ) );
121 elem.setAttribute( u"edl-distance-unit"_s, static_cast<int>( mEyeDomeLightingDistanceUnit ) );
122
123 elem.setAttribute( u"hillshading-is-active"_s, mRenderHillshading ? 1 : 0 );
124 elem.setAttribute( u"hillshading-z-factor"_s, qgsDoubleToString( mHillshadingZFactor ) );
125 elem.setAttribute( u"hillshading-is-multidirectional"_s, mHillshadingMultiDir ? 1 : 0 );
126
127 elem.setAttribute( u"light-altitude"_s, qgsDoubleToString( mLightAltitude ) );
128 elem.setAttribute( u"light-azimuth"_s, qgsDoubleToString( mLightAzimuth ) );
129}
130
131void QgsElevationShadingRenderer::readXml( const QDomElement &element, const QgsReadWriteContext & )
132{
133 if ( element.hasAttribute( u"is-active"_s ) )
134 mIsActive = element.attribute( u"is-active"_s ).toInt() == 1;
135
136 if ( element.hasAttribute( u"combined-method"_s ) )
137 mCombinedElevationMethod = static_cast<Qgis::ElevationMapCombineMethod>( element.attribute( u"combined-method"_s ).toInt() );
138
139 if ( element.hasAttribute( u"edl-is-active"_s ) )
140 mRenderEdl = element.attribute( u"edl-is-active"_s ).toInt() == 1;
141
142 if ( element.hasAttribute( u"edl-strength"_s ) )
143 mEyeDomeLightingStrength = element.attribute( u"edl-strength"_s ).toDouble();
144
145 if ( element.hasAttribute( u"edl-distance"_s ) )
146 mEyeDomeLightingDistance = element.attribute( u"edl-distance"_s ).toDouble();
147
148 if ( element.hasAttribute( u"edl-distance-unit"_s ) )
149 mEyeDomeLightingDistanceUnit = static_cast<Qgis::RenderUnit>( element.attribute( u"edl-distance-unit"_s ).toInt() );
150
151 if ( element.hasAttribute( u"hillshading-is-active"_s ) )
152 mRenderHillshading = element.attribute( u"hillshading-is-active"_s ).toInt() == 1;
153
154 if ( element.hasAttribute( u"hillshading-z-factor"_s ) )
155 mHillshadingZFactor = element.attribute( u"hillshading-z-factor"_s ).toDouble();
156
157 if ( element.hasAttribute( u"hillshading-is-multidirectional"_s ) )
158 mHillshadingMultiDir = element.attribute( u"hillshading-is-multidirectional"_s ).toInt() == 1;
159
160 if ( element.hasAttribute( u"light-altitude"_s ) )
161 mLightAltitude = element.attribute( u"light-altitude"_s ).toDouble();
162
163 if ( element.hasAttribute( u"light-azimuth"_s ) )
164 mLightAzimuth = element.attribute( u"light-azimuth"_s ).toDouble();
165}
166
171
173{
174 mCombinedElevationMethod = newCombinedElevationMethod;
175}
176
178{
179 return mEyeDomeLightingDistanceUnit;
180}
181
183{
184 mEyeDomeLightingDistanceUnit = newEyeDomeLightingDistanceUnit;
185}
186
188{
189 return mEyeDomeLightingDistance;
190}
191
193{
194 mEyeDomeLightingDistance = distance;
195}
196
198{
199 return mEyeDomeLightingStrength;
200}
201
203{
204 mEyeDomeLightingStrength = strength;
205}
206
207void QgsElevationShadingRenderer::renderEdl( const QgsElevationMap &elevation, QImage &image, const QgsRenderContext &context ) const
208{
209 double strength = mEyeDomeLightingStrength;
210 double distanceDouble = context.convertToPainterUnits( mEyeDomeLightingDistance, mEyeDomeLightingDistanceUnit );
211 int distance = static_cast<int>( std::round( distanceDouble ) );
212
213 elevation.applyEyeDomeLighting( image, distance, static_cast<float>( strength ), static_cast<float>( context.rendererScale() ) );
214}
215
216void QgsElevationShadingRenderer::renderHillshading( const QgsElevationMap &elevation, QImage &image, const QgsRenderContext &context ) const
217{
218 double pixelSize = context.mapToPixel().mapUnitsPerPixel();
219
220 // We suppose that the elevation are in meter, so we need to use meter for pixel size if possible
221 Qgis::DistanceUnit destinationUnit = context.distanceArea().lengthUnits();
222 double effPixelSize;
223 if ( destinationUnit != Qgis::DistanceUnit::Unknown )
224 effPixelSize = QgsUnitTypes::fromUnitToUnitFactor( destinationUnit, Qgis::DistanceUnit::Meters ) * pixelSize;
225 else
226 effPixelSize = pixelSize;
227
228 elevation.applyHillshading( image, mHillshadingMultiDir, mLightAltitude, mLightAzimuth, mHillshadingZFactor, effPixelSize, effPixelSize );
229}
DistanceUnit
Units of distance.
Definition qgis.h:5170
@ Meters
Meters.
Definition qgis.h:5171
@ Unknown
Unknown distance unit.
Definition qgis.h:5220
ElevationMapCombineMethod
Methods used to select the elevation when two elevation maps are combined.
Definition qgis.h:5072
RenderUnit
Rendering size units.
Definition qgis.h:5340
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:6893