35using namespace Qt::StringLiterals;
39class QgsAlgorithmElevationProfilePlotItem :
public Qgs2DXyPlot
42 explicit QgsAlgorithmElevationProfilePlotItem(
int width,
int height,
int dpi )
47 setSize( QSizeF( width, height ) );
50 void setRenderer( QgsProfilePlotRenderer *renderer )
57 if ( !mPlotArea.isNull() )
63 QgsRenderContext context;
66 QgsPlotRenderContext plotContext;
72 void renderContent( QgsRenderContext &rc, QgsPlotRenderContext &,
const QRectF &plotArea,
const QgsPlotData & )
override
79 rc.
painter()->translate( mPlotArea.left(), mPlotArea.top() );
80 const QStringList sourceIds = mRenderer->sourceIds();
81 for (
const QString &source : sourceIds )
83 mRenderer->render( rc, mPlotArea.width(), mPlotArea.height(), xMinimum(),
xMaximum(),
yMinimum(),
yMaximum(), source );
85 rc.
painter()->translate( -mPlotArea.left(), -mPlotArea.top() );
91 QgsProfilePlotRenderer *mRenderer =
nullptr;
94void QgsGenerateElevationProfileAlgorithm::initAlgorithm(
const QVariantMap & )
102 auto minimumDistanceParam = std::make_unique<QgsProcessingParameterNumber>( u
"MINIMUM_DISTANCE"_s, QObject::tr(
"Chart minimum distance (X axis)" ),
Qgis::ProcessingNumberParameterType::Double, QVariant(),
true );
104 addParameter( minimumDistanceParam.release() );
105 auto maximumDistanceParam = std::make_unique<QgsProcessingParameterNumber>( u
"MAXIMUM_DISTANCE"_s, QObject::tr(
"Chart maximum distance (X axis)" ),
Qgis::ProcessingNumberParameterType::Double, QVariant(),
true );
107 addParameter( maximumDistanceParam.release() );
108 auto minimumElevationParam = std::make_unique<QgsProcessingParameterNumber>( u
"MINIMUM_ELEVATION"_s, QObject::tr(
"Chart minimum elevation (Y axis)" ),
Qgis::ProcessingNumberParameterType::Double, QVariant(),
true );
110 addParameter( minimumElevationParam.release() );
111 auto maximumElevationParam = std::make_unique<QgsProcessingParameterNumber>( u
"MAXIMUM_ELEVATION"_s, QObject::tr(
"Chart maximum elevation (Y axis)" ),
Qgis::ProcessingNumberParameterType::Double, QVariant(),
true );
113 addParameter( maximumElevationParam.release() );
115 auto textColorParam = std::make_unique<QgsProcessingParameterColor>( u
"TEXT_COLOR"_s, QObject::tr(
"Chart text color" ), QColor( 0, 0, 0 ),
true,
true );
117 addParameter( textColorParam.release() );
119 auto textFontFamilyParam = std::make_unique<QgsProcessingParameterString>( u
"TEXT_FONT_FAMILY"_s, QObject::tr(
"Chart text font family" ), QVariant(),
false,
true );
121 addParameter( textFontFamilyParam.release() );
123 auto textFontStyleParam = std::make_unique<QgsProcessingParameterString>( u
"TEXT_FONT_STYLE"_s, QObject::tr(
"Chart text font style" ), QVariant(),
false,
true );
125 addParameter( textFontStyleParam.release() );
129 addParameter( textFontSizeParam.release() );
131 auto backgroundColorParam = std::make_unique<QgsProcessingParameterColor>( u
"BACKGROUND_COLOR"_s, QObject::tr(
"Chart background color" ), QColor( 255, 255, 255 ),
true,
true );
133 addParameter( backgroundColorParam.release() );
134 auto borderColorParam = std::make_unique<QgsProcessingParameterColor>( u
"BORDER_COLOR"_s, QObject::tr(
"Chart border color" ), QColor( 99, 99, 99 ),
true,
true );
136 addParameter( borderColorParam.release() );
140 addParameter( toleranceParam.release() );
144 addParameter( dpiParam.release() );
149QString QgsGenerateElevationProfileAlgorithm::name()
const
151 return u
"generateelevationprofileimage"_s;
154QString QgsGenerateElevationProfileAlgorithm::displayName()
const
156 return QObject::tr(
"Generate elevation profile image" );
159QStringList QgsGenerateElevationProfileAlgorithm::tags()
const
161 return QObject::tr(
"altitude,elevation,terrain,dem" ).split(
',' );
164QString QgsGenerateElevationProfileAlgorithm::group()
const
166 return QObject::tr(
"Plots" );
169QString QgsGenerateElevationProfileAlgorithm::groupId()
const
174QString QgsGenerateElevationProfileAlgorithm::shortHelpString()
const
176 return QObject::tr(
"This algorithm creates an elevation profile image from a list of map layer and an optional terrain." );
179QString QgsGenerateElevationProfileAlgorithm::shortDescription()
const
181 return QObject::tr(
"Creates an elevation profile image from a list of map layer and an optional terrain." );
184QgsGenerateElevationProfileAlgorithm *QgsGenerateElevationProfileAlgorithm::createInstance()
const
186 return new QgsGenerateElevationProfileAlgorithm();
191 const QgsGeometry curveGeom = parameterAsGeometry( parameters, u
"CURVE"_s, context );
194 QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u
"MAP_LAYERS"_s, context );
195 QgsMapLayer *terrainLayer = parameterAsLayer( parameters, u
"TERRAIN_LAYER"_s, context );
197 const double tolerance = parameterAsDouble( parameters, u
"TOLERANCE"_s, context );
199 QList<QgsAbstractProfileSource *> sources;
203 sources.append( source );
207 request.setCrs( curveCrs );
208 request.setTolerance( tolerance );
216 auto terrainProvider = std::make_unique<QgsRasterDemTerrainProvider>();
217 terrainProvider->setLayer( rasterLayer );
218 request.setTerrainProvider( terrainProvider.release() );
222 auto terrainProvider = std::make_unique<QgsMeshTerrainProvider>();
223 terrainProvider->setLayer( meshLayer );
224 request.setTerrainProvider( terrainProvider.release() );
229 mRenderer = std::make_unique<QgsProfilePlotRenderer>( sources, request );
236 const QgsGeometry curveGeom = parameterAsGeometry( parameters, u
"CURVE"_s, context );
238 const bool hasMinimumDistance = parameters.value( u
"MINIMUM_DISTANCE"_s ).isValid();
239 const double minimumDistance = parameterAsDouble( parameters, u
"MINIMUM_DISTANCE"_s, context );
240 const bool hasMaximumDistance = parameters.value( u
"MAXIMUM_DISTANCE"_s ).isValid();
241 const double maximumDistance = parameterAsDouble( parameters, u
"MAXIMUM_DISTANCE"_s, context );
242 const bool hasMinimumElevation = parameters.value( u
"MINIMUM_ELEVATION"_s ).isValid();
243 const double minimumElevation = parameterAsDouble( parameters, u
"MINIMUM_ELEVATION"_s, context );
244 const bool hasMaximumElevation = parameters.value( u
"MAXIMUM_ELEVATION"_s ).isValid();
245 const double maximumElevation = parameterAsDouble( parameters, u
"MAXIMUM_ELEVATION"_s, context );
247 const int width = parameterAsInt( parameters, u
"WIDTH"_s, context );
248 const int height = parameterAsInt( parameters, u
"HEIGHT"_s, context );
249 const int dpi = parameterAsInt( parameters, u
"DPI"_s, context );
251 const QString outputImage = parameterAsString( parameters, u
"OUTPUT"_s, context );
253 const QColor textColor = parameterAsColor( parameters, u
"TEXT_COLOR"_s, context );
254 const QColor backgroundColor = parameterAsColor( parameters, u
"BACKGROUND_COLOR"_s, context );
255 const QColor borderColor = parameterAsColor( parameters, u
"BORDER_COLOR"_s, context );
257 QgsAlgorithmElevationProfilePlotItem plotItem( width, height, dpi );
259 const QString textFontFamily = parameterAsString( parameters, u
"TEXT_FONT_FAMILY"_s, context );
260 const QString textFontStyle = parameterAsString( parameters, u
"TEXT_FONT_STYLE"_s, context );
261 const double textFontSize = parameterAsDouble( parameters, u
"TEXT_FONT_SIZE"_s, context );
263 if ( !textFontFamily.isEmpty() || !textFontStyle.isEmpty() || textFontSize > 0 )
266 QFont font = textFormat.
font();
267 if ( !textFontFamily.isEmpty() )
271 if ( !textFontStyle.isEmpty() )
276 if ( textFontSize > 0 )
278 textFormat.
setSize( textFontSize );
282 plotItem.xAxis().setTextFormat( textFormat );
283 plotItem.yAxis().setTextFormat( textFormat );
286 if ( textColor.isValid() )
290 plotItem.xAxis().setTextFormat( textFormat );
291 textFormat = plotItem.yAxis().textFormat();
293 plotItem.yAxis().setTextFormat( textFormat );
296 if ( borderColor.isValid() )
298 auto lineSymbolLayer = std::make_unique<QgsSimpleLineSymbolLayer>( borderColor, 0.1 );
299 lineSymbolLayer->setPenCapStyle( Qt::FlatCap );
307 if ( backgroundColor.isValid() )
309 auto fillSymbolLayer = std::make_unique<QgsSimpleFillSymbolLayer>( backgroundColor, Qt::SolidPattern, backgroundColor );
314 generationContext.
setDpi( dpi );
318 mRenderer->setContext( generationContext );
320 mRenderer->startGeneration();
321 mRenderer->waitForFinished();
335 zMinimum = zRange.
lower() - 5;
336 zMaximum = zRange.
lower() + 5;
341 const double margin = ( zRange.
upper() - zRange.
lower() ) * 0.05;
342 zMinimum = zRange.
lower() - margin;
343 zMaximum = zRange.
upper() + margin;
346 plotItem.setYMinimum( hasMinimumElevation ? minimumElevation : zMinimum );
347 plotItem.setYMaximum( hasMaximumElevation ? maximumElevation : zMaximum );
348 plotItem.setXMinimum( hasMinimumDistance ? minimumDistance : 0 );
349 plotItem.setXMaximum( hasMaximumDistance ? maximumDistance : curveGeom.
constGet()->
length() );
351 plotItem.setRenderer( mRenderer.get() );
353 QImage image(
static_cast<int>( plotItem.size().width() ),
static_cast<int>( plotItem.size().height() ), QImage::Format_ARGB32_Premultiplied );
354 image.fill( Qt::transparent );
356 QPainter painter( &image );
357 painter.setRenderHint( QPainter::Antialiasing,
true );
362 plotItem.calculateOptimisedIntervals( renderContext, plotContext );
363 plotItem.render( renderContext, plotContext );
365 image.save( outputImage );
368 outputs.insert( u
"OUTPUT"_s, outputImage );
@ MapLayer
Any map layer type (raster, vector, mesh, point cloud, annotation or plugin layer).
@ Points
Points (e.g., for font sizes).
@ Hidden
Parameter is hidden and should not be shown to users.
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
@ Double
Double/float values.
virtual void renderContent(QgsRenderContext &context, QgsPlotRenderContext &plotContext, const QRectF &plotArea, const QgsPlotData &plotData=QgsPlotData())
Renders the plot content.
void setSize(QSizeF size)
Sets the overall size of the plot (including titles and over components which sit outside the plot ar...
Base class for 2-dimensional plot/chart/graphs with an X and Y axes.
double yMaximum() const
Returns the maximum value of the y axis.
void setYMinimum(double minimum)
Sets the minimum value of the y axis.
void calculateOptimisedIntervals(QgsRenderContext &context, QgsPlotRenderContext &plotContext)
Automatically sets the grid and label intervals to optimal values for display in the given render con...
double yMinimum() const
Returns the minimum value of the y axis.
QRectF interiorPlotArea(QgsRenderContext &context, QgsPlotRenderContext &plotContext) const override
Returns the area of the plot which corresponds to the actual plot content (excluding all titles and o...
void setYMaximum(double maximum)
Sets the maximum value of the y axis.
double xMaximum() const
Returns the maximum value of the x axis.
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.
QgsRange which stores a range of double values.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
static bool updateFontViaStyle(QFont &f, const QString &fontstyle, bool fallback=false)
Updates font with named style and retain all font properties.
static void setFontFamily(QFont &font, const QString &family)
Sets the family for a font object.
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.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Contains information about the context of a plot rendering operation.
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.
Encapsulates properties and constraints relating to fetching elevation profiles from different source...
T lower() const
Returns the lower bound of the range.
T upper() const
Returns the upper bound of the range.
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.
void setSize(double size)
Sets the size for rendered text.
void setFont(const QFont &font)
Sets the font used for rendering text.
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units for the size of rendered text.
QFont font() const
Returns the font used for rendering text.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
QList< QgsSymbolLayer * > QgsSymbolLayerList