Skip to content
Permalink
Browse files

Make QgsGeometry::interpolate handle polygon geometries

  • Loading branch information
nyalldawson committed Aug 28, 2016
1 parent 93c7f5f commit 9a9a49c16ab6fd460059437311739fb266c783bb
Showing with 32 additions and 1 deletion.
  1. +6 −1 src/core/geometry/qgsgeometry.cpp
  2. +26 −0 tests/src/python/test_qgsgeometry.py
@@ -1483,7 +1483,12 @@ QgsGeometry QgsGeometry::interpolate( double distance ) const
{
return QgsGeometry();
}
QgsGeos geos( d->geometry );

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

QgsGeos geos( line.geometry() );
QgsAbstractGeometry* result = geos.interpolate( distance );
if ( !result )
{
@@ -3535,5 +3535,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 9a9a49c

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