Skip to content
Permalink
Browse files

Merge pull request #5475 from nyalldawson/snap_to_grid

[FEATURE][processing] Native c++ snap to grid algorithm
  • Loading branch information
nyalldawson committed Oct 27, 2017
2 parents edecd49 + 4372ac2 commit 68179e224cace2b158e796c441c0adb7a96a1c77
Showing with 1,040 additions and 320 deletions.
  1. +34 −0 python/core/geometry/qgsabstractgeometry.sip
  2. +25 −5 python/core/geometry/qgscircularstring.sip
  3. +28 −5 python/core/geometry/qgscompoundcurve.sip
  4. +1 −0 python/core/geometry/qgscurve.sip
  5. +6 −0 python/core/geometry/qgscurvepolygon.sip
  6. +15 −0 python/core/geometry/qgsgeometry.sip
  7. +5 −0 python/core/geometry/qgsgeometrycollection.sip
  8. +3 −0 python/core/geometry/qgslinestring.sip
  9. +5 −0 python/core/geometry/qgsmulticurve.sip
  10. +1 −0 python/core/geometry/qgsmultilinestring.sip
  11. +1 −0 python/core/geometry/qgsmultipoint.sip
  12. +1 −0 python/core/geometry/qgsmultipolygon.sip
  13. +4 −0 python/core/geometry/qgsmultisurface.sip
  14. +5 −0 python/core/geometry/qgspoint.sip
  15. +7 −0 python/core/geometry/qgspolygon.sip
  16. +5 −0 python/core/geometry/qgstriangle.sip
  17. +0 −3 python/plugins/processing/algs/help/qgis.yaml
  18. +0 −174 python/plugins/processing/algs/qgis/Gridify.py
  19. +0 −2 python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
  20. +0 −16 python/plugins/processing/tests/testdata/expected/gridify_lines.gfs
  21. +8 −6 python/plugins/processing/tests/testdata/expected/gridify_lines.gml
  22. +0 −32 python/plugins/processing/tests/testdata/expected/gridify_polys.gfs
  23. +6 −6 python/plugins/processing/tests/testdata/expected/gridify_polys.gml
  24. +2 −2 python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
  25. +1 −0 src/analysis/CMakeLists.txt
  26. +105 −0 src/analysis/processing/qgsalgorithmsnaptogrid.cpp
  27. +61 −0 src/analysis/processing/qgsalgorithmsnaptogrid.h
  28. +2 −0 src/analysis/processing/qgsnativealgorithms.cpp
  29. +1 −0 src/core/CMakeLists.txt
  30. +32 −0 src/core/geometry/qgsabstractgeometry.h
  31. +20 −0 src/core/geometry/qgscircularstring.cpp
  32. +25 −29 src/core/geometry/qgscircularstring.h
  33. +26 −0 src/core/geometry/qgscompoundcurve.cpp
  34. +27 −30 src/core/geometry/qgscompoundcurve.h
  35. +93 −0 src/core/geometry/qgscurve.cpp
  36. +11 −0 src/core/geometry/qgscurve.h
  37. +41 −0 src/core/geometry/qgscurvepolygon.cpp
  38. +4 −2 src/core/geometry/qgscurvepolygon.h
  39. +9 −0 src/core/geometry/qgsgeometry.cpp
  40. +14 −0 src/core/geometry/qgsgeometry.h
  41. +27 −0 src/core/geometry/qgsgeometrycollection.cpp
  42. +4 −2 src/core/geometry/qgsgeometrycollection.h
  43. +22 −0 src/core/geometry/qgslinestring.cpp
  44. +2 −1 src/core/geometry/qgslinestring.h
  45. +7 −0 src/core/geometry/qgsmulticurve.cpp
  46. +4 −0 src/core/geometry/qgsmulticurve.h
  47. +7 −0 src/core/geometry/qgsmultilinestring.cpp
  48. +1 −1 src/core/geometry/qgsmultilinestring.h
  49. +7 −0 src/core/geometry/qgsmultipoint.cpp
  50. +1 −1 src/core/geometry/qgsmultipoint.h
  51. +7 −0 src/core/geometry/qgsmultipolygon.cpp
  52. +1 −1 src/core/geometry/qgsmultipolygon.h
  53. +7 −0 src/core/geometry/qgsmultisurface.cpp
  54. +3 −0 src/core/geometry/qgsmultisurface.h
  55. +29 −0 src/core/geometry/qgspoint.cpp
  56. +4 −2 src/core/geometry/qgspoint.h
  57. +7 −0 src/core/geometry/qgspolygon.cpp
  58. +7 −0 src/core/geometry/qgspolygon.h
  59. +22 −0 src/core/geometry/qgssurface.cpp
  60. +7 −0 src/core/geometry/qgstriangle.cpp
  61. +4 −0 src/core/geometry/qgstriangle.h
  62. +226 −0 tests/src/core/testqgsgeometry.cpp
@@ -402,6 +402,28 @@ Returns the centroid of the geometry
:rtype: QgsAbstractGeometry
%End

