QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsmeshlayerprofilegenerator.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmeshlayerprofilegenerator.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
20#include "qgscurve.h"
21#include "qgsfillsymbol.h"
22#include "qgsgeos.h"
23#include "qgslinesymbol.h"
24#include "qgsmeshlayer.h"
26#include "qgsmeshlayerutils.h"
27#include "qgsprofilepoint.h"
28#include "qgsprofilerequest.h"
29#include "qgsprofilesnapping.h"
30#include "qgsterrainprovider.h"
31
32//
33// QgsMeshLayerProfileGenerator
34//
35
37{
38 return QStringLiteral( "mesh" );
39}
40
41QVector<QgsProfileIdentifyResults> QgsMeshLayerProfileResults::identify( const QgsProfilePoint &point, const QgsProfileIdentifyContext &context )
42{
43 const QVector<QgsProfileIdentifyResults> noLayerResults = QgsAbstractProfileSurfaceResults::identify( point, context );
44
45 // we have to make a new list, with the correct layer reference set
46 QVector<QgsProfileIdentifyResults> res;
47 res.reserve( noLayerResults.size() );
48 for ( const QgsProfileIdentifyResults &result : noLayerResults )
49 {
50 res.append( QgsProfileIdentifyResults( mLayer, result.results() ) );
51 }
52 return res;
53}
54
55//
56// QgsMeshLayerProfileGenerator
57//
58
61 , mId( layer->id() )
62 , mFeedback( std::make_unique< QgsFeedback >() )
63 , mProfileCurve( request.profileCurve() ? request.profileCurve()->clone() : nullptr )
64 , mSourceCrs( layer->crs() )
65 , mTargetCrs( request.crs() )
66 , mTransformContext( request.transformContext() )
67 , mOffset( layer->elevationProperties()->zOffset() )
68 , mScale( layer->elevationProperties()->zScale() )
69 , mLayer( layer )
70 , mStepDistance( request.stepDistance() )
71{
72 layer->updateTriangularMesh();
73 mTriangularMesh = *layer->triangularMesh();
74
75 mSymbology = qgis::down_cast< QgsMeshLayerElevationProperties * >( layer->elevationProperties() )->profileSymbology();
76 mElevationLimit = qgis::down_cast< QgsMeshLayerElevationProperties * >( layer->elevationProperties() )->elevationLimit();
77 mLineSymbol.reset( qgis::down_cast< QgsMeshLayerElevationProperties * >( layer->elevationProperties() )->profileLineSymbol()->clone() );
78 mFillSymbol.reset( qgis::down_cast< QgsMeshLayerElevationProperties * >( layer->elevationProperties() )->profileFillSymbol()->clone() );
79}
80
82{
83 return mId;
84}
85
87
89{
90 if ( !mProfileCurve || mFeedback->isCanceled() )
91 return false;
92
93 // we need to transform the profile curve to the mesh's CRS
94 QgsGeometry transformedCurve( mProfileCurve->clone() );
95 mLayerToTargetTransform = QgsCoordinateTransform( mSourceCrs, mTargetCrs, mTransformContext );
96
97 try
98 {
99 transformedCurve.transform( mLayerToTargetTransform, Qgis::TransformDirection::Reverse );
100 }
101 catch ( QgsCsException & )
102 {
103 QgsDebugError( QStringLiteral( "Error transforming profile line to mesh CRS" ) );
104 return false;
105 }
106
107 if ( mFeedback->isCanceled() )
108 return false;
109
110 mResults = std::make_unique< QgsMeshLayerProfileResults >();
111 mResults->mLayer = mLayer;
112 mResults->mId = mId;
113 mResults->copyPropertiesFromGenerator( this );
114
115 // we don't currently have any method to determine line->mesh intersection points, so for now we just sample at about 100(?) points over the line
116 const double curveLength = transformedCurve.length();
117
118 if ( !std::isnan( mStepDistance ) )
119 transformedCurve = transformedCurve.densifyByDistance( mStepDistance );
120 else
121 transformedCurve = transformedCurve.densifyByDistance( curveLength / 100 );
122
123 if ( mFeedback->isCanceled() )
124 return false;
125
126 for ( auto it = transformedCurve.vertices_begin(); it != transformedCurve.vertices_end(); ++it )
127 {
128 if ( mFeedback->isCanceled() )
129 return false;
130
131 QgsPoint point = ( *it );
132 const double height = heightAt( point.x(), point.y() );
133
134 try
135 {
136 point.transform( mLayerToTargetTransform );
137 }
138 catch ( QgsCsException & )
139 {
140 continue;
141 }
142 mResults->mRawPoints.append( QgsPoint( point.x(), point.y(), height ) );
143 }
144
145 if ( mFeedback->isCanceled() )
146 return false;
147
148 // convert x/y values back to distance/height values
149 QgsGeos originalCurveGeos( mProfileCurve.get() );
150 originalCurveGeos.prepareGeometry();
151 QString lastError;
152 for ( const QgsPoint &pixel : std::as_const( mResults->mRawPoints ) )
153 {
154 if ( mFeedback->isCanceled() )
155 return false;
156
157 const double distance = originalCurveGeos.lineLocatePoint( pixel, &lastError );
158
159 if ( !std::isnan( pixel.z() ) )
160 {
161 mResults->minZ = std::min( pixel.z(), mResults->minZ );
162 mResults->maxZ = std::max( pixel.z(), mResults->maxZ );
163 }
164 mResults->mDistanceToHeightMap.insert( distance, pixel.z() );
165 }
166
167 return true;
168}
169
174
176{
177 return mFeedback.get();
178}
179
180double QgsMeshLayerProfileGenerator::heightAt( double x, double y )
181{
182 return QgsMeshLayerUtils::interpolateZForPoint( mTriangularMesh, x, y ) * mScale + mOffset;
183}
184
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2673
Abstract base class for storage of elevation profiles.
QgsAbstractProfileSurfaceGenerator(const QgsProfileRequest &request)
Constructor for QgsAbstractProfileSurfaceGenerator.
QVector< QgsProfileIdentifyResults > identify(const QgsProfilePoint &point, const QgsProfileIdentifyContext &context) override
Identify results visible at the specified profile point.
Handles coordinate transforms between two coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
A geometry is the spatial representation of a feature.
double length() const
Returns the planar, 2-dimensional length of geometry.
QgsGeometry densifyByDistance(double distance) const
Densifies the geometry by adding regularly placed extra nodes inside each segment so that the maximum...
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
QgsAbstractGeometry::vertex_iterator vertices_begin() const
Returns STL-style iterator pointing to the first vertex of the geometry.
QgsAbstractGeometry::vertex_iterator vertices_end() const
Returns STL-style iterator pointing to the imaginary vertex after the last vertex of the geometry.
Does vector analysis using the GEOS library and handles import, export, and exception handling.
Definition qgsgeos.h:141
void prepareGeometry() override
Prepares the geometry, so that subsequent calls to spatial relation methods are much faster.
Definition qgsgeos.cpp:292
double lineLocatePoint(const QgsPoint &point, QString *errorMsg=nullptr) const
Returns a distance representing the location along this linestring of the closest point on this lines...
Definition qgsgeos.cpp:3185
~QgsMeshLayerProfileGenerator() override
QgsFeedback * feedback() const override
Access to feedback object of the generator (may be nullptr).
QgsMeshLayerProfileGenerator(QgsMeshLayer *layer, const QgsProfileRequest &request)
Constructor for QgsMeshLayerProfileGenerator.
QgsAbstractProfileResults * takeResults() override
Takes results from the generator.
bool generateProfile(const QgsProfileGenerationContext &context=QgsProfileGenerationContext()) override
Generate the profile (based on data stored in the class).
QString sourceId() const override
Returns a unique identifier representing the source of the profile.
QString type() const override
Returns the unique string identifier for the results type.
QVector< QgsProfileIdentifyResults > identify(const QgsProfilePoint &point, const QgsProfileIdentifyContext &context) override
Identify results visible at the specified profile point.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
void updateTriangularMesh(const QgsCoordinateTransform &transform=QgsCoordinateTransform())
Gets native mesh and updates (creates if it doesn't exist) the base triangular mesh.
QgsMapLayerElevationProperties * elevationProperties() override
Returns the layer's elevation properties.
QgsTriangularMesh * triangularMesh(double minimumTriangleSize=0) const
Returns triangular mesh (nullptr before rendering or calling to updateMesh).
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
double x
Definition qgspoint.h:52
void transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection d=Qgis::TransformDirection::Forward, bool transformZ=false) override
Transforms the geometry using a coordinate transform.
Definition qgspoint.cpp:387
double y
Definition qgspoint.h:53
Encapsulates the context in which an elevation profile is to be generated.
Encapsulates the context of identifying profile results.
Stores identify results generated by a QgsAbstractProfileResults object.
Encapsulates a point on a distance-elevation profile.
Encapsulates properties and constraints relating to fetching elevation profiles from different source...
#define QgsDebugError(str)
Definition qgslogger.h:57