Skip to content

Commit 098611c

Browse files
Gustrynyalldawson
authored andcommittedOct 11, 2017
add helper to build a QgsRectangle from a WKT string
1 parent 5515d68 commit 098611c

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed
 

‎python/core/geometry/qgsrectangle.sip

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ Copy constructor
4444

4545
~QgsRectangle();
4646

47+
static QgsRectangle fromWkt( const QString &wkt );
48+
%Docstring
49+
Creates a new rectangle from a WKT string.
50+
The WKT must contain only 5 vertices, representing a rectangle aligned with X and Y axes.
51+
.. versionadded:: 3.0
52+
:rtype: QgsRectangle
53+
%End
54+
4755
void set( const QgsPointXY &p1, const QgsPointXY &p2 );
4856
%Docstring
4957
Sets the rectangle from two QgsPoints. The rectangle is

‎src/core/geometry/qgsrectangle.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <QTransform>
2525
#include <QRegExp>
2626

27+
#include "qgsgeometry.h"
2728
#include "qgspointxy.h"
2829
#include "qgsrectangle.h"
2930
#include "qgslogger.h"
@@ -59,6 +60,24 @@ QgsRectangle::QgsRectangle( const QgsRectangle &r )
5960
mYmax = r.yMaximum();
6061
}
6162

63+
QgsRectangle QgsRectangle::fromWkt( const QString &wkt )
64+
{
65+
QgsGeometry geom = QgsGeometry::fromWkt( wkt );
66+
if ( geom.isMultipart() )
67+
return QgsRectangle();
68+
69+
QgsPolygon poly = geom.asPolygon();
70+
71+
if ( poly.size() != 1 )
72+
return QgsRectangle();
73+
74+
QgsPolyline polyline = geom.asPolygon().at( 0 );
75+
if ( polyline.size() == 5 && polyline.at( 0 ) == polyline.at( 4 ) && geom.isGeosValid() )
76+
return QgsRectangle( polyline.at( 0 ).x(), polyline.at( 0 ).y(), polyline.at( 2 ).x(), polyline.at( 2 ).y() );
77+
else
78+
return QgsRectangle();
79+
}
80+
6281
void QgsRectangle::set( const QgsPointXY &p1, const QgsPointXY &p2 )
6382
{
6483
mXmin = p1.x();

‎src/core/geometry/qgsrectangle.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ class CORE_EXPORT QgsRectangle
5353
// see https://github.com/qgis/QGIS/pull/4720#issuecomment-308652392
5454
~QgsRectangle() = default;
5555

56+
/**
57+
* Creates a new rectangle from a WKT string.
58+
* The WKT must contain only 5 vertices, representing a rectangle aligned with X and Y axes.
59+
* \since QGIS 3.0
60+
*/
61+
static QgsRectangle fromWkt( const QString &wkt );
62+
5663
/**
5764
* Sets the rectangle from two QgsPoints. The rectangle is
5865
* normalised after construction.

‎tests/src/core/testqgsrectangle.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class TestQgsRectangle: public QObject
2626
Q_OBJECT
2727
private slots:
2828
void isEmpty();
29+
void fromWkt();
2930
void manipulate();
3031
void regression6194();
3132
void operators();
@@ -56,6 +57,25 @@ void TestQgsRectangle::isEmpty()
5657
QVERIFY( r.isEmpty() );
5758
}
5859

60+
void TestQgsRectangle::fromWkt()
61+
{
62+
QgsRectangle rect = QgsRectangle::fromWkt( QStringLiteral( "POLYGON((0 0,1 0,1 1,0 1,0 0))" ) );
63+
QVERIFY( ! rect.isEmpty() );
64+
QCOMPARE( rect.xMinimum(), 0.0 );
65+
QCOMPARE( rect.yMinimum(), 0.0 );
66+
QCOMPARE( rect.xMaximum(), 1.0 );
67+
QCOMPARE( rect.yMaximum(), 1.0 );
68+
69+
QVERIFY( rect == QgsRectangle::fromWkt( rect.asWktPolygon() ) );
70+
71+
QVERIFY( QgsRectangle::fromWkt( QStringLiteral( "LINESTRING (0 0,1 0,1 1,0 1,0 0)" ) ).isEmpty() );
72+
QVERIFY( QgsRectangle::fromWkt( QStringLiteral( "MULTIPOLYGON(((0 0,3 0,3 3,0 3,0 0)),((1 1, 1 2, 2 2, 2 1)))" ) ).isEmpty() );
73+
QVERIFY( QgsRectangle::fromWkt( QStringLiteral( "MULTIPOLYGON(((0 0,3 0,3 3,0 3,0 0), (10 10,13 10,13 13,10 13,10 10)))" ) ).isEmpty() );
74+
QVERIFY( QgsRectangle::fromWkt( QStringLiteral( "POLYGON((0 0,1 0,1 1,0 1,0 1))" ) ).isEmpty() );
75+
QVERIFY( QgsRectangle::fromWkt( QStringLiteral( "POLYGON((0 0,1 0,1 1,0 1,0 1))" ) ).isEmpty() );
76+
QVERIFY( QgsRectangle::fromWkt( QStringLiteral( "POLYGON((0 0,1 0,1 1,0 1))" ) ).isEmpty() );
77+
}
78+
5979
void TestQgsRectangle::manipulate()
6080
{
6181
// Set up two intersecting rectangles and normalize

0 commit comments

Comments
 (0)
Please sign in to comment.