QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 "qgis.h"
19 #include "qgsmeshdataprovider.h"
21 #include "qgsrectangle.h"
22 
24  QgsDataProvider::ReadFlags flags )
25  : QgsDataProvider( uri, options, flags )
26 {
27 }
28 
30 {
31  return mTemporalCapabilities.get();
32 }
33 
35 {
36  return mTemporalCapabilities.get();
37 }
38 
40 {
41  const QgsUnitTypes::TemporalUnit oldUnit = mTemporalCapabilities->temporalUnit();
42  mTemporalCapabilities->setTemporalUnit( unit );
43  if ( oldUnit != unit )
44  reloadData();
45 }
46 
48 
50  const QDateTime &referenceTime,
51  int groupIndex, qint64 time,
53 {
54  const QDateTime requestDateTime = referenceTime.addMSecs( time );
55  qint64 providerTime;
56  const QDateTime providerReferenceTime = mTemporalCapabilities->referenceTime();
57  if ( providerReferenceTime.isValid() )
58  providerTime = providerReferenceTime.msecsTo( requestDateTime );
59  else
60  providerTime = time;
61 
62  switch ( method )
63  {
65  return mTemporalCapabilities->datasetIndexClosestBeforeRelativeTime( groupIndex, providerTime );
66  break;
68  return mTemporalCapabilities->datasetIndexClosestFromRelativeTime( groupIndex, providerTime );
69  break;
70  }
71 
72  return QgsMeshDatasetIndex();
73 }
74 
75 QList<QgsMeshDatasetIndex> QgsMeshDatasetSourceInterface::datasetIndexInTimeInterval( const QDateTime &referenceTime, int groupIndex, qint64 time1, qint64 time2 ) const
76 {
77  const QDateTime requestDateTime = referenceTime.addMSecs( time1 );
78  qint64 providerTime1;
79  qint64 providerTime2;
80  const QDateTime providerReferenceTime = mTemporalCapabilities->referenceTime();
81  if ( mTemporalCapabilities->referenceTime().isValid() )
82  {
83  providerTime1 = providerReferenceTime.msecsTo( requestDateTime );
84  providerTime2 = providerTime1 - time1 + time2;
85  }
86  else
87  {
88  providerTime1 = time1;
89  providerTime2 = time2;
90  }
91 
92  QList<QgsMeshDatasetIndex> ret;
93  for ( int i = 0; i < datasetCount( groupIndex ); ++i )
94  {
95  QgsMeshDatasetIndex datasetIndex( groupIndex, i );
96  qint64 time = mTemporalCapabilities->datasetTime( datasetIndex );
97  if ( time >= providerTime1 && time <= providerTime2 )
98  ret.append( datasetIndex );
99  }
100 
101  return ret;
102 }
103 
105  mTemporalCapabilities( std::make_unique<QgsMeshDataProviderTemporalCapabilities>() ) {}
106 
108 {
109  return datasetCount( index.group() );
110 }
111 
113 {
114  return datasetGroupMetadata( index.group() );
115 }
116 
118  const QString &path,
119  const QgsMeshDatasetGroupMetadata &meta,
120  const QVector<QgsMeshDataBlock> &datasetValues,
121  const QVector<QgsMeshDataBlock> &datasetActive,
122  const QVector<double> &times )
123 {
124  // Form DRIVER:filename
125  QString filename = path;
126  // ASCII dat supports face, edge and vertex datasets
127  QString driverName = QStringLiteral( "DAT" );
128  QStringList parts = path.split( ':' );
129  if ( parts.size() > 1 )
130  {
131  driverName = parts[0];
132  parts.removeFirst();
133  filename = parts.join( QString() );
134  }
135  return persistDatasetGroup( filename, driverName, meta, datasetValues, datasetActive, times );
136 }
137 
138 QgsMeshVertex QgsMesh::vertex( int index ) const
139 {
140  if ( index < vertices.size() && index >= 0 )
141  return vertices[index];
142  return QgsMeshVertex();
143 }
144 
145 QgsMeshFace QgsMesh::face( int index ) const
146 {
147  if ( index < faces.size() && index >= 0 )
148  return faces[index];
149  return QgsMeshFace();
150 }
151 
152 QgsMeshEdge QgsMesh::edge( int index ) const
153 {
154  if ( index < edges.size() && index >= 0 )
155  return edges[index];
156  return QgsMeshEdge();
157 }
158 
160 {
161  vertices.clear();
162  edges.clear();
163  faces.clear();
164 }
165 
166 bool QgsMesh::compareFaces( const QgsMeshFace &face1, const QgsMeshFace &face2 )
167 {
168  if ( face1.count() != face2.count() )
169  return false;
170 
171  int startFace2 = 0;
172  for ( int i = 0; i < face2.count(); ++i )
173  if ( face2.at( i ) == face1.at( 0 ) )
174  {
175  startFace2 = i;
176  break;
177  }
178 
179  for ( int i = 0; i < face1.count(); ++i )
180  if ( face1.at( i ) != face2.at( ( i + startFace2 ) % ( face2.count() ) ) )
181  return false;
182 
183  return true;
184 }
185 
186 bool QgsMesh::contains( const QgsMesh::ElementType &type ) const
187 {
188  switch ( type )
189  {
190  case ElementType::Vertex:
191  return !vertices.isEmpty();
192  case ElementType::Edge:
193  return !edges.isEmpty();
194  case ElementType::Face:
195  return !faces.isEmpty();
196  }
197  return false;
198 }
199 
201 {
202  return vertices.size();
203 }
204 
206 {
207  return faces.size();
208 }
209 
211 {
212  return edges.size();
213 }
214 
216 {
217  switch ( type )
218  {
219  case QgsMesh::ElementType::Vertex:
220  return vertexCount() != 0;
221  case QgsMesh::ElementType::Edge:
222  return edgeCount() != 0;
223  case QgsMesh::ElementType::Face:
224  return faceCount() != 0;
225  }
226  return false;
227 }
QgsMeshDatasetSourceInterface::datasetCount
virtual int datasetCount(int groupIndex) const =0
Returns number of datasets loaded in the group.
QgsDataProvider::ProviderOptions
Setting options for creating vector data providers.
Definition: qgsdataprovider.h:107
QgsDataProvider
Abstract base class for spatial data provider implementations.
Definition: qgsdataprovider.h:40
qgsrectangle.h
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:48
QgsMeshVertex
QgsPoint QgsMeshVertex
xyz coords of vertex
Definition: qgsmeshdataprovider.h:36
QgsMeshDataProviderTemporalCapabilities
Class for handling properties relating to a mesh data provider's temporal capabilities.
Definition: qgsmeshdataprovidertemporalcapabilities.h:34
qgis.h
QgsMeshDataSourceInterface::contains
bool contains(const QgsMesh::ElementType &type) const
Returns whether the mesh contains at mesh elements of given type.
Definition: qgsmeshdataprovider.cpp:215
QgsUnitTypes::TemporalUnit
TemporalUnit
Temporal units.
Definition: qgsunittypes.h:149
QgsMesh::edges
QVector< QgsMeshEdge > edges
Definition: qgsmeshdataprovider.h:113
QgsMeshDatasetSourceInterface::datasetIndexInTimeInterval
QList< QgsMeshDatasetIndex > datasetIndexInTimeInterval(const QDateTime &referenceTime, int groupIndex, qint64 time1, qint64 time2) const
Returns a list of dataset indexes of the dataset in a specific dataset group that are between time1 a...
Definition: qgsmeshdataprovider.cpp:75
QgsMeshDataProviderTemporalCapabilities::FindClosestDatasetFromStartRangeTime
@ FindClosestDatasetFromStartRangeTime
Finds the closest dataset which have its time before the requested start range time.
Definition: qgsmeshdataprovidertemporalcapabilities.h:44
QgsMeshDataProvider::temporalCapabilities
QgsMeshDataProviderTemporalCapabilities * temporalCapabilities() override
Returns the provider's temporal capabilities.
Definition: qgsmeshdataprovider.cpp:29
QgsMeshDriverMetadata
Holds metadata about mesh driver.
Definition: qgsprovidermetadata.h:57
QgsMeshDatasetSourceInterface::datasetValues
virtual QgsMeshDataBlock datasetValues(QgsMeshDatasetIndex index, int valueIndex, int count) const =0
Returns N vector/scalar values from the index from the dataset.
QgsDataProvider::reloadData
virtual void reloadData()
Reloads the data from the source for providers with data caches to synchronize, changes in the data s...
Definition: qgsdataprovider.cpp:45
QgsMesh::faceCount
int faceCount() const
Returns number of faces.
Definition: qgsmeshdataprovider.cpp:205
QgsMeshDatasetIndex::group
int group() const
Returns a group index.
Definition: qgsmeshdataset.cpp:27
QgsMesh::vertexCount
int vertexCount() const
Returns number of vertices.
Definition: qgsmeshdataprovider.cpp:200
QgsMesh::ElementType
ElementType
Defines type of mesh elements.
Definition: qgsmeshdataprovider.h:65
QgsMesh::faces
QVector< QgsMeshFace > faces
Definition: qgsmeshdataprovider.h:114
QgsMesh::edgeCount
int edgeCount() const
Returns number of edge.
Definition: qgsmeshdataprovider.cpp:210
QgsMesh::edge
QgsMeshEdge edge(int index) const
Returns an edge at the index.
Definition: qgsmeshdataprovider.cpp:152
QgsMeshDatasetSourceInterface::QgsMeshDatasetSourceInterface
QgsMeshDatasetSourceInterface()
Definition: qgsmeshdataprovider.cpp:104
QgsMeshDatasetIndex
QgsMeshDatasetIndex is index that identifies the dataset group (e.g. wind speed) and a dataset in thi...
Definition: qgsmeshdataset.h:48
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
QgsMeshDataProvider::setTemporalUnit
void setTemporalUnit(QgsUnitTypes::TemporalUnit unit)
Sets the temporal unit of the provider and reload data if it changes.
Definition: qgsmeshdataprovider.cpp:39
QgsMesh::face
QgsMeshFace face(int index) const
Returns a face at the index.
Definition: qgsmeshdataprovider.cpp:145
QgsMeshDataProviderTemporalCapabilities::MatchingTemporalDatasetMethod
MatchingTemporalDatasetMethod
Method for selection of temporal mesh dataset from a range time.
Definition: qgsmeshdataprovidertemporalcapabilities.h:41
QgsMeshDataSourceInterface::edgeCount
virtual int edgeCount() const =0
Returns number of edges in the native mesh.
QgsMeshDataSourceInterface::vertexCount
virtual int vertexCount() const =0
Returns number of vertices in the native mesh.
QgsMeshFace
QVector< int > QgsMeshFace
List of vertex indexes.
Definition: qgsmeshdataprovider.h:42
QgsMesh::clear
void clear()
Remove all vertices, edges and faces.
Definition: qgsmeshdataprovider.cpp:159
QgsMeshDataProvider::QgsMeshDataProvider
QgsMeshDataProvider(const QString &uri, const QgsDataProvider::ProviderOptions &providerOptions, QgsDataProvider::ReadFlags flags=QgsDataProvider::ReadFlags())
Ctor.
Definition: qgsmeshdataprovider.cpp:23
QgsMeshDataSourceInterface::faceCount
virtual int faceCount() const =0
Returns number of faces in the native mesh.
QgsMesh::contains
bool contains(const ElementType &type) const
Returns whether the mesh contains at mesh elements of given type.
Definition: qgsmeshdataprovider.cpp:186
QgsMeshDatasetGroupMetadata
QgsMeshDatasetGroupMetadata is a collection of dataset group metadata such as whether the data is vec...
Definition: qgsmeshdataset.h:351
QgsMeshDataProvider::driverMetadata
virtual QgsMeshDriverMetadata driverMetadata() const
Returns the mesh driver metadata of the provider.
Definition: qgsmeshdataprovider.cpp:47
QgsMeshDataProviderTemporalCapabilities::FindClosestDatasetBeforeStartRangeTime
@ FindClosestDatasetBeforeStartRangeTime
Definition: qgsmeshdataprovidertemporalcapabilities.h:43
QgsMeshEdge
QPair< int, int > QgsMeshEdge
Edge is a straight line seqment between 2 points.
Definition: qgsmeshdataprovider.h:49
QgsMeshDatasetSourceInterface::datasetIndexAtTime
QgsMeshDatasetIndex datasetIndexAtTime(const QDateTime &referenceTime, int groupIndex, qint64 time, QgsMeshDataProviderTemporalCapabilities::MatchingTemporalDatasetMethod method) const
Returns the dataset index of the dataset in a specific dataset group at time from the reference time.
Definition: qgsmeshdataprovider.cpp:49
QgsMesh::vertices
QVector< QgsMeshVertex > vertices
Definition: qgsmeshdataprovider.h:112
qgsmeshdataprovidertemporalcapabilities.h
QgsMesh::compareFaces
static bool compareFaces(const QgsMeshFace &face1, const QgsMeshFace &face2)
Compare two faces, return true if they are equivalent : same indexes and same clock wise.
Definition: qgsmeshdataprovider.cpp:166
QgsMesh::vertex
QgsMeshVertex vertex(int index) const
Returns a vertex at the index.
Definition: qgsmeshdataprovider.cpp:138
qgsmeshdataprovider.h
QgsMeshDatasetSourceInterface::datasetGroupMetadata
virtual QgsMeshDatasetGroupMetadata datasetGroupMetadata(int groupIndex) const =0
Returns dataset group metadata.
QgsMeshDatasetSourceInterface::mTemporalCapabilities
std::unique_ptr< QgsMeshDataProviderTemporalCapabilities > mTemporalCapabilities
Definition: qgsmeshdataprovider.h:422