Skip to content

Commit

Permalink
Make asGridified and newSameType (helper function)
Browse files Browse the repository at this point in the history
  • Loading branch information
mangelats authored and nyalldawson committed Oct 27, 2017
1 parent 99b1c74 commit 3f5b11b
Show file tree
Hide file tree
Showing 49 changed files with 576 additions and 14 deletions.
36 changes: 36 additions & 0 deletions python/core/geometry/qgsabstractgeometry.sip
Expand Up @@ -71,6 +71,16 @@ class QgsAbstractGeometry
virtual ~QgsAbstractGeometry();
QgsAbstractGeometry( const QgsAbstractGeometry &geom );

virtual QgsAbstractGeometry *createEmptyWithSameType() const = 0 /Factory/;
%Docstring
Makes a new geometry with the same class and same WKB and transfers ownership.
To create it, the geometry is default constructedand then the WKB is changed.
:return: the new empty geometry. Callee takes ownership.
.. seealso:: clone
.. versionadded:: 3.0
:rtype: QgsAbstractGeometry
%End

virtual QgsAbstractGeometry *clone() const = 0 /Factory/;
%Docstring
Clones the geometry by performing a deep copy
Expand Down Expand Up @@ -402,6 +412,32 @@ Returns the centroid of the geometry
:rtype: QgsAbstractGeometry
%End

virtual QgsAbstractGeometry *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0, double tolerance = M_PI / 180., SegmentationToleranceType toleranceType = MaximumAngle ) const = 0 /Factory/;
%Docstring
Makes a new geometry with all the points or vertices snapped to the closest point of the grid.
It transfers ownership to the callee.
If it couldn't make the gridified geometry it returns None.
It may generate an invalid geometry (in some corner cases).
It can also be thought as rounding the edges and it may be useful for removing errors.
If the geometry is curved, it will be segmentized before gridifying it.
Example:
\code
geometry->snappedToGrid(1, 1);
\endcode
In this case we use a 2D grid of 1x1 to gridify.
In this case, it can be thought like rounding the x and y of all the points/vertices to full units (remove all decimals).
\param hSpacing Horizontal spacing of the grid (x axis). 0 to disable.
\param vSpacing Vertical spacing of the grid (y axis). 0 to disable.
\param dSpacing Depth spacing of the grid (z axis). 0 (default) to disable.
\param mSpacing Custom dimension spacing of the grid (m axis). 0 (default) to disable.
\param tolerance In case of segmentation, the tolerance to use (passed to segmentize as is).
\param toleranceType In case of segmentation, the toleranceType to use (passed to segmentize as is).
:return: the segmentized geometry or None if it wasn't possible to make. Caller takes ownership.
.. seealso:: segmentize
.. versionadded:: 3.0
:rtype: QgsAbstractGeometry
%End

virtual double vertexAngle( QgsVertexId vertex ) const = 0;
%Docstring
Returns approximate angle at a vertex. This is usually the average angle between adjacent
Expand Down
1 change: 1 addition & 0 deletions python/core/geometry/qgscircularstring.sip
Expand Up @@ -29,6 +29,7 @@ class QgsCircularString: QgsCurve

virtual QString geometryType() const;
virtual int dimension() const;
virtual QgsCircularString *createEmptyWithSameType() const /Factory/;
virtual QgsCircularString *clone() const /Factory/;
virtual void clear();

Expand Down
1 change: 1 addition & 0 deletions python/core/geometry/qgscompoundcurve.sip
Expand Up @@ -29,6 +29,7 @@ class QgsCompoundCurve: QgsCurve

virtual QString geometryType() const;
virtual int dimension() const;
virtual QgsCompoundCurve *createEmptyWithSameType() const /Factory/;
virtual QgsCompoundCurve *clone() const /Factory/;
virtual void clear();

Expand Down
2 changes: 2 additions & 0 deletions python/core/geometry/qgscurve.sip
Expand Up @@ -126,6 +126,8 @@ class QgsCurve: QgsAbstractGeometry

virtual QgsAbstractGeometry *boundary() const /Factory/;

virtual QgsCurve *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0,
double tolerance = M_PI / 180., SegmentationToleranceType toleranceType = MaximumAngle ) const /Factory/;

virtual QgsCurve *segmentize( double tolerance = M_PI_2 / 90, SegmentationToleranceType toleranceType = MaximumAngle ) const /Factory/;

Expand Down
5 changes: 3 additions & 2 deletions python/core/geometry/qgscurvepolygon.sip
Expand Up @@ -35,7 +35,7 @@ class QgsCurvePolygon: QgsSurface
virtual QString geometryType() const;

