Skip to content

Commit 45b264d

Browse files
committed
Fixes after review
1 parent 4f829fd commit 45b264d

File tree

9 files changed

+103
-254
lines changed

9 files changed

+103
-254
lines changed

python/core/geometry/qgsgeometry.sip.in

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,18 @@ If it is already a single part geometry, it will return true and
13391339
not change the geometry.
13401340

13411341
:return: true in case of success and false else
1342+
%End
1343+
1344+
bool convertGeometryCollectionToSubclass( QgsWkbTypes::GeometryType geomType );
1345+
%Docstring
1346+
Converts geometry collection to a the desired geometry type subclass (multi-point,
1347+
multi-linestring or multi-polygon). Child geometries of different type are filtered out.
1348+
Does nothing the geometry is not a geometry collection. May leave the geometry
1349+
empty if none of the child geometries match the desired type.
1350+
1351+
:return: true in case of success and false else
1352+
1353+
.. versionadded:: 3.2
13421354
%End
13431355

13441356
int avoidIntersections( const QList<QgsVectorLayer *> &avoidIntersectionsLayers );

python/plugins/processing/algs/help/qgis.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,6 @@ qgis:importintopostgis: >
226226

227227
Prior to this a connection between QGIS and the PostGIS database has to be created (for example with the DB Manager).
228228

229-
qgis:intersection: >
230-
This algorithm extracts the overlapping portions of features in the Input and Intersect layers. Features in the Intersection layer are assigned the attributes of the overlapping features from both the Input and Intersect layers.
231-
232-
Attributes are not modified.
233-
234229
qgis:joinattributesbylocation: >
235230
This algorithm takes an input vector layer and creates a new vector layer that is an extended version of the input one, with additional attributes in its attribute table.
236231

python/plugins/processing/algs/qgis/Intersection.py

Lines changed: 0 additions & 196 deletions
This file was deleted.

python/plugins/processing/algs/qgis/QgisAlgorithmProvider.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
from .IdwInterpolation import IdwInterpolation
8080
from .ImportIntoPostGIS import ImportIntoPostGIS
8181
from .ImportIntoSpatialite import ImportIntoSpatialite
82-
from .Intersection import Intersection
8382
from .KeepNBiggestParts import KeepNBiggestParts
8483
from .LinesToPolygons import LinesToPolygons
8584
from .MinimumBoundingGeometry import MinimumBoundingGeometry
@@ -198,7 +197,6 @@ def getAlgs(self):
198197
IdwInterpolation(),
199198
ImportIntoPostGIS(),
200199
ImportIntoSpatialite(),
201-
Intersection(),
202200
KeepNBiggestParts(),
203201
LinesToPolygons(),
204202
MinimumBoundingGeometry(),

src/analysis/processing/qgsalgorithmintersection.cpp

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,8 @@
1818
#include "qgsgeometrycollection.h"
1919
#include "qgsgeometryengine.h"
2020

21-
#include "qgsmultipoint.h"
22-
#include "qgsmultipolygon.h"
23-
#include "qgsmultilinestring.h"
24-
2521
///@cond PRIVATE
2622

27-
static QgsGeometry geometryCollectionToSingleGeometryType( const QgsGeometry &geom, QgsWkbTypes::GeometryType geomType )
28-
{
29-
const QgsGeometryCollection *origGeom = qgsgeometry_cast<const QgsGeometryCollection *>( geom.constGet() );
30-
if ( !origGeom )
31-
return QgsGeometry();
32-
33-
QgsGeometryCollection *resGeom = nullptr;
34-
switch ( geomType )
35-
{
36-
case QgsWkbTypes::PointGeometry:
37-
resGeom = new QgsMultiPoint;
38-
break;
39-
case QgsWkbTypes::LineGeometry:
40-
resGeom = new QgsMultiLineString;
41-
break;
42-
case QgsWkbTypes::PolygonGeometry:
43-
resGeom = new QgsMultiPolygon;
44-
break;
45-
default:
46-
break;
47-
}
48-
if ( !resGeom )
49-
return QgsGeometry();
50-
51-
for ( int i = 0; i < origGeom->numGeometries(); ++i )
52-
{
53-
const QgsAbstractGeometry *g = origGeom->geometryN( i );
54-
if ( QgsWkbTypes::geometryType( g->wkbType() ) == geomType )
55-
resGeom->addGeometry( g->clone() );
56-
}
57-
return QgsGeometry( resGeom );
58-
}
59-
6023

6124
QString QgsIntersectionAlgorithm::name() const
6225
{
@@ -78,6 +41,12 @@ QString QgsIntersectionAlgorithm::groupId() const
7841
return QStringLiteral( "vectoroverlay" );
7942
}
8043

