diff --git a/python/core/auto_generated/pointcloud/qgspointcloudattribute.sip.in b/python/core/auto_generated/pointcloud/qgspointcloudattribute.sip.in index aed793e2f38..7d576e91bf5 100644 --- a/python/core/auto_generated/pointcloud/qgspointcloudattribute.sip.in +++ b/python/core/auto_generated/pointcloud/qgspointcloudattribute.sip.in @@ -150,6 +150,11 @@ Returns -1 if a matching attribute was not found. int pointRecordSize() const; %Docstring Returns total size of record +%End + + QgsFields toFields() const; +%Docstring +Converts the attribute collection to an equivalent QgsFields collection. %End }; diff --git a/src/core/pointcloud/qgspointcloudattribute.cpp b/src/core/pointcloud/qgspointcloudattribute.cpp index ed68c04483e..86b5eca3e27 100644 --- a/src/core/pointcloud/qgspointcloudattribute.cpp +++ b/src/core/pointcloud/qgspointcloudattribute.cpp @@ -153,6 +153,16 @@ int QgsPointCloudAttributeCollection::indexOf( const QString &name ) const return -1; } +QgsFields QgsPointCloudAttributeCollection::toFields() const +{ + QgsFields fields; + for ( const QgsPointCloudAttribute &attribute : mAttributes ) + { + fields.append( QgsField( attribute.name(), attribute.variantType(), attribute.displayType() ) ); + } + return fields; +} + template void _attribute( const char *data, std::size_t offset, QgsPointCloudAttribute::DataType type, T &value ) { diff --git a/src/core/pointcloud/qgspointcloudattribute.h b/src/core/pointcloud/qgspointcloudattribute.h index da0e91a4397..63cd26116dd 100644 --- a/src/core/pointcloud/qgspointcloudattribute.h +++ b/src/core/pointcloud/qgspointcloudattribute.h @@ -20,6 +20,7 @@ #include "qgis.h" #include "qgis_core.h" +#include "qgsfields.h" #include #include @@ -164,6 +165,11 @@ class CORE_EXPORT QgsPointCloudAttributeCollection //! Returns total size of record int pointRecordSize() const { return mSize; } + /** + * Converts the attribute collection to an equivalent QgsFields collection. + */ + QgsFields toFields() const; + private: int mSize = 0; QVector mAttributes; diff --git a/tests/src/core/testqgspointcloudattribute.cpp b/tests/src/core/testqgspointcloudattribute.cpp index a8d5ace0868..483249f4dd3 100644 --- a/tests/src/core/testqgspointcloudattribute.cpp +++ b/tests/src/core/testqgspointcloudattribute.cpp @@ -43,6 +43,7 @@ class TestQgsPointCloudAttribute: public QObject void testVariantType(); void testIsNumeric(); void testCollection(); + void testToFields(); private: @@ -187,5 +188,25 @@ void TestQgsPointCloudAttribute::testCollection() QCOMPARE( offset, 6 ); } +void TestQgsPointCloudAttribute::testToFields() +{ + QgsFields fields = QgsPointCloudAttributeCollection().toFields(); + QCOMPARE( fields.size(), 0 ); + + QgsPointCloudAttributeCollection collection( QVector< QgsPointCloudAttribute >() + << QgsPointCloudAttribute( QStringLiteral( "at1" ), QgsPointCloudAttribute::DataType::Float ) + << QgsPointCloudAttribute( QStringLiteral( "at2" ), QgsPointCloudAttribute::DataType::Short ) + << QgsPointCloudAttribute( QStringLiteral( "at3" ), QgsPointCloudAttribute::DataType::Double ) ); + fields = collection.toFields(); + QCOMPARE( fields.size(), 3 ); + + QCOMPARE( fields.at( 0 ).name(), QStringLiteral( "at1" ) ); + QCOMPARE( fields.at( 0 ).type(), QVariant::Double ); + QCOMPARE( fields.at( 1 ).name(), QStringLiteral( "at2" ) ); + QCOMPARE( fields.at( 1 ).type(), QVariant::Int ); + QCOMPARE( fields.at( 2 ).name(), QStringLiteral( "at3" ) ); + QCOMPARE( fields.at( 2 ).type(), QVariant::Double ); +} + QGSTEST_MAIN( TestQgsPointCloudAttribute ) #include "testqgspointcloudattribute.moc"