Skip to content

Commit

Permalink
Handle geometrycollections also in difference results
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Nov 2, 2020
1 parent d6247fd commit b9b6171
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/analysis/processing/qgsoverlayutils.cpp
Expand Up @@ -54,14 +54,22 @@ bool QgsOverlayUtils::sanitizeIntersectionResult( QgsGeometry &geom, QgsWkbTypes


//! Makes sure that what came out from difference of two geometries is good to be used in the output
static bool sanitizeDifferenceResult( QgsGeometry &geom )
static bool sanitizeDifferenceResult( QgsGeometry &geom, QgsWkbTypes::GeometryType geometryType )
{
if ( geom.isNull() )
{
// TODO: not sure if this ever happens - if it does, that means GEOS failed badly - would be good to have a test for such situation
throw QgsProcessingException( QStringLiteral( "%1\n\n%2" ).arg( QObject::tr( "GEOS geoprocessing error: difference failed." ), geom.lastError() ) );
}

//fix geometry collections
if ( QgsWkbTypes::flatType( geom.wkbType() ) == QgsWkbTypes::GeometryCollection )
{
// try to filter out irrelevant parts with different geometry type than what we want
geom.convertGeometryCollectionToSubclass( geometryType );
}


// if geomB covers the whole source geometry, we get an empty geometry collection
if ( geom.isEmpty() )
return false;
Expand All @@ -76,6 +84,7 @@ static bool sanitizeDifferenceResult( QgsGeometry &geom )

void QgsOverlayUtils::difference( const QgsFeatureSource &sourceA, const QgsFeatureSource &sourceB, QgsFeatureSink &sink, QgsProcessingContext &context, QgsProcessingFeedback *feedback, int &count, int totalCount, QgsOverlayUtils::DifferenceOutput outputAttrs )
{
QgsWkbTypes::GeometryType geometryType = QgsWkbTypes::geometryType( QgsWkbTypes::multiType( sourceA.wkbType() ) );
QgsFeatureRequest requestB;
requestB.setNoAttributes();
if ( outputAttrs != OutputBA )
Expand Down Expand Up @@ -147,7 +156,7 @@ void QgsOverlayUtils::difference( const QgsFeatureSource &sourceA, const QgsFeat
geom = geom.difference( geomB );
}

if ( !sanitizeDifferenceResult( geom ) )
if ( !sanitizeDifferenceResult( geom, geometryType ) )
continue;

const QgsAttributes attrsA( featA.attributes() );
Expand Down Expand Up @@ -369,7 +378,7 @@ void QgsOverlayUtils::resolveOverlaps( const QgsFeatureSource &source, QgsFeatur
index.deleteFeature( f );
geometries.remove( fid1 );

if ( sanitizeDifferenceResult( g12 ) )
if ( sanitizeDifferenceResult( g12, geometryType ) )
{
geometries.insert( fid1, g12 );

Expand All @@ -390,7 +399,7 @@ void QgsOverlayUtils::resolveOverlaps( const QgsFeatureSource &source, QgsFeatur

geometries.remove( fid2 );

if ( sanitizeDifferenceResult( g21 ) )
if ( sanitizeDifferenceResult( g21, geometryType ) )
{
geometries.insert( fid2, g21 );

Expand Down

0 comments on commit b9b6171

Please sign in to comment.