Skip to content

Commit

Permalink
Make all QgsGeometry methods return values, not pointers
Browse files Browse the repository at this point in the history
Reduces likelihood of crashes and leaks
  • Loading branch information
nyalldawson committed Aug 1, 2016
1 parent bd7d913 commit 4b78b1c
Show file tree
Hide file tree
Showing 103 changed files with 1,158 additions and 1,562 deletions.
10 changes: 9 additions & 1 deletion doc/api_break.dox
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,18 @@ true, as the method will return an empty geometry if the feature has no geometry
<li>setFields( const QgsFields*, bool ) has been removed, use setFields( const QgsFields&, bool ) instead.</li>
</ul>

\subsection qgis_api_break_3_0_QgsGeometry QgsGeometry

<ul>
<li>All QgsGeometry methods now accept geometry references instead of pointers, and return a QgsGeometry
value instead of a pointer.</li>
</ul>

\subsection qgis_api_break_3_0_QgsGeometryAnalyzer QgsGeometryAnalyzer

<ul>
<li>locateBetweenMeasures() and locateAlongMeasure() now take geometry references, not pointers</li>
<li>locateBetweenMeasures() and locateAlongMeasure() now take geometry references, not pointers, and return
a QgsGeometry value rather than a pointer.</li>
</ul>

\subsection qgis_api_break_3_0_QgsGroupWMSDataDialog QgsGroupWMSDataDialog
Expand Down
4 changes: 2 additions & 2 deletions python/analysis/vector/qgsgeometryanalyzer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ class QgsGeometryAnalyzer
bool forceSingleGeometry = false, QgsVectorDataProvider* memoryProvider = 0, QProgressDialog* p = 0 );

/** Returns linear reference geometry as a multiline (or 0 if no match). Currently, the z-coordinates are considered to be the measures (no support for m-values in QGIS)*/
QgsGeometry* locateBetweenMeasures( double fromMeasure, double toMeasure, const QgsGeometry& lineGeom );
QgsGeometry locateBetweenMeasures( double fromMeasure, double toMeasure, const QgsGeometry& lineGeom );
/** Returns linear reference geometry. Unlike the PostGIS function, this method always returns multipoint or 0 if no match (not geometry collection).
* Currently, the z-coordinates are considered to be the measures (no support for m-values in QGIS)
*/
QgsGeometry* locateAlongMeasure( double measure, const QgsGeometry& lineGeom );
QgsGeometry locateAlongMeasure( double measure, const QgsGeometry& lineGeom );

};
70 changes: 36 additions & 34 deletions python/core/geometry/qgsgeometry.sip
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,21 @@ class QgsGeometry
bool isEmpty() const;

/** Creates a new geometry from a WKT string */
static QgsGeometry* fromWkt( const QString& wkt ) /Factory/;
static QgsGeometry fromWkt( const QString& wkt );
/** Creates a new geometry from a QgsPoint object*/
static QgsGeometry* fromPoint( const QgsPoint& point ) /Factory/;
static QgsGeometry fromPoint( const QgsPoint& point );
/** Creates a new geometry from a QgsMultiPoint object */
static QgsGeometry* fromMultiPoint( const QgsMultiPoint& multipoint ) /Factory/;
static QgsGeometry fromMultiPoint( const QgsMultiPoint& multipoint );
/** Creates a new geometry from a QgsPolyline object */
static QgsGeometry* fromPolyline( const QgsPolyline& polyline ) /Factory/;
static QgsGeometry fromPolyline( const QgsPolyline& polyline );
/** Creates a new geometry from a QgsMultiPolyline object*/
static QgsGeometry* fromMultiPolyline( const QgsMultiPolyline& multiline ) /Factory/;
static QgsGeometry fromMultiPolyline( const QgsMultiPolyline& multiline );
/** Creates a new geometry from a QgsPolygon */
static QgsGeometry* fromPolygon( const QgsPolygon& polygon ) /Factory/;
static QgsGeometry fromPolygon( const QgsPolygon& polygon );
/** Creates a new geometry from a QgsMultiPolygon */
static QgsGeometry* fromMultiPolygon( const QgsMultiPolygon& multipoly ) /Factory/;
static QgsGeometry fromMultiPolygon( const QgsMultiPolygon& multipoly );
/** Creates a new geometry from a QgsRectangle */
static QgsGeometry* fromRect( const QgsRectangle& rect ) /Factory/;
static QgsGeometry fromRect( const QgsRectangle& rect );

