Skip to content
Permalink
Browse files

Make asGridified and newSameType (helper function)

  • Loading branch information
copying authored and nyalldawson committed Aug 31, 2017
1 parent 99b1c74 commit 3f5b11bffe22a7726bcf5fdbd4e18fe7d660642f
Showing with 576 additions and 14 deletions.
  1. +36 −0 python/core/geometry/qgsabstractgeometry.sip
  2. +1 −0 python/core/geometry/qgscircularstring.sip
  3. +1 −0 python/core/geometry/qgscompoundcurve.sip
  4. +2 −0 python/core/geometry/qgscurve.sip
  5. +3 −2 python/core/geometry/qgscurvepolygon.sip
  6. +3 −1 python/core/geometry/qgsgeometrycollection.sip
  7. +3 −1 python/core/geometry/qgslinestring.sip
  8. +1 −1 python/core/geometry/qgsmulticurve.sip
  9. +1 −1 python/core/geometry/qgsmultilinestring.sip
  10. +1 −1 python/core/geometry/qgsmultipoint.sip
  11. +1 −1 python/core/geometry/qgsmultipolygon.sip
  12. +1 −2 python/core/geometry/qgsmultisurface.sip
  13. +3 −2 python/core/geometry/qgspoint.sip
  14. +1 −1 python/core/geometry/qgspolygon.sip
  15. +3 −0 python/core/geometry/qgssurface.sip
  16. +1 −1 python/core/geometry/qgstriangle.sip
  17. +1 −0 src/core/CMakeLists.txt
  18. +32 −0 src/core/geometry/qgsabstractgeometry.h
  19. +7 −0 src/core/geometry/qgscircularstring.cpp
  20. +1 −0 src/core/geometry/qgscircularstring.h
  21. +7 −0 src/core/geometry/qgscompoundcurve.cpp
  22. +1 −0 src/core/geometry/qgscompoundcurve.h
  23. +9 −0 src/core/geometry/qgscurve.cpp
  24. +3 −0 src/core/geometry/qgscurve.h
  25. +50 −0 src/core/geometry/qgscurvepolygon.cpp
  26. +3 −0 src/core/geometry/qgscurvepolygon.h
  27. +28 −0 src/core/geometry/qgsgeometrycollection.cpp
  28. +3 −0 src/core/geometry/qgsgeometrycollection.h
  29. +87 −0 src/core/geometry/qgslinestring.cpp
  30. +3 −0 src/core/geometry/qgslinestring.h
  31. +7 −0 src/core/geometry/qgsmulticurve.cpp
  32. +1 −0 src/core/geometry/qgsmulticurve.h
  33. +7 −0 src/core/geometry/qgsmultilinestring.cpp
  34. +1 −0 src/core/geometry/qgsmultilinestring.h
  35. +7 −0 src/core/geometry/qgsmultipoint.cpp
  36. +1 −0 src/core/geometry/qgsmultipoint.h
  37. +7 −0 src/core/geometry/qgsmultipolygon.cpp
  38. +1 −0 src/core/geometry/qgsmultipolygon.h
  39. +7 −0 src/core/geometry/qgsmultisurface.cpp
  40. +1 −0 src/core/geometry/qgsmultisurface.h
  41. +30 −0 src/core/geometry/qgspoint.cpp
  42. +3 −0 src/core/geometry/qgspoint.h
  43. +7 −0 src/core/geometry/qgspolygon.cpp
  44. +1 −0 src/core/geometry/qgspolygon.h
  45. +32 −0 src/core/geometry/qgssurface.cpp
  46. +3 −0 src/core/geometry/qgssurface.h
  47. +7 −0 src/core/geometry/qgstriangle.cpp
  48. +1 −0 src/core/geometry/qgstriangle.h
  49. +155 −0 tests/src/core/testqgsgeometry.cpp
@@ -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
@@ -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
@@ -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();

@@ -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();

@@ -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/;

@@ -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();
@@ -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
@@ -26,6 +26,7 @@ class QgsGeometryCollection: QgsAbstractGeometry
QgsGeometryCollection( const QgsGeometryCollection &c );
virtual ~QgsGeometryCollection();

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


@@ -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;
@@ -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 );

@@ -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();
@@ -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();
@@ -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/;
@@ -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/;
@@ -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/;
@@ -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 );
@@ -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();
@@ -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
@@ -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();
@@ -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
@@ -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
*/
@@ -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
@@ -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" );
@@ -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;

@@ -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" );
@@ -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;

@@ -15,6 +15,8 @@
* *
***************************************************************************/

#include <memory>

#include "qgscurve.h"
#include "qgslinestring.h"
#include "qgspoint.h"
@@ -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 );
@@ -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

0 comments on commit 3f5b11b

Please sign in to comment.
You can’t perform that action at this time.