Skip to content

Commit cb5c4e9

Browse files
committed
add QgsDistanceArea tests and enhance QgsGeometry tests a bit
(in preparation for a QgsGeometry wkb update)
1 parent 0d2c564 commit cb5c4e9

File tree

4 files changed

+817
-6
lines changed

4 files changed

+817
-6
lines changed

python/core/qgsgeometry.sip

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class QgsGeometry
199199
* Searches for the closest vertex in this geometry to the given point.
200200
* @param point Specifiest the point for search
201201
* @param atVertex Receives index of the closest vertex
202-
* @return The squared cartesian distance is also returned in sqrDist, negative number on error
202+
* @return The squared cartesian distance, negative number on error
203203
*/
204204
double closestVertexWithContext( const QgsPoint& point, int& atVertex /Out/ );
205205

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

218218
/**Adds a new ring to this geometry. This makes only sense for polygon and multipolygons.
219219
@return 0 in case of success (ring added), 1 problem with geometry type, 2 ring not closed,

tests/src/python/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ ADD_PYTHON_TEST(PyQgsVectorFileWriter test_qgsvectorfilewriter.py)
3333
ADD_PYTHON_TEST(PyQgsSpatialiteProvider test_qgsspatialiteprovider.py)
3434
ADD_PYTHON_TEST(PyQgsZonalStatistics test_qgszonalstatistics.py)
3535
ADD_PYTHON_TEST(PyQgsAppStartup test_qgsappstartup.py)
36+
ADD_PYTHON_TEST(PyQgsDistanceArea test_qgsdistancearea.py)
+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsDistanceArea.
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Jürgen E. Fischer'
10+
__date__ = '19/01/2014'
11+
__copyright__ = 'Copyright 2014, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import os
16+
17+
import qgis
18+
19+
from qgis.core import (QgsGeometry,
20+
QgsPoint,
21+
QgsDistanceArea,
22+
QGis)
23+
24+
from utilities import (getQgisTestApp,
25+
TestCase,
26+
unittest)
27+
28+
from PyQt4.QtCore import qDebug
29+
30+
# Convenience instances in case you may need them
31+
# not used in this test
32+
33+
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
34+
35+
class TestQgsDistanceArea(TestCase):
36+
37+
def testMeasureLine(self):
38+
# +-+
39+
# | |
40+
# +-+ +
41+
linestring = QgsGeometry.fromPolyline(
42+
[ QgsPoint(0,0), QgsPoint(1,0), QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,0), ]
43+
)
44+
da = QgsDistanceArea()
45+
length = da.measure(linestring)
46+
myMessage = ('Expected:\n%f\nGot:\n%f\n' %
47+
(4, length))
48+
assert length == 4, myMessage
49+
50+
def testMeasureMultiLine(self):
51+
# +-+ +-+-+
52+
# | | | |
53+
# +-+ + + +-+
54+
linestring = QgsGeometry.fromMultiPolyline(
55+
[
56+
[ QgsPoint(0,0), QgsPoint(1,0), QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,0), ],
57+
[ QgsPoint(3,0), QgsPoint(3,1), QgsPoint(5,1), QgsPoint(5,0), QgsPoint(6,0), ]
58+
]
59+
)
60+
da = QgsDistanceArea()
61+
length = da.measure(linestring)
62+
myMessage = ('Expected:\n%f\nGot:\n%f\n' %
63+
(9, length))
64+
assert length == 9, myMessage
65+
66+
def testMeasurePolygon(self):
67+
# +-+-+
68+
# | |
69+
# + +-+
70+
# | |
71+
# +-+
72+
polygon = QgsGeometry.fromPolygon(
73+
[[
74+
QgsPoint(0,0), QgsPoint(1,0), QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,2), QgsPoint(0,2), QgsPoint(0,0),
75+
]]
76+
)
77+
78+
da = QgsDistanceArea()
79+
area = da.measure(polygon)
80+
assert area == 3, 'Expected:\n%f\nGot:\n%f\n' % (3, area)
81+
82+
perimeter = da.measurePerimeter(polygon)
83+
assert perimeter == 8, 'Expected:\n%f\nGot:\n%f\n' % (8, perimeter)
84+
85+
def testMeasurePolygonWithHole(self):
86+
# +-+-+-+
87+
# | |
88+
# + +-+ +
89+
# | | | |
90+
# + +-+ +
91+
# | |
92+
# +-+-+-+
93+
polygon = QgsGeometry.fromPolygon(
94+
[
95+
[ QgsPoint(0,0), QgsPoint(3,0), QgsPoint(3,3), QgsPoint(0,3), QgsPoint(0,0) ],
96+
[ QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,2), QgsPoint(1,2), QgsPoint(1,1) ],
97+
]
98+
)
99+
da = QgsDistanceArea()
100+
area = da.measure(polygon)
101+
assert area == 8, "Expected:\n%f\nGot:\n%f\n" % (8, area)
102+
103+
perimeter = da.measurePerimeter(polygon)
104+
assert perimeter == 12, "Expected:\n%f\nGot:\n%f\n" % (12, perimeter)
105+
106+
def testMeasureMultiPolygon(self):
107+
# +-+-+ +-+-+
108+
# | | | |
109+
# + +-+ +-+ +
110+
# | | | |
111+
# +-+ +-+
112+
polygon = QgsGeometry.fromMultiPolygon(
113+
[
114+
[ [ QgsPoint(0,0), QgsPoint(1,0), QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,2), QgsPoint(0,2), QgsPoint(0,0), ] ],
115+
[ [ QgsPoint(4,0), QgsPoint(5,0), QgsPoint(5,2), QgsPoint(3,2), QgsPoint(3,1), QgsPoint(4,1), QgsPoint(4,0), ] ]
116+
]
117+
)
118+
119+
da = QgsDistanceArea()
120+
area = da.measure(polygon)
121+
assert area == 6, 'Expected:\n%f\nGot:\n%f\n' % (6, area)
122+
123+
perimeter = da.measurePerimeter(polygon)
124+
assert perimeter == 16, "Expected:\n%f\nGot:\n%f\n" % (16, perimeter)
125+
126+
if __name__ == '__main__':
127+
unittest.main()
128+

0 commit comments

Comments
 (0)