/**
* Set the geometry, feeding in a geometry in GEOS format.
Expand Down Expand Up @@ -408,42 +408,42 @@ class QgsGeometry
bool intersects( const QgsRectangle& r ) const;

/** Test for intersection with a geometry (uses GEOS) */
bool intersects( const QgsGeometry* geometry ) const;
bool intersects( const QgsGeometry& geometry ) const;

/** Test for containment of a point (uses GEOS) */
bool contains( const QgsPoint* p ) const;

/** Test for if geometry is contained in another (uses GEOS)
* @note added in 1.5 */
bool contains( const QgsGeometry* geometry ) const;
bool contains( const QgsGeometry& geometry ) const;

/** Test for if geometry is disjoint of another (uses GEOS)
* @note added in 1.5 */
bool disjoint( const QgsGeometry* geometry ) const;
bool disjoint( const QgsGeometry& geometry ) const;

/** Test for if geometry equals another (uses GEOS)
* @note added in 1.5 */
bool equals( const QgsGeometry* geometry ) const;
bool equals( const QgsGeometry& geometry ) const;

/** Test for if geometry touch another (uses GEOS)
* @note added in 1.5 */
bool touches( const QgsGeometry* geometry ) const;
bool touches( const QgsGeometry& geometry ) const;

/** Test for if geometry overlaps another (uses GEOS)
* @note added in 1.5 */
bool overlaps( const QgsGeometry* geometry ) const;
bool overlaps( const QgsGeometry& geometry ) const;

/** Test for if geometry is within another (uses GEOS)
* @note added in 1.5 */
bool within( const QgsGeometry* geometry ) const;
bool within( const QgsGeometry& geometry ) const;

/** Test for if geometry crosses another (uses GEOS)
* @note added in 1.5 */
bool crosses( const QgsGeometry* geometry ) const;
bool crosses( const QgsGeometry& geometry ) const;

/** Returns a buffer region around this geometry having the given width and with a specified number
of segments used to approximate curves */
QgsGeometry* buffer( double distance, int segments ) const /Factory/;
QgsGeometry buffer( double distance, int segments ) const;

/** Returns a buffer region around the geometry, with additional style options.
* @param distance buffer distance
Expand All @@ -454,44 +454,44 @@ class QgsGeometry
* @note added in 2.4
* @note needs GEOS >= 3.3 - otherwise always returns 0
*/
QgsGeometry* buffer( double distance, int segments, int endCapStyle, int joinStyle, double mitreLimit ) const /Factory/;
QgsGeometry buffer( double distance, int segments, int endCapStyle, int joinStyle, double mitreLimit ) const;

/** Returns an offset line at a given distance and side from an input line.
* See buffer() method for details on parameters.
* @note added in 2.4
* @note needs GEOS >= 3.3 - otherwise always returns 0
*/
QgsGeometry* offsetCurve( double distance, int segments, int joinStyle, double mitreLimit ) const /Factory/;
QgsGeometry offsetCurve( double distance, int segments, int joinStyle, double mitreLimit ) const;

/** Returns a simplified version of this geometry using a specified tolerance value */
QgsGeometry* simplify( double tolerance ) const /Factory/;
QgsGeometry simplify( double tolerance ) const;

/** Returns the center of mass of a geometry
* @note for line based geometries, the center point of the line is returned,
* and for point based geometries, the point itself is returned
*/
QgsGeometry* centroid() const /Factory/;
QgsGeometry centroid() const;