44+
QString QgsIntersectionAlgorithm::shortHelpString() const
45+
{
46+
return QObject::tr( "This algorithm extracts the overlapping portions of features in the Input and Overlay layers. Features in the Overlay layer are assigned the attributes of the overlapping features from both the Input and Overlay layers." )
47+
+ QStringLiteral( "\n\n" )
48+
+ QObject::tr( "Optionally, the rotation can occur around a preset point. If not set the rotation occurs around each feature's centroid." );
49+
}
8150

8251
QgsProcessingAlgorithm *QgsIntersectionAlgorithm::createInstance() const
8352
{
@@ -225,27 +194,25 @@ QVariantMap QgsIntersectionAlgorithm::processAlgorithm( const QVariantMap &param
225194

226195
if ( intGeom.isNull() )
227196
{
228-
// TODO: not sure if this ever happens - if it does, that means GEOS failed badly and we should try to provide an error from it
229-
throw QgsProcessingException( QObject::tr( "GEOS geoprocessing error: intersection failed." ) );
197+
// 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
198+
throw QgsProcessingException( QStringLiteral( "%1\n\n%2" ).arg( QObject::tr( "GEOS geoprocessing error: intersection failed." ), intGeom.lastError() ) );
230199
}
231200

232201
// Intersection of geometries may give use also geometries we do not want in our results.
233202
// For example, two square polygons touching at the corner have a point as the intersection, but no area.
234203
// In other cases we may get a mixture of geometries in the output - we want to keep only the expected types.
235-
if ( QgsWkbTypes::geometryType( intGeom.wkbType() ) != QgsWkbTypes::geometryType( geomType ) )
204+
if ( QgsWkbTypes::flatType( intGeom.wkbType() ) == QgsWkbTypes::GeometryCollection )
236205
{
237-
if ( QgsWkbTypes::flatType( intGeom.wkbType() ) == QgsWkbTypes::GeometryCollection )
238-
{
239-
// try to filter out irrelevant parts with different geometry type than what we want
240-
intGeom = geometryCollectionToSingleGeometryType( intGeom, QgsWkbTypes::geometryType( geomType ) );
241-
if ( intGeom.isEmpty() )
242-
continue;
243-
}
244-
else
245-
{
246-
// we can't make use of this resulting geometry -
206+
// try to filter out irrelevant parts with different geometry type than what we want
207+
intGeom.convertGeometryCollectionToSubclass( QgsWkbTypes::geometryType( geomType ) );
208+
if ( intGeom.isEmpty() )
247209
continue;
248-
}
210+
}
211+
212+
if ( QgsWkbTypes::geometryType( intGeom.wkbType() ) != QgsWkbTypes::geometryType( geomType ) )
213+
{
214+
// we can't make use of this resulting geometry
215+
continue;
249216
}
250217

251218
const QgsAttributes attrsB( featB.attributes() );

src/analysis/processing/qgsalgorithmintersection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class QgsIntersectionAlgorithm : public QgsProcessingAlgorithm
3131
virtual QString displayName() const override;
3232
virtual QString group() const override;
3333
virtual QString groupId() const override;
34+
QString shortHelpString() const override;
3435

3536
protected:
3637
virtual QgsProcessingAlgorithm *createInstance() const override;

src/core/geometry/qgsgeometry.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,43 @@ bool QgsGeometry::convertToSingleType()
13151315
return true;
13161316
}
13171317

1318+
1319+
bool QgsGeometry::convertGeometryCollectionToSubclass( QgsWkbTypes::GeometryType geomType )
1320+
{
1321+
const QgsGeometryCollection *origGeom = qgsgeometry_cast<const QgsGeometryCollection *>( constGet() );
1322+
if ( !origGeom )
1323+
return false;
1324+
1325+
std::unique_ptr<QgsGeometryCollection> resGeom;
1326+
switch ( geomType )
1327+
{
1328+
case QgsWkbTypes::PointGeometry:
1329+
resGeom = qgis::make_unique<QgsMultiPoint>();
1330+
break;
1331+
case QgsWkbTypes::LineGeometry:
1332+
resGeom = qgis::make_unique<QgsMultiLineString>();
1333+
break;
1334+
case QgsWkbTypes::PolygonGeometry:
1335+
resGeom = qgis::make_unique<QgsMultiPolygon>();
1336+
break;
1337+
default:
1338+
break;
1339+
}
1340+
if ( !resGeom )
1341+
return false;
1342+
1343+
for ( int i = 0; i < origGeom->numGeometries(); ++i )
1344+
{
1345+
const QgsAbstractGeometry *g = origGeom->geometryN( i );
1346+
if ( QgsWkbTypes::geometryType( g->wkbType() ) == geomType )
1347+
resGeom->addGeometry( g->clone() );
1348+
}
1349+
1350+
set( resGeom.release() );
1351+
return true;
1352+
}
1353+
1354+
13181355
QgsPointXY QgsGeometry::asPoint() const
13191356
{
13201357
if ( !d->geometry || QgsWkbTypes::flatType( d->geometry->wkbType() ) != QgsWkbTypes::Point )

0 commit comments

Comments
 (0)