|
| 1 | +/*************************************************************************** |
| 2 | + testqgsgeometryimport.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : 03 Sept 2014 |
| 5 | + Copyright : (C) 2014 by Marco Hugentobler |
| 6 | + Email : marco@sourcepole.ch |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | + |
| 16 | +#include "qgsapplication.h" |
| 17 | +#include "qgsgeometry.h" |
| 18 | +#include "qgspoint.h" |
| 19 | +#include <QPolygonF> |
| 20 | + |
| 21 | +#include <QtTest> |
| 22 | +#include <QObject> |
| 23 | + |
| 24 | +class TestQgsGeometryImport: public QObject |
| 25 | +{ |
| 26 | + Q_OBJECT |
| 27 | + |
| 28 | + private slots: |
| 29 | + void pointWkt_data(); |
| 30 | + void pointWkt(); |
| 31 | + |
| 32 | + void pointWkb_data(); |
| 33 | + void pointWkb(); |
| 34 | + |
| 35 | + void pointGeos_data(); |
| 36 | + void pointGeos(); |
| 37 | + |
| 38 | + void linestringWkt_data(); |
| 39 | + void linestringWkt(); |
| 40 | + |
| 41 | + void linestringWkb_data(); |
| 42 | + void linestringWkb(); |
| 43 | + |
| 44 | + void linestringGeos_data(); |
| 45 | + void linestringGeos(); |
| 46 | + |
| 47 | + private: |
| 48 | + bool compareLineStrings( const QgsPolyline& polyline, QVariantList& line ); |
| 49 | +}; |
| 50 | + |
| 51 | +void TestQgsGeometryImport::pointWkt_data() |
| 52 | +{ |
| 53 | + QTest::addColumn<QString>( "wktString" ); |
| 54 | + QTest::addColumn<double>( "x" ); |
| 55 | + QTest::addColumn<double>( "y" ); |
| 56 | + |
| 57 | + QTest::newRow( "point_wkt_1" ) << "POINT(30 10)" << 30.0 << 10.0; |
| 58 | +} |
| 59 | + |
| 60 | +void TestQgsGeometryImport::pointWkt() |
| 61 | +{ |
| 62 | + QFETCH( QString, wktString ); |
| 63 | + QFETCH( double, x ); |
| 64 | + QFETCH( double, y ); |
| 65 | + |
| 66 | + QgsGeometry* geom = QgsGeometry::fromWkt( wktString ); |
| 67 | + |
| 68 | + QCOMPARE( geom->wkbType(), QGis::WKBPoint ); |
| 69 | + QgsPoint point = geom->asPoint(); |
| 70 | + delete geom; |
| 71 | + |
| 72 | + QVERIFY( qgsDoubleNear( point.x(), x ) ); |
| 73 | + QVERIFY( qgsDoubleNear( point.y(), y ) ); |
| 74 | +} |
| 75 | + |
| 76 | +void TestQgsGeometryImport::pointWkb_data() |
| 77 | +{ |
| 78 | + QTest::addColumn<double>( "x" ); |
| 79 | + QTest::addColumn<double>( "y" ); |
| 80 | + |
| 81 | + QTest::newRow( "point_wkb_1" ) << 30.0 << 10.0; |
| 82 | +} |
| 83 | + |
| 84 | +void TestQgsGeometryImport::pointWkb() |
| 85 | +{ |
| 86 | + QFETCH( double, x ); |
| 87 | + QFETCH( double, y ); |
| 88 | + |
| 89 | + //create wkb |
| 90 | + char byteOrder = QgsApplication::endian(); |
| 91 | + unsigned char* geomPtr = new unsigned char[21]; |
| 92 | + QgsWkbPtr wkb( geomPtr ); |
| 93 | + wkb << byteOrder << QGis::WKBPoint << x << y; |
| 94 | + |
| 95 | + QgsGeometry geom; |
| 96 | + geom.fromWkb( geomPtr, 21 ); |
| 97 | + QgsPoint point = geom.asPoint(); |
| 98 | + |
| 99 | + QCOMPARE( geom.wkbType(), QGis::WKBPoint ); |
| 100 | + QVERIFY( qgsDoubleNear( point.x(), x ) ); |
| 101 | + QVERIFY( qgsDoubleNear( point.y(), y ) ); |
| 102 | +} |
| 103 | + |
| 104 | +void TestQgsGeometryImport::pointGeos_data() |
| 105 | +{ |
| 106 | + QTest::addColumn<double>( "x" ); |
| 107 | + QTest::addColumn<double>( "y" ); |
| 108 | + |
| 109 | + QTest::newRow( "point_geos_1" ) << 30.0 << 10.0; |
| 110 | +} |
| 111 | + |
| 112 | +void TestQgsGeometryImport::pointGeos() |
| 113 | +{ |
| 114 | + QFETCH( double, x ); |
| 115 | + QFETCH( double, y ); |
| 116 | + |
| 117 | + GEOSCoordSequence *coord = GEOSCoordSeq_create( 1, 2 ); |
| 118 | + GEOSCoordSeq_setX( coord, 0, x ); |
| 119 | + GEOSCoordSeq_setY( coord, 0, y ); |
| 120 | + GEOSGeometry* geosPt = GEOSGeom_createPoint( coord ); |
| 121 | + |
| 122 | + QgsGeometry geom; |
| 123 | + geom.fromGeos( geosPt ); |
| 124 | + QVERIFY( geom.wkbType() == QGis::WKBPoint ); |
| 125 | + |
| 126 | + QgsPoint geomPt = geom.asPoint(); |
| 127 | + |
| 128 | + QVERIFY( qgsDoubleNear( x, geomPt.x() ) ); |
| 129 | + QVERIFY( qgsDoubleNear( y, geomPt.y() ) ); |
| 130 | +} |
| 131 | + |
| 132 | +void TestQgsGeometryImport::linestringWkt_data() |
| 133 | +{ |
| 134 | + QTest::addColumn<QString>( "wktString" ); |
| 135 | + QTest::addColumn<QVariantList>( "line" ); |
| 136 | + |
| 137 | + QVariantList line; |
| 138 | + line << QVariant( QPointF( 30.0, 10.0 ) ) << QVariant( QPointF( 10.0, 30.0 ) ) << QVariant( QPointF( 40.0, 40.0 ) ); |
| 139 | + |
| 140 | + QTest::newRow( "linestring_wkt_1" ) << "LINESTRING (30 10, 10 30, 40 40)" << line; |
| 141 | +} |
| 142 | + |
| 143 | +void TestQgsGeometryImport::linestringWkt() |
| 144 | +{ |
| 145 | + QFETCH( QString, wktString ); |
| 146 | + QFETCH( QVariantList, line ); |
| 147 | + |
| 148 | + QgsGeometry* geom = QgsGeometry::fromWkt( wktString ); |
| 149 | + QCOMPARE( geom->wkbType(), QGis::WKBLineString ); |
| 150 | + |
| 151 | + QgsPolyline polyLine = geom->asPolyline(); |
| 152 | + QVERIFY( compareLineStrings( polyLine, line ) ); |
| 153 | + delete geom; |
| 154 | +} |
| 155 | + |
| 156 | +void TestQgsGeometryImport::linestringWkb_data() |
| 157 | +{ |
| 158 | + QTest::addColumn<QVariantList>( "line" ); |
| 159 | + QVariantList line; |
| 160 | + line << QVariant( QPointF( 30.0, 10.0 ) ) << QVariant( QPointF( 10.0, 30.0 ) ) << QVariant( QPointF( 40.0, 40.0 ) ); |
| 161 | + QTest::newRow( "linestring_wkb_1" ) << line; |
| 162 | +} |
| 163 | + |
| 164 | +void TestQgsGeometryImport::linestringWkb() |
| 165 | +{ |
| 166 | + QFETCH( QVariantList, line ); |
| 167 | + |
| 168 | + char byteOrder = QgsApplication::endian(); |
| 169 | + int wkbSize = 1 + 2 * sizeof( int ) + line.size() * 2 * sizeof( double ); |
| 170 | + unsigned char* geomPtr = new unsigned char[wkbSize]; |
| 171 | + QgsWkbPtr wkb( geomPtr ); |
| 172 | + wkb << byteOrder << QGis::WKBLineString << line.size(); |
| 173 | + |
| 174 | + for ( int i = 0; i < line.size(); ++i ) |
| 175 | + { |
| 176 | + QPointF linePt = line.at( i ).toPointF(); |
| 177 | + wkb << linePt.x() << linePt.y(); |
| 178 | + } |
| 179 | + |
| 180 | + QgsGeometry geom; |
| 181 | + geom.fromWkb( geomPtr, wkbSize ); |
| 182 | + |
| 183 | + QVERIFY( geom.wkbType() == QGis::WKBLineString ); |
| 184 | + QgsPolyline polyline = geom.asPolyline(); |
| 185 | + QVERIFY( compareLineStrings( polyline, line ) ); |
| 186 | +} |
| 187 | + |
| 188 | +void TestQgsGeometryImport::linestringGeos_data() |
| 189 | +{ |
| 190 | + QTest::addColumn<QVariantList>( "line" ); |
| 191 | + QVariantList line; |
| 192 | + line << QVariant( QPointF( 30.0, 10.0 ) ) << QVariant( QPointF( 10.0, 30.0 ) ) << QVariant( QPointF( 40.0, 40.0 ) ); |
| 193 | + QTest::newRow( "linestring_geos_1" ) << line; |
| 194 | +} |
| 195 | + |
| 196 | +void TestQgsGeometryImport::linestringGeos() |
| 197 | +{ |
| 198 | + QFETCH( QVariantList, line ); |
| 199 | + |
| 200 | + //create geos coord sequence first |
| 201 | + GEOSCoordSequence *coord = GEOSCoordSeq_create( line.count(), 2 ); |
| 202 | + for ( int i = 0; i < line.count(); i++ ) |
| 203 | + { |
| 204 | + QPointF pt = line.at( i ).toPointF(); |
| 205 | + GEOSCoordSeq_setX( coord, i, pt.x() ); |
| 206 | + GEOSCoordSeq_setY( coord, i, pt.y() ); |
| 207 | + } |
| 208 | + GEOSGeometry* geosLine = GEOSGeom_createLineString( coord ); |
| 209 | + QgsGeometry geom; |
| 210 | + geom.fromGeos( geosLine ); |
| 211 | + QVERIFY( geom.wkbType() == QGis::WKBLineString ); |
| 212 | + |
| 213 | + QgsPolyline polyline = geom.asPolyline(); |
| 214 | + QVERIFY( compareLineStrings( polyline, line ) ); |
| 215 | +} |
| 216 | + |
| 217 | +bool TestQgsGeometryImport::compareLineStrings( const QgsPolyline& polyline, QVariantList& line ) |
| 218 | +{ |
| 219 | + bool sizeEqual = ( polyline.size() == line.size() ); |
| 220 | + if ( !sizeEqual ) |
| 221 | + { |
| 222 | + return false; |
| 223 | + } |
| 224 | + |
| 225 | + for ( int i = 0; i < polyline.size(); ++i ) |
| 226 | + { |
| 227 | + const QgsPoint& polylinePt = polyline.at( i ); |
| 228 | + QPointF linePt = line.at( i ).toPointF(); |
| 229 | + if ( !qgsDoubleNear( polylinePt.x(), linePt.x() ) || !qgsDoubleNear( polylinePt.y(), linePt.y() ) ) |
| 230 | + { |
| 231 | + return false; |
| 232 | + } |
| 233 | + } |
| 234 | + return true; |
| 235 | +} |
| 236 | + |
| 237 | +QTEST_MAIN( TestQgsGeometryImport ) |
| 238 | +#include "moc_testqgsgeometryimport.cxx" |
| 239 | + |
0 commit comments