Skip to content
Permalink
Browse files
Move some more useful methods to QgsOgrProviderUtils
  • Loading branch information
nyalldawson committed Jun 21, 2021
1 parent b422306 commit dd317900c47a934e58934cd45e9dba20a20ed22e
@@ -539,7 +539,7 @@ bool QgsOgrFeatureIterator::readFeature( const gdal::ogr_feature_unique_ptr &fet
{
// OK
}
else if ( ( geometryTypeFilter && ( !feature.hasGeometry() || QgsOgrProvider::ogrWkbSingleFlatten( ( OGRwkbGeometryType )feature.geometry().wkbType() ) != mSource->mOgrGeometryTypeFilter ) )
else if ( ( geometryTypeFilter && ( !feature.hasGeometry() || QgsOgrProviderUtils::ogrWkbSingleFlatten( ( OGRwkbGeometryType )feature.geometry().wkbType() ) != mSource->mOgrGeometryTypeFilter ) )
|| ( useIntersect && ( !feature.hasGeometry()
|| ( mRequest.flags() & QgsFeatureRequest::ExactIntersect && !feature.geometry().intersects( mFilterRect ) )
|| ( !( mRequest.flags() & QgsFeatureRequest::ExactIntersect ) && !feature.geometry().boundingBoxIntersects( mFilterRect ) )
@@ -594,7 +594,7 @@ QgsOgrFeatureSource::QgsOgrFeatureSource( const QgsOgrProvider *p )
, mEncoding( p->textEncoding() ) // no copying - this is a borrowed pointer from Qt
, mFields( p->mAttributeFields )
, mFirstFieldIsFid( p->mFirstFieldIsFid )
, mOgrGeometryTypeFilter( QgsOgrProvider::ogrWkbSingleFlatten( p->mOgrGeometryTypeFilter ) )
, mOgrGeometryTypeFilter( QgsOgrProviderUtils::ogrWkbSingleFlatten( p->mOgrGeometryTypeFilter ) )
, mDriverName( p->mGDALDriverName )
, mCrs( p->crs() )
, mWkbType( p->wkbType() )
@@ -561,111 +561,6 @@ QString QgsOgrProvider::subsetString() const
return mSubsetString;
}

QString QgsOgrProvider::ogrWkbGeometryTypeName( OGRwkbGeometryType type ) const
{
QString geom;

// GDAL 2.1 can return M/ZM geometries
if ( wkbHasM( type ) )
{
geom = ogrWkbGeometryTypeName( wkbFlatten( type ) );
if ( wkbHasZ( type ) )
geom += QLatin1Char( 'Z' );
if ( wkbHasM( type ) )
geom += QLatin1Char( 'M' );
return geom;
}

switch ( static_cast<unsigned>( type ) )
{
case wkbUnknown:
geom = QStringLiteral( "Unknown" );
break;
case wkbPoint:
geom = QStringLiteral( "Point" );
break;
case wkbLineString:
geom = QStringLiteral( "LineString" );
break;
case wkbPolygon:
geom = QStringLiteral( "Polygon" );
break;
case wkbMultiPoint:
geom = QStringLiteral( "MultiPoint" );
break;
case wkbMultiLineString:
geom = QStringLiteral( "MultiLineString" );
break;
case wkbMultiPolygon:
geom = QStringLiteral( "MultiPolygon" );
break;
case wkbGeometryCollection:
geom = QStringLiteral( "GeometryCollection" );
break;
case wkbCircularString:
geom = QStringLiteral( "CircularString" );
break;
case wkbCompoundCurve:
geom = QStringLiteral( "CompoundCurve" );
break;
case wkbCurvePolygon:
geom = QStringLiteral( "CurvePolygon" );
break;
case wkbMultiCurve:
geom = QStringLiteral( "MultiCurve" );
break;
case wkbMultiSurface:
geom = QStringLiteral( "MultiSurface" );
break;
case wkbCircularStringZ:
geom = QStringLiteral( "CircularStringZ" );
break;
case wkbCompoundCurveZ:
geom = QStringLiteral( "CompoundCurveZ" );
break;
case wkbCurvePolygonZ:
geom = QStringLiteral( "CurvePolygonZ" );
break;
case wkbMultiCurveZ:
geom = QStringLiteral( "MultiCurveZ" );
break;
case wkbMultiSurfaceZ:
geom = QStringLiteral( "MultiSurfaceZ" );
break;
case wkbNone:
geom = QStringLiteral( "None" );
break;
case static_cast<unsigned>( wkbUnknown ) | static_cast<unsigned>( wkb25DBit ):
geom = QStringLiteral( "Unknown25D" );
break;
case static_cast<unsigned>( wkbPoint25D ):
geom = QStringLiteral( "Point25D" );
break;
case static_cast<unsigned>( wkbLineString25D ):
geom = QStringLiteral( "LineString25D" );
break;
case static_cast<unsigned>( wkbPolygon25D ):
geom = QStringLiteral( "Polygon25D" );
break;
case static_cast<unsigned>( wkbMultiPoint25D ):
geom = QStringLiteral( "MultiPoint25D" );
break;
case static_cast<unsigned>( wkbMultiLineString25D ):
geom = QStringLiteral( "MultiLineString25D" );
break;
case static_cast<unsigned>( wkbMultiPolygon25D ):
geom = QStringLiteral( "MultiPolygon25D" );
break;
case static_cast<unsigned>( wkbGeometryCollection25D ):
geom = QStringLiteral( "GeometryCollection25D" );
break;
default:
// Do not use ':', as it will mess with the separator used by QgsSublayersDialog::populateLayers()
geom = QStringLiteral( "Unknown WKB (%1)" ).arg( type );
}
return geom;
}

void QgsOgrProvider::addSubLayerDetailsToSubLayerList( int i, QgsOgrLayer *layer, bool withFeatureCount ) const
{
QString layerName = QString::fromUtf8( layer->name() );
@@ -706,7 +601,7 @@ void QgsOgrProvider::addSubLayerDetailsToSubLayerList( int i, QgsOgrLayer *layer
{
long long layerFeatureCount = withFeatureCount ? layer->GetApproxFeatureCount() : -1;

QString geom = ogrWkbGeometryTypeName( layerGeomType );
QString geom = QgsOgrProviderUtils::ogrWkbGeometryTypeName( layerGeomType );

// For feature count, -1 indicates an unknown count state
QStringList parts = QStringList()
@@ -734,7 +629,7 @@ void QgsOgrProvider::addSubLayerDetailsToSubLayerList( int i, QgsOgrLayer *layer
OGRGeometryH geom = OGR_F_GetGeometryRef( fet.get() );
if ( geom )
{
OGRwkbGeometryType gType = ogrWkbSingleFlatten( OGR_G_GetGeometryType( geom ) );
OGRwkbGeometryType gType = QgsOgrProviderUtils::ogrWkbSingleFlatten( OGR_G_GetGeometryType( geom ) );
fCount[gType] = fCount.value( gType ) + 1;
}
}
@@ -779,7 +674,7 @@ void QgsOgrProvider::addSubLayerDetailsToSubLayerList( int i, QgsOgrLayer *layer
QMap<OGRwkbGeometryType, int>::const_iterator countIt = fCount.constBegin();
for ( ; countIt != fCount.constEnd(); ++countIt )
{
QString geom = ogrWkbGeometryTypeName( ( bIs25D ) ? wkbSetZ( countIt.key() ) : countIt.key() );
QString geom = QgsOgrProviderUtils::ogrWkbGeometryTypeName( ( bIs25D ) ? wkbSetZ( countIt.key() ) : countIt.key() );

QStringList parts = QStringList()
<< QString::number( i )
@@ -2460,7 +2355,7 @@ bool QgsOgrProvider::_setSubsetString( const QString &theSQL, bool updateFeature

if ( mOgrGeometryTypeFilter != wkbUnknown )
{
parts.insert( QStringLiteral( "geometryType" ), ogrWkbGeometryTypeName( mOgrGeometryTypeFilter ) );
parts.insert( QStringLiteral( "geometryType" ), QgsOgrProviderUtils::ogrWkbGeometryTypeName( mOgrGeometryTypeFilter ) );
}

if ( !mOpenOptions.isEmpty() )
@@ -3585,14 +3480,14 @@ void QgsOgrProvider::recalculateFeatureCount() const
mOgrLayer->ResetReading();
gdal::ogr_feature_unique_ptr fet;
const OGRwkbGeometryType flattenGeomTypeFilter =
QgsOgrProvider::ogrWkbSingleFlatten( mOgrGeometryTypeFilter );
QgsOgrProviderUtils::ogrWkbSingleFlatten( mOgrGeometryTypeFilter );
while ( fet.reset( mOgrLayer->GetNextFeature() ), fet )
{
OGRGeometryH geom = OGR_F_GetGeometryRef( fet.get() );
if ( geom )
{
OGRwkbGeometryType gType = OGR_G_GetGeometryType( geom );
gType = QgsOgrProvider::ogrWkbSingleFlatten( gType );
gType = QgsOgrProviderUtils::ogrWkbSingleFlatten( gType );
if ( gType == flattenGeomTypeFilter ) mFeaturesCounted++;
}
}
@@ -3623,26 +3518,6 @@ QgsFeatureRenderer *QgsOgrProvider::createRenderer( const QVariantMap & ) const
return new QgsEmbeddedSymbolRenderer( defaultSymbol.release() );
}

OGRwkbGeometryType QgsOgrProvider::ogrWkbSingleFlatten( OGRwkbGeometryType type )
{
type = wkbFlatten( type );
switch ( type )
{
case wkbMultiPoint:
return wkbPoint;
case wkbMultiLineString:
return wkbLineString;
case wkbMultiPolygon:
return wkbPolygon;
case wkbMultiCurve:
return wkbCompoundCurve;
case wkbMultiSurface:
return wkbCurvePolygon;
default:
return type;
}
}

void QgsOgrProvider::open( OpenMode mode )
{
bool openReadOnly = false;
@@ -135,9 +135,6 @@ class QgsOgrProvider final: public QgsVectorDataProvider
//! Returns OGR geometry type
static OGRwkbGeometryType getOgrGeomType( const QString &driverName, OGRLayerH ogrLayer );

//! Gets single flatten geometry type
static OGRwkbGeometryType ogrWkbSingleFlatten( OGRwkbGeometryType type );

QString layerName() const { return mLayerName; }

QString filePath() const { return mFilePath; }
@@ -186,7 +183,7 @@ class QgsOgrProvider final: public QgsVectorDataProvider

private:
unsigned char *getGeometryPointer( OGRFeatureH fet );
QString ogrWkbGeometryTypeName( OGRwkbGeometryType type ) const;

static QString createIndexName( QString tableName, QString field );

//! Starts a transaction if possible and return true in that case
@@ -1897,6 +1897,131 @@ OGRwkbGeometryType QgsOgrProviderUtils::ogrWkbGeometryTypeFromName( const QStrin
return wkbUnknown;
}

OGRwkbGeometryType QgsOgrProviderUtils::ogrWkbSingleFlatten( OGRwkbGeometryType type )
{
type = wkbFlatten( type );
switch ( type )
{
case wkbMultiPoint:
return wkbPoint;
case wkbMultiLineString:
return wkbLineString;
case wkbMultiPolygon:
return wkbPolygon;
case wkbMultiCurve:
return wkbCompoundCurve;
case wkbMultiSurface:
return wkbCurvePolygon;
default:
return type;
}
}

QString QgsOgrProviderUtils::ogrWkbGeometryTypeName( OGRwkbGeometryType type )
{
QString geom;

// GDAL 2.1 can return M/ZM geometries
if ( wkbHasM( type ) )
{
geom = ogrWkbGeometryTypeName( wkbFlatten( type ) );
if ( wkbHasZ( type ) )
geom += QLatin1Char( 'Z' );
if ( wkbHasM( type ) )
geom += QLatin1Char( 'M' );
return geom;
}

switch ( static_cast<unsigned>( type ) )
{
case wkbUnknown:
geom = QStringLiteral( "Unknown" );
break;
case wkbPoint:
geom = QStringLiteral( "Point" );
break;
case wkbLineString:
geom = QStringLiteral( "LineString" );
break;
case wkbPolygon:
geom = QStringLiteral( "Polygon" );
break;
case wkbMultiPoint:
geom = QStringLiteral( "MultiPoint" );
break;
case wkbMultiLineString:
geom = QStringLiteral( "MultiLineString" );
break;
case wkbMultiPolygon:
geom = QStringLiteral( "MultiPolygon" );
break;
case wkbGeometryCollection:
geom = QStringLiteral( "GeometryCollection" );
break;
case wkbCircularString:
geom = QStringLiteral( "CircularString" );
break;
case wkbCompoundCurve:
geom = QStringLiteral( "CompoundCurve" );
break;
case wkbCurvePolygon:
geom = QStringLiteral( "CurvePolygon" );
break;
case wkbMultiCurve:
geom = QStringLiteral( "MultiCurve" );
break;
case wkbMultiSurface:
geom = QStringLiteral( "MultiSurface" );
break;
case wkbCircularStringZ:
geom = QStringLiteral( "CircularStringZ" );
break;
case wkbCompoundCurveZ:
geom = QStringLiteral( "CompoundCurveZ" );
break;
case wkbCurvePolygonZ:
geom = QStringLiteral( "CurvePolygonZ" );
break;
case wkbMultiCurveZ:
geom = QStringLiteral( "MultiCurveZ" );
break;
case wkbMultiSurfaceZ:
geom = QStringLiteral( "MultiSurfaceZ" );
break;
case wkbNone:
geom = QStringLiteral( "None" );
break;
case static_cast<unsigned>( wkbUnknown ) | static_cast<unsigned>( wkb25DBit ):
geom = QStringLiteral( "Unknown25D" );
break;
case static_cast<unsigned>( wkbPoint25D ):
geom = QStringLiteral( "Point25D" );
break;
case static_cast<unsigned>( wkbLineString25D ):
geom = QStringLiteral( "LineString25D" );
break;
case static_cast<unsigned>( wkbPolygon25D ):
geom = QStringLiteral( "Polygon25D" );
break;
case static_cast<unsigned>( wkbMultiPoint25D ):
geom = QStringLiteral( "MultiPoint25D" );
break;
case static_cast<unsigned>( wkbMultiLineString25D ):
geom = QStringLiteral( "MultiLineString25D" );
break;
case static_cast<unsigned>( wkbMultiPolygon25D ):
geom = QStringLiteral( "MultiPolygon25D" );
break;
case static_cast<unsigned>( wkbGeometryCollection25D ):
geom = QStringLiteral( "GeometryCollection25D" );
break;
default:
// Do not use ':', as it will mess with the separator used by QgsSublayersDialog::populateLayers()
geom = QStringLiteral( "Unknown WKB (%1)" ).arg( type );
}
return geom;
}

QString QgsOgrProviderUtils::expandAuthConfig( const QString &dsName )
{
QString uri( dsName );
@@ -235,6 +235,11 @@ class CORE_EXPORT QgsOgrProviderUtils
//! Conerts a string to an OGR WKB geometry type
static OGRwkbGeometryType ogrWkbGeometryTypeFromName( const QString &typeName );

//! Gets single flatten geometry type
static OGRwkbGeometryType ogrWkbSingleFlatten( OGRwkbGeometryType type );

static QString ogrWkbGeometryTypeName( OGRwkbGeometryType type );

static QString analyzeURI( QString const &uri,
bool &isSubLayer,
int &layerIndex,

0 comments on commit dd31790

Please sign in to comment.