Skip to content

Commit 86e8da7

Browse files
3nidsm-kuhn
authored andcommitted
Return enum instead of int from QgsGeometry operations
1 parent 8d61554 commit 86e8da7

13 files changed

+597
-475
lines changed

python/core/geometry/qgsgeometry.sip

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ class QgsGeometry
4444
#include "qgsgeometry.h"
4545
%End
4646
public:
47+
48+
enum OperationResult
49+
{
50+
Success,
51+
NothingHappened,
52+
InvalidBaseGeometry,
53+
InvalidInput,
54+
GeometryEngineError,
55+
AddPartSelectedGeometryNotFound,
56+
AddPartNotMultiGeometry,
57+
AddRingNotClosed,
58+
AddRingNotValid,
59+
AddRingCrossesExistingRings,
60+
AddRingNotInExistingFeature,
61+
SplitCannotSplitPoint,
62+
};
63+
4764
QgsGeometry();
4865
%Docstring
4966
Constructor
@@ -380,63 +397,58 @@ Returns true if WKB of the geometry is of WKBMulti* type
380397
:rtype: float
381398
%End
382399

383-
int addRing( const QList<QgsPointXY> &ring );
400+
OperationResult addRing( const QList<QgsPointXY> &ring );
384401
%Docstring
385402
Adds a new ring to this geometry. This makes only sense for polygon and multipolygons.
386-
:return: 0 in case of success (ring added), 1 problem with geometry type, 2 ring not closed,
387-
3 ring is not valid geometry, 4 ring not disjoint with existing rings, 5 no polygon found which contained the ring*
388-
:rtype: int
403+
\param ring The ring to be added
404+
:return: OperationResult a result code: success or reason of failure
405+
:rtype: OperationResult
389406
%End
390407

391-
int addRing( QgsCurve *ring /Transfer/ );
408+
OperationResult addRing( QgsCurve *ring /Transfer/ );
392409
%Docstring
393410
Adds a new ring to this geometry. This makes only sense for polygon and multipolygons.
394-
:return: 0 in case of success (ring added), 1 problem with geometry type, 2 ring not closed,
395-
3 ring is not valid geometry, 4 ring not disjoint with existing rings, 5 no polygon found which contained the ring*
396-
:rtype: int
411+
\param ring The ring to be added
412+
:return: OperationResult a result code: success or reason of failure
413+
:rtype: OperationResult
397414
%End
398415

399-
int addPart( const QList<QgsPointXY> &points, QgsWkbTypes::GeometryType geomType = QgsWkbTypes::UnknownGeometry ) /PyName=addPoints/;
416+
OperationResult addPart( const QList<QgsPointXY> &points, QgsWkbTypes::GeometryType geomType = QgsWkbTypes::UnknownGeometry ) /PyName=addPoints/;
400417
%Docstring
401418
Adds a new part to a the geometry.
402419
\param points points describing part to add
403420
\param geomType default geometry type to create if no existing geometry
404-
:return: 0 in case of success, 1 if not a multipolygon, 2 if ring is not a valid geometry, 3 if new polygon ring
405-
not disjoint with existing polygons of the feature
406-
:rtype: int
421+
:return: OperationResult a result code: success or reason of failure
422+
:rtype: OperationResult
407423
%End
408424

409-
int addPart( const QgsPointSequence &points, QgsWkbTypes::GeometryType geomType = QgsWkbTypes::UnknownGeometry ) /PyName=addPointsV2/;
425+
OperationResult addPart( const QgsPointSequence &points, QgsWkbTypes::GeometryType geomType = QgsWkbTypes::UnknownGeometry ) /PyName=addPointsV2/;
410426
%Docstring
411427
Adds a new part to a the geometry.
412428
\param points points describing part to add
413429
\param geomType default geometry type to create if no existing geometry
414-
:return: 0 in case of success, 1 if not a multipolygon, 2 if ring is not a valid geometry, 3 if new polygon ring
415-
not disjoint with existing polygons of the feature
416-
:rtype: int
430+
:return: OperationResult a result code: success or reason of failure
431+
:rtype: OperationResult
417432
%End
418433

