21 static const QString TEXT_PROVIDER_KEY = QStringLiteral(
"mesh_memory" );
22 static const QString TEXT_PROVIDER_DESCRIPTION = QStringLiteral(
"Mesh memory provider" );
24 bool QgsMeshMemoryDataProvider::isValid()
const 29 QString QgsMeshMemoryDataProvider::name()
const 31 return TEXT_PROVIDER_KEY;
34 QString QgsMeshMemoryDataProvider::description()
const 36 return TEXT_PROVIDER_DESCRIPTION;
44 QgsMeshMemoryDataProvider::QgsMeshMemoryDataProvider(
const QString &uri,
const ProviderOptions &options )
47 mIsValid = splitMeshSections( uri );
50 QString QgsMeshMemoryDataProvider::providerKey()
52 return TEXT_PROVIDER_KEY;
55 QString QgsMeshMemoryDataProvider::providerDescription()
57 return TEXT_PROVIDER_DESCRIPTION;
60 QgsMeshMemoryDataProvider *QgsMeshMemoryDataProvider::createProvider(
const QString &uri,
const ProviderOptions &options )
62 return new QgsMeshMemoryDataProvider( uri, options );
65 bool QgsMeshMemoryDataProvider::splitMeshSections(
const QString &uri )
67 const QStringList sections = uri.split( QStringLiteral(
"---" ), QString::SkipEmptyParts );
68 if ( sections.size() != 2 )
70 setError(
QgsError( tr(
"Invalid mesh definition, does not contain 2 sections" ),
71 QStringLiteral(
"Mesh Memory Provider" ) ) );
75 if ( addMeshVertices( sections[0] ) )
76 return addMeshFaces( sections[1] );
81 bool QgsMeshMemoryDataProvider::addMeshVertices(
const QString &def )
83 QVector<QgsMeshVertex> vertices;
85 const QStringList verticesCoords = def.split(
'\n', QString::SkipEmptyParts );
86 for (
int i = 0; i < verticesCoords.size(); ++i )
88 const QStringList coords = verticesCoords[i].split(
',', QString::SkipEmptyParts );
89 if ( coords.size() != 2 )
91 setError(
QgsError( tr(
"Invalid mesh definition, vertex definition does not contain x, y" ),
92 QStringLiteral(
"Mesh Memory Provider" ) ) );
95 double x = coords.at( 0 ).toDouble();
96 double y = coords.at( 1 ).toDouble();
98 vertices.push_back( vertex );
101 mVertices = vertices;
105 bool QgsMeshMemoryDataProvider::addMeshFaces(
const QString &def )
107 QVector<QgsMeshFace> faces;
109 const QStringList facesVertices = def.split(
'\n', QString::SkipEmptyParts );
110 for (
int i = 0; i < facesVertices.size(); ++i )
112 const QStringList vertices = facesVertices[i].split(
',', QString::SkipEmptyParts );
113 if ( vertices.size() < 3 )
115 setError(
QgsError( tr(
"Invalid mesh definition, face must contain at least 3 vertices" ),
116 QStringLiteral(
"Mesh Memory Provider" ) ) );
120 for (
int j = 0; j < vertices.size(); ++j )
122 int vertex_id = vertices[j].toInt();
125 setError(
QgsError( tr(
"Invalid mesh definition, vertex index must be positive value" ),
126 QStringLiteral(
"Mesh Memory Provider" ) ) );
129 if ( mVertices.size() < vertex_id )
131 setError(
QgsError( tr(
"Invalid mesh definition, missing vertex id defined in face" ),
132 QStringLiteral(
"Mesh Memory Provider" ) ) );
136 face.push_back( vertex_id );
138 faces.push_back( face );
146 bool QgsMeshMemoryDataProvider::splitDatasetSections(
const QString &uri, QgsMeshMemoryDatasetGroup &datasetGroup )
148 const QStringList sections = uri.split( QStringLiteral(
"---" ), QString::SkipEmptyParts );
150 bool success = sections.size() > 2;
153 setError(
QgsError( tr(
"Invalid dataset definition, does not contain 3+ sections" ),
154 QStringLiteral(
"Mesh Memory Provider" ) ) );
158 success = setDatasetGroupType( sections[0], datasetGroup );
160 success = addDatasetGroupMetadata( sections[1], datasetGroup );
162 for (
int i = 2; i < sections.size(); ++i )
166 QgsMeshMemoryDataset dataset;
167 success = addDatasetValues( sections[i], dataset, datasetGroup.isScalar );
169 success = checkDatasetValidity( dataset, datasetGroup.isOnVertices );
171 datasetGroup.datasets.push_back( dataset );
177 bool QgsMeshMemoryDataProvider::setDatasetGroupType(
const QString &def, QgsMeshMemoryDatasetGroup &datasetGroup )
179 const QStringList types = def.split(
' ', QString::SkipEmptyParts );
181 if ( types.size() != 3 )
183 setError(
QgsError( tr(
"Invalid type definition, must be Vertex/Face Vector/Scalar Name" ),
184 QStringLiteral(
"Mesh Memory Provider" ) ) );
188 datasetGroup.isOnVertices = 0 == QString::compare( types[0].trimmed(), QStringLiteral(
"vertex" ), Qt::CaseInsensitive );
189 datasetGroup.isScalar = 0 == QString::compare( types[1].trimmed(), QStringLiteral(
"scalar" ), Qt::CaseInsensitive );
190 datasetGroup.name = types[2].trimmed();
195 bool QgsMeshMemoryDataProvider::addDatasetGroupMetadata(
const QString &def, QgsMeshMemoryDatasetGroup &datasetGroup )
197 const QStringList metadataLines = def.split(
'\n', QString::SkipEmptyParts );
198 for (
int i = 0; i < metadataLines.size(); ++i )
200 const QStringList keyVal = metadataLines[i].split(
':', QString::SkipEmptyParts );
201 if ( keyVal.size() != 2 )
203 setError(
QgsError( tr(
"Invalid dataset definition, dataset metadata does not contain key: value" ),
204 QStringLiteral(
"Mesh Memory Provider" ) ) );
208 datasetGroup.metadata.insert( keyVal.at( 0 ).trimmed(), keyVal.at( 1 ).trimmed() );
213 bool QgsMeshMemoryDataProvider::addDatasetValues(
const QString &def, QgsMeshMemoryDataset &dataset,
bool isScalar )
215 const QStringList valuesLines = def.split(
'\n', QString::SkipEmptyParts );
217 if ( valuesLines.size() < 2 )
219 setError(
QgsError( tr(
"Invalid dataset definition, must contain at least 1 line (time)" ),
220 QStringLiteral(
"Mesh Memory Provider" ) ) );
224 dataset.time = valuesLines[0].toDouble();
226 for (
int i = 1; i < valuesLines.size(); ++i )
228 const QStringList values = valuesLines[i].split(
',', QString::SkipEmptyParts );
233 if ( values.size() != 1 )
235 setError(
QgsError( tr(
"Invalid dataset definition, dataset scalar values must be x" ),
236 QStringLiteral(
"Mesh Memory Provider" ) ) );
241 point.
setX( values[0].toDouble() );
246 if ( values.size() < 2 )
248 setError(
QgsError( tr(
"Invalid dataset definition, dataset vector values must be x, y" ),
249 QStringLiteral(
"Mesh Memory Provider" ) ) );
254 point.
setX( values[0].toDouble() );
255 point.
setY( values[1].toDouble() );
259 dataset.values.push_back( point );
264 bool QgsMeshMemoryDataProvider::checkDatasetValidity( QgsMeshMemoryDataset &dataset,
bool isOnVertices )
270 if ( dataset.values.count() != vertexCount() )
273 setError(
QgsError( tr(
"Dataset defined on vertices has {} values, but mesh {}" ).arg( dataset.values.count(), vertexCount() ),
274 QStringLiteral(
"Mesh Memory Provider" ) ) );
280 if ( dataset.values.count() != faceCount() )
283 setError(
QgsError( tr(
"Dataset defined on faces has {} values, but mesh {}" ).arg( dataset.values.count(), faceCount() ),
284 QStringLiteral(
"Mesh Memory Provider" ) ) );
288 dataset.valid = valid;
292 int QgsMeshMemoryDataProvider::vertexCount()
const 294 return mVertices.size();
297 int QgsMeshMemoryDataProvider::faceCount()
const 299 return mFaces.size();
302 QgsMeshVertex QgsMeshMemoryDataProvider::vertex(
int index )
const 304 Q_ASSERT( vertexCount() > index );
305 return mVertices[index];
308 QgsMeshFace QgsMeshMemoryDataProvider::face(
int index )
const 310 Q_ASSERT( faceCount() > index );
311 return mFaces[index];
314 bool QgsMeshMemoryDataProvider::addDataset(
const QString &uri )
316 QgsMeshMemoryDatasetGroup group;
321 valid = splitDatasetSections( uri, group );
325 setError(
QgsError( tr(
"Unable to add dataset group to invalid mesh" ),
326 QStringLiteral(
"Mesh Memory Provider" ) ) );
329 mDatasetGroups.push_back( group );
333 mExtraDatasetUris << uri;
334 emit datasetGroupsAdded( 1 );
341 QStringList QgsMeshMemoryDataProvider::extraDatasets()
const 343 return mExtraDatasetUris;
346 int QgsMeshMemoryDataProvider::datasetGroupCount()
const 348 return mDatasetGroups.count();
351 int QgsMeshMemoryDataProvider::datasetCount(
int groupIndex )
const 353 if ( ( groupIndex >= 0 ) && ( groupIndex < datasetGroupCount() ) )
354 return mDatasetGroups[groupIndex].datasets.count();
361 if ( ( groupIndex >= 0 ) && ( groupIndex < datasetGroupCount() ) )
364 mDatasetGroups[groupIndex].name,
365 mDatasetGroups[groupIndex].isScalar,
366 mDatasetGroups[groupIndex].isOnVertices,
367 mDatasetGroups[groupIndex].metadata
379 if ( ( index.
group() >= 0 ) && ( index.
group() < datasetGroupCount() ) &&
383 const QgsMeshMemoryDatasetGroup &grp = mDatasetGroups.at( index.
group() );
385 grp.datasets[index.
dataset()].time,
386 grp.datasets[index.
dataset()].valid
398 if ( ( index.
group() >= 0 ) && ( index.
group() < datasetGroupCount() ) &&
400 ( valueIndex >= 0 ) && ( valueIndex < mDatasetGroups[index.
group()].datasets[index.
dataset()].values.count() ) )
402 return mDatasetGroups[index.
group()].datasets[index.
dataset()].values[valueIndex];
410 bool QgsMeshMemoryDataProvider::isFaceActive(
QgsMeshDatasetIndex index,
int faceIndex )
const 413 Q_UNUSED( faceIndex );
int dataset() const
Returns a dataset index within group()
void setY(double y)
Sets Y value.
const QgsCoordinateReferenceSystem & crs
Point geometry type, with support for z-dimension and m-values.
Base class for providing data for QgsMeshLayer.
QgsError is container for error messages (report).
QVector< int > QgsMeshFace
List of vertex indexes.
This class represents a coordinate reference system (CRS).
QgsMeshDatasetIndex is index that identifies the dataset group (e.g.
int group() const
Returns a group index.
QgsMeshDatasetValue represents single dataset value.
void setX(double x)
Sets X value.