Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #3349 from nyalldawson/feature_geom
Browse files Browse the repository at this point in the history
Mega API break - fix QgsFeature geometry getters/setters
  • Loading branch information
nyalldawson committed Aug 1, 2016
2 parents f7a3fd7 + b7ca001 commit e259e62
Show file tree
Hide file tree
Showing 285 changed files with 2,862 additions and 3,106 deletions.
70 changes: 70 additions & 0 deletions doc/api_break.dox
Expand Up @@ -224,12 +224,50 @@ attributeIndexes(), pkAttributeIndexes(), isSaveAndLoadStyleToDBSupported()</li>
</li>
</ul>

\subsection qgis_api_break_3_0_QgsFeature QgsFeature

<ul>
<li>geometryAndOwnership() has been removed. Use geometry() instead.</li>
<li>setGeometryAndOwnership() has been removed. Use setGeometry() instead.</li>
<li>The setGeometry( QgsGeometry* ) method has been removed, use setGeometry( const QgsGeometry& ) instead.</li>
<li>The geometry() method now returns a copy of the geometry, not a pointer. Since QgsGeometry objects are
implicitly shared this is a low-cost copy, and avoids ownership and dangling pointer issues. <b>Very important: Testing that
a feature has a geometry is now done using the new hasGeometry() method. Any code which compares QgsFeature::geometry() to
None will need to be modified, as the method will return an empty geometry if the feature has no geometry.</b></li>
<li>The temporary constGeometry() method has been removed. Use geometry() instead.</li>
<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. The biggest impact with this change is that PyQGIS code should not compare a geometry
result to None, but instead either use a boolean test (`if g.buffer(10):`) or explicitly use the isEmpty()
method to determine if a geometry is valid.</li>
</ul>

\subsection qgis_api_break_3_0_QgsGeometryAnalyzer QgsGeometryAnalyzer

<ul>
<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

<ul>
<li>QgsGroupWMSDataDialo has been renamed to QgsGroupWmsDataDialog</li>
</ul>

\subsection qgis_api_break_3_0_QgsHighlight QgsHighlight

<ul>
<li>The QgsHighlight constructor now takes a geometry reference, not a pointer.</li>
</ul>



\subsection qgis_api_break_3_0_QgsVectorDataProvider QgsVectorDataProvider

<ul>
Expand All @@ -239,6 +277,7 @@ only affects third party c++ providers, and does not affect PyQGIS scripts.</li>
<li>The SaveAsShapefile, SelectGeometryAtId, RandomSelectGeometryAtId and SequentialSelectGeometryAtId
capabilities have been removed, as they were unused and had no effect.</li>
<li>capabilities() now returns a typesafe QgsVectorDataProvider::Capabilities object, not an integer.</li>
<li>convertToProviderType() now takes a geometry reference, not a pointer.</li>
</ul>

\subsection qgis_api_break_3_0_QgsLabelingEngineInterface QgsLabelingEngineInterface
Expand Down Expand Up @@ -334,6 +373,7 @@ be returned instead of a null pointer if no transformation is required.</li>

<ul>
<li>init(QgsMapRenderer*) has been removed. Use init(const QgsMapSettings&) instead.</li>
<li>prepareGeometry and geometryRequiresPreparation now take geometry references, not pointers.</li>
</ul>

\subsection qgis_api_break_3_0_QgsOSMElement QgsOSMElement
Expand Down Expand Up @@ -396,6 +436,12 @@ be returned instead of a null pointer if no transformation is required.</li>
<li>setCoordinateTransform() now takes a QgsCoordinateTransform reference, not a pointer. An invalid QgsCoordinateTransform should be used instead of a null pointer if no transformation is required.</li>
</ul>

\subsection qgis_api_break_3_0_QgsRubberBand QgsRubberBand

<ul>
<li>setToGeometry() and addGeometry() now take geometry references, not pointers.</li>
</ul>

