Skip to content

Commit

Permalink
Use a feature source instead of vector layer for QgsGeometrySnapper
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 6, 2017
1 parent 8a84e13 commit 3601138
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
6 changes: 3 additions & 3 deletions python/analysis/vector/qgsgeometrysnapper.sip
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class QgsGeometrySnapper : QObject
EndPointToEndPoint,
};

QgsGeometrySnapper( QgsVectorLayer *referenceLayer );
QgsGeometrySnapper( QgsFeatureSource *referenceSource );
%Docstring
Constructor for QgsGeometrySnapper. A reference layer which contains geometries to snap to must be
Constructor for QgsGeometrySnapper. A reference feature source which contains geometries to snap to must be
set. It is assumed that all geometries snapped using this object will have the
same CRS as the reference layer (ie, no reprojection is performed).
same CRS as the reference source (ie, no reprojection is performed).
%End

QgsGeometry snapGeometry( const QgsGeometry &geometry, double snapTolerance, SnapMode mode = PreferNodes ) const;
Expand Down
10 changes: 4 additions & 6 deletions src/analysis/vector/qgsgeometrysnapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,13 +459,11 @@ QgsSnapIndex::SnapItem *QgsSnapIndex::getSnapItem( const QgsPoint &pos, double t
// QgsGeometrySnapper
//

QgsGeometrySnapper::QgsGeometrySnapper( QgsVectorLayer *referenceLayer )
: mReferenceLayer( referenceLayer )
QgsGeometrySnapper::QgsGeometrySnapper( QgsFeatureSource *referenceSource )
: mReferenceSource( referenceSource )
{
// Build spatial index
QgsFeatureRequest req;
req.setSubsetOfAttributes( QgsAttributeList() );
mIndex = QgsSpatialIndex( mReferenceLayer->getFeatures( req ) );
mIndex = QgsSpatialIndex( *mReferenceSource );
}

QgsFeatureList QgsGeometrySnapper::snapFeatures( const QgsFeatureList &features, double snapTolerance, SnapMode mode )
Expand Down Expand Up @@ -496,7 +494,7 @@ QgsGeometry QgsGeometrySnapper::snapGeometry( const QgsGeometry &geometry, doubl
QgsFeatureRequest refFeatureRequest = QgsFeatureRequest().setFilterFids( refFeatureIds ).setSubsetOfAttributes( QgsAttributeList() );
mReferenceLayerMutex.lock();
QgsFeature refFeature;
QgsFeatureIterator refFeatureIt = mReferenceLayer->getFeatures( refFeatureRequest );
QgsFeatureIterator refFeatureIt = mReferenceSource->getFeatures( refFeatureRequest );

while ( refFeatureIt.nextFeature( refFeature ) )
{
Expand Down
8 changes: 4 additions & 4 deletions src/analysis/vector/qgsgeometrysnapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ class ANALYSIS_EXPORT QgsGeometrySnapper : public QObject
};

/**
* Constructor for QgsGeometrySnapper. A reference layer which contains geometries to snap to must be
* Constructor for QgsGeometrySnapper. A reference feature source which contains geometries to snap to must be
* set. It is assumed that all geometries snapped using this object will have the
* same CRS as the reference layer (ie, no reprojection is performed).
* same CRS as the reference source (ie, no reprojection is performed).
*/
QgsGeometrySnapper( QgsVectorLayer *referenceLayer );
QgsGeometrySnapper( QgsFeatureSource *referenceSource );

/**
* Snaps a geometry to the reference layer and returns the result. The geometry must be in the same
Expand Down Expand Up @@ -99,7 +99,7 @@ class ANALYSIS_EXPORT QgsGeometrySnapper : public QObject

enum PointFlag { SnappedToRefNode, SnappedToRefSegment, Unsnapped };

QgsVectorLayer *mReferenceLayer = nullptr;
QgsFeatureSource *mReferenceSource = nullptr;
QgsFeatureList mInputFeatures;

QgsSpatialIndex mIndex;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsspatialindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ QgsSpatialIndex::QgsSpatialIndex( const QgsFeatureIterator &fi )

QgsSpatialIndex::QgsSpatialIndex( const QgsFeatureSource &source )
{
d = new QgsSpatialIndexData( source.getFeatures() );
d = new QgsSpatialIndexData( source.getFeatures( QgsFeatureRequest().setSubsetOfAttributes( QgsAttributeList() ) ) );
}

QgsSpatialIndex::QgsSpatialIndex( const QgsSpatialIndex &other ) //NOLINT
Expand Down

0 comments on commit 3601138

Please sign in to comment.