Skip to content
Permalink
Browse files

Add optimised conversion methods for ogr (multi)polygons to QgsGeometry

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 c578658d177b895d64fab51a1d75cc187fc4ae6a
Showing with 46 additions and 3 deletions.
  1. +46 −3 src/core/qgsogrutils.cpp
@@ -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>
@@ -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 )
@@ -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

0 comments on commit c578658

Please sign in to comment.