virtual int dimension() const;

virtual QgsCurvePolygon *createEmptyWithSameType() const /Factory/;
virtual QgsCurvePolygon *clone() const /Factory/;

virtual void clear();
Expand Down Expand Up @@ -64,7 +64,8 @@ class QgsCurvePolygon: QgsSurface
virtual QgsPolygonV2 *surfaceToPolygon() const /Factory/;

virtual QgsAbstractGeometry *boundary() const /Factory/;

virtual QgsCurvePolygon *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0,
double tolerance = M_PI / 180., SegmentationToleranceType toleranceType = MaximumAngle ) const /Factory/;

int numInteriorRings() const;
%Docstring
Expand Down
4 changes: 3 additions & 1 deletion python/core/geometry/qgsgeometrycollection.sip
Expand Up @@ -26,6 +26,7 @@ class QgsGeometryCollection: QgsAbstractGeometry
QgsGeometryCollection( const QgsGeometryCollection &c );
virtual ~QgsGeometryCollection();

virtual QgsGeometryCollection *createEmptyWithSameType() const /Factory/;
virtual QgsGeometryCollection *clone() const /Factory/;


Expand All @@ -50,7 +51,8 @@ class QgsGeometryCollection: QgsAbstractGeometry
virtual QString geometryType() const;

virtual void clear();

virtual QgsGeometryCollection *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0,
double tolerance = M_PI / 180., SegmentationToleranceType toleranceType = MaximumAngle ) const /Factory/;
virtual QgsAbstractGeometry *boundary() const /Factory/;

virtual void adjacentVertices( QgsVertexId vertex, QgsVertexId &previousVertex /Out/, QgsVertexId &nextVertex /Out/ ) const;
Expand Down
4 changes: 3 additions & 1 deletion python/core/geometry/qgslinestring.sip
Expand Up @@ -170,13 +170,15 @@ Closes the line string by appending the first point to the end of the line, if i
virtual QString geometryType() const;

virtual int dimension() const;

virtual QgsLineString *createEmptyWithSameType() const /Factory/;
virtual QgsLineString *clone() const /Factory/;

virtual void clear();

virtual bool isEmpty() const;

virtual QgsLineString *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0,
double tolerance = M_PI / 180., SegmentationToleranceType toleranceType = MaximumAngle ) const /Factory/;

virtual bool fromWkb( QgsConstWkbPtr &wkb );

Expand Down
2 changes: 1 addition & 1 deletion python/core/geometry/qgsmulticurve.sip
Expand Up @@ -21,7 +21,7 @@ class QgsMultiCurve: QgsGeometryCollection
public:
QgsMultiCurve();
virtual QString geometryType() const;

virtual QgsMultiCurve *createEmptyWithSameType() const /Factory/;
virtual QgsMultiCurve *clone() const /Factory/;

virtual void clear();
Expand Down
2 changes: 1 addition & 1 deletion python/core/geometry/qgsmultilinestring.sip
Expand Up @@ -22,7 +22,7 @@ class QgsMultiLineString: QgsMultiCurve
QgsMultiLineString();

virtual QString geometryType() const;

virtual QgsMultiLineString *createEmptyWithSameType() const /Factory/;
virtual QgsMultiLineString *clone() const /Factory/;

virtual void clear();
Expand Down
2 changes: 1 addition & 1 deletion python/core/geometry/qgsmultipoint.sip
Expand Up @@ -22,7 +22,7 @@ class QgsMultiPointV2: QgsGeometryCollection
QgsMultiPointV2();

virtual QString geometryType() const;

virtual QgsMultiPointV2 *createEmptyWithSameType() const /Factory/;
virtual QgsMultiPointV2 *clone() const /Factory/;

virtual QgsMultiPointV2 *toCurveType() const /Factory/;
Expand Down
2 changes: 1 addition & 1 deletion python/core/geometry/qgsmultipolygon.sip
Expand Up @@ -21,7 +21,7 @@ class QgsMultiPolygonV2: QgsMultiSurface
public:
QgsMultiPolygonV2();
virtual QString geometryType() const;

virtual QgsMultiPolygonV2 *createEmptyWithSameType() const /Factory/;
virtual void clear();

virtual QgsMultiPolygonV2 *clone() const /Factory/;
Expand Down
3 changes: 1 addition & 2 deletions python/core/geometry/qgsmultisurface.sip
Expand Up @@ -21,9 +21,8 @@ class QgsMultiSurface: QgsGeometryCollection
public:
QgsMultiSurface();
virtual QString geometryType() const;

