Skip to content

Commit

Permalink
Add method to convert QgsPointCloudAttributeCollection to QgsFields
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 18, 2021
1 parent d6c0b2f commit 78cb67a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
Expand Up @@ -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

};
Expand Down
10 changes: 10 additions & 0 deletions src/core/pointcloud/qgspointcloudattribute.cpp
Expand Up @@ -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 )
{
Expand Down
6 changes: 6 additions & 0 deletions src/core/pointcloud/qgspointcloudattribute.h
Expand Up @@ -20,6 +20,7 @@

#include "qgis.h"
#include "qgis_core.h"
#include "qgsfields.h"
#include <QString>
#include <QVector>

Expand Down Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions tests/src/core/testqgspointcloudattribute.cpp
Expand Up @@ -43,6 +43,7 @@ class TestQgsPointCloudAttribute: public QObject
void testVariantType();
void testIsNumeric();
void testCollection();
void testToFields();

private:

Expand Down Expand Up @@ -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"

0 comments on commit 78cb67a

Please sign in to comment.