Skip to content

Commit 430953c

Browse files
committedJul 30, 2017
Fix crash for rectangle (centerPoint - extent)
1 parent 0451e6d commit 430953c

File tree

6 files changed

+41
-339
lines changed

6 files changed

+41
-339
lines changed
 

Diff for: ‎python/core/geometry/qgsrectangle.sip

-327
Original file line numberDiff line numberDiff line change
@@ -1,327 +0,0 @@
1-
/************************************************************************
2-
* This file has been generated automatically from *
3-
* *
4-
* src/core/geometry/qgsrectangle.h *
5-
* *
6-
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7-
************************************************************************/
8-
9-
10-
11-
12-
13-
14-
class QgsRectangle
15-
{
16-
%Docstring
17-
A rectangle specified with double values.
18-
19-
QgsRectangle is used to store a rectangle when double values are required.
20-
Examples are storing a layer extent or the current view extent of a map
21-
.. seealso:: QgsBox3d
22-
%End
23-
24-
%TypeHeaderCode
25-
#include "qgsrectangle.h"
26-
%End
27-
public:
28-
QgsRectangle( double xMin = 0, double yMin = 0, double xMax = 0, double yMax = 0 );
29-
%Docstring
30-
Constructor
31-
%End
32-
QgsRectangle( const QgsPointXY &p1, const QgsPointXY &p2 );
33-
%Docstring
34-
Construct a rectangle from two points. The rectangle is normalized after construction.
35-
%End
36-
QgsRectangle( const QRectF &qRectF );
37-
%Docstring
38-
Construct a rectangle from a QRectF. The rectangle is normalized after construction.
39-
%End
40-
QgsRectangle( const QgsRectangle &other );
41-
%Docstring
42-
Copy constructor
43-
%End
44-
45-
void set( const QgsPointXY &p1, const QgsPointXY &p2 );
46-
%Docstring
47-
Sets the rectangle from two QgsPoints. The rectangle is
48-
normalised after construction.
49-
%End
50-
51-
void set( double mXmin, double mYmin, double mXmax, double mYmax );
52-
%Docstring
53-
Sets the rectangle from four points. The rectangle is
54-
normalised after construction.
55-
%End
56-
57-
void setXMinimum( double x );
58-
%Docstring
59-
Set the minimum x value.
60-
%End
61-
62-
void setXMaximum( double x );
63-
%Docstring
64-
Set the maximum x value.
65-
%End
66-
67-
void setYMinimum( double y );
68-
%Docstring
69-
Set the minimum y value.
70-
%End
71-
72-
void setYMaximum( double y );
73-
%Docstring
74-
Set the maximum y value.
75-
%End
76-
77-
void setMinimal();
78-
%Docstring
79-
Set a rectangle so that min corner is at max
80-
and max corner is at min. It is NOT normalized.
81-
%End
82-
83-
double xMaximum() const;
84-
%Docstring
85-
Returns the x maximum value (right side of rectangle).
86-
:rtype: float
87-
%End
88-
89-
double xMinimum() const;
90-
%Docstring
91-
Returns the x minimum value (left side of rectangle).
92-
:rtype: float
93-
%End
94-
95-
double yMaximum() const;
96-
%Docstring
97-
Returns the y maximum value (top side of rectangle).
98-
:rtype: float
99-
%End
100-
101-
double yMinimum() const;
102-
%Docstring
103-
Returns the y minimum value (bottom side of rectangle).
104-
:rtype: float
105-
%End
106-
107-
void normalize();
108-
%Docstring
109-
Normalize the rectangle so it has non-negative width/height.
110-
%End
111-
112-
double width() const;
113-
%Docstring
114-
Returns the width of the rectangle.
115-
.. seealso:: height()
116-
.. seealso:: area()
117-
:rtype: float
118-
%End
119-
120-
double height() const;
121-
%Docstring
122-
Returns the height of the rectangle.
123-
.. seealso:: width()
124-
.. seealso:: area()
125-
:rtype: float
126-
%End
127-
128-
double area() const;
129-
%Docstring
130-
Returns the area of the rectangle.
131-
.. versionadded:: 3.0
132-
.. seealso:: width()
133-
.. seealso:: height()
134-
.. seealso:: perimeter()
135-
:rtype: float
136-
%End
137-
138-
double perimeter() const;
139-
%Docstring
140-
Returns the perimeter of the rectangle.
141-
.. versionadded:: 3.0
142-
.. seealso:: area()
143-
:rtype: float
144-
%End
145-
146-
QgsPointXY center() const;
147-
%Docstring
148-
Returns the center point of the rectangle.
149-
:rtype: QgsPointXY
150-
%End
151-
152-
void scale( double scaleFactor, const QgsPointXY *c = 0 );
153-
%Docstring
154-
Scale the rectangle around its center point.
155-
%End
156-
157-
void scale( double scaleFactor, double centerX, double centerY );
158-
%Docstring
159-
Scale the rectangle around its center point.
160-
%End
161-
162-
void grow( double delta );
163-
%Docstring
164-
Grows the rectangle by the specified amount.
165-
%End
166-
167-
void include( const QgsPointXY &p );
168-
%Docstring
169-
Updates the rectangle to include the specified point.
170-
%End
171-
172-
QgsRectangle buffer( double width );
173-
%Docstring
174-
Get rectangle enlarged by buffer.
175-
.. versionadded:: 2.1
176-
:rtype: QgsRectangle
177-
%End
178-
179-
QgsRectangle intersect( const QgsRectangle *rect ) const;
180-
%Docstring
181-
Return the intersection with the given rectangle.
182-
:rtype: QgsRectangle
183-
%End
184-
185-
bool intersects( const QgsRectangle &rect ) const;
186-
%Docstring
187-
Returns true when rectangle intersects with other rectangle.
188-
:rtype: bool
189-
%End
190-
191-
bool contains( const QgsRectangle &rect ) const;
192-
%Docstring
193-
Return true when rectangle contains other rectangle.
194-
:rtype: bool
195-
%End
196-
197-
bool contains( const QgsPointXY &p ) const;
198-
%Docstring
199-
Return true when rectangle contains a point.
200-
:rtype: bool
201-
%End
202-
203-
void combineExtentWith( const QgsRectangle &rect );
204-
%Docstring
205-
Expand the rectangle so that covers both the original rectangle and the given rectangle.
206-
%End
207-
208-
void combineExtentWith( double x, double y );
209-
%Docstring
210-
Expand the rectangle so that covers both the original rectangle and the given point.
211-
%End
212-
213-
QgsRectangle operator-( const QgsVector v ) const;
214-
%Docstring
215-
Returns a rectangle offset from this one in the direction of the reversed vector.
216-
.. versionadded:: 3.0
217-
:rtype: QgsRectangle
218-
%End
219-
220-
QgsRectangle operator+( const QgsVector v ) const;
221-
%Docstring
222-
Returns a rectangle offset from this one in the direction of the vector.
223-
.. versionadded:: 3.0
224-
:rtype: QgsRectangle
225-
%End
226-
227-
QgsRectangle &operator-=( const QgsVector v );
228-
%Docstring
229-
Moves this rectangle in the direction of the reversed vector.
230-
.. versionadded:: 3.0
231-
:rtype: QgsRectangle
232-
%End
233-
234-
QgsRectangle &operator+=( const QgsVector v );
235-
%Docstring
236-
Moves this rectangle in the direction of the vector.
237-
.. versionadded:: 3.0
238-
:rtype: QgsRectangle
239-
%End
240-
241-
bool isEmpty() const;
242-
%Docstring
243-
Returns true if the rectangle is empty.
244-
An empty rectangle may still be non-null if it contains valid information (e.g. bounding box of a point).
245-
:rtype: bool
246-
%End
247-
248-
bool isNull() const;
249-
%Docstring
250-
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
251-
A null rectangle is also an empty rectangle.
252-
.. versionadded:: 2.4
253-
:rtype: bool
254-
%End
255-
256-
QString asWktCoordinates() const;
257-
%Docstring
258-
Returns a string representation of the rectangle in WKT format.
259-
:rtype: str
260-
%End
261-
262-
QString asWktPolygon() const;
263-
%Docstring
264-
Returns a string representation of the rectangle as a WKT Polygon.
265-
:rtype: str
266-
%End
267-
268-
QRectF toRectF() const;
269-
%Docstring
270-
Returns a QRectF with same coordinates as the rectangle.
271-
:rtype: QRectF
272-
%End
273-
274-
QString toString( int precision = 16 ) const;
275-
%Docstring
276-
Returns a string representation of form xmin,ymin : xmax,ymax
277-
Coordinates will be truncated to the specified precision.
278-
If the specified precision is less than 0, a suitable minimum precision is used.
279-
:rtype: str
280-
%End
281-
282-
QString asPolygon() const;
283-
%Docstring
284-
Returns the rectangle as a polygon.
285-
:rtype: str
286-
%End
287-
288-
bool operator==( const QgsRectangle &r1 ) const;
289-
290-
bool operator!=( const QgsRectangle &r1 ) const;
291-
%Docstring
292-
Comparison operator
293-
:return: False if rectangles are equal
294-
:rtype: bool
295-
%End
296-
297-
298-
bool isFinite() const;
299-
%Docstring
300-
Returns true if the rectangle has finite boundaries. Will
301-
return false if any of the rectangle boundaries are NaN or Inf.
302-
:rtype: bool
303-
%End
304-
305-
void invert();
306-
%Docstring
307-
Swap x/y coordinates in the rectangle.
308-
%End
309-
310-
QgsBox3d toBox3d( double zMin, double zMax ) const;
311-
%Docstring
312-
Converts the rectangle to a 3D box, with the specified
313-
``zMin`` and ``zMax`` z values.
314-
.. versionadded:: 3.0
315-
:rtype: QgsBox3d
316-
%End
317-
318-
};
319-
320-
321-
/************************************************************************
322-
* This file has been generated automatically from *
323-
* *
324-
* src/core/geometry/qgsrectangle.h *
325-
* *
326-
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
327-
************************************************************************/

