Skip to content

Commit 3703bf7

Browse files
lbartolettinyalldawson
authored andcommitted
Move midpoint to QgsGeometryUtils
1 parent e851b68 commit 3703bf7

File tree

8 files changed

+58
-75
lines changed

8 files changed

+58
-75
lines changed

python/core/geometry/qgsgeometryutils.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,6 @@ class QgsGeometryUtils
6565

6666
static double averageAngle( double a1, double a2 );
6767

68+
static QgsPointV2 midpoint (const QgsPointV2& pt1, const QgsPointV2& pt2);
69+
6870
};

python/core/geometry/qgspointv2.sip

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -243,27 +243,6 @@ class QgsPointV2: public QgsAbstractGeometry
243243
*/
244244
QgsPointV2 project( double distance, double azimuth, double inclination = 90.0 ) const;
245245

246-
/** Returns a middle point between this point and other one.
247-
* Z value is computed if one of this point have Z.
248-
* M value is computed if one of this point have M.
249-
* @param other other point.
250-
* @return New point at middle between this and other one.
251-
* Example:
252-
* \code{.py}
253-
* p = QgsPointV2( 4, 6 ) # 2D point
254-
* pr = p.midpoint ( QgsPointV2( 2, 2 ) )
255-
* # pr is a 2D point: 'Point (3 4)'
256-
* pr = p.midpoint ( QgsPointV2( QgsWkbTypes.PointZ, 2, 2, 2 ) )
257-
* # pr is a 3D point: 'PointZ (3 4 1)'
258-
* pr = p.midpoint ( QgsPointV2( QgsWkbTypes.PointM, 2, 2, 0, 2 ) )
259-
* # pr is a 3D point: 'PointM (3 4 1)'
260-
* pr = p.midpoint ( QgsPointV2( QgsWkbTypes.PointZM, 2, 2, 2, 2 ) )
261-
* # pr is a 4D point: 'PointZM (3 4 1 1)'
262-
* \endcode
263-
* @note added in QGIS 3.0
264-
*/
265-
QgsPointV2 midpoint (const QgsPointV2& other) const;
266-
267246
/**
268247
* Calculates the vector obtained by subtracting a point from this point.
269248
* @note added in QGIS 3.0

src/core/geometry/qgsgeometryutils.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,31 @@ QStringList QgsGeometryUtils::wktGetChildBlocks( const QString &wkt, const QStri
819819
return blocks;
820820
}
821821

822+
QgsPointV2 QgsGeometryUtils::midpoint(const QgsPointV2 &pt1, const QgsPointV2 &pt2)
823+
{
824+
QgsWkbTypes::Type pType( QgsWkbTypes::Point );
825+
826+
827+
double x = ( pt1.x() + pt2.x() ) / 2.0;
828+
double y = ( pt1.y() + pt2.y() ) / 2.0;
829+
double z = 0.0;
830+
double m = 0.0;
831+
832+
if ( pt1.is3D() || pt2.is3D() )
833+
{
834+
pType = QgsWkbTypes::addZ( pType );
835+
z = ( pt1.z() + pt2.z()) / 2.0;
836+
}
837+
838+
if ( pt1.isMeasure() || pt2.isMeasure() )
839+
{
840+
pType = QgsWkbTypes::addM( pType );
841+
m = ( pt1.m() + pt2.m()) / 2.0;
842+
}
843+
844+
return QgsPointV2( pType, x, y, z, m );
845+
}
846+
822847
double QgsGeometryUtils::lineAngle( double x1, double y1, double x2, double y2 )
823848
{
824849
double at = atan2( y2 - y1, x2 - x1 );

src/core/geometry/qgsgeometryutils.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,28 @@ class CORE_EXPORT QgsGeometryUtils
269269
*/
270270
static QStringList wktGetChildBlocks( const QString& wkt , const QString &defaultType = "" );
271271