419-
int addPart( QgsAbstractGeometry *part /Transfer/, QgsWkbTypes::GeometryType geomType = QgsWkbTypes::UnknownGeometry );
434+
OperationResult addPart( QgsAbstractGeometry *part /Transfer/, QgsWkbTypes::GeometryType geomType = QgsWkbTypes::UnknownGeometry );
420435
%Docstring
421436
Adds a new part to this geometry.
422437
\param part part to add (ownership is transferred)
423438
\param geomType default geometry type to create if no existing geometry
424-
:return: 0 in case of success, 1 if not a multipolygon, 2 if ring is not a valid geometry, 3 if new polygon ring
425-
not disjoint with existing polygons of the feature
426-
:rtype: int
439+
:return: OperationResult a result code: success or reason of failure
440+
:rtype: OperationResult
427441
%End
428442

429443

430-
int addPart( const QgsGeometry &newPart ) /PyName=addPartGeometry/;
444+
OperationResult addPart( const QgsGeometry &newPart ) /PyName=addPartGeometry/;
431445
%Docstring
432446
Adds a new island polygon to a multipolygon feature
433-
:return: 0 in case of success, 1 if not a multipolygon, 2 if ring is not a valid geometry, 3 if new polygon ring
434-
not disjoint with existing polygons of the feature
447+
:return: OperationResult a result code: success or reason of failure
435448
.. note::
436449

437-
available in Python bindings as addPartGeometry
438-
.. versionadded:: 2.2
439-
:rtype: int
450+
available in python bindings as addPartGeometry
451+
:rtype: OperationResult
440452
%End
441453

442454
QgsGeometry removeInteriorRings( double minimumAllowedArea = -1 ) const;
@@ -448,52 +460,52 @@ not disjoint with existing polygons of the feature
448460
:rtype: QgsGeometry
449461
%End
450462

451-
int translate( double dx, double dy );
463+
OperationResult translate( double dx, double dy );
452464
%Docstring
453465
Translate this geometry by dx, dy
454-
:return: 0 in case of success*
455-
:rtype: int
466+
:return: OperationResult a result code: success or reason of failure
467+
:rtype: OperationResult
456468
%End
457469

458-
int transform( const QgsCoordinateTransform &ct );
470+
OperationResult transform( const QgsCoordinateTransform &ct );
459471
%Docstring
460472
Transform this geometry as described by CoordinateTransform ct
461-
:return: 0 in case of success*
462-
:rtype: int
473+
:return: OperationResult a result code: success or reason of failure
474+
:rtype: OperationResult
463475
%End
464476

465-
int transform( const QTransform &ct );
477+
OperationResult transform( const QTransform &ct );
466478
%Docstring
467479
Transform this geometry as described by QTransform ct
468-
.. versionadded:: 2.8
469-
:return: 0 in case of success*
470-
:rtype: int
480+
:return: OperationResult a result code: success or reason of failure
481+
:rtype: OperationResult
471482
%End
472483

473-
int rotate( double rotation, const QgsPointXY &center );
484+
OperationResult rotate( double rotation, const QgsPointXY &center );
474485
%Docstring
475486
Rotate this geometry around the Z axis
476-
.. versionadded:: 2.8
477-
\param rotation clockwise rotation in degrees
478-
\param center rotation center
479-
:return: 0 in case of success*
480-
:rtype: int
487+
\param rotation clockwise rotation in degrees
488+
\param center rotation center
489+
:return: OperationResult a result code: success or reason of failure
490+
:rtype: OperationResult
481491
%End
482492

