From e568470c6409febdb5187e3f53af32164c63169f Mon Sep 17 00:00:00 2001 From: Paolo Angelelli Date: Fri, 5 Aug 2016 22:21:12 +0200 Subject: Add support for QGeoPath This patch adds an addition qgeoshapes, QGeoPath. This represents a geographical path where each consecutive geopoints are connected along the shortest line of constant bearing (rhumb line). The path has a width, in meters, that is used in the contains() method to decide whether a coordinate is or not on the path, based on shortest distance to the rhumb line segments. Change-Id: Ia02780e3c8ac6c6d63a6083f53ea0677f8a21a1d Reviewed-by: Alex Blasche --- tests/auto/auto.pro | 1 + tests/auto/qgeocircle/tst_qgeocircle.cpp | 2 +- tests/auto/qgeopath/qgeopath.pro | 8 + tests/auto/qgeopath/tst_qgeopath.cpp | 385 +++++++++++++++++++++++++++++++ 4 files changed, 395 insertions(+), 1 deletion(-) create mode 100644 tests/auto/qgeopath/qgeopath.pro create mode 100644 tests/auto/qgeopath/tst_qgeopath.cpp (limited to 'tests') diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index e3236deb..ba7dc523 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -74,6 +74,7 @@ SUBDIRS += \ qgeoshape \ qgeorectangle \ qgeocircle \ + qgeopath \ qgeocoordinate \ qgeolocation \ qgeopositioninfo \ diff --git a/tests/auto/qgeocircle/tst_qgeocircle.cpp b/tests/auto/qgeocircle/tst_qgeocircle.cpp index 6422a8aa..fb2f248b 100644 --- a/tests/auto/qgeocircle/tst_qgeocircle.cpp +++ b/tests/auto/qgeocircle/tst_qgeocircle.cpp @@ -246,7 +246,7 @@ void tst_QGeoCircle::valid() QCOMPARE(c.isValid(), valid); QGeoShape area = c; - QCOMPARE(c.isValid(), valid); + QCOMPARE(area.isValid(), valid); } void tst_QGeoCircle::empty_data() diff --git a/tests/auto/qgeopath/qgeopath.pro b/tests/auto/qgeopath/qgeopath.pro new file mode 100644 index 00000000..eec05974 --- /dev/null +++ b/tests/auto/qgeopath/qgeopath.pro @@ -0,0 +1,8 @@ +TEMPLATE = app +CONFIG += testcase +TARGET = tst_qgeopath + +SOURCES += \ + tst_qgeopath.cpp + +QT += positioning testlib diff --git a/tests/auto/qgeopath/tst_qgeopath.cpp b/tests/auto/qgeopath/tst_qgeopath.cpp new file mode 100644 index 00000000..8c4ac767 --- /dev/null +++ b/tests/auto/qgeopath/tst_qgeopath.cpp @@ -0,0 +1,385 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include + +QT_USE_NAMESPACE + +class tst_QGeoPath : public QObject +{ + Q_OBJECT + +private slots: + void defaultConstructor(); + void listConstructor(); + void assignment(); + + void comparison(); + void type(); + + void path(); + void width(); + + void translate_data(); + void translate(); + + void valid_data(); + void valid(); + + void contains_data(); + void contains(); + + void boundingGeoRectangle_data(); + void boundingGeoRectangle(); + + void extendShape(); + void extendShape_data(); +}; + +void tst_QGeoPath::defaultConstructor() +{ + QGeoPath p; + QVERIFY(!p.path().size()); + QCOMPARE(p.width(), qreal(0.0)); +} + +void tst_QGeoPath::listConstructor() +{ + QList coords; + coords.append(QGeoCoordinate(1,1)); + coords.append(QGeoCoordinate(2,2)); + coords.append(QGeoCoordinate(3,0)); + + QGeoPath p(coords, 1.0); + QCOMPARE(p.width(), qreal(1.0)); + QCOMPARE(p.path().size(), 3); + + for (const QGeoCoordinate &c : coords) { + QCOMPARE(p.path().contains(c), true); + } +} + +void tst_QGeoPath::assignment() +{ + QGeoPath p1; + QList coords; + coords.append(QGeoCoordinate(1,1)); + coords.append(QGeoCoordinate(2,2)); + coords.append(QGeoCoordinate(3,0)); + QGeoPath p2(coords, 1.0); + + QVERIFY(p1 != p2); + + p1 = p2; + QCOMPARE(p1.path(), coords); + QCOMPARE(p1.width(), 1.0); + QCOMPARE(p1, p2); + + // Assign c1 to an area + QGeoShape area = p1; + QCOMPARE(area.type(), p1.type()); + QVERIFY(area == p1); + + // Assign the area back to a bounding circle + QGeoPath p3 = area; + QCOMPARE(p3.path(), coords); + QCOMPARE(p3.width(), 1.0); + + // Check that the copy is not modified when modifying the original. + p1.setWidth(2.0); + QVERIFY(p3.width() != p1.width()); + QVERIFY(p3 != p1); +} + +void tst_QGeoPath::comparison() +{ + QList coords; + coords.append(QGeoCoordinate(1,1)); + coords.append(QGeoCoordinate(2,2)); + coords.append(QGeoCoordinate(3,0)); + QList coords2; + coords2.append(QGeoCoordinate(3,1)); + coords2.append(QGeoCoordinate(4,2)); + coords2.append(QGeoCoordinate(3,0)); + QGeoPath c1(coords, qreal(50.0)); + QGeoPath c2(coords, qreal(50.0)); + QGeoPath c3(coords, qreal(35.0)); + QGeoPath c4(coords2, qreal(50.0)); + + QVERIFY(c1 == c2); + QVERIFY(!(c1 != c2)); + + QVERIFY(!(c1 == c3)); + QVERIFY(c1 != c3); + + QVERIFY(!(c1 == c4)); + QVERIFY(c1 != c4); + + QVERIFY(!(c2 == c3)); + QVERIFY(c2 != c3); + + QGeoRectangle b1(QGeoCoordinate(20,20),QGeoCoordinate(10,30)); + QVERIFY(!(c1 == b1)); + QVERIFY(c1 != b1); + + QGeoShape *c2Ptr = &c2; + QVERIFY(c1 == *c2Ptr); + QVERIFY(!(c1 != *c2Ptr)); + + QGeoShape *c3Ptr = &c3; + QVERIFY(!(c1 == *c3Ptr)); + QVERIFY(c1 != *c3Ptr); +} + +void tst_QGeoPath::type() +{ + QGeoPath c; + QCOMPARE(c.type(), QGeoShape::PathType); +} + +void tst_QGeoPath::path() +{ + QList coords; + coords.append(QGeoCoordinate(1,1)); + coords.append(QGeoCoordinate(2,2)); + coords.append(QGeoCoordinate(3,0)); + + QGeoPath p; + p.setPath(coords); + QCOMPARE(p.path().size(), 3); + + for (const QGeoCoordinate &c : coords) { + QCOMPARE(p.path().contains(c), true); + } +} + +void tst_QGeoPath::width() +{ + QGeoPath p; + p.setWidth(10.0); + QCOMPARE(p.width(), qreal(10.0)); +} + +void tst_QGeoPath::translate_data() +{ + QTest::addColumn("c1"); + QTest::addColumn("c2"); + QTest::addColumn("c3"); + QTest::addColumn("lat"); + QTest::addColumn("lon"); + + QTest::newRow("Simple") << QGeoCoordinate(1,1) << QGeoCoordinate(2,2) << + QGeoCoordinate(3,0) << 5.0 << 4.0; + QTest::newRow("Backward") << QGeoCoordinate(1,1) << QGeoCoordinate(2,2) << + QGeoCoordinate(3,0) << -5.0 << -4.0; +} + +void tst_QGeoPath::translate() +{ + QFETCH(QGeoCoordinate, c1); + QFETCH(QGeoCoordinate, c2); + QFETCH(QGeoCoordinate, c3); + QFETCH(double, lat); + QFETCH(double, lon); + + QList coords; + coords.append(c1); + coords.append(c2); + coords.append(c3); + QGeoPath p(coords); + + p.translate(lat, lon); + + for (int i = 0; i < p.path().size(); i++) { + QCOMPARE(coords[i].latitude(), p.path()[i].latitude() - lat ); + QCOMPARE(coords[i].longitude(), p.path()[i].longitude() - lon ); + } +} + +void tst_QGeoPath::valid_data() +{ + QTest::addColumn("c1"); + QTest::addColumn("c2"); + QTest::addColumn("c3"); + QTest::addColumn("width"); + QTest::addColumn("valid"); + + QTest::newRow("empty coords") << QGeoCoordinate() << QGeoCoordinate() << QGeoCoordinate() << qreal(5.0) << false; + QTest::newRow("invalid coord") << QGeoCoordinate(50, 50) << QGeoCoordinate(60, 60) << QGeoCoordinate(700, 700) << qreal(5.0) << false; + QTest::newRow("bad width") << QGeoCoordinate(10, 10) << QGeoCoordinate(11, 11) << QGeoCoordinate(10, 12) << qreal(-5.0) << true; + QTest::newRow("NaN width") << QGeoCoordinate(10, 10) << QGeoCoordinate(11, 11) << QGeoCoordinate(10, 12) << qreal(qQNaN()) << true; + QTest::newRow("zero width") << QGeoCoordinate(10, 10) << QGeoCoordinate(11, 11) << QGeoCoordinate(10, 12) << qreal(0) << true; + QTest::newRow("good") << QGeoCoordinate(10, 10) << QGeoCoordinate(11, 11) << QGeoCoordinate(10, 12) << qreal(5) << true; +} + +void tst_QGeoPath::valid() +{ + QFETCH(QGeoCoordinate, c1); + QFETCH(QGeoCoordinate, c2); + QFETCH(QGeoCoordinate, c3); + QFETCH(qreal, width); + QFETCH(bool, valid); + + QList coords; + coords.append(c1); + coords.append(c2); + coords.append(c3); + QGeoPath p(coords, width); + + QCOMPARE(p.isValid(), valid); + + QGeoShape area = p; + QCOMPARE(area.isValid(), valid); +} + +void tst_QGeoPath::contains_data() +{ + QTest::addColumn("c1"); + QTest::addColumn("c2"); + QTest::addColumn("c3"); + QTest::addColumn("width"); + QTest::addColumn("probe"); + QTest::addColumn("result"); + + QList c; + c.append(QGeoCoordinate(1,1)); + c.append(QGeoCoordinate(2,2)); + c.append(QGeoCoordinate(3,0)); + + QTest::newRow("One of the points") << c[0] << c[1] << c[2] << 0.0 << QGeoCoordinate(2, 2) << true; + QTest::newRow("Not so far away") << c[0] << c[1] << c[2] << 0.0 << QGeoCoordinate(0, 0) << false; + QTest::newRow("Not so far away and large line") << c[0] << c[1] << c[2] << 100000.0 << QGeoCoordinate(0, 0) << true; +} + +void tst_QGeoPath::contains() +{ + QFETCH(QGeoCoordinate, c1); + QFETCH(QGeoCoordinate, c2); + QFETCH(QGeoCoordinate, c3); + QFETCH(qreal, width); + QFETCH(QGeoCoordinate, probe); + QFETCH(bool, result); + + QList coords; + coords.append(c1); + coords.append(c2); + coords.append(c3); + QGeoPath p(coords, width); + + QCOMPARE(p.contains(probe), result); + + QGeoShape area = p; + QCOMPARE(area.contains(probe), result); +} + +void tst_QGeoPath::boundingGeoRectangle_data() +{ + QTest::addColumn("c1"); + QTest::addColumn("c2"); + QTest::addColumn("c3"); + QTest::addColumn("width"); + QTest::addColumn("probe"); + QTest::addColumn("result"); + + QList c; + c.append(QGeoCoordinate(1,1)); + c.append(QGeoCoordinate(2,2)); + c.append(QGeoCoordinate(3,0)); + + QTest::newRow("One of the points") << c[0] << c[1] << c[2] << 0.0 << QGeoCoordinate(2, 2) << true; + QTest::newRow("Not so far away") << c[0] << c[1] << c[2] << 0.0 << QGeoCoordinate(0, 0) << false; + QTest::newRow("Inside the bounds") << c[0] << c[1] << c[2] << 100.0 << QGeoCoordinate(1, 0) << true; + QTest::newRow("Inside the bounds") << c[0] << c[1] << c[2] << 100.0 << QGeoCoordinate(1.1, 0.1) << true; +} + +void tst_QGeoPath::boundingGeoRectangle() +{ + QFETCH(QGeoCoordinate, c1); + QFETCH(QGeoCoordinate, c2); + QFETCH(QGeoCoordinate, c3); + QFETCH(qreal, width); + QFETCH(QGeoCoordinate, probe); + QFETCH(bool, result); + + QList coords; + coords.append(c1); + coords.append(c2); + coords.append(c3); + QGeoPath p(coords, width); + + QGeoRectangle box = p.boundingGeoRectangle(); + QCOMPARE(box.contains(probe), result); +} + +void tst_QGeoPath::extendShape() +{ + QFETCH(QGeoCoordinate, c1); + QFETCH(QGeoCoordinate, c2); + QFETCH(QGeoCoordinate, c3); + QFETCH(qreal, width); + QFETCH(QGeoCoordinate, probe); + QFETCH(bool, before); + QFETCH(bool, after); + + QList coords; + coords.append(c1); + coords.append(c2); + coords.append(c3); + QGeoPath p(coords, width); + + + QCOMPARE(p.contains(probe), before); + p.extendShape(probe); + QCOMPARE(p.contains(probe), after); +} + +void tst_QGeoPath::extendShape_data() +{ + QTest::addColumn("c1"); + QTest::addColumn("c2"); + QTest::addColumn("c3"); + QTest::addColumn("width"); + QTest::addColumn("probe"); + QTest::addColumn("before"); + QTest::addColumn("after"); + + QList c; + c.append(QGeoCoordinate(1,1)); + c.append(QGeoCoordinate(2,2)); + c.append(QGeoCoordinate(3,0)); + + QTest::newRow("One of the points") << c[0] << c[1] << c[2] << 0.0 << QGeoCoordinate(2, 2) << true << true; + QTest::newRow("Not so far away") << c[0] << c[1] << c[2] << 0.0 << QGeoCoordinate(0, 0) << false << true; + QTest::newRow("Not so far away and large line") << c[0] << c[1] << c[2] << 100000.0 << QGeoCoordinate(0, 0) << true << true; +} + +QTEST_MAIN(tst_QGeoPath) +#include "tst_qgeopath.moc" -- cgit v1.2.1