virtual void clear();

virtual QgsMultiSurface *createEmptyWithSameType() const /Factory/;
virtual QgsMultiSurface *clone() const /Factory/;

virtual QgsMultiSurface *toCurveType() const /Factory/;
Expand Down
5 changes: 3 additions & 2 deletions python/core/geometry/qgspoint.sip
Expand Up @@ -335,9 +335,10 @@ class QgsPoint: QgsAbstractGeometry
virtual QString geometryType() const;

virtual int dimension() const;

virtual QgsPoint *createEmptyWithSameType() const /Factory/;
virtual QgsPoint *clone() const /Factory/;

virtual QgsPoint *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0,
double tolerance = M_PI / 180., SegmentationToleranceType toleranceType = MaximumAngle ) const /Factory/;
virtual void clear();

virtual bool fromWkb( QgsConstWkbPtr &wkb );
Expand Down
2 changes: 1 addition & 1 deletion python/core/geometry/qgspolygon.sip
Expand Up @@ -23,7 +23,7 @@ class QgsPolygonV2: QgsCurvePolygon
QgsPolygonV2();

virtual QString geometryType() const;

virtual QgsPolygonV2 *createEmptyWithSameType() const /Factory/;
virtual QgsPolygonV2 *clone() const /Factory/;

virtual void clear();
Expand Down
3 changes: 3 additions & 0 deletions python/core/geometry/qgssurface.sip
Expand Up @@ -25,6 +25,9 @@ class QgsSurface: QgsAbstractGeometry
:rtype: QgsPolygonV2
%End

virtual QgsSurface *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0,
double tolerance = M_PI / 180., SegmentationToleranceType toleranceType = MaximumAngle ) const /Factory/;

virtual QgsRectangle boundingBox() const;
%Docstring
Returns the minimal bounding box for the geometry
Expand Down
2 changes: 1 addition & 1 deletion python/core/geometry/qgstriangle.sip
Expand Up @@ -53,7 +53,7 @@ class QgsTriangle : QgsPolygonV2
%End

virtual QString geometryType() const;

virtual QgsTriangle *createEmptyWithSameType() const /Factory/;
virtual QgsTriangle *clone() const /Factory/;

virtual void clear();
Expand Down
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -467,6 +467,7 @@ SET(QGIS_CORE_SRCS
geometry/qgsrectangle.cpp
geometry/qgsreferencedgeometry.cpp
geometry/qgsregularpolygon.cpp
geometry/qgssurface.cpp
geometry/qgstriangle.cpp
geometry/qgswkbptr.cpp
geometry/qgswkbtypes.cpp
Expand Down
32 changes: 32 additions & 0 deletions src/core/geometry/qgsabstractgeometry.h
Expand Up @@ -110,6 +110,14 @@ class CORE_EXPORT QgsAbstractGeometry
QgsAbstractGeometry( const QgsAbstractGeometry &geom );
QgsAbstractGeometry &operator=( const QgsAbstractGeometry &geom );

/** Makes a new geometry with the same class and same WKB and transfers ownership.
* To create it, the geometry is default constructedand then the WKB is changed.
* \returns the new empty geometry. Callee takes ownership.
* \see clone
* \since 3.0
*/
virtual QgsAbstractGeometry *createEmptyWithSameType() const = 0 SIP_FACTORY;

/**
* Clones the geometry by performing a deep copy
*/
Expand Down Expand Up @@ -411,6 +419,30 @@ class CORE_EXPORT QgsAbstractGeometry
*/
virtual QgsAbstractGeometry *toCurveType() const = 0 SIP_FACTORY;

/** Makes a new geometry with all the points or vertices snapped to the closest point of the grid.
* It transfers ownership to the callee.
* If it couldn't make the gridified geometry it returns nullptr.
* It may generate an invalid geometry (in some corner cases).
* It can also be thought as rounding the edges and it may be useful for removing errors.
* If the geometry is curved, it will be segmentized before gridifying it.
* Example:
* \code
* geometry->snappedToGrid(1, 1);
* \endcode
* In this case we use a 2D grid of 1x1 to gridify.
* In this case, it can be thought like rounding the x and y of all the points/vertices to full units (remove all decimals).
* \param hSpacing Horizontal spacing of the grid (x axis). 0 to disable.
* \param vSpacing Vertical spacing of the grid (y axis). 0 to disable.
* \param dSpacing Depth spacing of the grid (z axis). 0 (default) to disable.
* \param mSpacing Custom dimension spacing of the grid (m axis). 0 (default) to disable.
* \param tolerance In case of segmentation, the tolerance to use (passed to segmentize as is).
* \param toleranceType In case of segmentation, the toleranceType to use (passed to segmentize as is).
* \returns the segmentized geometry or nullptr if it wasn't possible to make. Caller takes ownership.
* \see segmentize
* \since 3.0
*/
virtual QgsAbstractGeometry *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0, double tolerance = M_PI / 180., SegmentationToleranceType toleranceType = MaximumAngle ) const = 0 SIP_FACTORY;

