Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make QgsGeometry::interpolate handle polygon geometries
(cherry-picked from 9a9a49c)
  • Loading branch information
nyalldawson committed Aug 29, 2016
1 parent a2d4bbe commit 41a9631
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/core/geometry/qgsgeometry.cpp
Expand Up @@ -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 )
{
Expand Down
26 changes: 26 additions & 0 deletions tests/src/python/test_qgsgeometry.py
Expand Up @@ -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.