Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Rewrite addTopologicalPoints() and remove legacy snapping code + unit…
… test
  • Loading branch information
wonder-sk committed Apr 19, 2017
1 parent cbf5e9c commit 24c5b03
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 349 deletions.
5 changes: 5 additions & 0 deletions doc/api_break.dox
Expand Up @@ -286,6 +286,7 @@ should now call QgsCoordinateReferenceSystem::invalidateCache() and QgsCoordinat
- QgsSymbologyV2Conversion was removed. Reading of renderers from pre-1.0 versions is not supported anymore.
- QgsTextAnnotationItem. Use QgsTextAnnotation instead.
- QgsSnapper. Use QgsSnappingUtils instead.
- QgsSnappingResult. Use QgsSnappingUtils instead.
- QgsMapCanvasSnapper. Use QgsMapCanvas::snappingUtils() instead.


Expand Down Expand Up @@ -2095,6 +2096,9 @@ displayExpression instead. For the map tip use mapTipTemplate() instead.
- readXml() does not resolve references to joined layers. Call resolveReferences() when joined layers are available.
- snapWithContext(), snapToGeometry() - last argument has changed from QgsSnapper::SnappingType to QgsSnappingResult::SnappingType (no change in functionality).
- snapPoint() has been removed - use QgsPointLocator class instead.
- snapWithContext() has been removed - use QgsPointLocator class instead.
- insertSegmentVerticesForSnap() has been removed - use addTopologicalPoints() directly.