/**
* Returns approximate angle at a vertex. This is usually the average angle between adjacent
* segments, and can be pictured as the orientation of a line following the curvature of the
Expand Down
7 changes: 7 additions & 0 deletions src/core/geometry/qgscircularstring.cpp
Expand Up @@ -66,6 +66,13 @@ bool QgsCircularString::operator!=( const QgsCurve &other ) const
return !operator==( other );
}

QgsCircularString *QgsCircularString::createEmptyWithSameType() const
{
auto result = new QgsCircularString();
result->mWkbType = mWkbType;
return result;
}

QString QgsCircularString::geometryType() const
{
return QStringLiteral( "CircularString" );
Expand Down
1 change: 1 addition & 0 deletions src/core/geometry/qgscircularstring.h
Expand Up @@ -41,6 +41,7 @@ class CORE_EXPORT QgsCircularString: public QgsCurve

virtual QString geometryType() const override;
virtual int dimension() const override;
virtual QgsCircularString *createEmptyWithSameType() const override SIP_FACTORY;
virtual QgsCircularString *clone() const override SIP_FACTORY;
virtual void clear() override;

Expand Down
7 changes: 7 additions & 0 deletions src/core/geometry/qgscompoundcurve.cpp
Expand Up @@ -61,6 +61,13 @@ bool QgsCompoundCurve::operator!=( const QgsCurve &other ) const
return !operator==( other );
}

QgsCompoundCurve *QgsCompoundCurve::createEmptyWithSameType() const
{
auto result = new QgsCompoundCurve();
result->mWkbType = mWkbType;
return result;
}

QString QgsCompoundCurve::geometryType() const
{
return QStringLiteral( "CompoundCurve" );
Expand Down
1 change: 1 addition & 0 deletions src/core/geometry/qgscompoundcurve.h
Expand Up @@ -41,6 +41,7 @@ class CORE_EXPORT QgsCompoundCurve: public QgsCurve

virtual QString geometryType() const override;
virtual int dimension() const override;
virtual QgsCompoundCurve *createEmptyWithSameType() const override SIP_FACTORY;
virtual QgsCompoundCurve *clone() const override SIP_FACTORY;
virtual void clear() override;

Expand Down
9 changes: 9 additions & 0 deletions src/core/geometry/qgscurve.cpp
Expand Up @@ -15,6 +15,8 @@
* *
***************************************************************************/

#include <memory>

#include "qgscurve.h"
#include "qgslinestring.h"
#include "qgspoint.h"
Expand Down Expand Up @@ -129,6 +131,13 @@ QgsAbstractGeometry *QgsCurve::boundary() const
return multiPoint;
}

QgsCurve *QgsCurve::snappedToGrid( double hSpacing, double vSpacing, double dSpacing, double mSpacing,
double tolerance, SegmentationToleranceType toleranceType ) const
{
std::unique_ptr<QgsLineString> line { curveToLine( tolerance, toleranceType ) };
return line->snappedToGrid( hSpacing, vSpacing, dSpacing, mSpacing );
}

QgsCurve *QgsCurve::segmentize( double tolerance, SegmentationToleranceType toleranceType ) const
{
return curveToLine( tolerance, toleranceType );
Expand Down
3 changes: 3 additions & 0 deletions src/core/geometry/qgscurve.h
Expand Up @@ -123,6 +123,9 @@ class CORE_EXPORT QgsCurve: public QgsAbstractGeometry

QgsAbstractGeometry *boundary() const override SIP_FACTORY;

virtual QgsCurve *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0,
double tolerance = M_PI / 180., SegmentationToleranceType toleranceType = MaximumAngle ) const override SIP_FACTORY;

/**
* Returns a geometry without curves. Caller takes ownership
* \param tolerance segmentation tolerance
Expand Down

0 comments on commit 3f5b11b

Please sign in to comment.