/** Returns a point within a geometry */
QgsGeometry* pointOnSurface() const /Factory/;
QgsGeometry pointOnSurface() const;

/** Returns the smallest convex polygon that contains all the points in the geometry. */
QgsGeometry* convexHull() const /Factory/;
QgsGeometry convexHull() const;

/**
* Return interpolated point on line at distance
* @note added in 1.9
*/
QgsGeometry* interpolate( double distance ) /Factory/;
QgsGeometry interpolate( double distance );

/** Returns a geometry representing the points shared by this geometry and other. */
QgsGeometry* intersection( const QgsGeometry* geometry ) const /Factory/;
QgsGeometry intersection( const QgsGeometry& geometry ) const;

/** Returns a geometry representing all the points in this geometry and other (a
* union geometry operation).
* @note this operation is not called union since its a reserved word in C++.
*/
QgsGeometry* combine( const QgsGeometry* geometry ) const /Factory/;
QgsGeometry combine( const QgsGeometry& geometry ) const;

/** Merges any connected lines in a LineString/MultiLineString geometry and
* converts them to single line strings.
Expand All @@ -503,10 +503,10 @@ class QgsGeometry
QgsGeometry mergeLines() const;

/** Returns a geometry representing the points making up this geometry that do not make up other. */
QgsGeometry* difference( const QgsGeometry* geometry ) const /Factory/;
QgsGeometry difference( const QgsGeometry& geometry ) const;

/** Returns a geometry representing the points making up this geometry that do not make up other. */
QgsGeometry* symDifference( const QgsGeometry* geometry ) const /Factory/;
QgsGeometry symDifference( const QgsGeometry& geometry ) const;

/** Returns an extruded version of this geometry. */
QgsGeometry extrude( double x, double y );
Expand All @@ -531,7 +531,7 @@ class QgsGeometry
* @return the converted geometry or nullptr if the conversion fails.
* @note added in 2.2
*/
QgsGeometry* convertToType( Qgis::GeometryType destType, bool destMultipart = false ) const /Factory/;
QgsGeometry convertToType( Qgis::GeometryType destType, bool destMultipart = false ) const;

/* Accessor functions for getting geometry data */

Expand Down Expand Up @@ -564,7 +564,7 @@ class QgsGeometry

/** Return contents of the geometry as a list of geometries
@note added in version 1.1 */
QList<QgsGeometry*> asGeometryCollection() const /Factory/;
QList<QgsGeometry> asGeometryCollection() const;

/** Return contents of the geometry as a QPointF if wkbType is WKBPoint,
* otherwise returns a null QPointF.
Expand Down Expand Up @@ -643,7 +643,7 @@ class QgsGeometry
* @param geometryList a list of QgsGeometry* as input
* @returns the new computed QgsGeometry, or null
*/
static QgsGeometry *unaryUnion( const QList<QgsGeometry*>& geometryList ) /Factory/;
static QgsGeometry unaryUnion( const QList<QgsGeometry>& geometryList );

/** Converts the geometry to straight line segments, if it is a curved geometry type.
* @note added in QGIS 2.10
Expand Down Expand Up @@ -704,15 +704,15 @@ class QgsGeometry
* @param point source QPointF
* @note added in QGIS 2.7
*/
static QgsGeometry* fromQPointF( QPointF point ) /Factory/;
static QgsGeometry fromQPointF( QPointF point );

/** Construct geometry from a QPolygonF. If the polygon is closed than
* the resultant geometry will be a polygon, if it is open than the
* geometry will be a polyline.
* @param polygon source QPolygonF
* @note added in QGIS 2.7
*/
static QgsGeometry* fromQPolygonF( const QPolygonF& polygon ) /Factory/;
static QgsGeometry fromQPolygonF( const QPolygonF& polygon );

