Skip to content

Commit

Permalink
Add optimised conversion methods for ogr (multi)polygons to QgsGeometry
Browse files Browse the repository at this point in the history
Avoids conversion back and forth from wkb to transfer these geometries
from ogr to qgis
  • Loading branch information
nyalldawson committed Mar 11, 2021
1 parent 43d5b0c commit c578658
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions src/core/qgsogrutils.cpp
Expand Up @@ -23,6 +23,9 @@
#include "qgsmultilinestring.h"
#include "qgsogrprovider.h"
#include "qgslinesymbollayer.h"
#include "qgspolygon.h"
#include "qgsmultipolygon.h"

#include <QTextCodec>
#include <QUuid>
#include <cpl_error.h>
Expand Down Expand Up @@ -427,6 +430,38 @@ std::unique_ptr< QgsMultiLineString > ogrGeometryToQgsMultiLineString( OGRGeomet
return mp;
}

std::unique_ptr< QgsPolygon > ogrGeometryToQgsPolygon( OGRGeometryH geom )
{
std::unique_ptr< QgsPolygon > polygon = std::make_unique< QgsPolygon >();

const int count = OGR_G_GetGeometryCount( geom );
if ( count >= 1 )
{
polygon->setExteriorRing( ogrGeometryToQgsLineString( OGR_G_GetGeometryRef( geom, 0 ) ).release() );
}

for ( int i = 1; i < count; ++i )
{
polygon->addInteriorRing( ogrGeometryToQgsLineString( OGR_G_GetGeometryRef( geom, i ) ).release() );
}

return polygon;
}

std::unique_ptr< QgsMultiPolygon > ogrGeometryToQgsMultiPolygon( OGRGeometryH geom )
{
std::unique_ptr< QgsMultiPolygon > polygon = std::make_unique< QgsMultiPolygon >();

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

return polygon;
}

QgsWkbTypes::Type QgsOgrUtils::ogrGeometryTypeToQgsWkbType( OGRwkbGeometryType ogrGeomType )
{
switch ( ogrGeomType )
Expand Down Expand Up @@ -537,19 +572,27 @@ QgsGeometry QgsOgrUtils::ogrGeometryToQgsGeometry( OGRGeometryH geom )

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

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

case QgsWkbTypes::Polygon:
{
return QgsGeometry( ogrGeometryToQgsPolygon( geom ) );
}

case QgsWkbTypes::MultiPolygon:
{
return QgsGeometry( ogrGeometryToQgsMultiPolygon( geom ) );
}

default:
break;
};
}

// Fallback to inefficient WKB conversions

Expand Down

0 comments on commit c578658

Please sign in to comment.