Skip to content

Commit 7e48719

Browse files
committed
Add an easy to use Z/M default value constructor to QgsPoint
1 parent 4eae087 commit 7e48719

File tree

4 files changed

+84
-5
lines changed

4 files changed

+84
-5
lines changed

python/core/geometry/qgspoint.sip

+30-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,36 @@ class QgsPoint: QgsAbstractGeometry
2222
%End
2323
public:
2424

25-
QgsPoint( double x = 0.0, double y = 0.0, double z = 0.0, double m = 0.0 );
26-
%Docstring
27-
Construct a point with the provided initial coordinate values.
25+
QgsPoint( double x = 0.0, double y = 0.0, SIP_PYOBJECT z = Py_None, SIP_PYOBJECT m = Py_None ) [( double x = 0.0, double y = 0.0, double z = 0.0, double m = 0.0 )];
26+
% Docstring
27+
Construct a point with the provided initial coordinate values.
28+
29+
If only z and m are not specified, the type will be a 2D point.
30+
If any or both of the others are specified, the Z and M values will be added accordingly.
31+
%End
32+
%MethodCode
33+
double z;
34+
double m;
35+
36+
if ( a2 == Py_None )
37+
{
38+
z = std::numeric_limits<double>::quiet_NaN();
39+
}
40+
else
41+
{
42+
z = PyFloat_AsDouble( a2 );
43+
}
44+
45+
if ( a3 == Py_None )
46+
{
47+
m = std::numeric_limits<double>::quiet_NaN();
48+
}
49+
else
50+
{
51+
m = PyFloat_AsDouble( a3 );
52+
}
53+
54+
sipCpp = new sipQgsPoint( a0, a1, z, m );
2855
%End
2956

3057
explicit QgsPoint( const QgsPointXY &p );

src/core/geometry/qgspoint.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,17 @@ QgsPoint::QgsPoint( double x, double y, double z, double m )
3838
, mZ( z )
3939
, mM( m )
4040
{
41-
mWkbType = QgsWkbTypes::Point;
41+
if ( qIsNaN( z ) )
42+
{
43+
if ( qIsNaN( m ) )
44+
mWkbType = QgsWkbTypes::Point;
45+
else
46+
mWkbType = QgsWkbTypes::PointM;
47+
}
48+
else if ( qIsNaN( m ) )
49+
mWkbType = QgsWkbTypes::PointZ;
50+
else
51+
mWkbType = QgsWkbTypes::PointZM;
4252
}
4353

4454
QgsPoint::QgsPoint( const QgsPointXY &p )

src/core/geometry/qgspoint.h

+37-1
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,44 @@ class CORE_EXPORT QgsPoint: public QgsAbstractGeometry
4646

4747
/**
4848
* Construct a point with the provided initial coordinate values.
49+
*
50+
* If only z and m are not specified, the type will be a 2D point.
51+
* If any or both of the others are specified, the Z and M values will be added accordingly.
4952
*/
50-
QgsPoint( double x = 0.0, double y = 0.0, double z = 0.0, double m = 0.0 );
53+
QgsPoint( double x = 0.0, double y = 0.0, double z = std::numeric_limits<double>::quiet_NaN(), double m = std::numeric_limits<double>::quiet_NaN() ) SIP_SKIP;
54+
#ifdef SIP_RUN
55+
QgsPoint( double x = 0.0, double y = 0.0, SIP_PYOBJECT z = Py_None, SIP_PYOBJECT m = Py_None ) [( double x = 0.0, double y = 0.0, double z = 0.0, double m = 0.0 )];
56+
% Docstring
57+
Construct a point with the provided initial coordinate values.
58+
59+
If only z and m are not specified, the type will be a 2D point.
60+
If any or both of the others are specified, the Z and M values will be added accordingly.
61+
% End
62+
% MethodCode
63+
double z;
64+
double m;
65+
66+
if ( a2 == Py_None )
67+
{
68+
z = std::numeric_limits<double>::quiet_NaN();
69+
}
70+
else
71+
{
72+
z = PyFloat_AsDouble( a2 );
73+
}
74+
75+
if ( a3 == Py_None )
76+
{
77+
m = std::numeric_limits<double>::quiet_NaN();
78+
}
79+
else
80+
{
81+
m = PyFloat_AsDouble( a3 );
82+
}
83+
84+
sipCpp = new sipQgsPoint( a0, a1, z, m );
85+
% End
86+
#endif
5187

5288
/** Construct a QgsPoint from a QgsPointXY object
5389
*/

tests/src/python/test_qgsgeometry.py

+6
Original file line numberDiff line numberDiff line change
@@ -4129,6 +4129,12 @@ def testCompare(self):
41294129
self.assertFalse(QgsGeometry.compare(lp, lp2))
41304130
self.assertTrue(QgsGeometry.compare(lp, lp2, 1e-6))
41314131

4132+
def testPoint(self):
4133+
self.assertEqual(QgsPoint(1, 2).wkbType(), QgsWkbTypes.Point)
4134+
self.assertEqual(QgsPoint(1, 2, 3).wkbType(), QgsWkbTypes.PointZ)
4135+
self.assertEqual(QgsPoint(1, 2, m=3).wkbType(), QgsWkbTypes.PointM)
4136+
self.assertEqual(QgsPoint(1, 2, 3, 4).wkbType(), QgsWkbTypes.PointZM)
4137+
41324138

41334139
if __name__ == '__main__':
41344140
unittest.main()

0 commit comments

Comments
 (0)