QGIS API Documentation  3.20.0-Odense (decaadbb31)
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  QgsUnitTypes::TemporalUnit oldUnit = mTemporalCapabilities->temporalUnit();
42  mTemporalCapabilities->setTemporalUnit( unit );
43  if ( oldUnit != unit )
44  reloadData();
45 }
46 
48  const QDateTime &referenceTime,
49  int groupIndex, quint64 time,
51 {
52  QDateTime requestDateTime = referenceTime.addMSecs( time );
53  quint64 providerTime;
54  QDateTime providerReferenceTime = mTemporalCapabilities->referenceTime();
55  if ( mTemporalCapabilities->referenceTime().isValid() )
56  providerTime = referenceTime.msecsTo( requestDateTime );
57  else
58  providerTime = time;
59 
60  switch ( method )
61  {
63  return mTemporalCapabilities->datasetIndexClosestBeforeRelativeTime( groupIndex, providerTime );
64  break;
66  return mTemporalCapabilities->datasetIndexClosestFromRelativeTime( groupIndex, providerTime );
67  break;
68  }
69 
70  return QgsMeshDatasetIndex();
71 }
72 
74  mTemporalCapabilities( std::make_unique<QgsMeshDataProviderTemporalCapabilities>() ) {}
75 
77 {
78  return datasetCount( index.group() );
79 }
80 
82 {
83  return datasetGroupMetadata( index.group() );
84 }
85 
87  const QString &path,
88  const QgsMeshDatasetGroupMetadata &meta,
89  const QVector<QgsMeshDataBlock> &datasetValues,
90  const QVector<QgsMeshDataBlock> &datasetActive,
91  const QVector<double> &times )
92 {
93  // Form DRIVER:filename
94  QString filename = path;
95  // ASCII dat supports face, edge and vertex datasets
96  QString driverName = QStringLiteral( "DAT" );
97  QStringList parts = path.split( ':' );
98  if ( parts.size() > 1 )
99  {
100  driverName = parts[0];
101  parts.removeFirst();
102  filename = parts.join( QString() );
103  }
104  return persistDatasetGroup( filename, driverName, meta, datasetValues, datasetActive, times );
105 }
106 
107 QgsMeshVertex QgsMesh::vertex( int index ) const
108 {
109  if ( index < vertices.size() && index >= 0 )
110  return vertices[index];
111  return QgsMeshVertex();
112 }
113 
114 QgsMeshFace QgsMesh::face( int index ) const
115 {
116  if ( index < faces.size() && index >= 0 )
117  return faces[index];
118  return QgsMeshFace();
119 }
120 
121 QgsMeshEdge QgsMesh::edge( int index ) const
122 {
123  if ( index < edges.size() && index >= 0 )
124  return edges[index];
125  return QgsMeshEdge();
126 }
127 
129 {
130  vertices.clear();
131  edges.clear();
132  faces.clear();
133 }
134 
135 bool QgsMesh::compareFaces( const QgsMeshFace &face1, const QgsMeshFace &face2 )
136 {
137  if ( face1.count() != face2.count() )
138  return false;
139 
140  int startFace2 = 0;
141  for ( int i = 0; i < face2.count(); ++i )
142  if ( face2.at( i ) == face1.at( 0 ) )
143  {
144  startFace2 = i;
145  break;
146  }
147 
148  for ( int i = 0; i < face1.count(); ++i )
149  if ( face1.at( i ) != face2.at( ( i + startFace2 ) % ( face2.count() ) ) )
150  return false;
151 
152  return true;
153 }
154 
155 bool QgsMesh::contains( const QgsMesh::ElementType &type ) const
156 {
157  switch ( type )
158  {
159  case ElementType::Vertex:
160  return !vertices.isEmpty();
161  case ElementType::Edge:
162  return !edges.isEmpty();
163  case ElementType::Face:
164  return !faces.isEmpty();
165  }
166  return false;
167 }
168 
170 {
171  return vertices.size();
172 }
173 
175 {
176  return faces.size();
177 }
178 
180 {
181  return edges.size();
182 }
183 
185 {
186  switch ( type )
187  {
188  case QgsMesh::ElementType::Vertex:
189  return vertexCount() != 0;
190  case QgsMesh::ElementType::Edge:
191  return edgeCount() != 0;
192  case QgsMesh::ElementType::Face:
193  return faceCount() != 0;
194  }
195  return false;
196 }
Abstract base class for spatial data provider implementations.
virtual void reloadData()
Reloads the data from the source for providers with data caches to synchronize, changes in the data s...
Class for handling properties relating to a mesh data provider's temporal capabilities.
MatchingTemporalDatasetMethod
Method for selection of temporal mesh dataset from a range time.
@ FindClosestDatasetFromStartRangeTime
Finds the closest dataset which have its time before the requested start range time.
QgsMeshDataProviderTemporalCapabilities * temporalCapabilities() override
Returns the provider's temporal capabilities.
void setTemporalUnit(QgsUnitTypes::TemporalUnit unit)
Sets the temporal unit of the provider and reload data if it changes.
QgsMeshDataProvider(const QString &uri, const QgsDataProvider::ProviderOptions &providerOptions, QgsDataProvider::ReadFlags flags=QgsDataProvider::ReadFlags())
Ctor.
virtual int vertexCount() const =0
Returns number of vertices in the native mesh.
bool contains(const QgsMesh::ElementType &type) const
Returns whether the mesh contains at mesh elements of given type.
virtual int edgeCount() const =0
Returns number of edges in the native mesh.
virtual int faceCount() const =0
Returns number of faces in the native mesh.
QgsMeshDatasetGroupMetadata is a collection of dataset group metadata such as whether the data is vec...
QgsMeshDatasetIndex is index that identifies the dataset group (e.g.
int group() const
Returns a group index.
virtual QgsMeshDataBlock datasetValues(QgsMeshDatasetIndex index, int valueIndex, int count) const =0
Returns N vector/scalar values from the index from the dataset.
virtual int datasetCount(int groupIndex) const =0
Returns number of datasets loaded in the group.
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.
virtual QgsMeshDatasetGroupMetadata datasetGroupMetadata(int groupIndex) const =0
Returns dataset group metadata.
std::unique_ptr< QgsMeshDataProviderTemporalCapabilities > mTemporalCapabilities
QgsMeshDatasetIndex datasetIndexAtTime(const QDateTime &referenceTime, int groupIndex, quint64 time, QgsMeshDataProviderTemporalCapabilities::MatchingTemporalDatasetMethod method) const
Returns the dataset index of the dataset in a specific dataet group at time from the reference time.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
TemporalUnit
Temporal units.
Definition: qgsunittypes.h:150
QVector< int > QgsMeshFace
List of vertex indexes.
QPair< int, int > QgsMeshEdge
Edge is a straight line seqment between 2 points.
QgsPoint QgsMeshVertex
xyz coords of vertex
Setting options for creating vector data providers.
int vertexCount() const
Returns number of vertices.
QVector< QgsMeshVertex > vertices
QgsMeshFace face(int index) const
Returns a face at the index.
bool contains(const ElementType &type) const
Returns whether the mesh contains at mesh elements of given type.
QVector< QgsMeshFace > faces
static bool compareFaces(const QgsMeshFace &face1, const QgsMeshFace &face2)
Compare two faces, return true if they are equivalent : same indexes and same clock wise.
void clear()
Remove all vertices, edges and faces.
int faceCount() const
Returns number of faces.
ElementType
Defines type of mesh elements.
QgsMeshEdge edge(int index) const
Returns an edge at the index.
QgsMeshVertex vertex(int index) const
Returns a vertex at the index.
int edgeCount() const
Returns number of edge.
QVector< QgsMeshEdge > edges