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, QgsMeshMemoryDataset &dataset )
148 const QStringList sections = uri.split( QStringLiteral(
"---" ), QString::SkipEmptyParts );
150 bool success = sections.size() == 3;
153 setError(
QgsError( tr(
"Invalid dataset definition, does not contain 3 sections" ),
154 QStringLiteral(
"Mesh Memory Provider" ) ) );
158 success = setDatasetType( sections[0], dataset );
160 success = addDatasetMetadata( sections[1], dataset );
162 success = addDatasetValues( sections[2], dataset );
164 success = checkDatasetValidity( dataset );
169 bool QgsMeshMemoryDataProvider::setDatasetType(
const QString &def, QgsMeshMemoryDataset &dataset )
171 const QStringList types = def.split(
' ', QString::SkipEmptyParts );
173 if ( types.size() != 2 )
175 setError(
QgsError( tr(
"Invalid type definition, must be Vertex/Face Vector/Scalar" ),
176 QStringLiteral(
"Mesh Memory Provider" ) ) );
180 dataset.isOnVertices = 0 == QString::compare( types[0].trimmed(), QStringLiteral(
"vertex" ), Qt::CaseInsensitive );
181 dataset.isScalar = 0 == QString::compare( types[1].trimmed(), QStringLiteral(
"scalar" ), Qt::CaseInsensitive );
186 bool QgsMeshMemoryDataProvider::addDatasetMetadata(
const QString &def, QgsMeshMemoryDataset &dataset )
188 const QStringList metadataLines = def.split(
'\n', QString::SkipEmptyParts );
189 for (
int i = 0; i < metadataLines.size(); ++i )
191 const QStringList keyVal = metadataLines[i].split(
':', QString::SkipEmptyParts );
192 if ( keyVal.size() != 2 )
194 setError(
QgsError( tr(
"Invalid dataset definition, dataset metadata does not contain key: value" ),
195 QStringLiteral(
"Mesh Memory Provider" ) ) );
199 dataset.metadata.insert( keyVal.at( 0 ).trimmed(), keyVal.at( 1 ).trimmed() );
204 bool QgsMeshMemoryDataProvider::addDatasetValues(
const QString &def, QgsMeshMemoryDataset &dataset )
206 const QStringList valuesLines = def.split(
'\n', QString::SkipEmptyParts );
207 for (
int i = 0; i < valuesLines.size(); ++i )
209 const QStringList values = valuesLines[i].split(
',', QString::SkipEmptyParts );
212 if ( dataset.isScalar )
214 if ( values.size() != 1 )
216 setError(
QgsError( tr(
"Invalid dataset definition, dataset scalar values must be x" ),
217 QStringLiteral(
"Mesh Memory Provider" ) ) );
222 point.
setX( values[0].toDouble() );
227 if ( values.size() < 2 )
229 setError(
QgsError( tr(
"Invalid dataset definition, dataset vector values must be x, y" ),
230 QStringLiteral(
"Mesh Memory Provider" ) ) );
235 point.
setX( values[0].toDouble() );
236 point.
setY( values[1].toDouble() );
240 dataset.values.push_back( point );
245 bool QgsMeshMemoryDataProvider::checkDatasetValidity( QgsMeshMemoryDataset &dataset )
249 if ( dataset.isOnVertices )
251 if ( dataset.values.count() != vertexCount() )
254 setError(
QgsError( tr(
"Dataset defined on vertices has {} values, but mesh {}" ).arg( dataset.values.count(), vertexCount() ),
255 QStringLiteral(
"Mesh Memory Provider" ) ) );
261 if ( dataset.values.count() != faceCount() )
264 setError(
QgsError( tr(
"Dataset defined on faces has {} values, but mesh {}" ).arg( dataset.values.count(), faceCount() ),
265 QStringLiteral(
"Mesh Memory Provider" ) ) );
272 int QgsMeshMemoryDataProvider::vertexCount()
const 274 return mVertices.size();
277 int QgsMeshMemoryDataProvider::faceCount()
const 279 return mFaces.size();
282 QgsMeshVertex QgsMeshMemoryDataProvider::vertex(
int index )
const 284 Q_ASSERT( vertexCount() > index );
285 return mVertices[index];
288 QgsMeshFace QgsMeshMemoryDataProvider::face(
int index )
const 290 Q_ASSERT( faceCount() > index );
291 return mFaces[index];
294 bool QgsMeshMemoryDataProvider::addDataset(
const QString &uri )
296 QgsMeshMemoryDataset ds;
300 ds.valid = splitDatasetSections( uri, ds );
304 setError(
QgsError( tr(
"Unable to add dataset to invalid mesh" ),
305 QStringLiteral(
"Mesh Memory Provider" ) ) );
308 mDatasets.push_back( ds );
313 int QgsMeshMemoryDataProvider::datasetCount()
const 315 return mDatasets.count();
320 if ( ( index >= 0 ) && ( index < datasetCount() ) )
323 mDatasets[index].isScalar,
324 mDatasets[index].valid,
325 mDatasets[index].isOnVertices,
326 mDatasets[index].metadata
336 QgsMeshDatasetValue QgsMeshMemoryDataProvider::datasetValue(
int datasetIndex,
int valueIndex )
const 338 if ( ( datasetIndex >= 0 ) &&
339 ( datasetIndex < datasetCount() ) &&
340 ( valueIndex >= 0 ) &&
341 ( valueIndex < mDatasets[datasetIndex].values.count() ) )
343 return mDatasets[datasetIndex].values[valueIndex];
void setY(double y)
Sets Y value.
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).
QgsMeshDatasetValue represents single mesh dataset value.
void setX(double x)
Sets X value.