483-
int splitGeometry( const QList<QgsPointXY> &splitLine,
484-
QList<QgsGeometry> &newGeometries /Out/,
485-
bool topological,
486-
QList<QgsPointXY> &topologyTestPoints /Out/ );
493+
OperationResult splitGeometry( const QList<QgsPointXY> &splitLine, QList<QgsGeometry> &newGeometries, bool topological, QList<QgsPointXY> &topologyTestPoints );
487494
%Docstring
488-
:rtype: int
495+
Splits this geometry according to a given line.
496+
\param splitLine the line that splits the geometry
497+
\param[out] newGeometries list of new geometries that have been created with the split
498+
\param topological true if topological editing is enabled
499+
\param[out] topologyTestPoints points that need to be tested for topological completeness in the dataset
500+
:return: OperationResult a result code: success or reason of failure
501+
:rtype: OperationResult
489502
%End
490503

491-
int reshapeGeometry( const QgsLineString &reshapeLineString );
504+
OperationResult reshapeGeometry( const QgsLineString &reshapeLineString );
492505
%Docstring
493506
Replaces a part of this geometry with another line
494-
:return: 0 in case of success
495-
.. versionadded:: 1.3
496-
:rtype: int
507+
:return: OperationResult a result code: success or reason of failure
508+
:rtype: OperationResult
497509
%End
498510

499511

python/core/geometry/qgsgeometryengine.sip

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ class QgsGeometryEngine
2121
#include "qgsgeometryengine.h"
2222
%End
2323
public:
24+
25+
enum EngineOperationResult
26+
{
27+
Success,
28+
NothingHappened,
29+
MethodNotImplemented,
30+
EngineError,
31+
NodedGeometryError,
32+
InvalidBaseGeometry,
33+
InvalidInput,
34+
SplitCannotSplitPoint,
35+
};
36+
2437
virtual ~QgsGeometryEngine();
2538

2639
virtual void geometryChanged() = 0;
@@ -230,12 +243,12 @@ class QgsGeometryEngine
230243
:rtype: bool
231244
%End
232245

233-
virtual int splitGeometry( const QgsLineString &splitLine,
234-
QList<QgsAbstractGeometry *> &newGeometries,
235-
bool topological,
236-
QgsPointSequence &topologyTestPoints, QString *errorMsg = 0 ) const;
246+
virtual QgsGeometryEngine::EngineOperationResult splitGeometry( const QgsLineString &splitLine,
247+
QList<QgsAbstractGeometry *> &newGeometries,
248+
bool topological,
249+
QgsPointSequence &topologyTestPoints, QString *errorMsg = 0 ) const;
237250
%Docstring
238-
:rtype: int
251+
:rtype: QgsGeometryEngine.EngineOperationResult
239252
%End
240253

241254
virtual QgsAbstractGeometry *offsetCurve( double distance, int segments, int joinStyle, double miterLimit, QString *errorMsg = 0 ) const = 0 /Factory/;

python/core/qgsvectorlayereditutils.sip

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,29 +63,51 @@ class QgsVectorLayerEditUtils
6363
:rtype: QgsVectorLayer.EditResult
6464
%End
6565

66-
int addRing( const QList<QgsPointXY> &ring, const QgsFeatureIds &targetFeatureIds = QgsFeatureIds(), QgsFeatureId *modifiedFeatureId = 0 );
66+
QgsGeometry::OperationResult addRing( const QList<QgsPointXY> &ring, const QgsFeatureIds &targetFeatureIds = QgsFeatureIds(), QgsFeatureId *modifiedFeatureId = 0 );
6767
%Docstring
68-
:rtype: int
68+
Adds a ring to polygon/multipolygon features
69+
@param ring ring to add
70+
@param targetFeatureIds if specified, only these features will be the candidates for adding a ring. Otherwise
71+
all intersecting features are tested and the ring is added to the first valid feature.
72+
@param modifiedFeatureId if specified, feature ID for feature that ring was added to will be stored in this parameter
73+
@return OperationResult result code: success or reason of failure
74+
:rtype: QgsGeometry.OperationResult
6975
%End
7076

