QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsmeshcalculator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmeshcalculator.cpp
3  ---------------------
4  begin : December 18th, 2018
5  copyright : (C) 2018 by Peter Petrik
6  email : zilolv 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 <QFileInfo>
19 #include <limits>
20 #include <memory>
21 
22 #include "qgsfeedback.h"
23 #include "qgsmeshcalcnode.h"
24 #include "qgsmeshcalculator.h"
25 #include "qgsmeshcalcutils.h"
26 #include "qgsmeshlayer.h"
27 #include "qgsmeshmemorydataprovider.h"
29 #include "qgis.h"
30 
31 QgsMeshCalculator::QgsMeshCalculator( const QString &formulaString,
32  const QString &outputFile,
33  const QgsRectangle &outputExtent,
34  double startTime,
35  double endTime,
36  QgsMeshLayer *layer )
37  : mFormulaString( formulaString )
38  , mOutputDriver( QStringLiteral( "DAT" ) )
39  , mOutputFile( outputFile )
40  , mOutputExtent( outputExtent )
41  , mUseMask( false )
42  , mStartTime( startTime )
43  , mEndTime( endTime )
44  , mMeshLayer( layer )
45 {
46  if ( !mOutputFile.isEmpty() )
47  mOutputGroupName = QFileInfo( mOutputFile ).baseName();
48 }
49 
50 QgsMeshCalculator::QgsMeshCalculator( const QString &formulaString,
51  const QString &outputFile,
52  const QgsGeometry &outputMask,
53  double startTime,
54  double endTime,
55  QgsMeshLayer *layer )
56  : mFormulaString( formulaString )
57  , mOutputDriver( QStringLiteral( "DAT" ) )
58  , mOutputFile( outputFile )
59  , mOutputMask( outputMask )
60  , mUseMask( true )
61  , mStartTime( startTime )
62  , mEndTime( endTime )
63  , mMeshLayer( layer )
64 {
65  if ( !mOutputFile.isEmpty() )
66  mOutputGroupName = QFileInfo( mOutputFile ).baseName();
67 }
68 
69 
70 QgsMeshCalculator::QgsMeshCalculator( const QString &formulaString,
71  const QString &outputDriver,
72  const QString &outputGroupName,
73  const QString &outputFile,
74  const QgsRectangle &outputExtent,
75  double startTime,
76  double endTime,
77  QgsMeshLayer *layer )
78  : mFormulaString( formulaString )
79  , mOutputDriver( outputDriver )
80  , mOutputGroupName( outputGroupName )
81  , mOutputFile( outputFile )
82  , mOutputExtent( outputExtent )
83  , mUseMask( false )
84  , mDestination( QgsMeshDatasetGroup::Persistent )
85  , mStartTime( startTime )
86  , mEndTime( endTime )
87  , mMeshLayer( layer )
88 {
89 }
90 
91 QgsMeshCalculator::QgsMeshCalculator( const QString &formulaString,
92  const QString &outputDriver,
93  const QString &outputGroupName,
94  const QString &outputFile,
95  const QgsGeometry &outputMask,
96  double startTime,
97  double endTime,
98  QgsMeshLayer *layer )
99  : mFormulaString( formulaString )
100  , mOutputDriver( outputDriver )
101  , mOutputGroupName( outputGroupName )
102  , mOutputFile( outputFile )
103  , mOutputMask( outputMask )
104  , mUseMask( true )
105  , mDestination( QgsMeshDatasetGroup::Persistent )
106  , mStartTime( startTime )
107  , mEndTime( endTime )
108  , mMeshLayer( layer )
109 {
110 }
111 
112 QgsMeshCalculator::QgsMeshCalculator( const QString &formulaString,
113  const QString &outputGroupName,
114  const QgsRectangle &outputExtent,
115  const QgsMeshDatasetGroup::Type &destination,
116  QgsMeshLayer *layer,
117  double startTime,
118  double endTime )
119  : mFormulaString( formulaString )
120  , mOutputGroupName( outputGroupName )
121  , mOutputExtent( outputExtent )
122  , mUseMask( false )
123  , mDestination( destination )
124  , mStartTime( startTime )
125  , mEndTime( endTime )
126  , mMeshLayer( layer )
127 {
128 }
129 
130 QgsMeshCalculator::QgsMeshCalculator( const QString &formulaString,
131  const QString &outputGroupName,
132  const QgsGeometry &outputMask,
133  const QgsMeshDatasetGroup::Type &destination,
134  QgsMeshLayer *layer,
135  double startTime,
136  double endTime )
137  : mFormulaString( formulaString )
138  , mOutputGroupName( outputGroupName )
139  , mOutputMask( outputMask )
140  , mUseMask( true )
141  , mDestination( destination )
142  , mStartTime( startTime )
143  , mEndTime( endTime )
144  , mMeshLayer( layer )
145 {
146 }
147 
149  const QString &formulaString,
150  QgsMeshLayer *layer )
151 {
153  return QgsMeshCalculator::expressionIsValid( formulaString, layer, cap );
154 }
155 
157  const QString &formulaString,
158  QgsMeshLayer *layer,
159  QgsMeshDriverMetadata::MeshDriverCapability &requiredCapability )
160 {
161  QString errorString;
162  std::unique_ptr< QgsMeshCalcNode > calcNode( QgsMeshCalcNode::parseMeshCalcString( formulaString, errorString ) );
163  if ( !calcNode )
164  return ParserError;
165 
166  if ( !layer || !layer->dataProvider() )
167  return InputLayerError;
168 
169  const QgsMeshDatasetGroupMetadata::DataType dataType = QgsMeshCalcUtils::determineResultDataType( layer, calcNode->usedDatasetGroupNames() );
170 
171  requiredCapability = dataType == QgsMeshDatasetGroupMetadata::DataOnFaces ? QgsMeshDriverMetadata::MeshDriverCapability::CanWriteFaceDatasets :
172  QgsMeshDriverMetadata::MeshDriverCapability::CanWriteVertexDatasets;
173 
174  return Success;
175 }
176 
178 {
179  // check input
180  if ( mOutputFile.isEmpty() && mDestination == QgsMeshDatasetGroup::Persistent )
181  {
182  return CreateOutputError;
183  }
184 
185  if ( !mMeshLayer ||
186  !mMeshLayer->dataProvider() ||
187  mMeshLayer->providerType() != QStringLiteral( "mdal" )
188  )
189  {
190  return CreateOutputError;
191  }
192 
193  //prepare search string / tree
194  QString errorString;
195  std::unique_ptr< QgsMeshCalcNode > calcNode( QgsMeshCalcNode::parseMeshCalcString( mFormulaString, errorString ) );
196  if ( !calcNode )
197  {
198  return ParserError;
199  }
200 
201  // proceed eventually on the fly
202  bool err;
203  if ( mDestination == QgsMeshDatasetGroup::Virtual )
204  {
205  std::unique_ptr<QgsMeshDatasetGroup> virtualDatasetGroup =
206  std::make_unique<QgsMeshVirtualDatasetGroup> ( mOutputGroupName, mFormulaString, mMeshLayer, mStartTime * 3600 * 1000, mEndTime * 3600 * 1000 );
207  virtualDatasetGroup->initialize();
208  virtualDatasetGroup->setReferenceTime( static_cast<QgsMeshLayerTemporalProperties *>( mMeshLayer->temporalProperties() )->referenceTime() );
209  err = !mMeshLayer->addDatasets( virtualDatasetGroup.release() );
210  if ( err )
211  {
212  return CreateOutputError;
213  }
214 
215  if ( feedback )
216  {
217  feedback->setProgress( 100.0 );
218  }
219  return Success;
220  }
221 
222  //open output dataset
223  const QgsMeshCalcUtils dsu( mMeshLayer, calcNode->usedDatasetGroupNames(), mStartTime, mEndTime );
224  if ( !dsu.isValid() )
225  {
226  return InvalidDatasets;
227  }
228 
229  std::unique_ptr<QgsMeshMemoryDatasetGroup> outputGroup = std::make_unique<QgsMeshMemoryDatasetGroup> ( mOutputGroupName, dsu.outputType() );
230 
231  // calculate
232  const bool ok = calcNode->calculate( dsu, *outputGroup );
233  if ( !ok )
234  {
235  return EvaluateError;
236  }
237 
238  if ( feedback && feedback->isCanceled() )
239  {
240  return Canceled;
241  }
242  if ( feedback )
243  {
244  feedback->setProgress( 60.0 );
245  }
246 
247  // Finalize dataset
248  if ( mUseMask )
249  {
250  dsu.filter( *outputGroup, mOutputMask );
251  }
252  else
253  {
254  dsu.filter( *outputGroup, mOutputExtent );
255  }
256  outputGroup->setIsScalar( true );
257 
258  // before storing the file, find out if the process is not already canceled
259  if ( feedback && feedback->isCanceled() )
260  {
261  return Canceled;
262  }
263  if ( feedback )
264  {
265  feedback->setProgress( 80.0 );
266  }
267 
268  // store to file or in memory
269  QVector<QgsMeshDataBlock> datasetValues;
270  QVector<QgsMeshDataBlock> datasetActive;
271  QVector<double> times;
272 
273  const auto datasize = outputGroup->datasetCount();
274  datasetValues.reserve( datasize );
275  times.reserve( datasize );
276 
277  for ( int i = 0; i < datasize; ++i )
278  {
279  const std::shared_ptr<QgsMeshMemoryDataset> dataset = outputGroup->memoryDatasets.at( i );
280 
281  times.push_back( dataset->time );
282  datasetValues.push_back(
283  dataset->datasetValues( outputGroup->isScalar(),
284  0,
285  dataset->values.size() )
286  );
287  if ( !dataset->active.isEmpty() )
288  {
289  datasetActive.push_back(
290  dataset->areFacesActive(
291  0,
292  dataset->active.size() )
293  );
294  }
295  }
296 
297  // calculate statistics
298  outputGroup->initialize();
299  outputGroup->setReferenceTime( static_cast<QgsMeshLayerTemporalProperties *>( mMeshLayer->temporalProperties() )->referenceTime() );
300 
301  const QgsMeshDatasetGroupMetadata meta = outputGroup->groupMetadata();
302 
303  if ( mDestination == QgsMeshDatasetGroup::Memory )
304  {
305  err = !mMeshLayer->addDatasets( outputGroup.release() );
306  }
307  else
308  {
309  err = mMeshLayer->dataProvider()->persistDatasetGroup(
310  mOutputFile,
311  mOutputDriver,
312  meta,
313  datasetValues,
314  datasetActive,
315  times
316  );
317  }
318 
319 
320  if ( err )
321  {
322  return CreateOutputError;
323  }
324 
325  if ( feedback )
326  {
327  feedback->setProgress( 100.0 );
328  }
329  return Success;
330 }
QgsMeshCalculator::ParserError
@ ParserError
Error parsing formula.
Definition: qgsmeshcalculator.h:61
QgsFeedback::setProgress
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:76
qgsmeshcalcnode.h
qgsmeshvirtualdatasetgroup.h
QgsMeshCalculator::processCalculation
Result processCalculation(QgsFeedback *feedback=nullptr)
Starts the calculation, creates new dataset group and adds it to the mesh layer.
Definition: qgsmeshcalculator.cpp:177
QgsMeshDatasetGroup::Persistent
@ Persistent
Generic type used for non typed dataset group.
Definition: qgsmeshdataset.h:590
QgsFeedback::isCanceled
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:67
qgis.h
QgsMeshLayerTemporalProperties
Implementation of map layer temporal properties for mesh layers.
Definition: qgsmeshlayertemporalproperties.h:51
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsMeshDatasetGroupMetadata::DataOnFaces
@ DataOnFaces
Data is defined on faces.
Definition: qgsmeshdataset.h:358
QgsMeshCalculator::QgsMeshCalculator
Q_DECL_DEPRECATED QgsMeshCalculator(const QString &formulaString, const QString &outputFile, const QgsRectangle &outputExtent, double startTime, double endTime, QgsMeshLayer *layer)
Creates calculator with bounding box (rectangular) mask.
Definition: qgsmeshcalculator.cpp:31
QgsMeshCalculator::expression_valid
static Q_DECL_DEPRECATED Result expression_valid(const QString &formulaString, QgsMeshLayer *layer)
Returns whether formula is valid for particular mesh layer.
Definition: qgsmeshcalculator.cpp:148
qgsmeshcalculator.h
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
QgsMeshLayer
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:98
QgsMeshDatasetSourceInterface::persistDatasetGroup
virtual Q_DECL_DEPRECATED bool persistDatasetGroup(const QString &path, const QgsMeshDatasetGroupMetadata &meta, const QVector< QgsMeshDataBlock > &datasetValues, const QVector< QgsMeshDataBlock > &datasetActive, const QVector< double > &times)
Creates a new dataset group from a data and persists it into a destination path.
Definition: qgsmeshdataprovider.cpp:117
QgsMeshLayerTemporalProperties::referenceTime
QDateTime referenceTime() const
Returns the reference time.
Definition: qgsmeshlayertemporalproperties.cpp:92
QgsMeshCalculator::Success
@ Success
Calculation successful.
Definition: qgsmeshcalculator.h:57
QgsMeshCalculator::Canceled
@ Canceled
Calculation canceled.
Definition: qgsmeshcalculator.h:58
QgsMeshDatasetGroup
Abstract class that represents a dataset group.
Definition: qgsmeshdataset.h:578
QgsMeshCalculator::CreateOutputError
@ CreateOutputError
Error creating output data file.
Definition: qgsmeshcalculator.h:59
qgsmeshlayer.h
QgsMeshCalculator::expressionIsValid
static Result expressionIsValid(const QString &formulaString, QgsMeshLayer *layer, QgsMeshDriverMetadata::MeshDriverCapability &requiredCapability)
Returns whether formula is valid for particular mesh layer.
Definition: qgsmeshcalculator.cpp:156
QgsMeshDatasetGroupMetadata
QgsMeshDatasetGroupMetadata is a collection of dataset group metadata such as whether the data is vec...
Definition: qgsmeshdataset.h:351
QgsMeshLayer::addDatasets
bool addDatasets(const QString &path, const QDateTime &defaultReferenceTime=QDateTime())
Adds datasets to the mesh from file with path.
Definition: qgsmeshlayer.cpp:231
QgsMeshCalculator::InputLayerError
@ InputLayerError
Error reading input layer.
Definition: qgsmeshcalculator.h:60
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsMeshDatasetGroupMetadata::DataType
DataType
Location of where data is specified for datasets in the dataset group.
Definition: qgsmeshdataset.h:356
QgsMeshDatasetGroup::Type
Type
Type of the dataset group.
Definition: qgsmeshdataset.h:587
QgsMeshCalculator::InvalidDatasets
@ InvalidDatasets
Datasets with different time outputs or not part of the mesh.
Definition: qgsmeshcalculator.h:62
qgsmeshcalcutils.h
QgsMeshLayer::temporalProperties
QgsMapLayerTemporalProperties * temporalProperties() override
Returns the layer's temporal properties.
Definition: qgsmeshlayer.cpp:1824
QgsMeshLayer::providerType
QString providerType() const
Returns the provider type for this layer.
Definition: qgsmeshlayer.cpp:151
qgsfeedback.h
QgsMeshLayer::dataProvider
QgsMeshDataProvider * dataProvider() override
Returns the layer's data provider, it may be nullptr.
Definition: qgsmeshlayer.cpp:110
QgsMeshCalculator::EvaluateError
@ EvaluateError
Error during evaluation.
Definition: qgsmeshcalculator.h:63
QgsMeshCalculator::Result
Result
Result of the calculation.
Definition: qgsmeshcalculator.h:55
QgsMeshDatasetGroup::Virtual
@ Virtual
Temporary dataset group in memory.
Definition: qgsmeshdataset.h:592
QgsMeshDatasetGroup::Memory
@ Memory
Dataset group store in a file.
Definition: qgsmeshdataset.h:591
QgsMeshDriverMetadata::MeshDriverCapability
MeshDriverCapability
Flags for the capabilities of the driver.
Definition: qgsprovidermetadata.h:66