272+
/** Returns a middle point between points pt1 and pt2.
273+
* Z value is computed if one of this point have Z.
274+
* M value is computed if one of this point have M.
275+
* @param pt1 first point.
276+
* @param pt2 second point.
277+
* @return New point at middle between points pt1 and pt2.
278+
* * Example:
279+
* \code{.py}
280+
* p = QgsPointV2( 4, 6 ) # 2D point
281+
* pr = midpoint ( p, QgsPointV2( 2, 2 ) )
282+
* # pr is a 2D point: 'Point (3 4)'
283+
* pr = midpoint ( p, QgsPointV2( QgsWkbTypes.PointZ, 2, 2, 2 ) )
284+
* # pr is a 3D point: 'PointZ (3 4 1)'
285+
* pr = midpoint ( p, QgsPointV2( QgsWkbTypes.PointM, 2, 2, 0, 2 ) )
286+
* # pr is a 3D point: 'PointM (3 4 1)'
287+
* pr = midpoint ( p, QgsPointV2( QgsWkbTypes.PointZM, 2, 2, 2, 2 ) )
288+
* # pr is a 3D point: 'PointZM (3 4 1 1)'
289+
* \endcode
290+
* @note added in QGIS 3.0
291+
*/
292+
static QgsPointV2 midpoint( const QgsPointV2& pt1, const QgsPointV2& pt2 );
293+
272294
//! @note not available in Python bindings
273295
enum ComponentType
274296
{

src/core/geometry/qgspointv2.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -514,28 +514,3 @@ QgsPointV2 QgsPointV2::project( double distance, double azimuth, double inclinat
514514

515515
return QgsPointV2( pType, mX + dx, mY + dy, mZ + dz, mM );
516516
}
517-
518-
QgsPointV2 QgsPointV2::midpoint (const QgsPointV2& other) const
519-
{
520-
QgsWkbTypes::Type pType( QgsWkbTypes::Point );
521-
522-
523-
double x = ( mX + other.x() ) / 2.0;
524-
double y = ( mY + other.y() ) / 2.0;
525-
double z = 0.0;
526-
double m = 0.0;
527-
528-
if ( is3D() || other.is3D() )
529-
{
530-
pType = QgsWkbTypes::addZ( pType );
531-
z = ( mZ + other.z()) / 2.0;
532-
}
533-
534-
if ( isMeasure() || other.isMeasure() )
535-
{
536-
pType = QgsWkbTypes::addM( pType );
537-
m = ( mM + other.m()) / 2.0;
538-
}
539-
540-
return QgsPointV2( pType, x, y, z, m );
541-
}

src/core/geometry/qgspointv2.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -257,27 +257,6 @@ class CORE_EXPORT QgsPointV2: public QgsAbstractGeometry
257257
*/
258258
QgsPointV2 project( double distance, double azimuth, double inclination = 90.0 ) const;
259259

260-
/** Returns a middle point between this point and other one.
261-
* Z value is computed if one of this point have Z.
262-
* M value is computed if one of this point have M.
263-
* @param other other point.
264-
* @return New point at middle between this and other one.
265-
* Example:
266-
* \code{.py}
267-
* p = QgsPointV2( 4, 6 ) # 2D point
268-
* pr = p.midpoint ( QgsPointV2( 2, 2 ) )
269-
* # pr is a 2D point: 'Point (3 4)'
270-
* pr = p.midpoint ( QgsPointV2( QgsWkbTypes.PointZ, 2, 2, 2 ) )
271-
* # pr is a 3D point: 'PointZ (3 4 1)'
272-
* pr = p.midpoint ( QgsPointV2( QgsWkbTypes.PointM, 2, 2, 0, 2 ) )
273-
* # pr is a 3D point: 'PointM (3 4 1)'
274-
* pr = p.midpoint ( QgsPointV2( QgsWkbTypes.PointZM, 2, 2, 2, 2 ) )
275-
* # pr is a 4D point: 'PointZM (3 4 1 1)'
276-
* \endcode
277-
* @note added in QGIS 3.0
278-
*/
279-
QgsPointV2 midpoint (const QgsPointV2& other) const;
280-
281260
/**
282261
* Calculates the vector obtained by subtracting a point from this point.
283262
* @note added in QGIS 3.0

tests/src/core/testqgsgeometry.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -910,13 +910,6 @@ void TestQgsGeometry::point()
910910
QCOMPARE( p34.project( 5, 450 ), QgsPointV2( QgsWkbTypes::PointZM, 6, 2, 2, 5 ) );
911911
QCOMPARE( p34.project( 5, 450, 450 ), QgsPointV2( QgsWkbTypes::PointZM, 6, 2, 2, 5 ) );
912912

913-
// midpoint
914-
QgsPointV2 p35 = QgsPointV2( 4, 6 );
915-
QCOMPARE( p35.midpoint( QgsPointV2( 2, 2 ) ), QgsPointV2( 3, 4 ) );
916-
QCOMPARE( p35.midpoint( QgsPointV2( QgsWkbTypes::PointZ, 2, 2, 2 ) ), QgsPointV2( QgsWkbTypes::PointZ, 3, 4, 1 ) );
917-
QCOMPARE( p35.midpoint( QgsPointV2( QgsWkbTypes::PointM, 2, 2, 0, 2 ) ), QgsPointV2( QgsWkbTypes::PointM, 3, 4, 0, 1 ) );
918-
QCOMPARE( p35.midpoint( QgsPointV2( QgsWkbTypes::PointZM, 2, 2, 2, 2 ) ), QgsPointV2( QgsWkbTypes::PointZM, 3, 4, 1, 1 ) );
919-
920913
}
921914

922915
void TestQgsGeometry::lineString()

tests/src/core/testqgsgeometryutils.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class TestQgsGeometryUtils: public QObject
5151
void testCircleCenterRadius();
5252
void testSqrDistToLine();
5353
void testAngleThreePoints();
54+
void testMidPoint();
5455
};
5556

5657

@@ -539,7 +540,14 @@ void TestQgsGeometryUtils::testAngleThreePoints()
539540
( void )QgsGeometryUtils::angleBetweenThreePoints( p1.x(), p1.y(), p2.x(), p2.y(), p3.x(), p3.y() );
540541
}
541542

542-
543+
void TestQgsGeometryUtils::testMidPoint()
544+
{
545+
QgsPointV2 p1( 4, 6 );
546+
QCOMPARE( QgsGeometryUtils::midpoint( p1, QgsPointV2( 2, 2 ) ), QgsPointV2( 3, 4 ) );
547+
QCOMPARE( QgsGeometryUtils::midpoint( p1, QgsPointV2( QgsWkbTypes::PointZ, 2, 2, 2 ) ), QgsPointV2( QgsWkbTypes::PointZ, 3, 4, 1 ) );
548+
QCOMPARE( QgsGeometryUtils::midpoint( p1, QgsPointV2( QgsWkbTypes::PointM, 2, 2, 0, 2 ) ), QgsPointV2( QgsWkbTypes::PointM, 3, 4, 0, 1 ) );
549+
QCOMPARE( QgsGeometryUtils::midpoint( p1, QgsPointV2( QgsWkbTypes::PointZM, 2, 2, 2, 2 ) ), QgsPointV2( QgsWkbTypes::PointZM, 3, 4, 1, 1 ) );
550+
}
543551

544552
QGSTEST_MAIN( TestQgsGeometryUtils )
545553
#include "testqgsgeometryutils.moc"

0 commit comments

Comments
 (0)