Skip to content
Permalink
Browse files

Add method to QgsGeometryFactory to create a multigeometry

from any wkb type

Eg calling QgsGeometryFactory::createCollectionOfType( QgsWkbTypes::PolygonM )
will return a new QgsMultiPolygonV2 with M values.
  • Loading branch information
nyalldawson committed Jun 14, 2017
1 parent f5f0a29 commit b620b6e00ef95fea1cd5d79411e94f4e638385ca
@@ -264,3 +264,39 @@ QgsAbstractGeometry *QgsGeometryFactory::geomFromWkbType( QgsWkbTypes::Type t )
return nullptr;
}
}

std::unique_ptr<QgsGeometryCollection> QgsGeometryFactory::createCollectionOfType( QgsWkbTypes::Type t )
{
QgsWkbTypes::Type type = QgsWkbTypes::flatType( QgsWkbTypes::multiType( t ) );
std::unique_ptr< QgsGeometryCollection > collect;
switch ( type )
{
case QgsWkbTypes::MultiPoint:
collect.reset( new QgsMultiPointV2() );
break;
case QgsWkbTypes::MultiLineString:
collect.reset( new QgsMultiLineString() );
break;
case QgsWkbTypes::MultiCurve:
collect.reset( new QgsMultiCurve() );
break;
case QgsWkbTypes::MultiPolygon:
collect.reset( new QgsMultiPolygonV2() );
break;
case QgsWkbTypes::MultiSurface:
collect.reset( new QgsMultiSurface() );
break;
case QgsWkbTypes::GeometryCollection:
collect.reset( new QgsGeometryCollection() );
break;
default:
// should not be possible
return nullptr;
}
if ( QgsWkbTypes::hasM( t ) )
collect->addMValue();
if ( QgsWkbTypes::hasZ( t ) )
collect->addZValue();

return collect;
}
@@ -20,11 +20,13 @@

#include "qgis_core.h"
#include <QString>
#include <memory>

class QgsAbstractGeometry;
class QgsLineString;
class QgsConstWkbPtr;
class QgsRectangle;
class QgsGeometryCollection;

//compatibility with old classes
#include "qgspointxy.h"
@@ -70,6 +72,12 @@ class CORE_EXPORT QgsGeometryFactory
//! Return empty geometry from wkb type
static QgsAbstractGeometry *geomFromWkbType( QgsWkbTypes::Type t );

/**
* Returns a new geometry collection matching a specified WKB \a type. For instance, if
* type is PolygonM the returned geometry will be a QgsMultiPolygonV2 with M values.
*/
static std::unique_ptr< QgsGeometryCollection > createCollectionOfType( QgsWkbTypes::Type type );

private:
static QgsLineString *linestringFromPolyline( const QgsPolyline &polyline );
};
@@ -127,6 +127,7 @@ class TestQgsGeometry : public QObject
void isSimple();

void reshapeGeometryLineMerge();
void createCollectionOfType();

private:
//! A helper method to do a render check to see if the geometry op is as expected
@@ -5540,5 +5541,38 @@ void TestQgsGeometry::reshapeGeometryLineMerge()
QCOMPARE( g3D_2.exportToWkt(), QString( "LineStringZ (-10 -10 -1, 10 10 1, 20 20 2)" ) );
}

void TestQgsGeometry::createCollectionOfType()
{
std::unique_ptr< QgsGeometryCollection > collect( QgsGeometryFactory::createCollectionOfType( QgsWkbTypes::Unknown ) );
QVERIFY( !collect );
collect = QgsGeometryFactory::createCollectionOfType( QgsWkbTypes::Point );
QCOMPARE( collect->wkbType(), QgsWkbTypes::MultiPoint );
QVERIFY( dynamic_cast< QgsMultiPointV2 *>( collect.get() ) );
collect = QgsGeometryFactory::createCollectionOfType( QgsWkbTypes::PointM );
QCOMPARE( collect->wkbType(), QgsWkbTypes::MultiPointM );
QVERIFY( dynamic_cast< QgsMultiPointV2 *>( collect.get() ) );
collect = QgsGeometryFactory::createCollectionOfType( QgsWkbTypes::PointZM );
QCOMPARE( collect->wkbType(), QgsWkbTypes::MultiPointZM );
QVERIFY( dynamic_cast< QgsMultiPointV2 *>( collect.get() ) );
collect = QgsGeometryFactory::createCollectionOfType( QgsWkbTypes::PointZ );
QCOMPARE( collect->wkbType(), QgsWkbTypes::MultiPointZ );
QVERIFY( dynamic_cast< QgsMultiPointV2 *>( collect.get() ) );
collect = QgsGeometryFactory::createCollectionOfType( QgsWkbTypes::MultiPoint );
QCOMPARE( collect->wkbType(), QgsWkbTypes::MultiPoint );
QVERIFY( dynamic_cast< QgsMultiPointV2 *>( collect.get() ) );
collect = QgsGeometryFactory::createCollectionOfType( QgsWkbTypes::LineStringZ );
QCOMPARE( collect->wkbType(), QgsWkbTypes::MultiLineStringZ );
QVERIFY( dynamic_cast< QgsMultiLineString *>( collect.get() ) );
collect = QgsGeometryFactory::createCollectionOfType( QgsWkbTypes::PolygonM );
QCOMPARE( collect->wkbType(), QgsWkbTypes::MultiPolygonM );
QVERIFY( dynamic_cast< QgsMultiPolygonV2 *>( collect.get() ) );
collect = QgsGeometryFactory::createCollectionOfType( QgsWkbTypes::GeometryCollectionZ );
QCOMPARE( collect->wkbType(), QgsWkbTypes::GeometryCollectionZ );
QVERIFY( dynamic_cast< QgsGeometryCollection *>( collect.get() ) );
collect = QgsGeometryFactory::createCollectionOfType( QgsWkbTypes::CurvePolygonM );
QCOMPARE( collect->wkbType(), QgsWkbTypes::MultiSurfaceM );
QVERIFY( dynamic_cast< QgsMultiSurface *>( collect.get() ) );
}

QGSTEST_MAIN( TestQgsGeometry )
#include "testqgsgeometry.moc"

0 comments on commit b620b6e

Please sign in to comment.
You can’t perform that action at this time.