Skip to content
Permalink
Browse files

Make QgsGeometry::interpolate handle polygon geometries

(cherry-picked from 9a9a49c)
  • Loading branch information
nyalldawson committed Aug 29, 2016
1 parent a2d4bbe commit 41a96310ff9fa5ac651ac59df0e978f2cb1c7b76
Showing with 32 additions and 1 deletion.
  1. +6 −1 src/core/geometry/qgsgeometry.cpp
  2. +26 −0 tests/src/python/test_qgsgeometry.py
@@ -1382,7 +1382,12 @@ QgsGeometry* QgsGeometry::interpolate( double distance ) const
{
return nullptr;
}
QgsGeos geos( d->geometry );

QgsGeometry line = *this;
if ( type() == QGis::Polygon )
line = QgsGeometry( d->geometry->boundary() );

QgsGeos geos( line.geometry() );
QgsAbstractGeometryV2* result = geos.interpolate( distance );
if ( !result )
{
@@ -3456,5 +3456,31 @@ def testInterpolateAngle(self):
geom = QgsGeometry.fromWkt('CircularString (1 5, 6 2, 7 3)')
self.assertAlmostEqual(geom.interpolateAngle(5), 1.69120, places=3)

def testInterpolate(self):
""" test QgsGeometry.interpolate() """

empty = QgsGeometry()
# just test no crash
self.assertFalse(empty.interpolate(5))

# not a linestring
point = QgsGeometry.fromWkt('Point(1 2)')
# no meaning, just test no crash!
self.assertFalse(empty.interpolate(5))

# linestring
linestring = QgsGeometry.fromWkt('LineString(0 0, 10 0, 10 10)')
exp = 'Point(5 0)'
result = linestring.interpolate(5).exportToWkt()
self.assertTrue(compareWkt(result, exp, 0.00001), "Interpolate: mismatch Expected:\n{}\nGot:\n{}\n".format(exp, result))

# polygon
polygon = QgsGeometry.fromWkt('Polygon((0 0, 10 0, 10 10, 20 20, 10 20, 0 0))')
exp = 'Point(10 5)'
result = linestring.interpolate(15).exportToWkt()
self.assertTrue(compareWkt(result, exp, 0.00001),
"Interpolate: mismatch Expected:\n{}\nGot:\n{}\n".format(exp, result))


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

0 comments on commit 41a9631

Please sign in to comment.
You can’t perform that action at this time.