QGIS API Documentation 4.3.0-Master (b46232e4f9a)
Loading...
Searching...
No Matches
qgsabstractprofilesurfacegenerator.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsabstractprofilegenerator.cpp
3 ---------------
4 begin : March 2022
5 copyright : (C) 2022 by Nyall Dawson
6 email : nyall dot dawson at gmail 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 ***************************************************************************/
18
19#include <optional>
20
21#include "qgsfillsymbol.h"
22#include "qgslinestring.h"
23#include "qgslinesymbol.h"
24#include "qgspainting.h"
25#include "qgsprofilerequest.h"
26#include "qgsprofilesnapping.h"
27
28#include <QPainterPath>
29#include <QString>
30
31using namespace Qt::StringLiterals;
32
33//
34// QgsAbstractProfileSurfaceResults
35//
36
38
43
48
53
55{
56 QVector<QgsGeometry> res;
57 res.reserve( mRawPoints.size() );
58 for ( const QgsPoint &point : mRawPoints )
59 res.append( QgsGeometry( point.clone() ) );
60
61 return res;
62}
63
64QVector<QgsAbstractProfileResults::Feature> QgsAbstractProfileSurfaceResults::asFeatures( Qgis::ProfileExportType type, QgsFeedback *feedback ) const
65{
66 QVector< QgsAbstractProfileResults::Feature > res;
67 res.reserve( 1 );
68
69 QVector< double > currentLineX;
70 QVector< double > currentLineY;
71 QVector< double > currentLineZ;
72
73 switch ( type )
74 {
76 {
77 for ( auto pointIt = mDistanceToHeightMap.constBegin(); pointIt != mDistanceToHeightMap.constEnd(); ++pointIt )
78 {
79 if ( feedback && feedback->isCanceled() )
80 break;
81
82
83 if ( std::isnan( pointIt.value() ) )
84 {
85 if ( currentLineX.length() > 1 )
86 {
89 f.geometry = QgsGeometry( std::make_unique< QgsLineString >( currentLineX, currentLineY, currentLineZ ) );
90 res << f;
91 }
92 currentLineX.clear();
93 currentLineY.clear();
94 currentLineZ.clear();
95 continue;
96 }
97
98 std::unique_ptr< QgsPoint > curvePoint( mProfileCurve->interpolatePoint( pointIt.key() ) );
99 currentLineX << curvePoint->x();
100 currentLineY << curvePoint->y();
101 currentLineZ << pointIt.value();
102 }
103
104 if ( currentLineX.length() > 1 )
105 {
108 f.geometry = QgsGeometry( std::make_unique< QgsLineString >( currentLineX, currentLineY, currentLineZ ) );
109 res << f;
110 }
111 break;
112 }
113
115 {
116 for ( auto pointIt = mDistanceToHeightMap.constBegin(); pointIt != mDistanceToHeightMap.constEnd(); ++pointIt )
117 {
118 if ( feedback && feedback->isCanceled() )
119 break;
120
121 if ( std::isnan( pointIt.value() ) )
122 {
123 if ( currentLineX.length() > 1 )
124 {
127 f.geometry = QgsGeometry( std::make_unique< QgsLineString >( currentLineX, currentLineY ) );
128 res << f;
129 }
130 currentLineX.clear();
131 currentLineY.clear();
132 continue;
133 }
134
135 currentLineX << pointIt.key();
136 currentLineY << pointIt.value();
137 }
138 if ( currentLineX.length() > 1 )
139 {
142 f.geometry = QgsGeometry( std::make_unique< QgsLineString >( currentLineX, currentLineY ) );
143 res << f;
144 }
145 break;
146 }
147
149 {
150 res.reserve( mDistanceToHeightMap.size() );
151 for ( auto pointIt = mDistanceToHeightMap.constBegin(); pointIt != mDistanceToHeightMap.constEnd(); ++pointIt )
152 {
153 if ( feedback && feedback->isCanceled() )
154 break;
155
158 f.attributes = { { u"distance"_s, pointIt.key() }, { u"elevation"_s, pointIt.value() } };
159 std::unique_ptr< QgsPoint> point( mProfileCurve->interpolatePoint( pointIt.key() ) );
160 if ( point->is3D() )
161 point->setZ( pointIt.value() );
162 else
163 point->addZValue( pointIt.value() );
164 f.geometry = QgsGeometry( std::move( point ) );
165 res << f;
166 }
167 break;
168 }
169 }
170
171 return res;
172}
173
175{
176 // TODO -- consider an index if performance is an issue
178
179 double prevDistance = std::numeric_limits< double >::max();
180 double prevElevation = 0;
181 for ( auto it = mDistanceToHeightMap.constBegin(); it != mDistanceToHeightMap.constEnd(); ++it )
182 {
183 // find segment which corresponds to the given distance along curve
184 if ( it != mDistanceToHeightMap.constBegin() && prevDistance <= point.distance() && it.key() >= point.distance() )
185 {
186 const double dx = it.key() - prevDistance;
187 const double dy = it.value() - prevElevation;
188 const double snappedZ = ( dy / dx ) * ( point.distance() - prevDistance ) + prevElevation;
189
190 if ( std::fabs( point.elevation() - snappedZ ) > context.maximumSurfaceElevationDelta )
191 return QgsProfileSnapResult();
192
193 result.snappedPoint = QgsProfilePoint( point.distance(), snappedZ );
194 break;
195 }
196
197 prevDistance = it.key();
198 prevElevation = it.value();
199 }
200 return result;
201}
202
203QVector<QgsProfileIdentifyResults> QgsAbstractProfileSurfaceResults::identify( const QgsProfilePoint &point, const QgsProfileIdentifyContext &context )
204{
205 // TODO -- consider an index if performance is an issue
206 std::optional< QgsProfileIdentifyResults > result;
207
208 double prevDistance = std::numeric_limits< double >::max();
209 double prevElevation = 0;
210 for ( auto it = mDistanceToHeightMap.constBegin(); it != mDistanceToHeightMap.constEnd(); ++it )
211 {
212 // find segment which corresponds to the given distance along curve
213 if ( it != mDistanceToHeightMap.constBegin() && prevDistance <= point.distance() && it.key() >= point.distance() )
214 {
215 const double dx = it.key() - prevDistance;
216 const double dy = it.value() - prevElevation;
217 const double snappedZ = ( dy / dx ) * ( point.distance() - prevDistance ) + prevElevation;
218
219 if ( std::fabs( point.elevation() - snappedZ ) > context.maximumSurfaceElevationDelta )
220 return {};
221
222 result = QgsProfileIdentifyResults( nullptr, { QVariantMap( { { u"distance"_s, point.distance() }, { u"elevation"_s, snappedZ } } ) } );
223 break;
224 }
225
226 prevDistance = it.key();
227 prevElevation = it.value();
228 }
229 if ( result.has_value() )
230 return { *result };
231 else
232 return {};
233}
234
236{
237 QPainter *painter = context.renderContext().painter();
238 if ( !painter )
239 return;
240
241 const QgsScopedQPainterState painterState( painter );
242
243 painter->setBrush( Qt::NoBrush );
244 painter->setPen( Qt::NoPen );
245
246 const double minDistance = context.distanceRange().lower();
247 const double maxDistance = context.distanceRange().upper();
248 double minZ = context.elevationRange().lower();
249 double maxZ = context.elevationRange().upper();
250
251 const QRectF visibleRegion( minDistance, minZ, maxDistance - minDistance, maxZ - minZ );
252 QPainterPath clipPath;
253 clipPath.addPolygon( context.worldTransform().map( visibleRegion ) );
254 painter->setClipPath( clipPath, Qt::ClipOperation::IntersectClip );
255
256 switch ( symbology )
257 {
259 mLineSymbol->startRender( context.renderContext() );
260 break;
262 mFillSymbol->startRender( context.renderContext() );
263 if ( !std::isnan( mElevationLimit ) )
264 {
265 double dataLimit = std::numeric_limits< double >::max();
266 for ( auto pointIt = mDistanceToHeightMap.constBegin(); pointIt != mDistanceToHeightMap.constEnd(); ++pointIt )
267 {
268 if ( !std::isnan( pointIt.value() ) )
269 {
270 dataLimit = std::min( pointIt.value(), dataLimit );
271 }
272 }
273 if ( dataLimit > mElevationLimit )
274 minZ = std::max( minZ, mElevationLimit );
275 }
276 break;
278 mFillSymbol->startRender( context.renderContext() );
279 if ( !std::isnan( mElevationLimit ) )
280 {
281 double dataLimit = std::numeric_limits< double >::lowest();
282 for ( auto pointIt = mDistanceToHeightMap.constBegin(); pointIt != mDistanceToHeightMap.constEnd(); ++pointIt )
283 {
284 if ( !std::isnan( pointIt.value() ) )
285 {
286 dataLimit = std::max( pointIt.value(), dataLimit );
287 }
288 }
289 if ( dataLimit < mElevationLimit )
290 maxZ = std::min( maxZ, mElevationLimit );
291 }
292 break;
293 }
294
295 auto checkLine = [this]( QPolygonF &currentLine, QgsProfileRenderContext &context, double minZ, double maxZ, double prevDistance, double currentPartStartDistance ) {
296 if ( currentLine.length() > 1 )
297 {
298 switch ( symbology )
299 {
301 mLineSymbol->renderPolyline( currentLine, nullptr, context.renderContext() );
302 break;
304 currentLine.append( context.worldTransform().map( QPointF( prevDistance, minZ ) ) );
305 currentLine.append( context.worldTransform().map( QPointF( currentPartStartDistance, minZ ) ) );
306 currentLine.append( currentLine.at( 0 ) );
307 mFillSymbol->renderPolygon( currentLine, nullptr, nullptr, context.renderContext() );
308 break;
310 currentLine.append( context.worldTransform().map( QPointF( prevDistance, maxZ ) ) );
311 currentLine.append( context.worldTransform().map( QPointF( currentPartStartDistance, maxZ ) ) );
312 currentLine.append( currentLine.at( 0 ) );
313 mFillSymbol->renderPolygon( currentLine, nullptr, nullptr, context.renderContext() );
314 break;
315 }
316 }
317 };
318
319 QPolygonF currentLine;
320 double prevDistance = std::numeric_limits< double >::quiet_NaN();
321 double currentPartStartDistance = 0;
322 for ( auto pointIt = mDistanceToHeightMap.constBegin(); pointIt != mDistanceToHeightMap.constEnd(); ++pointIt )
323 {
324 if ( currentLine.empty() ) // new part
325 {
326 if ( std::isnan( pointIt.value() ) ) // skip emptiness
327 continue;
328 currentPartStartDistance = pointIt.key();
329 }
330
331 if ( std::isnan( pointIt.value() ) )
332 {
333 checkLine( currentLine, context, minZ, maxZ, prevDistance, currentPartStartDistance );
334 currentLine.clear();
335 }
336 else
337 {
338 currentLine.append( context.worldTransform().map( QPointF( pointIt.key(), pointIt.value() ) ) );
339 prevDistance = pointIt.key();
340 }
341 }
342
343 checkLine( currentLine, context, minZ, maxZ, prevDistance, currentPartStartDistance );
344
345 switch ( symbology )
346 {
348 mLineSymbol->stopRender( context.renderContext() );
349 break;
352 mFillSymbol->stopRender( context.renderContext() );
353 break;
354 }
355}
356
357
359{
360 const QgsAbstractProfileSurfaceGenerator *surfaceGenerator = qgis::down_cast< const QgsAbstractProfileSurfaceGenerator * >( generator );
361
362 mLineSymbol.reset( surfaceGenerator->lineSymbol()->clone() );
363 mFillSymbol.reset( surfaceGenerator->fillSymbol()->clone() );
364 symbology = surfaceGenerator->symbology();
365 mElevationLimit = surfaceGenerator->elevationLimit();
366
367 mProfileCurve.reset( surfaceGenerator->mProfileCurve->clone() );
368}
369
370//
371// QgsAbstractProfileSurfaceGenerator
372//
373
375 : mProfileCurve( request.profileCurve() ? request.profileCurve()->clone() : nullptr )
376{}
377
379
384
389
394
399
ProfileExportType
Types of export for elevation profiles.
Definition qgis.h:4702
@ Profile2D
Export profiles as 2D profile lines, with elevation stored in exported geometry Y dimension and dista...
Definition qgis.h:4704
@ Features3D
Export profiles as 3D features, with elevation values stored in exported geometry Z values.
Definition qgis.h:4703
@ DistanceVsElevationTable
Export profiles as a table of sampled distance vs elevation values.
Definition qgis.h:4705
ProfileSurfaceSymbology
Surface symbology type for elevation profile plots.
Definition qgis.h:4650
@ Line
The elevation surface will be rendered using a line symbol.
Definition qgis.h:4651
@ FillBelow
The elevation surface will be rendered using a fill symbol below the surface level.
Definition qgis.h:4652
@ FillAbove
The elevation surface will be rendered using a fill symbol above the surface level.
Definition qgis.h:4653
Abstract base class for objects which generate elevation profiles.
virtual QString type() const =0
Returns the unique string identifier for the results type.
Abstract base class for objects which generate elevation profiles which represent a continuous surfac...
QgsLineSymbol * lineSymbol() const
Returns the line symbol to be used for rendering the results.
Qgis::ProfileSurfaceSymbology symbology() const
Returns the symbology type for rendering the results.
void setElevationLimit(double limit)
Sets the elevation limit, which is used when symbology() is Qgis::ProfileSurfaceSymbology::FillBelow ...
QgsAbstractProfileSurfaceGenerator(const QgsProfileRequest &request)
Constructor for QgsAbstractProfileSurfaceGenerator.
double elevationLimit() const
Returns the elevation limit, which is used when symbology() is Qgis::ProfileSurfaceSymbology::FillBel...
QgsFillSymbol * fillSymbol() const
Returns the fill symbol to be used for rendering the results.
QgsPointSequence sampledPoints() const override
Returns a list of sampled points, with their calculated elevation as the point z value.
void renderResults(QgsProfileRenderContext &context) override
Renders the results to the specified context.
void copyPropertiesFromGenerator(const QgsAbstractProfileGenerator *generator) override
Copies properties from specified generator to the results object.
QVector< QgsAbstractProfileResults::Feature > asFeatures(Qgis::ProfileExportType type, QgsFeedback *feedback=nullptr) const override
Returns a list of features representing the calculated elevation results.
QVector< QgsGeometry > asGeometries() const override
Returns a list of geometries representing the calculated elevation results.
QVector< QgsProfileIdentifyResults > identify(const QgsProfilePoint &point, const QgsProfileIdentifyContext &context) override
Identify results visible at the specified profile point.
QgsProfileSnapResult snapPoint(const QgsProfilePoint &point, const QgsProfileSnapContext &context) override
Snaps a point to the generated elevation profile.
QgsDoubleRange zRange() const override
Returns the range of the retrieved elevation values.
QMap< double, double > distanceToHeightMap() const override
Returns the map of distance (chainage) to height.
QgsRange which stores a range of double values.
Definition qgsrange.h:217
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:56
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
QgsFillSymbol * clone() const override
Returns a deep copy of this symbol.
A geometry is the spatial representation of a feature.
A line symbol type, for rendering LineString and MultiLineString geometries.
QgsLineSymbol * clone() const override
Returns a deep copy of this symbol.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
Encapsulates the context of identifying profile results.
double maximumSurfaceElevationDelta
Maximum allowed snapping delta for the elevation values when identifying a continuous elevation surfa...
Stores identify results generated by a QgsAbstractProfileResults object.
Encapsulates a point on a distance-elevation profile.
double elevation() const
Returns the elevation of the point.
double distance() const
Returns the distance of the point.
Abstract base class for storage of elevation profiles.
const QTransform & worldTransform() const
Returns the transform from world coordinates to painter coordinates.
QgsDoubleRange elevationRange() const
Returns the range of elevations to include in the render.
QgsDoubleRange distanceRange() const
Returns the range of distances to include in the render.
QgsRenderContext & renderContext()
Returns a reference to the component QgsRenderContext.
Encapsulates properties and constraints relating to fetching elevation profiles from different source...
Encapsulates the context of snapping a profile point.
double maximumSurfaceElevationDelta
Maximum allowed snapping delta for the elevation values when snapping to a continuous elevation surfa...
Encapsulates results of snapping a profile point.
QgsProfilePoint snappedPoint
Snapped point.
T lower() const
Returns the lower bound of the range.
Definition qgsrange.h:79
T upper() const
Returns the upper bound of the range.
Definition qgsrange.h:86
QPainter * painter()
Returns the destination QPainter for the render operation.
Scoped object for saving and restoring a QPainter object's state.
QVector< QgsPoint > QgsPointSequence
Encapsulates information about a feature exported from the profile results.
QString layerIdentifier
Identifier for grouping output features.
QVariantMap attributes
Exported attributes.