Skip to content

Commit

Permalink
ogr virtual layers mix single and multi types in sublayers, fixed fea…
Browse files Browse the repository at this point in the history
…ture count, fixes #8001
  • Loading branch information
blazek committed Jun 6, 2013
1 parent bdb2a4f commit c8ff386
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/providers/ogr/qgsogrfeatureiterator.cpp
Expand Up @@ -218,7 +218,7 @@ bool QgsOgrFeatureIterator::readFeature( OGRFeatureH fet, QgsFeature& feature )
feature.setGeometryAndOwnership( wkb, OGR_G_WkbSize( geom ) );
}
if (( useIntersect && ( !feature.geometry() || !feature.geometry()->intersects( mRequest.filterRect() ) ) )
|| ( geometryTypeFilter && ( !feature.geometry() || wkbFlatten(( OGRwkbGeometryType )feature.geometry()->wkbType() ) != wkbFlatten( P->mOgrGeometryTypeFilter ) ) ) )
|| ( geometryTypeFilter && ( !feature.geometry() || QgsOgrProvider::ogrWkbSingleFlatten(( OGRwkbGeometryType )feature.geometry()->wkbType() ) != P->mOgrGeometryTypeFilter ) ) )
{
OGR_F_Destroy( fet );
return false;
Expand Down
42 changes: 28 additions & 14 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -544,32 +544,33 @@ QStringList QgsOgrProvider::subLayers() const
QMap<OGRwkbGeometryType, int> fCount;
// TODO: avoid reading attributes, setRelevantFields cannot be called here because it is not constant
//setRelevantFields( true, QgsAttributeList() );
OGR_L_ResetReading( ogrLayer );
OGR_L_ResetReading( layer );
OGRFeatureH fet;
while (( fet = OGR_L_GetNextFeature( ogrLayer ) ) )
while (( fet = OGR_L_GetNextFeature( layer ) ) )
{
if ( !fet ) continue;
OGRGeometryH geom = OGR_F_GetGeometryRef( fet );
if ( geom )
{
OGRwkbGeometryType gType = wkbFlatten( OGR_G_GetGeometryType( geom ) );
OGRwkbGeometryType gType = ogrWkbSingleFlatten( OGR_G_GetGeometryType( geom ) );
fCount[gType] = fCount.value( gType ) + 1;
}
OGR_F_Destroy( fet );
}
OGR_L_ResetReading( ogrLayer );
int i = 0;
if ( fCount.size() > 1 )
OGR_L_ResetReading( layer );
// it may happen that there are no features in the layer, in that case add unknown type
// to show to user that the layer exists but it is empty
if ( fCount.size() == 0 )
{
foreach ( OGRwkbGeometryType gType, fCount.keys() )
{
QString geom = ogrWkbGeometryTypeName( gType );
fCount[wkbUnknown] = 0;
}
foreach ( OGRwkbGeometryType gType, fCount.keys() )
{
QString geom = ogrWkbGeometryTypeName( gType );

QString sl = QString( "%1:%2:%3:%4" ).arg( i ).arg( theLayerName ).arg( fCount.value( gType ) ).arg( geom );
QgsDebugMsg( "sub layer: " + sl );
mSubLayerList << sl;
i++;
}
QString sl = QString( "%1:%2:%3:%4" ).arg( i ).arg( theLayerName ).arg( fCount.value( gType ) ).arg( geom );
QgsDebugMsg( "sub layer: " + sl );
mSubLayerList << sl;
}
}
}
Expand Down Expand Up @@ -2308,6 +2309,18 @@ void QgsOgrProvider::recalculateFeatureCount()
}
}

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

// ---------------------------------------------------------------------------

QGISEXTERN QgsVectorLayerImport::ImportError createEmptyLayer(
Expand All @@ -2325,3 +2338,4 @@ QGISEXTERN QgsVectorLayerImport::ImportError createEmptyLayer(
oldToNewAttrIdxMap, errorMessage, options
);
}

6 changes: 5 additions & 1 deletion src/providers/ogr/qgsogrprovider.h
Expand Up @@ -247,6 +247,9 @@ class QgsOgrProvider : public QgsVectorDataProvider
/** return OGR geometry type */
static int getOgrGeomType( OGRLayerH ogrLayer );

/** Get single flatten geometry type */
static OGRwkbGeometryType ogrWkbSingleFlatten( OGRwkbGeometryType type );

protected:
/** loads fields from input file to member attributeFields */
void loadFields();
Expand Down Expand Up @@ -284,7 +287,8 @@ class QgsOgrProvider : public QgsVectorDataProvider
int mLayerIndex;

/** Optional geometry type for layers with multiple geometries,
* otherwise wkbUnknown */
* otherwise wkbUnknown. This type is always flatten (2D) and single, it means
* that 2D, 25D, single and multi types are mixed in one sublayer */
OGRwkbGeometryType mOgrGeometryTypeFilter;

//! current spatial filter
Expand Down

0 comments on commit c8ff386

Please sign in to comment.