Diff for: ‎src/app/qgsmaptooladdrectangle.cpp

+33-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "qgsmaptooladdrectangle.h"
1717
#include "qgscompoundcurve.h"
1818
#include "qgscurvepolygon.h"
19+
#include "qgslinestring.h"
20+
#include "qgspolygon.h"
1921
#include "qgsgeometryrubberband.h"
2022
#include "qgsgeometryutils.h"
2123
#include "qgsmapcanvas.h"
@@ -97,16 +99,44 @@ void QgsMapToolAddRectangle::keyReleaseEvent( QKeyEvent *e )
9799
}
98100
}
99101

102+
QgsLineString *QgsMapToolAddRectangle::rectangleToLinestring( ) const
103+
{
104+
std::unique_ptr<QgsLineString> ext( new QgsLineString() );
105+
if ( mRectangle.isEmpty() )
106+
{
107+
return ext.release();
108+
}
109+
110+
ext->addVertex( QgsPoint( mRectangle.xMinimum(), mRectangle.yMinimum() ) );
111+
ext->addVertex( QgsPoint( mRectangle.xMinimum(), mRectangle.yMaximum() ) );
112+
ext->addVertex( QgsPoint( mRectangle.xMaximum(), mRectangle.yMaximum() ) );
113+
ext->addVertex( QgsPoint( mRectangle.xMaximum(), mRectangle.yMinimum() ) );
114+
ext->addVertex( QgsPoint( mRectangle.xMinimum(), mRectangle.yMinimum() ) );
115+
116+
return ext.release();
117+
}
118+
119+
QgsPolygonV2 *QgsMapToolAddRectangle::rectangleToPolygon() const
120+
{
121+
std::unique_ptr<QgsPolygonV2> polygon( new QgsPolygonV2() );
122+
if ( mRectangle.isEmpty() )
123+
{
124+
return polygon.release();
125+
}
126+
127+
polygon->setExteriorRing( rectangleToLinestring( ) );
128+
129+
return polygon.release();
130+
}
131+
100132
void QgsMapToolAddRectangle::deactivate()
101133
{
102134
if ( !mParentTool || mRectangle.isEmpty() )
103135
{
104136
return;
105137
}
106-
std::unique_ptr<QgsPolygonV2> rubber( new QgsPolygonV2() );
107-
rubber->fromWkt( mRectangle.asPolygon() );
108138

109-
mParentTool->addCurve( rubber.release()->exteriorRing()->clone() );
139+
mParentTool->addCurve( rectangleToLinestring() );
110140
delete mTempRubberBand;
111141
mTempRubberBand = nullptr;
112142
mPoints.clear();

Diff for: ‎src/app/qgsmaptooladdrectangle.h

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class QgsMapToolAddRectangle: public QgsMapToolCapture
5252
//! Rectangle
5353
QgsRectangle mRectangle;
5454

55+
QgsLineString *rectangleToLinestring() const;
56+
QgsPolygonV2 *rectangleToPolygon() const;
5557
};
5658

