Skip to content

Commit 386d52d

Browse files
authored
Merge pull request #5247 from nyalldawson/geometry_coverage
Geometry unit test coverage
2 parents 5f11793 + 57108f9 commit 386d52d

18 files changed

+1972
-54
lines changed

doc/api_break.dox

+1
Original file line numberDiff line numberDiff line change
@@ -2005,6 +2005,7 @@ QgsRectangle {#qgis_api_break_3_0_QgsRectangle}
20052005

20062006
- The protected members were removed - QgsRectangle is not intended to be subclassed.
20072007
- unionRect was removed. Use combineExtentWith instead.
2008+
- buffer was renamed to buffered.
20082009

20092010
QgsRelation {#qgis_api_break_3_0_QgsRelation}
20102011
-----------

python/core/geometry/qgsrectangle.sip

+4-2
Original file line numberDiff line numberDiff line change
@@ -163,18 +163,20 @@ Copy constructor
163163

164164
void grow( double delta );
165165
%Docstring
166-
Grows the rectangle by the specified amount.
166+
Grows the rectangle in place by the specified amount.
167+
.. seealso:: buffered()
167168
%End
168169

169170
void include( const QgsPointXY &p );
170171
%Docstring
171172
Updates the rectangle to include the specified point.
172173
%End
173174

174-
QgsRectangle buffer( double width );
175+
QgsRectangle buffered( double width ) const;
175176
%Docstring
176177
Get rectangle enlarged by buffer.
177178
.. versionadded:: 2.1
179+
.. seealso:: grow()
178180
:rtype: QgsRectangle
179181
%End
180182

python/plugins/db_manager/db_plugins/postgis/plugins/qgis_topoview/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def run(item, action, mainwindow):
263263
ext = node_extent
264264
ext.combineExtentWith(edge_extent)
265265
# Grow by 1/20 of largest side
266-
ext = ext.buffer(max(ext.width(), ext.height()) / 20)
266+
ext = ext.buffered(max(ext.width(), ext.height()) / 20)
267267
canvas.setExtent(ext)
268268

269269
# restore canvas render flag

src/core/geometry/qgscompoundcurve.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ void QgsCompoundCurve::removeCurve( int i )
436436

437437
void QgsCompoundCurve::addVertex( const QgsPoint &pt )
438438
{
439-
if ( mWkbType == QgsWkbTypes::Unknown )
439+
if ( mCurves.isEmpty() || mWkbType == QgsWkbTypes::Unknown )
440440
{
441441
setZMTypeFromSubGeometry( &pt, QgsWkbTypes::CompoundCurve );
442442
}
@@ -606,15 +606,15 @@ bool QgsCompoundCurve::deleteVertex( QgsVertexId position )
606606
if ( mCurves.at( curveIds.at( 0 ).first )->numPoints() == 0 &&
607607
mCurves.at( curveIds.at( 1 ).first )->numPoints() != 0 )
608608
{
609-
removeCurve( curveIds.at( 0 ).first );
610609
mCurves.at( curveIds.at( 1 ).first )->moveVertex( QgsVertexId( 0, 0, 0 ), startPoint );
610+
removeCurve( curveIds.at( 0 ).first );
611611
}
612612
else if ( mCurves.at( curveIds.at( 0 ).first )->numPoints() != 0 &&
613613
mCurves.at( curveIds.at( 1 ).first )->numPoints() == 0 )
614614
{
615-
removeCurve( curveIds.at( 1 ).first );
616615
mCurves.at( curveIds.at( 0 ).first )->moveVertex(
617616
QgsVertexId( 0, 0, mCurves.at( curveIds.at( 0 ).first )->numPoints() - 1 ), endPoint );
617+
removeCurve( curveIds.at( 1 ).first );
618618
}
619619
else if ( mCurves.at( curveIds.at( 0 ).first )->numPoints() == 0 &&
620620
mCurves.at( curveIds.at( 1 ).first )->numPoints() == 0 )

src/core/geometry/qgsgeometry.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2669,7 +2669,7 @@ bool QgsGeometry::compare( const QgsMultiPolygon &p1, const QgsMultiPolygon &p2,
26692669

26702670
QgsGeometry QgsGeometry::smooth( const unsigned int iterations, const double offset, double minimumDistance, double maxAngle ) const
26712671
{
2672-
if ( d->geometry->isEmpty() )
2672+
if ( !d->geometry || d->geometry->isEmpty() )
26732673
return QgsGeometry();
26742674

26752675
QgsGeometry geom = *this;

src/core/geometry/qgsgeometrycollection.cpp

+25-17
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ bool QgsGeometryCollection::insertGeometry( QgsAbstractGeometry *g, int index )
128128
return false;
129129
}
130130

131+
index = std::min( mGeometries.count(), index );
132+
131133
mGeometries.insert( index, g );
132134
clearCache(); //set bounding box invalid
133135
return true;
@@ -183,17 +185,6 @@ void QgsGeometryCollection::transform( const QTransform &t )
183185
clearCache(); //set bounding box invalid
184186
}
185187

186-
#if 0
187-
void QgsGeometryCollection::clip( const QgsRectangle &rect )
188-
{
189-
QVector< QgsAbstractGeometry * >::iterator it = mGeometries.begin();
190-
for ( ; it != mGeometries.end(); ++it )
191-
{
192-
( *it )->clip( rect );
193-
}
194-
}
195-
#endif
196-
197188
void QgsGeometryCollection::draw( QPainter &p ) const
198189
{
199190
QVector< QgsAbstractGeometry * >::const_iterator it = mGeometries.constBegin();
@@ -210,7 +201,11 @@ bool QgsGeometryCollection::fromWkb( QgsConstWkbPtr &wkbPtr )
210201
return false;
211202
}
212203

213-
mWkbType = wkbPtr.readHeader();
204+
QgsWkbTypes::Type wkbType = wkbPtr.readHeader();
205+
if ( QgsWkbTypes::flatType( wkbType ) != QgsWkbTypes::flatType( mWkbType ) )
206+
return false;
207+
208+
mWkbType = wkbType;
214209

215210
int nGeometries = 0;
216211
wkbPtr >> nGeometries;
@@ -419,6 +414,9 @@ bool QgsGeometryCollection::nextVertex( QgsVertexId &id, QgsPoint &vertex ) cons
419414
return false;
420415
}
421416

417+
if ( id.part >= mGeometries.count() )
418+
return false;
419+
422420
QgsAbstractGeometry *geom = mGeometries.at( id.part );
423421
if ( geom->nextVertex( id, vertex ) )
424422
{
@@ -451,7 +449,7 @@ bool QgsGeometryCollection::insertVertex( QgsVertexId position, const QgsPoint &
451449

452450
bool QgsGeometryCollection::moveVertex( QgsVertexId position, const QgsPoint &newPos )
453451
{
454-
if ( position.part >= mGeometries.size() )
452+
if ( position.part < 0 || position.part >= mGeometries.size() )
455453
{
456454
return false;
457455
}
@@ -466,7 +464,7 @@ bool QgsGeometryCollection::moveVertex( QgsVertexId position, const QgsPoint &ne
466464

467465
bool QgsGeometryCollection::deleteVertex( QgsVertexId position )
468466
{
469-
if ( position.part >= mGeometries.size() )
467+
if ( position.part < 0 || position.part >= mGeometries.size() )
470468
{
471469
return false;
472470
}
@@ -615,7 +613,7 @@ QgsAbstractGeometry *QgsGeometryCollection::segmentize( double tolerance, Segmen
615613

616614
double QgsGeometryCollection::vertexAngle( QgsVertexId vertex ) const
617615
{
618-
if ( vertex.part >= mGeometries.size() )
616+
if ( vertex.part < 0 || vertex.part >= mGeometries.size() )
619617
{
620618
return 0.0;
621619
}
@@ -631,11 +629,21 @@ double QgsGeometryCollection::vertexAngle( QgsVertexId vertex ) const
631629

632630
int QgsGeometryCollection::vertexCount( int part, int ring ) const
633631
{
632+
if ( part < 0 || part >= mGeometries.size() )
633+
{
634+
return 0;
635+
}
636+
634637
return mGeometries[part]->vertexCount( 0, ring );
635638
}
636639

637640
int QgsGeometryCollection::ringCount( int part ) const
638641
{
642+
if ( part < 0 || part >= mGeometries.size() )
643+
{
644+
return 0;
645+
}
646+
639647
return mGeometries[part]->ringCount();
640648
}
641649

@@ -682,7 +690,7 @@ bool QgsGeometryCollection::addMValue( double mValue )
682690

683691
bool QgsGeometryCollection::dropZValue()
684692
{
685-
if ( !is3D() )
693+
if ( mWkbType != QgsWkbTypes::GeometryCollection && !is3D() )
686694
return false;
687695

688696
mWkbType = QgsWkbTypes::dropZ( mWkbType );
@@ -696,7 +704,7 @@ bool QgsGeometryCollection::dropZValue()
696704

697705
bool QgsGeometryCollection::dropMValue()
698706
{
699-
if ( !isMeasure() )
707+
if ( mWkbType != QgsWkbTypes::GeometryCollection && !isMeasure() )
700708
return false;
701709

702710
mWkbType = QgsWkbTypes::dropM( mWkbType );

src/core/geometry/qgsrectangle.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void QgsRectangle::include( const QgsPointXY &p )
147147
setYMaximum( p.y() );
148148
}
149149

150-
QgsRectangle QgsRectangle::buffer( double width )
150+
QgsRectangle QgsRectangle::buffered( double width ) const
151151
{
152152
return QgsRectangle( mXmin - width, mYmin - width, mXmax + width, mYmax + width );
153153
}
@@ -254,7 +254,7 @@ QgsRectangle &QgsRectangle::operator+=( const QgsVector v )
254254

255255
bool QgsRectangle::isEmpty() const
256256
{
257-
return mXmax <= mXmin || mYmax <= mYmin;
257+
return mXmax < mXmin || mYmax < mYmin || qgsDoubleNear( mXmax, mXmin ) || qgsDoubleNear( mYmax, mYmin );
258258
}
259259

260260
bool QgsRectangle::isNull() const

src/core/geometry/qgsrectangle.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ class CORE_EXPORT QgsRectangle
161161
void scale( double scaleFactor, double centerX, double centerY );
162162

163163
/**
164-
* Grows the rectangle by the specified amount.
164+
* Grows the rectangle in place by the specified amount.
165+
* \see buffered()
165166
*/
166167
void grow( double delta );
167168

@@ -173,8 +174,9 @@ class CORE_EXPORT QgsRectangle
173174
/**
174175
* Get rectangle enlarged by buffer.
175176
* \since QGIS 2.1
177+
* \see grow()
176178
*/
177-
QgsRectangle buffer( double width );
179+
QgsRectangle buffered( double width ) const;
178180

179181
/**
180182
* Return the intersection with the given rectangle.

src/gui/qgsgeometryrubberband.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,5 @@ QgsRectangle QgsGeometryRubberBand::rubberBandRectangle() const
157157
qreal scale = mMapCanvas->mapUnitsPerPixel();
158158
qreal s = ( mIconSize - 1 ) / 2.0 * scale;
159159
qreal p = mPen.width() * scale;
160-
return mGeometry->boundingBox().buffer( s + p );
160+
return mGeometry->boundingBox().buffered( s + p );
161161
}

0 commit comments

Comments
 (0)