QGIS API Documentation 3.43.0-Master (6c62b930b02)
qgsalgorithmgenerateelevationprofile.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmgenerateelevationprofile.cpp
3 ---------------------
4 begin : October 2024
5 copyright : (C) 2024 by Mathieu Pellerin
6 email : mathieu at opengis dot ch
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
20#include "qgis.h"
22#include "qgstextformat.h"
23#include "qgsfillsymbol.h"
24#include "qgsfillsymbollayer.h"
25#include "qgslinesymbol.h"
26#include "qgslinesymbollayer.h"
27#include "qgsplot.h"
28#include "qgsprofilerequest.h"
29#include "qgsterrainprovider.h"
30#include "qgscurve.h"
31
33
34class QgsAlgorithmElevationProfilePlotItem : public Qgs2DPlot
35{
36 public:
37 explicit QgsAlgorithmElevationProfilePlotItem( int width, int height, int dpi )
38 : mDpi( dpi )
39 {
40 setYMinimum( 0 );
41 setYMaximum( 10 );
42 setSize( QSizeF( width, height ) );
43 }
44
45 void setRenderer( QgsProfilePlotRenderer *renderer )
46 {
47 mRenderer = renderer;
48 }
49
50 QRectF plotArea()
51 {
52 if ( !mPlotArea.isNull() )
53 {
54 return mPlotArea;
55 }
56
57 // calculate plot area
58 QgsRenderContext context;
59 context.setScaleFactor( mDpi / 25.4 );
60
62 mPlotArea = interiorPlotArea( context );
63 return mPlotArea;
64 }
65
66 void renderContent( QgsRenderContext &rc, const QRectF &plotArea ) override
67 {
68 mPlotArea = plotArea;
69
70 if ( !mRenderer )
71 return;
72
73 rc.painter()->translate( mPlotArea.left(), mPlotArea.top() );
74 const QStringList sourceIds = mRenderer->sourceIds();
75 for ( const QString &source : sourceIds )
76 {
77 mRenderer->render( rc, mPlotArea.width(), mPlotArea.height(), xMinimum(), xMaximum(), yMinimum(), yMaximum(), source );
78 }
79 rc.painter()->translate( -mPlotArea.left(), -mPlotArea.top() );
80 }
81
82 private:
83 int mDpi = 96;
84 QRectF mPlotArea;
85 QgsProfilePlotRenderer *mRenderer = nullptr;
86};
87
88void QgsGenerateElevationProfileAlgorithm::initAlgorithm( const QVariantMap & )
89{
90 addParameter( new QgsProcessingParameterGeometry( QStringLiteral( "CURVE" ), QObject::tr( "Profile curve" ), QVariant(), false, QList<int>() << static_cast<int>( Qgis::GeometryType::Line ) ) );
91 addParameter( new QgsProcessingParameterMultipleLayers( QStringLiteral( "MAP_LAYERS" ), QObject::tr( "Map layers" ), Qgis::ProcessingSourceType::MapLayer, QVariant(), false ) );
92 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "WIDTH" ), QObject::tr( "Chart width (in pixels)" ), Qgis::ProcessingNumberParameterType::Integer, 400, false, 0 ) );
93 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "HEIGHT" ), QObject::tr( "Chart height (in pixels)" ), Qgis::ProcessingNumberParameterType::Integer, 300, false, 0 ) );
94 addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "TERRAIN_LAYER" ), QObject::tr( "Terrain layer" ), QVariant(), true, QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Raster ) << static_cast<int>( Qgis::ProcessingSourceType::Mesh ) ) );
95
96 auto minimumDistanceParam = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "MINIMUM_DISTANCE" ), QObject::tr( "Chart minimum distance (X axis)" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true );
97 minimumDistanceParam->setFlags( minimumDistanceParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
98 addParameter( minimumDistanceParam.release() );
99 auto maximumDistanceParam = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "MAXIMUM_DISTANCE" ), QObject::tr( "Chart maximum distance (X axis)" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true );
100 maximumDistanceParam->setFlags( maximumDistanceParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
101 addParameter( maximumDistanceParam.release() );
102 auto minimumElevationParam = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "MINIMUM_ELEVATION" ), QObject::tr( "Chart minimum elevation (Y axis)" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true );
103 minimumElevationParam->setFlags( minimumElevationParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
104 addParameter( minimumElevationParam.release() );
105 auto maximumElevationParam = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "MAXIMUM_ELEVATION" ), QObject::tr( "Chart maximum elevation (Y axis)" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true );
106 maximumElevationParam->setFlags( maximumElevationParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
107 addParameter( maximumElevationParam.release() );
108
109 auto textColorParam = std::make_unique<QgsProcessingParameterColor>( QStringLiteral( "TEXT_COLOR" ), QObject::tr( "Chart text color" ), QColor( 0, 0, 0 ), true, true );
110 textColorParam->setFlags( textColorParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
111 addParameter( textColorParam.release() );
112 auto backgroundColorParam = std::make_unique<QgsProcessingParameterColor>( QStringLiteral( "BACKGROUND_COLOR" ), QObject::tr( "Chart background color" ), QColor( 255, 255, 255 ), true, true );
113 backgroundColorParam->setFlags( backgroundColorParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
114 addParameter( backgroundColorParam.release() );
115 auto borderColorParam = std::make_unique<QgsProcessingParameterColor>( QStringLiteral( "BORDER_COLOR" ), QObject::tr( "Chart border color" ), QColor( 99, 99, 99 ), true, true );
116 borderColorParam->setFlags( borderColorParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
117 addParameter( borderColorParam.release() );
118
119 auto toleranceParam = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "TOLERANCE" ), QObject::tr( "Profile tolerance" ), Qgis::ProcessingNumberParameterType::Double, 5.0, false, 0 );
120 toleranceParam->setFlags( toleranceParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
121 addParameter( toleranceParam.release() );
122
123 auto dpiParam = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "DPI" ), QObject::tr( "Chart DPI" ), Qgis::ProcessingNumberParameterType::Integer, 96, false, 0 );
124 dpiParam->setFlags( dpiParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
125 addParameter( dpiParam.release() );
126
127 addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Output image" ) ) );
128}
129
130QString QgsGenerateElevationProfileAlgorithm::name() const
131{
132 return QStringLiteral( "generateelevationprofileimage" );
133}
134
135QString QgsGenerateElevationProfileAlgorithm::displayName() const
136{
137 return QObject::tr( "Generate elevation profile image" );
138}
139
140QStringList QgsGenerateElevationProfileAlgorithm::tags() const
141{
142 return QObject::tr( "altitude,elevation,terrain,dem" ).split( ',' );
143}
144
145QString QgsGenerateElevationProfileAlgorithm::group() const
146{
147 return QObject::tr( "Plots" );
148}
149
150QString QgsGenerateElevationProfileAlgorithm::groupId() const
151{
152 return QStringLiteral( "plots" );
153}
154
155QString QgsGenerateElevationProfileAlgorithm::shortHelpString() const
156{
157 return QObject::tr( "This algorithm creates an elevation profile image from a list of map layer and an optional terrain." );
158}
159
160QString QgsGenerateElevationProfileAlgorithm::shortDescription() const
161{
162 return QObject::tr( "Creates an elevation profile image from a list of map layer and an optional terrain." );
163}
164
165QgsGenerateElevationProfileAlgorithm *QgsGenerateElevationProfileAlgorithm::createInstance() const
166{
167 return new QgsGenerateElevationProfileAlgorithm();
168}
169
170bool QgsGenerateElevationProfileAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
171{
172 const QgsGeometry curveGeom = parameterAsGeometry( parameters, QStringLiteral( "CURVE" ), context );
173 const QgsCoordinateReferenceSystem curveCrs = parameterAsGeometryCrs( parameters, QStringLiteral( "CURVE" ), context );
174
175 QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral( "MAP_LAYERS" ), context );
176 QgsMapLayer *terrainLayer = parameterAsLayer( parameters, QStringLiteral( "TERRAIN_LAYER" ), context );
177
178 const double tolerance = parameterAsDouble( parameters, QStringLiteral( "TOLERANCE" ), context );
179
180 QList<QgsAbstractProfileSource *> sources;
181 for ( QgsMapLayer *layer : layers )
182 {
183 if ( QgsAbstractProfileSource *source = dynamic_cast<QgsAbstractProfileSource *>( layer ) )
184 sources.append( source );
185 }
186
187 QgsProfileRequest request( static_cast<QgsCurve *>( curveGeom.constGet()->clone() ) );
188 request.setCrs( curveCrs );
189 request.setTolerance( tolerance );
190 request.setTransformContext( context.transformContext() );
191 request.setExpressionContext( context.expressionContext() );
192
193 if ( terrainLayer )
194 {
195 if ( QgsRasterLayer *rasterLayer = dynamic_cast<QgsRasterLayer *>( terrainLayer ) )
196 {
197 auto terrainProvider = std::make_unique<QgsRasterDemTerrainProvider>();
198 terrainProvider->setLayer( rasterLayer );
199 request.setTerrainProvider( terrainProvider.release() );
200 }
201 else if ( QgsMeshLayer *meshLayer = dynamic_cast<QgsMeshLayer *>( terrainLayer ) )
202 {
203 auto terrainProvider = std::make_unique<QgsMeshTerrainProvider>();
204 terrainProvider->setLayer( meshLayer );
205 request.setTerrainProvider( terrainProvider.release() );
206 }
207 }
208
209
210 mRenderer = std::make_unique<QgsProfilePlotRenderer>( sources, request );
211
212 return true;
213}
214
215QVariantMap QgsGenerateElevationProfileAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
216{
217 const QgsGeometry curveGeom = parameterAsGeometry( parameters, QStringLiteral( "CURVE" ), context );
218
219 const bool hasMinimumDistance = parameters.value( QStringLiteral( "MINIMUM_DISTANCE" ) ).isValid();
220 const double minimumDistance = parameterAsDouble( parameters, QStringLiteral( "MINIMUM_DISTANCE" ), context );
221 const bool hasMaximumDistance = parameters.value( QStringLiteral( "MAXIMUM_DISTANCE" ) ).isValid();
222 const double maximumDistance = parameterAsDouble( parameters, QStringLiteral( "MAXIMUM_DISTANCE" ), context );
223 const bool hasMinimumElevation = parameters.value( QStringLiteral( "MINIMUM_ELEVATION" ) ).isValid();
224 const double minimumElevation = parameterAsDouble( parameters, QStringLiteral( "MINIMUM_ELEVATION" ), context );
225 const bool hasMaximumElevation = parameters.value( QStringLiteral( "MAXIMUM_ELEVATION" ) ).isValid();
226 const double maximumElevation = parameterAsDouble( parameters, QStringLiteral( "MAXIMUM_ELEVATION" ), context );
227
228 const int width = parameterAsInt( parameters, QStringLiteral( "WIDTH" ), context );
229 const int height = parameterAsInt( parameters, QStringLiteral( "HEIGHT" ), context );
230 const int dpi = parameterAsInt( parameters, QStringLiteral( "DPI" ), context );
231
232 const QString outputImage = parameterAsString( parameters, QStringLiteral( "OUTPUT" ), context );
233
234 const QColor textColor = parameterAsColor( parameters, QStringLiteral( "TEXT_COLOR" ), context );
235 const QColor backgroundColor = parameterAsColor( parameters, QStringLiteral( "BACKGROUND_COLOR" ), context );
236 const QColor borderColor = parameterAsColor( parameters, QStringLiteral( "BORDER_COLOR" ), context );
237
238 QgsAlgorithmElevationProfilePlotItem plotItem( width, height, dpi );
239
240 if ( textColor.isValid() )
241 {
242 QgsTextFormat textFormat = plotItem.xAxis().textFormat();
243 textFormat.setColor( textColor );
244 plotItem.xAxis().setTextFormat( textFormat );
245 textFormat = plotItem.yAxis().textFormat();
246 textFormat.setColor( textColor );
247 plotItem.yAxis().setTextFormat( textFormat );
248 }
249
250 if ( borderColor.isValid() )
251 {
252 auto lineSymbolLayer = std::make_unique<QgsSimpleLineSymbolLayer>( borderColor, 0.1 );
253 lineSymbolLayer->setPenCapStyle( Qt::FlatCap );
254 plotItem.xAxis().setGridMinorSymbol( new QgsLineSymbol( QgsSymbolLayerList( { lineSymbolLayer->clone() } ) ) );
255 plotItem.yAxis().setGridMinorSymbol( new QgsLineSymbol( QgsSymbolLayerList( { lineSymbolLayer->clone() } ) ) );
256 plotItem.xAxis().setGridMajorSymbol( new QgsLineSymbol( QgsSymbolLayerList( { lineSymbolLayer->clone() } ) ) );
257 plotItem.yAxis().setGridMajorSymbol( new QgsLineSymbol( QgsSymbolLayerList( { lineSymbolLayer->clone() } ) ) );
258 plotItem.setChartBorderSymbol( new QgsFillSymbol( QgsSymbolLayerList( { lineSymbolLayer.release() } ) ) );
259 }
260
261 if ( backgroundColor.isValid() )
262 {
263 auto fillSymbolLayer = std::make_unique<QgsSimpleFillSymbolLayer>( backgroundColor, Qt::SolidPattern, backgroundColor );
264 plotItem.setChartBackgroundSymbol( new QgsFillSymbol( QgsSymbolLayerList( { fillSymbolLayer.release() } ) ) );
265 }
266
267 QgsProfileGenerationContext generationContext;
268 generationContext.setDpi( dpi );
269 generationContext.setMaximumErrorMapUnits( MAX_ERROR_PIXELS * ( curveGeom.constGet()->length() ) / plotItem.plotArea().width() );
270 generationContext.setMapUnitsPerDistancePixel( curveGeom.constGet()->length() / plotItem.plotArea().width() );
271
272 mRenderer->setContext( generationContext );
273
274 mRenderer->startGeneration();
275 mRenderer->waitForFinished();
276
277 const QgsDoubleRange zRange = mRenderer->zRange();
278 double zMinimum = 0;
279 double zMaximum = 0;
280 if ( zRange.upper() < zRange.lower() )
281 {
282 // invalid range, e.g. no features found in plot!
283 zMinimum = 0;
284 zMaximum = 10;
285 }
286 else if ( qgsDoubleNear( zRange.lower(), zRange.upper(), 0.0000001 ) )
287 {
288 // corner case ... a zero height plot! Just pick an arbitrary +/- 5 height range.
289 zMinimum = zRange.lower() - 5;
290 zMaximum = zRange.lower() + 5;
291 }
292 else
293 {
294 // add 5% margin to height range
295 const double margin = ( zRange.upper() - zRange.lower() ) * 0.05;
296 zMinimum = zRange.lower() - margin;
297 zMaximum = zRange.upper() + margin;
298 }
299
300 plotItem.setYMinimum( hasMinimumElevation ? minimumElevation : zMinimum );
301 plotItem.setYMaximum( hasMaximumElevation ? maximumElevation : zMaximum );
302 plotItem.setXMinimum( hasMinimumDistance ? minimumDistance : 0 );
303 plotItem.setXMaximum( hasMaximumDistance ? maximumDistance : curveGeom.constGet()->length() );
304
305 plotItem.setRenderer( mRenderer.get() );
306
307 QImage image( static_cast<int>( plotItem.size().width() ), static_cast<int>( plotItem.size().height() ), QImage::Format_ARGB32_Premultiplied );
308 image.fill( Qt::transparent );
309
310 QPainter painter( &image );
311 painter.setRenderHint( QPainter::Antialiasing, true );
312 QgsRenderContext renderContext = QgsRenderContext::fromQPainter( &painter );
313 renderContext.setScaleFactor( dpi / 25.4 );
314 renderContext.setExpressionContext( context.expressionContext() );
315 plotItem.calculateOptimisedIntervals( renderContext );
316 plotItem.render( renderContext );
317 painter.end();
318 image.save( outputImage );
319
320 QVariantMap outputs;
321 outputs.insert( QStringLiteral( "OUTPUT" ), outputImage );
322 return outputs;
323}
324
@ MapLayer
Any map layer type (raster, vector, mesh, point cloud, annotation or plugin layer)
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Base class for 2-dimensional plot/chart/graphs.
Definition qgsplot.h:273
void calculateOptimisedIntervals(QgsRenderContext &context)
Automatically sets the grid and label intervals to optimal values for display in the given render con...
Definition qgsplot.cpp:611
double yMaximum() const
Returns the maximum value of the y axis.
Definition qgsplot.h:383
void setSize(QSizeF size)
Sets the overall size of the plot (including titles and over components which sit outside the plot ar...
Definition qgsplot.cpp:491
double xMaximum() const
Returns the maximum value of the x axis.
Definition qgsplot.h:369
void setYMaximum(double maximum)
Sets the maximum value of the y axis.
Definition qgsplot.h:390
double yMinimum() const
Returns the minimum value of the y axis.
Definition qgsplot.h:355
QRectF interiorPlotArea(QgsRenderContext &context) const
Returns the area of the plot which corresponds to the actual plot content (excluding all titles and o...
Definition qgsplot.cpp:496
void setYMinimum(double minimum)
Sets the minimum value of the y axis.
Definition qgsplot.h:362
virtual void renderContent(QgsRenderContext &context, const QRectF &plotArea)
Renders the plot content.
Definition qgsplot.cpp:479
virtual double length() const
Returns the planar, 2-dimensional length of the geometry.
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
Interface for classes which can generate elevation profiles.
Represents a coordinate reference system (CRS).
Abstract base class for curved geometry type.
Definition qgscurve.h:35
QgsRange which stores a range of double values.
Definition qgsrange.h:233
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
A geometry is the spatial representation of a feature.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
A line symbol type, for rendering LineString and MultiLineString geometries.
Base class for all map layer types.
Definition qgsmaplayer.h:77
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Base class for providing feedback from a processing algorithm.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
A geometry parameter for processing algorithms.
A map layer parameter for processing algorithms.
A parameter for processing algorithms which accepts multiple map layers.
A numeric parameter for processing algorithms.
Encapsulates the context in which an elevation profile is to be generated.
void setDpi(double dpi)
Sets the dpi (dots per inch) for the profie, to be used in size conversions.
void setMaximumErrorMapUnits(double error)
Sets the maximum allowed error in the generated result, in profile curve map units.
void setMapUnitsPerDistancePixel(double units)
Sets the number of map units per pixel in the distance dimension.
Generates and renders elevation profile plots.
Encapsulates properties and constraints relating to fetching elevation profiles from different source...
T lower() const
Returns the lower bound of the range.
Definition qgsrange.h:78
T upper() const
Returns the upper bound of the range.
Definition qgsrange.h:85
Represents a raster layer.
Contains information about the context of a rendering operation.
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
QPainter * painter()
Returns the destination QPainter for the render operation.
static QgsRenderContext fromQPainter(QPainter *painter)
Creates a default render context given a pixel based QPainter destination.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
Container for all settings relating to text rendering.
void setColor(const QColor &color)
Sets the color that text will be rendered in.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:6367
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition qgssymbol.h:30