71-
int addRing( QgsCurve *ring, const QgsFeatureIds &targetFeatureIds = QgsFeatureIds(), QgsFeatureId *modifiedFeatureId = 0 ) /PyName=addCurvedRing/;
77+
QgsGeometry::OperationResult addRing( QgsCurve *ring, const QgsFeatureIds &targetFeatureIds = QgsFeatureIds(), QgsFeatureId *modifiedFeatureId = 0 ) /PyName=addCurvedRing/;
7278
%Docstring
73-
:rtype: int
79+
Adds a ring to polygon/multipolygon features
80+
@param ring ring to add
81+
@param targetFeatureIds if specified, only these features will be the candidates for adding a ring. Otherwise
82+
all intersecting features are tested and the ring is added to the first valid feature.
83+
@param modifiedFeatureId if specified, feature ID for feature that ring was added to will be stored in this parameter
84+
@return OperationResult result code: success or reason of failure
85+
.. note::
86+
87+
available in python bindings as addCurvedRing
88+
:rtype: QgsGeometry.OperationResult
7489
%End
7590

76-
int addPart( const QList<QgsPointXY> &ring, QgsFeatureId featureId );
91+
QgsGeometry::OperationResult addPart( const QList<QgsPointXY> &ring, QgsFeatureId featureId );
7792
%Docstring
78-
:rtype: int
93+
Adds a new part polygon to a multipart feature
94+
@returns QgsGeometry.OperationResult a result code: success or reason of failure
95+
:rtype: QgsGeometry.OperationResult
7996
%End
8097

81-
int addPart( const QgsPointSequence &ring, QgsFeatureId featureId );
98+
QgsGeometry::OperationResult addPart( const QgsPointSequence &ring, QgsFeatureId featureId );
8299
%Docstring
83-
:rtype: int
100+
Adds a new part polygon to a multipart feature
101+
@returns QgsGeometry.OperationResult a result code: success or reason of failure
102+
.. note::
103+
104+
available in python bindings as addPartV2
105+
:rtype: QgsGeometry.OperationResult
84106
%End
85107

86-
int addPart( QgsCurve *ring, QgsFeatureId featureId ) /PyName=addCurvedPart/;
108+
QgsGeometry::OperationResult addPart( QgsCurve *ring, QgsFeatureId featureId ) /PyName=addCurvedPart/;
87109
%Docstring
88-
:rtype: int
110+
:rtype: QgsGeometry.OperationResult
89111
%End
90112

91113
int translateFeature( QgsFeatureId featureId, double dx, double dy );
@@ -98,14 +120,14 @@ class QgsVectorLayerEditUtils
98120
:rtype: int
99121
%End
100122

101-
int splitParts( const QList<QgsPointXY> &splitLine, bool topologicalEditing = false );
123+
QgsGeometry::OperationResult splitParts( const QList<QgsPointXY> &splitLine, bool topologicalEditing = false );
102124
%Docstring
103-
:rtype: int
125+
:rtype: QgsGeometry.OperationResult
104126
%End
105127

106-
int splitFeatures( const QList<QgsPointXY> &splitLine, bool topologicalEditing = false );
128+
QgsGeometry::OperationResult splitFeatures( const QList<QgsPointXY> &splitLine, bool topologicalEditing = false );
107129
%Docstring
108-
:rtype: int
130+
:rtype: QgsGeometry.OperationResult
109131
%End
110132

111133
int addTopologicalPoints( const QgsGeometry &geom );
@@ -130,15 +152,6 @@ class QgsVectorLayerEditUtils
130152
:rtype: int
131153
%End
132154

133-
protected:
134-
135-
int boundingBoxFromPointList( const QList<QgsPointXY> &list, double &xmin, double &ymin, double &xmax, double &ymax ) const;
136-
%Docstring
137-
Little helper function that gives bounding box from a list of points.
138-
:return: 0 in case of success *
139-
:rtype: int
140-
%End
141-
142155
};
143156

144157
/************************************************************************

0 commit comments

Comments
 (0)