/** Creates a QgsPolyline from a QPolygonF.
* @param polygon source polygon
Expand Down Expand Up @@ -768,7 +768,7 @@ class QgsGeometry
* of the geometry for each iteration. Smaller values result in "tighter" smoothing.
* @note added in 2.9
*/
QgsGeometry* smooth( const unsigned int iterations, const double offset ) const;
QgsGeometry smooth( const unsigned int iterations, const double offset ) const;

/** Smooths a polygon using the Chaikin algorithm*/
QgsPolygon smoothPolygon( const QgsPolygon &polygon, const unsigned int iterations = 1, const double offset = 0.25 ) const;
Expand All @@ -794,5 +794,7 @@ class QgsGeometry
//! Allows direct construction of QVariants from geometry.
operator QVariant() const;

operator bool() const;

}; // class QgsGeometry

4 changes: 2 additions & 2 deletions python/core/qgsgeometrysimplifier.sip
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class QgsAbstractGeometrySimplifier
virtual ~QgsAbstractGeometrySimplifier();

//! Returns a simplified version the specified geometry
virtual QgsGeometry* simplify( QgsGeometry* geometry ) const = 0;
virtual QgsGeometry simplify( const QgsGeometry& geometry ) const = 0;
//! Simplifies the specified geometry
virtual bool simplifyGeometry( QgsGeometry* geometry ) const = 0;

Expand Down Expand Up @@ -41,7 +41,7 @@ class QgsTopologyPreservingSimplifier : QgsAbstractGeometrySimplifier
virtual ~QgsTopologyPreservingSimplifier();

//! Returns a simplified version the specified geometry
virtual QgsGeometry* simplify( QgsGeometry* geometry ) const;
virtual QgsGeometry simplify( const QgsGeometry& geometry ) const;
//! Simplifies the specified geometry
virtual bool simplifyGeometry( QgsGeometry* geometry ) const;
};
2 changes: 1 addition & 1 deletion python/core/qgsmaptopixelgeometrysimplifier.sip
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class QgsMapToPixelSimplifier : QgsAbstractGeometrySimplifier
void setSimplifyAlgorithm( SimplifyAlgorithm simplifyAlgorithm );

//! Returns a simplified version the specified geometry
virtual QgsGeometry* simplify( QgsGeometry* geometry ) const;
virtual QgsGeometry simplify( const QgsGeometry& geometry ) const;
//! Simplifies the specified geometry
virtual bool simplifyGeometry( QgsGeometry* geometry ) const;