\subsection qgis_api_break_3_0_QgsPalLayerSettings QgsPalLayerSettings

<ul>
Expand Down Expand Up @@ -430,6 +476,23 @@ plugins calling this method will need to be updated.</li>
setExcludeAttributesWms()</li>
<li>excludeAttributesWFS() and setExcludeAttributesWFS() have been renamed to excludeAttributesWfs() and
setExcludeAttributesWfs()</li>
<li>changeGeometry() now accepts a geometry reference, not a pointer.</li>
<li>The geometryChanged() signal now uses a const QgsGeometry reference.</li>
<li>The deprecated removePolygonIntersections has been removed.</li>
<li>addTopologicalPoints() now takes a geometry reference, not a pointer.</li>
</ul>

\subsection qgis_api_break_3_0_QgsVectorLayerEditBuffer QgsVectorLayerEditBuffer

<ul>
<li>changeGeometry() now accepts a geometry reference, not a pointer.</li>
<li>The geometryChanged() signal now uses a const QgsGeometry reference.</li>
</ul>

\subsection qgis_api_break_3_0_QgsVectorLayerEditUtils QgsVectorLayerEditUtils

<ul>
<li>addTopologicalPoints() now accepts a geometry reference, not a pointer.</li>
</ul>

\subsection qgis_api_break_3_0_QgsVectorLayerImport QgsVectorLayerImport
Expand All @@ -441,6 +504,13 @@ pointers makes for more robust, safer code. Use an invalid (default constructed)
in code which previously passed a null pointer to QgsVectorLayerImport.</li>
</ul>

\subsection qgis_api_break_3_0_QgsVectorLayerUndoCommand QgsVectorLayerUndoCommand

<ul>
<li>QgsVectorLayerUndoCommandChangeGeometry constructor now accepts a geometry reference, not a pointer.</li>
</ul>


\subsection qgis_api_break_3_0_QgsPointLocator QgsPointLocator

<ul>
Expand Down
4 changes: 2 additions & 2 deletions python/analysis/vector/qgsgeometryanalyzer.sip
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 );

};
8 changes: 8 additions & 0 deletions python/core/__init__.py
Expand Up @@ -29,6 +29,14 @@
import string
from qgis._core import *

# Boolean evaluation of QgsGeometry


def _geometryNonZero(self):
return not self.isEmpty()
QgsGeometry.__nonzero__ = _geometryNonZero
QgsGeometry.__bool__ = _geometryNonZero


def register_function(function, arg_count, group, usesgeometry=False, referenced_columns=[QgsFeatureRequest.AllAttributes], **kwargs):
"""
Expand Down
82 changes: 48 additions & 34 deletions python/core/geometry/qgsgeometry.sip
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 @@ -393,49 +393,57 @@ class QgsGeometry
*/
int makeDifference( const QgsGeometry* other );

/** Returns the geometry formed by modifying this geometry such that it does not
* intersect the other geometry.
* @param other geometry that should not be intersect
* @return difference geometry, or empty geometry if difference could not be calculated
* @note added in QGIS 3.0
*/
QgsGeometry makeDifference( const QgsGeometry& other ) const;

/** Returns the bounding box of this feature*/
QgsRectangle boundingBox() const;

/** Test for intersection with a rectangle (uses GEOS) */
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 @@ -446,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 @@ -495,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 @@ -523,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 @@ -556,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 @@ -635,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 @@ -696,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 @@ -760,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 @@ -786,5 +794,11 @@ class QgsGeometry
//! Allows direct construction of QVariants from geometry.
operator QVariant() const;

/** Returns true if the geometry is non empty (ie, isEmpty() returns false),
* or false if it is an empty, uninitialised geometry (ie, ieEmpty() returns true).
* @note added in QGIS 3.0
*/
operator bool() const;

}; // class QgsGeometry

0 comments on commit e259e62

Please sign in to comment.