Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
with
46 additions
and
3 deletions.
-
+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 |
|
|
|
|
|