QgsVectorLayerEditBuffer {#qgis_api_break_3_0_QgsVectorLayerEditBuffer}
------------------------
Expand All @@ -2110,6 +2114,7 @@ QgsVectorLayerEditUtils {#qgis_api_break_3_0_QgsVectorLayerEditUtils}
-----------------------

- addTopologicalPoints() now accepts a geometry reference, not a pointer.
- insertSegmentVerticesForSnap() has been removed.


QgsVectorLayerImport {#qgis_api_break_3_0_QgsVectorLayerImport}
Expand Down
1 change: 0 additions & 1 deletion python/core/core.sip
Expand Up @@ -137,7 +137,6 @@
%Include qgsscalecalculator.sip
%Include qgsscaleutils.sip
%Include qgssimplifymethod.sip
%Include qgssnapper.sip
%Include qgssnappingutils.sip
%Include qgsspatialindex.sip
%Include qgssqlstatement.sip
Expand Down
38 changes: 0 additions & 38 deletions python/core/qgssnapper.sip

This file was deleted.

23 changes: 0 additions & 23 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -1043,15 +1043,6 @@ TODO QGIS 3.0 returns an enum instead of a magic constant
:rtype: int
%End

int insertSegmentVerticesForSnap( const QList<QgsSnappingResult> &snapResults );
%Docstring
Inserts vertices to the snapped segments.
This is useful for topological editing if snap to segment is enabled.
\param snapResults results collected from the snapping operation
:return: 0 in case of success
:rtype: int
%End



virtual bool isEditable() const;
Expand All @@ -1068,20 +1059,6 @@ Returns true if the provider has been modified since the last commit
:rtype: bool
%End

int snapWithContext( const QgsPoint &startPoint,
double snappingTolerance,
QMultiMap < double, QgsSnappingResult > &snappingResults /Out/,
QgsSnappingResult::SnappingType snap_to );
%Docstring
Snaps to segment or vertex within given tolerance
\param startPoint point to snap (in layer coordinates)
\param snappingTolerance distance tolerance for snapping
\param snappingResults snapping results. Key is the distance between startPoint and snapping target
\param snap_to to segment / to vertex
:return: 0 in case of success
:rtype: int
%End

virtual void reload();
%Docstring
Synchronises with changes in the datasource
Expand Down
8 changes: 1 addition & 7 deletions python/core/qgsvectorlayereditutils.sip
Expand Up @@ -149,16 +149,10 @@ class QgsVectorLayerEditUtils
*/
int addTopologicalPoints( const QgsPoint& p );

/** Inserts vertices to the snapped segments.
* This is useful for topological editing if snap to segment is enabled.
* @param snapResults results collected from the snapping operation
* @return 0 in case of success
*/
int insertSegmentVerticesForSnap( const QList<QgsSnappingResult>& snapResults );

protected:

/** Little helper function that gives bounding box from a list of points.
@return 0 in case of success */
int boundingBoxFromPointList( const QList<QgsPoint>& list, double& xmin, double& ymin, double& xmax, double& ymax ) const;

};
1 change: 0 additions & 1 deletion src/core/CMakeLists.txt
Expand Up @@ -769,7 +769,6 @@ SET(QGIS_CORE_HDRS
qgsscalecalculator.h
qgsscaleutils.h
qgssimplifymethod.h
qgssnapper.h
qgssnappingutils.h
qgsspatialindex.h
qgssqlexpressioncompiler.h
Expand Down
65 changes: 0 additions & 65 deletions src/core/qgssnapper.h

This file was deleted.

121 changes: 0 additions & 121 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -2582,127 +2582,6 @@ bool QgsVectorLayer::addFeatures( QgsFeatureList features, bool makeSelected )
}


int QgsVectorLayer::snapWithContext( const QgsPoint &startPoint, double snappingTolerance,
QMultiMap<double, QgsSnappingResult> &snappingResults,
QgsSnappingResult::SnappingType snap_to )
{
if ( !hasGeometryType() )
return 1;

if ( snappingTolerance <= 0 || !mDataProvider )
{
return 1;
}

QgsRectangle searchRect( startPoint.x() - snappingTolerance, startPoint.y() - snappingTolerance,
startPoint.x() + snappingTolerance, startPoint.y() + snappingTolerance );
double sqrSnappingTolerance = snappingTolerance * snappingTolerance;

int n = 0;
QgsFeature f;

if ( mCache->cachedGeometriesRect().contains( searchRect ) )
{
QgsGeometryMap &cachedGeometries = mCache->cachedGeometries();
for ( QgsGeometryMap::iterator it = cachedGeometries.begin(); it != cachedGeometries.end() ; ++it )
{
QgsGeometry g = it.value();
if ( g.boundingBox().intersects( searchRect ) )
{
snapToGeometry( startPoint, it.key(), g, sqrSnappingTolerance, snappingResults, snap_to );
++n;
}
}
}
else
{
// snapping outside cached area

QgsFeatureIterator fit = getFeatures( QgsFeatureRequest()
.setFilterRect( searchRect )
.setFlags( QgsFeatureRequest::ExactIntersect )
.setSubsetOfAttributes( QgsAttributeList() ) );

while ( fit.nextFeature( f ) )
{
snapToGeometry( startPoint, f.id(), f.geometry(), sqrSnappingTolerance, snappingResults, snap_to );
++n;
}
}

return n == 0 ? 2 : 0;
}

void QgsVectorLayer::snapToGeometry( const QgsPoint &startPoint,
QgsFeatureId featureId,
const QgsGeometry &geom,
double sqrSnappingTolerance,
QMultiMap<double, QgsSnappingResult> &snappingResults,
QgsSnappingResult::SnappingType snap_to ) const
{
if ( geom.isNull() )
{
return;
}

int atVertex, beforeVertex, afterVertex;
double sqrDistVertexSnap, sqrDistSegmentSnap;
QgsPoint snappedPoint;
QgsSnappingResult snappingResultVertex;
QgsSnappingResult snappingResultSegment;

if ( snap_to == QgsSnappingResult::SnapToVertex || snap_to == QgsSnappingResult::SnapToVertexAndSegment )
{
snappedPoint = geom.closestVertex( startPoint, atVertex, beforeVertex, afterVertex, sqrDistVertexSnap );
if ( sqrDistVertexSnap < sqrSnappingTolerance )
{
snappingResultVertex.snappedVertex = snappedPoint;
snappingResultVertex.snappedVertexNr = atVertex;
snappingResultVertex.beforeVertexNr = beforeVertex;
if ( beforeVertex != -1 ) // make sure the vertex is valid
{
snappingResultVertex.beforeVertex = geom.vertexAt( beforeVertex );
}
snappingResultVertex.afterVertexNr = afterVertex;
if ( afterVertex != -1 ) // make sure the vertex is valid
{
snappingResultVertex.afterVertex = geom.vertexAt( afterVertex );
}
snappingResultVertex.snappedAtGeometry = featureId;
snappingResultVertex.layer = this;
snappingResults.insert( sqrt( sqrDistVertexSnap ), snappingResultVertex );
return;
}
}
if ( snap_to == QgsSnappingResult::SnapToSegment || snap_to == QgsSnappingResult::SnapToVertexAndSegment ) // snap to segment
{
if ( geometryType() != QgsWkbTypes::PointGeometry ) // cannot snap to segment for points/multipoints
{
sqrDistSegmentSnap = geom.closestSegmentWithContext( startPoint, snappedPoint, afterVertex, nullptr, crs().isGeographic() ? 1e-12 : 1e-8 );

if ( sqrDistSegmentSnap < sqrSnappingTolerance )
{
snappingResultSegment.snappedVertex = snappedPoint;
snappingResultSegment.snappedVertexNr = -1;
snappingResultSegment.beforeVertexNr = afterVertex - 1;
snappingResultSegment.afterVertexNr = afterVertex;
snappingResultSegment.snappedAtGeometry = featureId;
snappingResultSegment.beforeVertex = geom.vertexAt( afterVertex - 1 );
snappingResultSegment.afterVertex = geom.vertexAt( afterVertex );
snappingResultSegment.layer = this;
snappingResults.insert( sqrt( sqrDistSegmentSnap ), snappingResultSegment );
}
}
}
}

int QgsVectorLayer::insertSegmentVerticesForSnap( const QList<QgsSnappingResult> &snapResults )
{
QgsVectorLayerEditUtils utils( this );
return utils.insertSegmentVerticesForSnap( snapResults );
}


void QgsVectorLayer::setCoordinateSystem()
{
QgsDebugMsg( "----- Computing Coordinate System" );
Expand Down
35 changes: 0 additions & 35 deletions src/core/qgsvectorlayer.h
Expand Up @@ -32,7 +32,6 @@
#include "qgsfeature.h"
#include "qgsfeaturerequest.h"
#include "qgsfields.h"
#include "qgssnapper.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorsimplifymethod.h"
#include "qgseditformconfig.h"
Expand Down Expand Up @@ -1029,13 +1028,6 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
*/
int addTopologicalPoints( const QgsPoint &p );

/** Inserts vertices to the snapped segments.
* This is useful for topological editing if snap to segment is enabled.
* \param snapResults results collected from the snapping operation
* \returns 0 in case of success
*/
int insertSegmentVerticesForSnap( const QList<QgsSnappingResult> &snapResults );

/** Access to labeling configuration.
* \since QGIS 2.12
* \note not available in Python bindings
Expand All @@ -1056,18 +1048,6 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
//! Returns true if the provider has been modified since the last commit
virtual bool isModified() const;

/** Snaps to segment or vertex within given tolerance
* \param startPoint point to snap (in layer coordinates)
* \param snappingTolerance distance tolerance for snapping
* \param snappingResults snapping results. Key is the distance between startPoint and snapping target
* \param snap_to to segment / to vertex
* \returns 0 in case of success
*/
int snapWithContext( const QgsPoint &startPoint,
double snappingTolerance,
QMultiMap < double, QgsSnappingResult > &snappingResults SIP_OUT,
QgsSnappingResult::SnappingType snap_to );

//! Synchronises with changes in the datasource
virtual void reload() override;

Expand Down Expand Up @@ -1904,21 +1884,6 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
//! Goes through all features and finds a free id (e.g. to give it temporarily to a not-committed feature)
QgsFeatureId findFreeId();

/** Snaps to a geometry and adds the result to the multimap if it is within the snapping result
* \param startPoint start point of the snap
* \param featureId id of feature
* \param geom geometry to snap
* \param sqrSnappingTolerance squared search tolerance of the snap
* \param snappingResults list to which the result is appended
* \param snap_to snap to vertex or to segment
*/
void snapToGeometry( const QgsPoint &startPoint,
QgsFeatureId featureId,
const QgsGeometry &geom,
double sqrSnappingTolerance,
QMultiMap<double, QgsSnappingResult> &snappingResults,
QgsSnappingResult::SnappingType snap_to ) const;

//! Add joined attributes to a feature
//void addJoinedAttributes( QgsFeature& f, bool all = false );

Expand Down

1 comment on commit 24c5b03

@nyalldawson
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So nice to see these cleanups!

Please sign in to comment.