Add method to convert QgsPointCloudAttributeCollection to QgsFields

This commit is contained in:
Nyall Dawson 2021-01-18 10:52:11 +10:00
parent d6c0b2f0a8
commit 78cb67a95d
4 changed files with 42 additions and 0 deletions

View File

@ -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
};

View File

@ -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 <typename T>
void _attribute( const char *data, std::size_t offset, QgsPointCloudAttribute::DataType type, T &value )
{

View File

@ -20,6 +20,7 @@
#include "qgis.h"
#include "qgis_core.h"
#include "qgsfields.h"
#include <QString>
#include <QVector>
@ -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<QgsPointCloudAttribute> mAttributes;

View File

@ -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"