QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
qgsmeshdataprovider.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmeshdataprovider.cpp
3  -----------------------
4  begin : April 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 "qgsmeshdataprovider.h"
19 #include "qgsrectangle.h"
20 #include "qgis.h"
21 
22 
23 
25  : mGroupIndex( group ), mDatasetIndex( dataset )
26 {}
27 
29 {
30  return mGroupIndex;
31 }
32 
34 {
35  return mDatasetIndex;
36 }
37 
39 {
40  return ( group() > -1 ) && ( dataset() > -1 );
41 }
42 
44 {
45  if ( isValid() && other.isValid() )
46  return other.group() == group() && other.dataset() == dataset();
47  else
48  return isValid() == other.isValid();
49 }
50 
52 {
53  return !( operator==( other ) );
54 }
55 
57  : QgsDataProvider( uri, options )
58 {
59 }
60 
62  : mX( x ), mY( y )
63 {}
64 
66  : mX( scalar )
67 {}
68 
70 {
71  if ( std::isnan( mY ) )
72  {
73  return mX;
74  }
75  else if ( std::isnan( mX ) )
76  {
77  return std::numeric_limits<double>::quiet_NaN();
78  }
79  else
80  {
81  return std::sqrt( ( mX ) * ( mX ) + ( mY ) * ( mY ) );
82  }
83 }
84 
86 {
87  setX( scalar );
88 }
89 
91 {
92  mX = x;
93 }
94 
96 {
97  mY = y;
98 }
99 
101 {
102  return mX;
103 }
104 
106 {
107  return mY;
108 }
109 
111 {
112  bool equal = std::isnan( mX ) == std::isnan( other.x() );
113  equal &= std::isnan( mY ) == std::isnan( other.y() );
114 
115  if ( equal )
116  {
117  if ( std::isnan( mY ) )
118  {
119  equal &= qgsDoubleNear( other.x(), mX, 1E-8 );
120  }
121  else
122  {
123  equal &= qgsDoubleNear( other.x(), mX, 1E-8 );
124  equal &= qgsDoubleNear( other.y(), mY, 1E-8 );
125  }
126  }
127  return equal;
128 }
129 
131  bool isScalar,
132  DataType dataType,
133  double minimum,
134  double maximum,
135  int maximumVerticalLevels,
136  const QDateTime &referenceTime,
137  const QMap<QString, QString> &extraOptions )
138  : mName( name )
139  , mIsScalar( isScalar )
140  , mDataType( dataType )
141  , mMinimumValue( minimum )
142  , mMaximumValue( maximum )
143  , mExtraOptions( extraOptions )
144  , mMaximumVerticalLevelsCount( maximumVerticalLevels )
145  , mReferenceTime( referenceTime )
146 {
147 }
148 
149 QMap<QString, QString> QgsMeshDatasetGroupMetadata::extraOptions() const
150 {
151  return mExtraOptions;
152 }
153 
155 {
156  return !mIsScalar;
157 }
158 
160 {
161  return mIsScalar;
162 }
163 
165 {
166  return mName;
167 }
168 
170 {
171  return mDataType;
172 }
173 
175 {
176  return mMinimumValue;
177 }
178 
180 {
181  return mMaximumValue;
182 }
183 
185 {
186  return mMaximumVerticalLevelsCount;
187 }
188 
190 {
191  return mReferenceTime;
192 }
193 
195 {
196  return datasetCount( index.group() );
197 }
198 
200 {
201  return datasetGroupMetadata( index.group() );
202 }
203 
205  double time,
206  bool isValid,
207  double minimum,
208  double maximum,
209  int maximumVerticalLevels )
210  : mTime( time )
211  , mIsValid( isValid )
212  , mMinimumValue( minimum )
213  , mMaximumValue( maximum )
214  , mMaximumVerticalLevelsCount( maximumVerticalLevels )
215 {
216 }
217 
219 {
220  return mTime;
221 }
222 
224 {
225  return mIsValid;
226 }
227 
229 {
230  return mMinimumValue;
231 }
232 
234 {
235  return mMaximumValue;
236 }
237 
239 {
240  return mMaximumVerticalLevelsCount;
241 }
242 
244  : mType( ActiveFlagInteger )
245 {
246 }
247 
249  : mType( type ),
250  mSize( count )
251 {
252 }
253 
255 {
256  return mType;
257 }
258 
260 {
261  return mSize;
262 }
263 
265 {
266  return ( count() > 0 ) && ( mIsValid );
267 }
268 
270 {
271  if ( !isValid() )
272  return QgsMeshDatasetValue();
273 
274  Q_ASSERT( mType != ActiveFlagInteger );
275 
276  if ( mType == ScalarDouble )
277  return QgsMeshDatasetValue( mDoubleBuffer[index] );
278 
279  return QgsMeshDatasetValue(
280  mDoubleBuffer[2 * index],
281  mDoubleBuffer[2 * index + 1]
282  );
283 }
284 
285 bool QgsMeshDataBlock::active( int index ) const
286 {
287  if ( !isValid() )
288  return false;
289 
290  Q_ASSERT( mType == ActiveFlagInteger );
291 
292  if ( mIntegerBuffer.empty() )
293  return true;
294  else
295  return bool( mIntegerBuffer[index] );
296 }
297 
298 void QgsMeshDataBlock::setActive( const QVector<int> &vals )
299 {
300  Q_ASSERT( mType == ActiveFlagInteger );
301  Q_ASSERT( vals.size() == count() );
302 
303  mIntegerBuffer = vals;
304  setValid( true );
305 }
306 
307 QVector<int> QgsMeshDataBlock::active() const
308 {
309  Q_ASSERT( mType == ActiveFlagInteger );
310  return mIntegerBuffer;
311 }
312 
313 QVector<double> QgsMeshDataBlock::values() const
314 {
315  Q_ASSERT( mType != ActiveFlagInteger );
316 
317  return mDoubleBuffer;
318 }
319 
320 void QgsMeshDataBlock::setValues( const QVector<double> &vals )
321 {
322  Q_ASSERT( mType != ActiveFlagInteger );
323  Q_ASSERT( mType == ScalarDouble ? vals.size() == count() : vals.size() == 2 * count() );
324 
325  mDoubleBuffer = vals;
326  setValid( true );
327 }
328 
329 void QgsMeshDataBlock::setValid( bool valid )
330 {
331  mIsValid = valid;
332 }
333 
334 QgsMeshVertex QgsMesh::vertex( int index ) const
335 {
336  if ( index < vertices.size() && index >= 0 )
337  return vertices[index];
338  return QgsMeshVertex();
339 }
340 
341 QgsMeshFace QgsMesh::face( int index ) const
342 {
343  if ( index < faces.size() && index >= 0 )
344  return faces[index];
345  return QgsMeshFace();
346 }
347 
349 {
350  return vertices.size();
351 }
352 
354 {
355  return faces.size();
356 }
357 
359 
361 
363  : mSize( count )
364  , mIsVector( isVector )
365 {
366 }
367 
369 {
370  return mIsValid;
371 }
372 
374 {
375  return mIsVector;
376 }
377 
379 {
380  return mSize;
381 }
382 
384 {
385  if ( mFaceToVolumeIndex.empty() )
386  return -1;
387  return mFaceToVolumeIndex[0];
388 }
389 
391 {
392  if ( mFaceToVolumeIndex.empty() || mVerticalLevelsCount.empty() )
393  return -1;
394  const int lastVolumeStartIndex = mFaceToVolumeIndex[mFaceToVolumeIndex.size() - 1];
395  const int volumesCountInLastRow = mVerticalLevelsCount[mVerticalLevelsCount.size() - 1];
396  return lastVolumeStartIndex + volumesCountInLastRow;
397 }
398 
400 {
401  return lastVolumeIndex() - firstVolumeIndex();
402 }
403 
405 {
406  Q_ASSERT( isValid() );
407  return mVerticalLevelsCount;
408 }
409 
411 {
412  Q_ASSERT( faceToVolumeIndex.size() == count() );
413  mFaceToVolumeIndex = faceToVolumeIndex;
414 }
415 
417 {
418  Q_ASSERT( verticalLevelsCount.size() == count() );
419  mVerticalLevelsCount = verticalLevelsCount;
420 }
421 
422 QVector<double> QgsMesh3dDataBlock::verticalLevels() const
423 {
424  Q_ASSERT( isValid() );
425  return mVerticalLevels;
426 }
427 
429 {
430  Q_ASSERT( verticalLevels.size() == volumesCount() + count() );
431  mVerticalLevels = verticalLevels;
432 }
433 
435 {
436  Q_ASSERT( isValid() );
437  return mFaceToVolumeIndex;
438 }
439 
440 QVector<double> QgsMesh3dDataBlock::values() const
441 {
442  Q_ASSERT( isValid() );
443  return mDoubleBuffer;
444 }
445 
447 {
448  if ( !isValid() )
449  return QgsMeshDatasetValue();
450 
451  if ( !mIsVector )
452  return QgsMeshDatasetValue( mDoubleBuffer[volumeIndex] );
453 
454  return QgsMeshDatasetValue(
455  mDoubleBuffer[2 * volumeIndex],
456  mDoubleBuffer[2 * volumeIndex + 1]
457  );
458 }
459 
460 void QgsMesh3dDataBlock::setValues( const QVector<double> &doubleBuffer )
461 {
462  Q_ASSERT( doubleBuffer.size() == ( isVector() ? 2 * volumesCount() : volumesCount() ) );
463  mDoubleBuffer = doubleBuffer;
464 }
465 
467 {
468  mIsValid = valid;
469 }
QDateTime referenceTime() const
Returns the reference time.
QgsMeshDatasetValue value(int volumeIndex) const
Returns the value at volume centers.
QVector< int > active() const
Returns active flag array.
void set(double scalar)
Sets scalar value.
void setVerticalLevels(const QVector< double > &verticalLevels)
Sets the vertical levels height.
QgsMeshDatasetValue()=default
Default Ctor, initialize to NaN.
int firstVolumeIndex() const
Index of the first volume stored in the buffer (absolute)
QgsMeshVertex vertex(int index) const
Returns a vertex at the index.
QString name() const
Returns name of the dataset group.
int dataset() const
Returns a dataset index within group()
QVector< int > faceToVolumeIndex() const
Returns the indexing between faces and volumes.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
double maximum() const
Returns maximum scalar value/vector magnitude present for the dataset.
int count() const
Number of items stored in the block.
int lastVolumeIndex() const
Index of the last volume stored in the buffer (absolute)
virtual int datasetCount(int groupIndex) const =0
Returns number of datasets loaded in the group.
QgsMeshDatasetIndex(int group=-1, int dataset=-1)
Creates an index. -1 represents invalid group/dataset.
QgsMesh3dDataBlock()
Constructs an invalid block.
double maximum() const
Returns maximum scalar value/vector magnitude present for whole dataset group.
QgsMeshDatasetGroupMetadata()=default
Constructs an empty metadata object.
Abstract base class for spatial data provider implementations.
void setY(double y)
Sets Y value.
QVector< int > verticalLevelsCount() const
Returns number of vertical level above 2d faces.
bool operator==(QgsMeshDatasetValue other) const
DataType type() const
Type of data stored in the block.
int group() const
Returns a group index.
bool isVector() const
Whether we store vector values.
bool isValid() const
Whether the block is valid.
double y() const
Returns y value.
QgsMeshDatasetMetadata()=default
Constructs an empty metadata object.
DataType dataType() const
Returns whether dataset group data is defined on vertices or faces or volumes.
int count() const
Number of 2d faces for which the volume data is stored in the block.
void setVerticalLevelsCount(const QVector< int > &verticalLevelsCount)
Sets the vertical level counts.
int maximumVerticalLevelsCount() const
Returns maximum number of vertical levels for 3d stacked meshes.
QVector< double > values() const
Returns buffer to the array with values For vector it is pairs (x1, y1, x2, y2, ...
DataType
Location of where data is specified for datasets in the dataset group.
void setValid(bool valid)
Sets block validity.
QVector< double > values() const
Returns the values at volume centers.
QgsMeshDataBlock()
Constructs an invalid block.
Integer boolean flag whether face is active.
int volumesCount() const
Returns number of volumes stored in the buffer.
double minimum() const
Returns minimum scalar value/vector magnitude present for the dataset.
double scalar() const
Returns magnitude of vector for vector data or scalar value for scalar data.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
int maximumVerticalLevelsCount() const
Returns maximum number of vertical levels for 3d stacked meshes.
DataType
Type of data stored in the block.
bool isValid() const
Returns whether dataset is valid.
bool isScalar() const
Returns whether dataset group has scalar data.
Setting options for creating vector data providers.
QgsMeshDatasetGroupMetadata is a collection of dataset group metadata such as whether the data is vec...
void setActive(const QVector< int > &vals)
Sets active flag values.
void setValid(bool valid)
Sets block validity.
QMap< QString, QString > extraOptions() const
Returns extra metadata options, for example description.
bool isVector() const
Returns whether dataset group has vector data.
bool isValid() const
Whether the block is valid.
bool operator!=(QgsMeshDatasetIndex other) const
Inequality operator.
void setFaceToVolumeIndex(const QVector< int > &faceToVolumeIndex)
Sets the indexing between faces and volumes.
QVector< int > QgsMeshFace
List of vertex indexes.
QgsMeshDatasetIndex is index that identifies the dataset group (e.g.
QgsMeshDataProvider(const QString &uri, const QgsDataProvider::ProviderOptions &providerOptions)
Ctor.
void setValues(const QVector< double > &doubleBuffer)
Sets the values at volume centers.
bool isValid() const
Returns whether index is valid, ie at least groups is set.
QgsMeshDatasetValue value(int index) const
Returns a value represented by the index For active flag the behavior is undefined.
bool operator==(QgsMeshDatasetIndex other) const
Equality operator.
QgsMeshFace face(int index) const
Returns a face at the index.
double time() const
Returns the time value for this dataset.
QgsMeshDatasetValue represents single dataset value.
int vertexCount() const
Returns number of vertices.
double minimum() const
Returns minimum scalar value/vector magnitude present for whole dataset group.
int faceCount() const
Returns number of faces.
void setX(double x)
Sets X value.
void setValues(const QVector< double > &vals)
Sets values.
double x() const
Returns x value.
QgsPoint QgsMeshVertex
xyz coords of vertex
virtual QgsMeshDatasetGroupMetadata datasetGroupMetadata(int groupIndex) const =0
Returns dataset group metadata.
QVector< double > verticalLevels() const
Returns the vertical levels height.