Expand Down
4 changes: 2 additions & 2 deletions python/core/qgsogcutils.sip
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class QgsOgcUtils
in default namespace (\verbatim {<Point>...</Point> \endverbatim) or in
"gml" namespace (\verbatim <gml:Point>...</gml:Point> \endverbatim)
*/
static QgsGeometry* geometryFromGML( const QString& xmlString ) /Factory/;
static QgsGeometry geometryFromGML( const QString& xmlString );

/** Static method that creates geometry from GML
*/
static QgsGeometry* geometryFromGML( const QDomNode& geometryNode ) /Factory/;
static QgsGeometry geometryFromGML( const QDomNode& geometryNode );

/** Read rectangle from GML2 Box */
static QgsRectangle rectangleFromGMLBox( const QDomNode& boxNode );
Expand Down
2 changes: 1 addition & 1 deletion python/core/qgspallabeling.sip
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ class QgsPalLayerSettings

QgsPoint ptZero;
QgsPoint ptOne;
QgsGeometry* extentGeom;
QgsGeometry extentGeom;
int mFeaturesToLabel; // total features that will probably be labeled, may be less (figured before PAL)
int mFeatsSendingToPal; // total features tested for sending into PAL (relative to maxNumLabels)
int mFeatsRegPal; // number of features registered in PAL, when using limitNumLabels
Expand Down
2 changes: 1 addition & 1 deletion python/gui/qgsrubberband.sip
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class QgsRubberBand: QgsMapCanvasItem
* Returns the rubberband as a Geometry.
* @return A geometry object which reflects the current state of the rubberband.
*/
QgsGeometry* asGeometry();
QgsGeometry asGeometry() const;

virtual void updatePosition();

Expand Down
4 changes: 1 addition & 3 deletions src/analysis/interpolation/DualEdgeTriangulation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3138,9 +3138,7 @@ bool DualEdgeTriangulation::saveAsShapefile( const QString& fileName ) const
QgsPolyline lineGeom;
lineGeom.push_back( QgsPoint( p1->getX(), p1->getY() ) );
lineGeom.push_back( QgsPoint( p2->getX(), p2->getY() ) );
QgsGeometry* geom = QgsGeometry::fromPolyline( lineGeom );
edgeLineFeature.setGeometry( *geom );
delete geom;
edgeLineFeature.setGeometry( QgsGeometry::fromPolyline( lineGeom ) );
edgeLineFeature.initAttributes( 1 );

//attributes
Expand Down
14 changes: 5 additions & 9 deletions src/analysis/openstreetmap/qgsosmdatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ void QgsOSMDatabase::exportSpatiaLiteNodes( const QString& tableName, const QStr
if ( skipNull )
continue;

QgsGeometry* geom = QgsGeometry::fromPoint( n.point() );
QgsGeometry geom = QgsGeometry::fromPoint( n.point() );
int col = 0;
sqlite3_bind_int64( stmtInsert, ++col, n.id() );

Expand All @@ -423,19 +423,17 @@ void QgsOSMDatabase::exportSpatiaLiteNodes( const QString& tableName, const QStr
sqlite3_bind_null( stmtInsert, ++col );
}

sqlite3_bind_blob( stmtInsert, ++col, geom->asWkb(), ( int ) geom->wkbSize(), SQLITE_STATIC );
sqlite3_bind_blob( stmtInsert, ++col, geom.asWkb(), ( int ) geom.wkbSize(), SQLITE_STATIC );

int insertRes = sqlite3_step( stmtInsert );
if ( insertRes != SQLITE_DONE )
{
mError = QString( "Error inserting node %1 [%2]" ).arg( n.id() ).arg( insertRes );
delete geom;
break;
}

sqlite3_reset( stmtInsert );
sqlite3_clear_bindings( stmtInsert );
delete geom;
}

sqlite3_finalize( stmtInsert );
Expand Down Expand Up @@ -492,7 +490,7 @@ void QgsOSMDatabase::exportSpatiaLiteWays( bool closed, const QString& tableName
if ( skipNull )
continue;

QgsGeometry* geom = closed ? QgsGeometry::fromPolygon( QgsPolygon() << polyline ) : QgsGeometry::fromPolyline( polyline );
QgsGeometry geom = closed ? QgsGeometry::fromPolygon( QgsPolygon() << polyline ) : QgsGeometry::fromPolyline( polyline );
int col = 0;
sqlite3_bind_int64( stmtInsert, ++col, w.id() );

Expand All @@ -505,22 +503,20 @@ void QgsOSMDatabase::exportSpatiaLiteWays( bool closed, const QString& tableName
sqlite3_bind_null( stmtInsert, ++col );
}

if ( geom )
sqlite3_bind_blob( stmtInsert, ++col, geom->asWkb(), ( int ) geom->wkbSize(), SQLITE_STATIC );
if ( !geom.isEmpty() )
sqlite3_bind_blob( stmtInsert, ++col, geom.asWkb(), ( int ) geom.wkbSize(), SQLITE_STATIC );
else
sqlite3_bind_null( stmtInsert, ++col );

int insertRes = sqlite3_step( stmtInsert );
if ( insertRes != SQLITE_DONE )
{
mError = QString( "Error inserting way %1 [%2]" ).arg( w.id() ).arg( insertRes );
delete geom;
break;
}

sqlite3_reset( stmtInsert );
sqlite3_clear_bindings( stmtInsert );
delete geom;
}

sqlite3_finalize( stmtInsert );
Expand Down

0 comments on commit 4b78b1c

Please sign in to comment.