virtual QgsAbstractGeometry *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const = 0 /Factory/;
%Docstring
Makes a new geometry with all the points or vertices snapped to the closest point of the grid.
Ownership is transferred to the caller.

If the gridified geometry could not be calculated a None will be returned.
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.
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.
.. 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
@@ -492,6 +514,18 @@ Returns the centroid of the geometry

protected:

virtual QgsAbstractGeometry *createEmptyWithSameType() const = 0 /Factory/;
%Docstring
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
To create it, the geometry is default constructed and then the WKB is changed.
.. seealso:: clone()
.. versionadded:: 3.0
.. note::

Not available in Python bindings
:rtype: QgsAbstractGeometry
%End

virtual bool hasChildGeometries() const;
%Docstring
Returns whether the geometry has any child geometries (false for point / curve, true otherwise)
@@ -25,16 +25,24 @@ class QgsCircularString: QgsCurve
QgsCircularString();

virtual bool operator==( const QgsCurve &other ) const;

virtual bool operator!=( const QgsCurve &other ) const;


virtual QString geometryType() const;

virtual int dimension() const;

virtual QgsCircularString *clone() const /Factory/;

virtual void clear();


virtual bool fromWkb( QgsConstWkbPtr &wkb );

virtual bool fromWkt( const QString &wkt );


virtual QByteArray asWkb() const;

virtual QString asWkt( int precision = 17 ) const;
@@ -66,10 +74,14 @@ class QgsCircularString: QgsCurve
%End

virtual double length() const;

virtual QgsPoint startPoint() const;

virtual QgsPoint endPoint() const;


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

%Docstring
Returns a new line string geometry corresponding to a segmentized approximation
of the curve.
@@ -81,6 +93,9 @@ class QgsCircularString: QgsCurve
:rtype: QgsLineString
%End

virtual QgsCircularString *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const /Factory/;


virtual void draw( QPainter &p ) const;

virtual void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform,
@@ -89,17 +104,15 @@ class QgsCircularString: QgsCurve

virtual void addToPainterPath( QPainterPath &path ) const;


virtual void drawAsPolygon( QPainter &p ) const;


virtual bool insertVertex( QgsVertexId position, const QgsPoint &vertex );

virtual bool moveVertex( QgsVertexId position, const QgsPoint &newPos );

virtual bool deleteVertex( QgsVertexId position );

virtual double closestSegment( const QgsPoint &pt, QgsPoint &segmentPt /Out/,
QgsVertexId &vertexAfter /Out/,
bool *leftOf /Out/ = 0, double epsilon = 4 * DBL_EPSILON ) const;
virtual double closestSegment( const QgsPoint &pt, QgsPoint &segmentPt /Out/, QgsVertexId &vertexAfter /Out/, bool *leftOf /Out/ = 0, double epsilon = 4 * DBL_EPSILON ) const;

virtual bool pointAt( int node, QgsPoint &point, QgsVertexId::VertexType &type ) const;

@@ -119,21 +132,28 @@ class QgsCircularString: QgsCurve

virtual QgsCircularString *reversed() const /Factory/;


virtual bool addZValue( double zValue = 0 );

virtual bool addMValue( double mValue = 0 );


virtual bool dropZValue();

virtual bool dropMValue();


virtual double xAt( int index ) const;

virtual double yAt( int index ) const;


protected:
virtual QgsCircularString *createEmptyWithSameType() const /Factory/;

virtual QgsRectangle calculateBoundingBox() const;


};


@@ -25,16 +25,24 @@ class QgsCompoundCurve: QgsCurve
~QgsCompoundCurve();

virtual bool operator==( const QgsCurve &other ) const;

virtual bool operator!=( const QgsCurve &other ) const;


virtual QString geometryType() const;

virtual int dimension() const;

virtual QgsCompoundCurve *clone() const /Factory/;

virtual void clear();


virtual bool fromWkb( QgsConstWkbPtr &wkb );

virtual bool fromWkt( const QString &wkt );


virtual QByteArray asWkb() const;

virtual QString asWkt( int precision = 17 ) const;
@@ -47,14 +55,20 @@ class QgsCompoundCurve: QgsCurve


virtual double length() const;

virtual QgsPoint startPoint() const;

virtual QgsPoint endPoint() const;

virtual void points( QgsPointSequence &pts /Out/ ) const;

virtual int numPoints() const;

virtual bool isEmpty() const;


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

%Docstring
Returns a new line string geometry corresponding to a segmentized approximation
of the curve.
@@ -63,6 +77,9 @@ class QgsCompoundCurve: QgsCurve
:rtype: QgsLineString
%End

virtual QgsCompoundCurve *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const /Factory/;


int nCurves() const;
%Docstring
Returns the number of curves in the geometry.
@@ -101,18 +118,16 @@ class QgsCompoundCurve: QgsCurve

virtual void drawAsPolygon( QPainter &p ) const;


virtual bool insertVertex( QgsVertexId position, const QgsPoint &vertex );

virtual bool moveVertex( QgsVertexId position, const QgsPoint &newPos );

