Skip to content

Commit

Permalink
Direct conversion from ogr multilinestrings to QgsGeometry
Browse files Browse the repository at this point in the history
Avoid expense of converting to/from wkb
  • Loading branch information
nyalldawson committed Jul 23, 2019
1 parent 2cd5334 commit a0aa36c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/core/qgsogrutils.cpp
Expand Up @@ -20,6 +20,7 @@
#include "qgsfields.h"
#include "qgslinestring.h"
#include "qgsmultipoint.h"
#include "qgsmultilinestring.h"
#include <QTextCodec>
#include <QUuid>
#include <cpl_error.h>
Expand Down Expand Up @@ -382,6 +383,19 @@ std::unique_ptr< QgsLineString > ogrGeometryToQgsLineString( OGRGeometryH geom )
return qgis::make_unique< QgsLineString>( x, y, z, m, wkbType == QgsWkbTypes::LineString25D );
}

std::unique_ptr< QgsMultiLineString > ogrGeometryToQgsMultiLineString( OGRGeometryH geom )
{
std::unique_ptr< QgsMultiLineString > mp = qgis::make_unique< QgsMultiLineString >();

const int count = OGR_G_GetGeometryCount( geom );
for ( int i = 0; i < count; ++i )
{
mp->addGeometry( ogrGeometryToQgsLineString( OGR_G_GetGeometryRef( geom, i ) ).release() );
}

return mp;
}

QgsWkbTypes::Type QgsOgrUtils::ogrGeometryTypeToQgsWkbType( OGRwkbGeometryType ogrGeomType )
{
switch ( ogrGeomType )
Expand Down Expand Up @@ -496,6 +510,12 @@ QgsGeometry QgsOgrUtils::ogrGeometryToQgsGeometry( OGRGeometryH geom )
return QgsGeometry( ogrGeometryToQgsLineString( geom ) );
}

case QgsWkbTypes::MultiLineString:
{
// optimised case for line -- avoid wkb conversion
return QgsGeometry( ogrGeometryToQgsMultiLineString( geom ) );
}

default:
break;
};
Expand Down
6 changes: 6 additions & 0 deletions tests/src/core/testqgsogrutils.cpp
Expand Up @@ -178,6 +178,12 @@ void TestQgsOgrUtils::ogrGeometryToQgsGeometry2_data()
QTest::newRow( "linestringm" ) << QStringLiteral( "LineStringM (1.1 2.2 3.3, 4.4 5.5 6.6)" ) << static_cast< int >( QgsWkbTypes::LineStringM );
QTest::newRow( "linestringzm" ) << QStringLiteral( "LineStringZM (1.1 2.2 3.3 4.4, 5.5 6.6 7.7 8.8)" ) << static_cast< int >( QgsWkbTypes::LineStringZM );
QTest::newRow( "linestring25d" ) << QStringLiteral( "LineString25D (1.1 2.2 3.3, 4.4 5.5 6.6)" ) << static_cast< int >( QgsWkbTypes::LineString25D );

QTest::newRow( "linestring" ) << QStringLiteral( "MultiLineString ((1.1 2.2, 3.3 4.4))" ) << static_cast< int >( QgsWkbTypes::MultiLineString );
QTest::newRow( "linestring" ) << QStringLiteral( "MultiLineString ((1.1 2.2, 3.3 4.4),(5 5, 6 6))" ) << static_cast< int >( QgsWkbTypes::MultiLineString );
QTest::newRow( "linestring" ) << QStringLiteral( "MultiLineStringZ ((1.1 2.2 3, 3.3 4.4 6),(5 5 3, 6 6 1))" ) << static_cast< int >( QgsWkbTypes::MultiLineStringZ );
QTest::newRow( "linestring" ) << QStringLiteral( "MultiLineStringM ((1.1 2.2 4, 3.3 4.4 7),(5 5 4, 6 6 2))" ) << static_cast< int >( QgsWkbTypes::MultiLineStringM );
QTest::newRow( "linestring" ) << QStringLiteral( "MultiLineStringZM ((1.1 2.2 4 5, 3.3 4.4 8 9),(5 5 7 1, 6 6 2 3))" ) << static_cast< int >( QgsWkbTypes::MultiLineStringZM );
}

void TestQgsOgrUtils::ogrGeometryToQgsGeometry2()
Expand Down

0 comments on commit a0aa36c

Please sign in to comment.