Navigation Menu

Skip to content

Commit

Permalink
Add an easy to use Z/M default value constructor to QgsPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Jun 14, 2017
1 parent 4eae087 commit 7e48719
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 5 deletions.
33 changes: 30 additions & 3 deletions python/core/geometry/qgspoint.sip
Expand Up @@ -22,9 +22,36 @@ class QgsPoint: QgsAbstractGeometry
%End
public:

QgsPoint( double x = 0.0, double y = 0.0, double z = 0.0, double m = 0.0 );
%Docstring
Construct a point with the provided initial coordinate values.
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 )];
% Docstring
Construct a point with the provided initial coordinate values.

If only z and m are not specified, the type will be a 2D point.
If any or both of the others are specified, the Z and M values will be added accordingly.
%End
%MethodCode
double z;
double m;

if ( a2 == Py_None )
{
z = std::numeric_limits<double>::quiet_NaN();
}
else
{
z = PyFloat_AsDouble( a2 );
}

if ( a3 == Py_None )
{
m = std::numeric_limits<double>::quiet_NaN();
}
else
{
m = PyFloat_AsDouble( a3 );
}

sipCpp = new sipQgsPoint( a0, a1, z, m );
%End

explicit QgsPoint( const QgsPointXY &p );
Expand Down
12 changes: 11 additions & 1 deletion src/core/geometry/qgspoint.cpp
Expand Up @@ -38,7 +38,17 @@ QgsPoint::QgsPoint( double x, double y, double z, double m )
, mZ( z )
, mM( m )
{
mWkbType = QgsWkbTypes::Point;
if ( qIsNaN( z ) )
{
if ( qIsNaN( m ) )
mWkbType = QgsWkbTypes::Point;
else
mWkbType = QgsWkbTypes::PointM;
}
else if ( qIsNaN( m ) )
mWkbType = QgsWkbTypes::PointZ;
else
mWkbType = QgsWkbTypes::PointZM;
}

QgsPoint::QgsPoint( const QgsPointXY &p )
Expand Down
38 changes: 37 additions & 1 deletion src/core/geometry/qgspoint.h
Expand Up @@ -46,8 +46,44 @@ class CORE_EXPORT QgsPoint: public QgsAbstractGeometry

/**
* Construct a point with the provided initial coordinate values.
*
* If only z and m are not specified, the type will be a 2D point.
* If any or both of the others are specified, the Z and M values will be added accordingly.
*/
QgsPoint( double x = 0.0, double y = 0.0, double z = 0.0, double m = 0.0 );
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;
#ifdef SIP_RUN
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 )];
% Docstring
Construct a point with the provided initial coordinate values.

If only z and m are not specified, the type will be a 2D point.
If any or both of the others are specified, the Z and M values will be added accordingly.
% End
% MethodCode
double z;
double m;

if ( a2 == Py_None )
{
z = std::numeric_limits<double>::quiet_NaN();
}
else
{
z = PyFloat_AsDouble( a2 );
}

if ( a3 == Py_None )
{
m = std::numeric_limits<double>::quiet_NaN();
}
else
{
m = PyFloat_AsDouble( a3 );
}

sipCpp = new sipQgsPoint( a0, a1, z, m );
% End
#endif

/** Construct a QgsPoint from a QgsPointXY object
*/
Expand Down
6 changes: 6 additions & 0 deletions tests/src/python/test_qgsgeometry.py
Expand Up @@ -4129,6 +4129,12 @@ def testCompare(self):
self.assertFalse(QgsGeometry.compare(lp, lp2))
self.assertTrue(QgsGeometry.compare(lp, lp2, 1e-6))

def testPoint(self):
self.assertEqual(QgsPoint(1, 2).wkbType(), QgsWkbTypes.Point)
self.assertEqual(QgsPoint(1, 2, 3).wkbType(), QgsWkbTypes.PointZ)
self.assertEqual(QgsPoint(1, 2, m=3).wkbType(), QgsWkbTypes.PointM)
self.assertEqual(QgsPoint(1, 2, 3, 4).wkbType(), QgsWkbTypes.PointZM)


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

0 comments on commit 7e48719

Please sign in to comment.