5759

Diff for: ‎src/app/qgsmaptoolrectanglecenter.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,9 @@ void QgsMapToolRectangleCenter::cadCanvasMoveEvent( QgsMapMouseEvent *e )
7272
double xOffset = fabs( mapPoint.x() - mPoints.at( 0 ).x() );
7373
double yOffset = fabs( mapPoint.y() - mPoints.at( 0 ).y() );
7474

75-
mRectangle = QgsRectangle( QgsPoint( -xOffset, -yOffset ), QgsPoint( xOffset, yOffset ) );
75+
mRectangle = QgsRectangle( QgsPoint( mPoints.at( 0 ).x() - xOffset, mPoints.at( 0 ).y() - yOffset ), QgsPoint( mPoints.at( 0 ).x() + xOffset, mPoints.at( 0 ).y() + yOffset ) );
7676

77-
std::unique_ptr<QgsPolygonV2> rubber( new QgsPolygonV2() );
78-
rubber->fromWkt( mRectangle.asPolygon() );
79-
80-
mTempRubberBand->setGeometry( rubber.release() );
77+
mTempRubberBand->setGeometry( QgsMapToolAddRectangle::rectangleToPolygon() );
8178
}
8279
break;
8380
default:

Diff for: ‎src/app/qgsmaptoolrectangleextent.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "qgsmaptoolrectangleextent.h"
1717
#include "qgsgeometryrubberband.h"
1818
#include "qgsgeometryutils.h"
19-
#include "qgspolygon.h"
2019
#include "qgsmapcanvas.h"
2120
#include "qgspoint.h"
2221
#include <QMouseEvent>
@@ -70,10 +69,8 @@ void QgsMapToolRectangleExtent::cadCanvasMoveEvent( QgsMapMouseEvent *e )
7069
case 1:
7170
{
7271
mRectangle = QgsRectangle( mPoints.at( 0 ), mapPoint );
73-
std::unique_ptr<QgsPolygonV2> rubber( new QgsPolygonV2() );
74-
rubber->fromWkt( mRectangle.asPolygon() );
7572

76-
mTempRubberBand->setGeometry( rubber.release() );
73+
mTempRubberBand->setGeometry( QgsMapToolAddRectangle::rectangleToPolygon() );
7774
}
7875
break;
7976
default:

Diff for: ‎src/core/geometry/qgsrectangle.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424
#include <QTransform>
2525
#include <QRegExp>
2626
#include <qnumeric.h>
27+
#include <memory>
2728

2829
#include "qgspointxy.h"
2930
#include "qgsrectangle.h"
3031
#include "qgslogger.h"
3132
#include "qgsbox3d.h"
33+
#include "qgspolygon.h"
34+
#include "qgslinestring.h"
3235

3336
QgsRectangle::QgsRectangle( double xMin, double yMin, double xMax, double yMax )
3437
: mXmin( xMin )

0 commit comments

Comments
 (0)
Please sign in to comment.