Skip to content

Commit

Permalink
add QgsDistanceArea tests and enhance QgsGeometry tests a bit
Browse files Browse the repository at this point in the history
(in preparation for a QgsGeometry wkb update)
  • Loading branch information
jef-n committed Jan 23, 2014
1 parent 0d2c564 commit cb5c4e9
Show file tree
Hide file tree
Showing 4 changed files with 817 additions and 6 deletions.
4 changes: 2 additions & 2 deletions python/core/qgsgeometry.sip
Expand Up @@ -199,7 +199,7 @@ class QgsGeometry
* Searches for the closest vertex in this geometry to the given point.
* @param point Specifiest the point for search
* @param atVertex Receives index of the closest vertex
* @return The squared cartesian distance is also returned in sqrDist, negative number on error
* @return The squared cartesian distance, negative number on error
*/
double closestVertexWithContext( const QgsPoint& point, int& atVertex /Out/ );

Expand All @@ -213,7 +213,7 @@ class QgsGeometry
* @param epsilon epsilon for segment snapping (added in 1.8)
* @return The squared cartesian distance is also returned in sqrDist, negative number on error
*/
double closestSegmentWithContext( const QgsPoint& point, QgsPoint& minDistPoint /Out/, int& beforeVertex /Out/ );
double closestSegmentWithContext( const QgsPoint& point, QgsPoint& minDistPoint /Out/, int& afterVertex /Out/ );

/**Adds a new ring to this geometry. This makes only sense for polygon and multipolygons.
@return 0 in case of success (ring added), 1 problem with geometry type, 2 ring not closed,
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -33,3 +33,4 @@ ADD_PYTHON_TEST(PyQgsVectorFileWriter test_qgsvectorfilewriter.py)
ADD_PYTHON_TEST(PyQgsSpatialiteProvider test_qgsspatialiteprovider.py)
ADD_PYTHON_TEST(PyQgsZonalStatistics test_qgszonalstatistics.py)
ADD_PYTHON_TEST(PyQgsAppStartup test_qgsappstartup.py)
ADD_PYTHON_TEST(PyQgsDistanceArea test_qgsdistancearea.py)
128 changes: 128 additions & 0 deletions tests/src/python/test_qgsdistancearea.py
@@ -0,0 +1,128 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsDistanceArea.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Jürgen E. Fischer'
__date__ = '19/01/2014'
__copyright__ = 'Copyright 2014, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os

import qgis

from qgis.core import (QgsGeometry,
QgsPoint,
QgsDistanceArea,
QGis)

from utilities import (getQgisTestApp,
TestCase,
unittest)

from PyQt4.QtCore import qDebug

# Convenience instances in case you may need them
# not used in this test

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()

class TestQgsDistanceArea(TestCase):

def testMeasureLine(self):
# +-+
# | |
# +-+ +
linestring = QgsGeometry.fromPolyline(
[ QgsPoint(0,0), QgsPoint(1,0), QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,0), ]
)
da = QgsDistanceArea()
length = da.measure(linestring)
myMessage = ('Expected:\n%f\nGot:\n%f\n' %
(4, length))
assert length == 4, myMessage

def testMeasureMultiLine(self):
# +-+ +-+-+
# | | | |
# +-+ + + +-+
linestring = QgsGeometry.fromMultiPolyline(
[
[ QgsPoint(0,0), QgsPoint(1,0), QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,0), ],
[ QgsPoint(3,0), QgsPoint(3,1), QgsPoint(5,1), QgsPoint(5,0), QgsPoint(6,0), ]
]
)
da = QgsDistanceArea()
length = da.measure(linestring)
myMessage = ('Expected:\n%f\nGot:\n%f\n' %
(9, length))
assert length == 9, myMessage

def testMeasurePolygon(self):
# +-+-+
# | |
# + +-+
# | |
# +-+
polygon = QgsGeometry.fromPolygon(
[[
QgsPoint(0,0), QgsPoint(1,0), QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,2), QgsPoint(0,2), QgsPoint(0,0),
]]
)

da = QgsDistanceArea()
area = da.measure(polygon)
assert area == 3, 'Expected:\n%f\nGot:\n%f\n' % (3, area)

perimeter = da.measurePerimeter(polygon)
assert perimeter == 8, 'Expected:\n%f\nGot:\n%f\n' % (8, perimeter)

def testMeasurePolygonWithHole(self):
# +-+-+-+
# | |
# + +-+ +
# | | | |
# + +-+ +
# | |
# +-+-+-+
polygon = QgsGeometry.fromPolygon(
[
[ QgsPoint(0,0), QgsPoint(3,0), QgsPoint(3,3), QgsPoint(0,3), QgsPoint(0,0) ],
[ QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,2), QgsPoint(1,2), QgsPoint(1,1) ],
]
)
da = QgsDistanceArea()
area = da.measure(polygon)
assert area == 8, "Expected:\n%f\nGot:\n%f\n" % (8, area)

perimeter = da.measurePerimeter(polygon)
assert perimeter == 12, "Expected:\n%f\nGot:\n%f\n" % (12, perimeter)

def testMeasureMultiPolygon(self):
# +-+-+ +-+-+
# | | | |
# + +-+ +-+ +
# | | | |
# +-+ +-+
polygon = QgsGeometry.fromMultiPolygon(
[
[ [ QgsPoint(0,0), QgsPoint(1,0), QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,2), QgsPoint(0,2), QgsPoint(0,0), ] ],
[ [ QgsPoint(4,0), QgsPoint(5,0), QgsPoint(5,2), QgsPoint(3,2), QgsPoint(3,1), QgsPoint(4,1), QgsPoint(4,0), ] ]
]
)

da = QgsDistanceArea()
area = da.measure(polygon)
assert area == 6, 'Expected:\n%f\nGot:\n%f\n' % (6, area)

perimeter = da.measurePerimeter(polygon)
assert perimeter == 16, "Expected:\n%f\nGot:\n%f\n" % (16, perimeter)

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

0 comments on commit cb5c4e9

Please sign in to comment.