virtual bool deleteVertex( QgsVertexId position );

virtual double closestSegment( const QgsPoint &pt, QgsPoint &segmentPt /Out/,
QgsVertexId &vertexAfter /Out/, bool *leftOf /Out/ = 0,
double epsilon = 4 * DBL_EPSILON ) const;
virtual double closestSegment( const QgsPoint &pt, QgsPoint &segmentPt /Out/, QgsVertexId &vertexAfter /Out/, bool *leftOf /Out/ = 0, double epsilon = 4 * DBL_EPSILON ) const;

virtual bool pointAt( int node, QgsPoint &point, QgsVertexId::VertexType &type ) const;


virtual void sumUpArea( double &sum /Out/ ) const;


@@ -135,12 +150,17 @@ Appends first point if not already closed.

virtual QgsCompoundCurve *reversed() const /Factory/;


virtual bool addZValue( double zValue = 0 );

virtual bool addMValue( double mValue = 0 );


virtual bool dropZValue();

virtual bool dropMValue();


virtual double xAt( int index ) const;

virtual double yAt( int index ) const;
@@ -150,6 +170,9 @@ Appends first point if not already closed.

virtual QgsRectangle calculateBoundingBox() const;

virtual QgsCompoundCurve *createEmptyWithSameType() const /Factory/;


};


@@ -184,6 +184,7 @@ class QgsCurve: QgsAbstractGeometry
virtual int childCount() const;
virtual QgsPoint childPoint( int index ) const;


};

/************************************************************************
@@ -65,6 +65,8 @@ class QgsCurvePolygon: QgsSurface

virtual QgsAbstractGeometry *boundary() const /Factory/;

virtual QgsCurvePolygon *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const /Factory/;


int numInteriorRings() const;
%Docstring
@@ -196,9 +198,13 @@ Adds an interior ring to the geometry (takes ownership)
virtual QgsCurvePolygon *toCurveType() const /Factory/;

protected:
virtual QgsCurvePolygon *createEmptyWithSameType() const /Factory/;

virtual int childCount() const;

virtual QgsAbstractGeometry *childGeometry( int index ) const;


protected:


@@ -665,6 +665,21 @@ Returns true if WKB of the geometry is of WKBMulti* type
:rtype: QgsGeometry
%End

QgsGeometry snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const;
%Docstring
Returns a new geometry with all points or vertices snapped to the closest point of the grid.

If the gridified geometry could not be calculated (or was totally collapsed) an empty geometry will be returned.
Note that snapping to grid 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.
\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.
.. versionadded:: 3.0
:rtype: QgsGeometry
%End

bool intersects( const QgsRectangle &r ) const;
%Docstring
Tests for intersection with a rectangle (uses GEOS)
@@ -51,6 +51,7 @@ class QgsGeometryCollection: QgsAbstractGeometry

virtual void clear();

virtual QgsGeometryCollection *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const /Factory/;
virtual QgsAbstractGeometry *boundary() const /Factory/;

virtual void adjacentVertices( QgsVertexId vertex, QgsVertexId &previousVertex /Out/, QgsVertexId &nextVertex /Out/ ) const;
@@ -173,9 +174,13 @@ Adds a geometry and takes ownership. Returns true in case of success.


protected:
virtual QgsGeometryCollection *createEmptyWithSameType() const /Factory/;

virtual int childCount() const;

virtual QgsAbstractGeometry *childGeometry( int index ) const;


protected:

virtual bool wktOmitChildType() const;
@@ -177,6 +177,8 @@ Closes the line string by appending the first point to the end of the line, if i

virtual bool isEmpty() const;

virtual QgsLineString *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const /Factory/;


virtual bool fromWkb( QgsConstWkbPtr &wkb );

@@ -268,6 +270,7 @@ Closes the line string by appending the first point to the end of the line, if i


protected:
virtual QgsLineString *createEmptyWithSameType() const /Factory/;

virtual QgsRectangle calculateBoundingBox() const;

@@ -52,6 +52,11 @@ class QgsMultiCurve: QgsGeometryCollection



protected:

virtual QgsMultiCurve *createEmptyWithSameType() const /Factory/;


};


@@ -49,6 +49,7 @@ class QgsMultiLineString: QgsMultiCurve
%End

protected:
virtual QgsMultiLineString *createEmptyWithSameType() const /Factory/;

virtual bool wktOmitChildType() const;

@@ -50,6 +50,7 @@ class QgsMultiPointV2: QgsGeometryCollection


protected:
virtual QgsMultiPointV2 *createEmptyWithSameType() const /Factory/;

virtual bool wktOmitChildType() const;

@@ -51,6 +51,7 @@ class QgsMultiPolygonV2: QgsMultiSurface


protected:
virtual QgsMultiPolygonV2 *createEmptyWithSameType() const /Factory/;

virtual bool wktOmitChildType() const;

@@ -44,6 +44,10 @@ class QgsMultiSurface: QgsGeometryCollection



protected:
virtual QgsMultiSurface *createEmptyWithSameType() const /Factory/;


};


0 comments on commit 68179e2

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