QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsalgorithmdrape.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmdrape.cpp
3  ---------------------
4  begin : November 2017
5  copyright : (C) 2017 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  ***************************************************************************/
17 
18 #include "qgsalgorithmdrape.h"
19 #include "qgsvectorlayer.h"
20 
22 
23 
24 QString QgsDrapeAlgorithmBase::group() const
25 {
26  return QObject::tr( "Vector geometry" );
27 }
28 
29 QString QgsDrapeAlgorithmBase::groupId() const
30 {
31  return QStringLiteral( "vectorgeometry" );
32 }
33 
34 QString QgsDrapeAlgorithmBase::outputName() const
35 {
36  return QObject::tr( "Draped" );
37 }
38 
39 void QgsDrapeAlgorithmBase::initParameters( const QVariantMap & )
40 {
41  addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "RASTER" ),
42  QObject::tr( "Raster layer" ) ) );
43  addParameter( new QgsProcessingParameterBand( QStringLiteral( "BAND" ),
44  QObject::tr( "Band number" ), 1, QStringLiteral( "RASTER" ) ) );
45 
46  // nodata value
47  std::unique_ptr< QgsProcessingParameterNumber > nodata = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "NODATA" ),
48  QObject::tr( "Value for nodata or non-intersecting vertices" ), QgsProcessingParameterNumber::Double,
49  0.0 );
50  nodata->setIsDynamic( true );
51  nodata->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "NODATA" ), QObject::tr( "Value for nodata or non-intersecting vertices" ), QgsPropertyDefinition::Double ) );
52  nodata->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
53  addParameter( nodata.release() );
54 
55  auto scaleParam = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "SCALE" ), QObject::tr( "Scale factor" ), QgsProcessingParameterNumber::Double, 1.0, false, 0.0 );
56  scaleParam->setIsDynamic( true );
57  scaleParam->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "SCALE" ), QObject::tr( "Scale factor" ), QgsPropertyDefinition::Double ) );
58  scaleParam->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
59  addParameter( scaleParam.release() );
60 }
61 
62 bool QgsDrapeAlgorithmBase::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
63 {
64  mNoData = parameterAsDouble( parameters, QStringLiteral( "NODATA" ), context );
65  mDynamicNoData = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "NODATA" ) );
66  if ( mDynamicNoData )
67  mNoDataProperty = parameters.value( QStringLiteral( "NODATA" ) ).value< QgsProperty >();
68 
69  mScale = parameterAsDouble( parameters, QStringLiteral( "SCALE" ), context );
70  mDynamicScale = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "SCALE" ) );
71  if ( mDynamicScale )
72  mScaleProperty = parameters.value( QStringLiteral( "SCALE" ) ).value< QgsProperty >();
73 
74  QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral( "RASTER" ), context );
75 
76  if ( !layer )
77  throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "RASTER" ) ) );
78 
79  mBand = parameterAsInt( parameters, QStringLiteral( "BAND" ), context );
80  if ( mBand < 1 || mBand > layer->bandCount() )
81  throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand )
82  .arg( layer->bandCount() ) );
83  mRasterExtent = layer->extent();
84 
85  std::unique_ptr< QgsRasterInterface > provider( layer->dataProvider()->clone() );
86  QgsRasterDataProvider *dp = dynamic_cast< QgsRasterDataProvider * >( provider.get() );
87  if ( !dp )
88  throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "RASTER" ) ) );
89 
90  mRasterProvider.reset( dp );
91  provider.release();
92 
93  return true;
94 }
95 
96 QgsFeatureList QgsDrapeAlgorithmBase::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
97 {
98  if ( !mCreatedTransform )
99  {
100  mCreatedTransform = true;
101  mTransform = QgsCoordinateTransform( sourceCrs(), mRasterProvider->crs(), context.transformContext() );
102 
103  // transform the raster extent back to the vector's crs, so that we can test
104  // whether individual vector geometries are actually covered by the raster
105  try
106  {
107  mRasterExtent = mTransform.transform( mRasterExtent, Qgis::TransformDirection::Reverse );
108  }
109  catch ( QgsCsException & )
110  {
111  mRasterExtent = QgsRectangle();
112  }
113  }
114 
115  QgsFeature f = feature;
116  if ( f.hasGeometry() )
117  {
118  QgsGeometry geometry = f.geometry();
119 
120  double nodata = mNoData;
121  if ( mDynamicNoData )
122  nodata = mNoDataProperty.valueAsDouble( context.expressionContext(), nodata );
123 
124  double scale = mScale;
125  if ( mDynamicScale )
126  scale = mScaleProperty.valueAsDouble( context.expressionContext(), scale );
127 
128  prepareGeometry( geometry, nodata );
129 
130  // only do the "draping" if the geometry intersects the raster - otherwise skip
131  // a pointless iteration over all vertices
132  if ( !mRasterExtent.isNull() && geometry.boundingBoxIntersects( mRasterExtent ) )
133  {
134  geometry.transformVertices( [ = ]( const QgsPoint & p )->QgsPoint
135  {
136  QgsPointXY t;
137  double val = nodata;
138  try
139  {
140  t = mTransform.transform( p );
141  bool ok = false;
142  val = mRasterProvider->sample( t, mBand, &ok );
143  if ( !ok )
144  val = nodata;
145  else
146  val *= scale;
147  }
148  catch ( QgsCsException & )
149  {
150  feedback->reportError( QObject::tr( "Transform error while reprojecting feature {}" ).arg( f.id() ) );
151  }
152 
153  return drapeVertex( p, val );
154  } );
155  }
156 
157  f.setGeometry( geometry );
158  }
159  return QgsFeatureList() << f;
160 }
161 
162 
163 //
164 // QgsDrapeToZAlgorithm
165 //
166 
167 QString QgsDrapeToZAlgorithm::name() const
168 {
169  return QStringLiteral( "setzfromraster" );
170 }
171 
172 QString QgsDrapeToZAlgorithm::displayName() const
173 {
174  return QObject::tr( "Drape (set Z value from raster)" );
175 }
176 
177 QStringList QgsDrapeToZAlgorithm::tags() const
178 {
179  return QObject::tr( "3d,vertex,vertices,elevation,height,sample,dem,update,feature" ).split( ',' );
180 }
181 
182 QString QgsDrapeToZAlgorithm::shortHelpString() const
183 {
184  return QObject::tr( "This algorithm sets the z value of every vertex in the feature geometry to a value sampled from a band within a raster layer." )
185  + QStringLiteral( "\n\n" )
186  + QObject::tr( "The raster values can optionally be scaled by a preset amount." );
187 }
188 
189 QString QgsDrapeToZAlgorithm::shortDescription() const
190 {
191  return QObject::tr( "Sets the z value for vertices to values sampled from a raster layer." );
192 }
193 
194 QgsDrapeToZAlgorithm *QgsDrapeToZAlgorithm::createInstance() const
195 {
196  return new QgsDrapeToZAlgorithm();
197 }
198 
199 bool QgsDrapeToZAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
200 {
201  const QgsVectorLayer *layer = qobject_cast< const QgsVectorLayer * >( l );
202  if ( !layer )
203  return false;
204 
205  if ( ! QgsDrapeAlgorithmBase::supportInPlaceEdit( layer ) )
206  return false;
207  return QgsWkbTypes::hasZ( layer->wkbType() );
208 }
209 
210 QgsWkbTypes::Type QgsDrapeToZAlgorithm::outputWkbType( QgsWkbTypes::Type inputWkbType ) const
211 {
212  const QgsWkbTypes::Type wkb = inputWkbType;
213  return QgsWkbTypes::addZ( wkb );
214 }
215 
216 void QgsDrapeToZAlgorithm::prepareGeometry( QgsGeometry &geometry, double defaultVal ) const
217 {
218  geometry.get()->addZValue( defaultVal );
219 }
220 
221 QgsPoint QgsDrapeToZAlgorithm::drapeVertex( const QgsPoint &p, double rasterVal ) const
222 {
223  return QgsPoint( p.wkbType(), p.x(), p.y(), rasterVal, p.m() );
224 }
225 
226 //
227 // QgsDrapeToMAlgorithm
228 //
229 
230 QString QgsDrapeToMAlgorithm::name() const
231 {
232  return QStringLiteral( "setmfromraster" );
233 }
234 
235 QString QgsDrapeToMAlgorithm::displayName() const
236 {
237  return QObject::tr( "Set M value from raster" );
238 }
239 
240 QStringList QgsDrapeToMAlgorithm::tags() const
241 {
242  return QObject::tr( "drape,vertex,vertices,sample,dem,update,feature,measure" ).split( ',' );
243 }
244 
245 QString QgsDrapeToMAlgorithm::shortHelpString() const
246 {
247  return QObject::tr( "This algorithm sets the M value for every vertex in the feature geometry to a value sampled from a band within a raster layer." )
248  + QStringLiteral( "\n\n" )
249  + QObject::tr( "The raster values can optionally be scaled by a preset amount." );
250 }
251 
252 QString QgsDrapeToMAlgorithm::shortDescription() const
253 {
254  return QObject::tr( "Sets the M value for vertices to values sampled from a raster layer." );
255 }
256 
257 QgsDrapeToMAlgorithm *QgsDrapeToMAlgorithm::createInstance() const
258 {
259  return new QgsDrapeToMAlgorithm();
260 }
261 
262 bool QgsDrapeToMAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
263 {
264  const QgsVectorLayer *layer = qobject_cast< const QgsVectorLayer * >( l );
265  if ( !layer )
266  return false;
267 
268  if ( ! QgsDrapeAlgorithmBase::supportInPlaceEdit( layer ) )
269  return false;
270  return QgsWkbTypes::hasM( layer->wkbType() );
271 }
272 
273 QgsWkbTypes::Type QgsDrapeToMAlgorithm::outputWkbType( QgsWkbTypes::Type inputWkbType ) const
274 {
275  const QgsWkbTypes::Type wkb = inputWkbType;
276  return QgsWkbTypes::addM( wkb );
277 }
278 
279 void QgsDrapeToMAlgorithm::prepareGeometry( QgsGeometry &geometry, double defaultVal ) const
280 {
281  geometry.get()->addMValue( defaultVal );
282 }
283 
284 QgsPoint QgsDrapeToMAlgorithm::drapeVertex( const QgsPoint &p, double rasterVal ) const
285 {
286  return QgsPoint( p.wkbType(), p.x(), p.y(), p.z(), rasterVal );
287 }
288 
289 
291 
292 
QgsProcessingParameterNumber::Double
@ Double
Double/float values.
Definition: qgsprocessingparameters.h:2187
QgsVectorLayer::wkbType
Q_INVOKABLE QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
Definition: qgsvectorlayer.cpp:725
QgsProperty
A store for object properties.
Definition: qgsproperty.h:230
QgsAbstractGeometry::addZValue
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
QgsProcessingParameters::isDynamic
static bool isDynamic(const QVariantMap &parameters, const QString &name)
Returns true if the parameter with matching name is a dynamic parameter, and must be evaluated once f...
Definition: qgsprocessingparameters.cpp:114
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:48
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsAbstractGeometry::addMValue
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
QgsProcessingFeedback::reportError
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
Definition: qgsprocessingfeedback.cpp:59
QgsWkbTypes::addZ
static Type addZ(Type type) SIP_HOLDGIL
Adds the z dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1176
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsPoint::z
double z
Definition: qgspoint.h:71
QgsPropertyDefinition::Double
@ Double
Double value (including negative values)
Definition: qgsproperty.h:57
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:69
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsFeature::id
QgsFeatureId id
Definition: qgsfeature.h:68
QgsPoint::y
double y
Definition: qgspoint.h:70
QgsWkbTypes::addM
static Type addM(Type type) SIP_HOLDGIL
Adds the m dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1201
QgsCsException
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
QgsAbstractGeometry::wkbType
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns the WKB type of the geometry.
Definition: qgsabstractgeometry.h:206
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
qgsalgorithmdrape.h
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QgsWkbTypes::hasM
static bool hasM(Type type) SIP_HOLDGIL
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:1130
QgsProcessingParameterRasterLayer
A raster layer parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2495
QgsPropertyDefinition
Definition for a property.
Definition: qgsproperty.h:46
QgsPoint::m
double m
Definition: qgspoint.h:72
QgsProcessingContext::transformContext
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Definition: qgsprocessingcontext.h:165
QgsGeometry::transformVertices
void transformVertices(const std::function< QgsPoint(const QgsPoint &) > &transform)
Transforms the vertices from the geometry in place, applying the transform function to every vertex.
Definition: qgsgeometry.cpp:3323
QgsRasterLayer
Represents a raster layer.
Definition: qgsrasterlayer.h:76
qgsvectorlayer.h
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
QgsGeometry::get
QgsAbstractGeometry * get()
Returns a modifiable (non-const) reference to the underlying abstract geometry primitive.
Definition: qgsgeometry.cpp:141
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsProcessingContext::expressionContext
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition: qgsprocessingcontext.h:149
QgsProperty::valueAsDouble
double valueAsDouble(const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a double.
Definition: qgsproperty.cpp:690
QgsProcessingParameterBand
A raster band parameter for Processing algorithms.
Definition: qgsprocessingparameters.h:3548
QgsWkbTypes::hasZ
static bool hasZ(Type type) SIP_HOLDGIL
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:1080
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:57
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
QgsRasterDataProvider
Base class for raster data providers.
Definition: qgsrasterdataprovider.h:88
QgsPoint::x
double x
Definition: qgspoint.h:69
QgsGeometry::boundingBoxIntersects
bool boundingBoxIntersects(const QgsRectangle &rectangle) const
Returns true if the bounding box of this geometry intersects with a rectangle.
Definition: qgsgeometry.cpp:1283