QList<QgsPointXY> QgsAdvancedDigitizingDockWidget::snapSegmentToAllLayers( const QgsPointXY &originalMapPoint, bool *snapped ) const { QList<QgsPointXY> segment; QgsPointXY pt1, pt2; QgsPointLocator::Match match; QgsSnappingUtils *snappingUtils = mMapCanvas->snappingUtils(); QgsSnappingConfig canvasConfig = snappingUtils->config(); QgsSnappingConfig localConfig = snappingUtils->config(); localConfig.setMode( QgsSnappingConfig::AllLayers ); localConfig.setType( QgsSnappingConfig::Segment ); snappingUtils->setConfig( localConfig ); match = snappingUtils->snapToMap( originalMapPoint ); snappingUtils->setConfig( canvasConfig ); if ( match.isValid() && match.hasEdge() ) { match.edgePoints( pt1, pt2 ); segment << pt1 << pt2; } if ( snapped ) { *snapped = segment.count() == 2; } return segment; }
void testSnapOnIntersection() { // testing with a layer with two crossing linestrings // (0,1) x x (1,1) // \/ // /\ . // (0,0) x x (1,0) QgsVectorLayer* vl = new QgsVectorLayer( QStringLiteral( "LineString" ), QStringLiteral( "x" ), QStringLiteral( "memory" ) ); QgsPolyline polyline1, polyline2; polyline1 << QgsPoint( 0, 0 ) << QgsPoint( 1, 1 ); polyline2 << QgsPoint( 1, 0 ) << QgsPoint( 0, 1 ); QgsFeature f1; QgsGeometry f1g = QgsGeometry::fromPolyline( polyline1 ) ; f1.setGeometry( f1g ); QgsFeature f2; QgsGeometry f2g = QgsGeometry::fromPolyline( polyline2 ); f2.setGeometry( f2g ); QgsFeatureList flist; flist << f1 << f2; vl->dataProvider()->addFeatures( flist ); QVERIFY( vl->dataProvider()->featureCount() == 2 ); QgsMapSettings mapSettings; mapSettings.setOutputSize( QSize( 100, 100 ) ); mapSettings.setExtent( QgsRectangle( 0, 0, 1, 1 ) ); QVERIFY( mapSettings.hasValidSettings() ); QgsSnappingUtils u; u.setMapSettings( mapSettings ); QgsSnappingConfig snappingConfig = u.config(); snappingConfig.setMode( QgsSnappingConfig::AdvancedConfiguration ); QgsSnappingConfig::IndividualLayerSettings layerSettings( true, QgsSnappingConfig::Vertex, 0.1, QgsTolerance::ProjectUnits ); snappingConfig.setIndividualLayerSettings( vl, layerSettings ); u.setConfig( snappingConfig ); // no snapping on intersections by default - should find nothing QgsPointLocator::Match m = u.snapToMap( QgsPoint( 0.45, 0.5 ) ); QVERIFY( !m.isValid() ); snappingConfig.setIntersectionSnapping( true ); u.setConfig( snappingConfig ); QgsPointLocator::Match m2 = u.snapToMap( QgsPoint( 0.45, 0.5 ) ); QVERIFY( m2.isValid() ); QCOMPARE( m2.type(), QgsPointLocator::Vertex ); QCOMPARE( m2.point(), QgsPoint( 0.5, 0.5 ) ); delete vl; }
void testSnapModeCurrent() { QgsMapSettings mapSettings; mapSettings.setOutputSize( QSize( 100, 100 ) ); mapSettings.setExtent( QgsRectangle( 0, 0, 1, 1 ) ); QVERIFY( mapSettings.hasValidSettings() ); QgsSnappingUtils u; u.setMapSettings( mapSettings ); u.setCurrentLayer( mVL ); // first try with no snapping enabled QgsSnappingConfig snappingConfig = u.config(); snappingConfig.setEnabled( false ); snappingConfig.setTolerance( 10 ); snappingConfig.setUnits( QgsTolerance::Pixels ); snappingConfig.setMode( QgsSnappingConfig::ActiveLayer ); u.setConfig( snappingConfig ); QgsPointLocator::Match m0 = u.snapToMap( QPoint( 100, 100 ) ); QVERIFY( !m0.isValid() ); QVERIFY( !m0.hasVertex() ); // now enable snapping snappingConfig.setEnabled( true ); snappingConfig.setType( QgsSnappingConfig::Vertex ); u.setConfig( snappingConfig ); QgsPointLocator::Match m = u.snapToMap( QPoint( 100, 100 ) ); QVERIFY( m.isValid() ); QVERIFY( m.hasVertex() ); QCOMPARE( m.point(), QgsPoint( 1, 0 ) ); QgsPointLocator::Match m2 = u.snapToMap( QPoint( 0, 100 ) ); QVERIFY( !m2.isValid() ); QVERIFY( !m2.hasVertex() ); // do not consider edges in the following test - on 32-bit platforms // result was an edge match very close to (1,0) instead of being exactly (1,0) snappingConfig.setType( QgsSnappingConfig::Vertex ); u.setConfig( snappingConfig ); // test with filtering FilterExcludePoint myFilter( QgsPoint( 1, 0 ) ); QgsPointLocator::Match m3 = u.snapToMap( QPoint( 100, 100 ), &myFilter ); QVERIFY( !m3.isValid() ); }
void testSnapModeAll() { QgsMapSettings mapSettings; mapSettings.setOutputSize( QSize( 100, 100 ) ); mapSettings.setExtent( QgsRectangle( 0, 0, 1, 1 ) ); QVERIFY( mapSettings.hasValidSettings() ); QgsSnappingUtils u; QgsSnappingConfig snappingConfig = u.config(); u.setMapSettings( mapSettings ); snappingConfig.setMode( QgsSnappingConfig::AllLayers ); u.setConfig( snappingConfig ); // right now there are no layers in map settings - snapping will fail QgsPointLocator::Match m = u.snapToMap( QPoint( 100, 100 ) ); QVERIFY( !m.isValid() ); // now check with our layer mapSettings.setLayers( QStringList() << mVL->id() ); u.setMapSettings( mapSettings ); QgsPointLocator::Match m2 = u.snapToMap( QPoint( 100, 100 ) ); QVERIFY( m2.isValid() ); QCOMPARE( m2.point(), QgsPoint( 1, 0 ) ); }
void testSnapModeAdvanced() { QgsMapSettings mapSettings; mapSettings.setOutputSize( QSize( 100, 100 ) ); mapSettings.setExtent( QgsRectangle( 0, 0, 1, 1 ) ); QVERIFY( mapSettings.hasValidSettings() ); QgsSnappingUtils u; QgsSnappingConfig snappingConfig = u.config(); u.setMapSettings( mapSettings ); snappingConfig.setMode( QgsSnappingConfig::AdvancedConfiguration ); snappingConfig.setIndividualLayerSettings( mVL, QgsSnappingConfig::IndividualLayerSettings( true, QgsSnappingConfig::Vertex, 10, QgsTolerance::Pixels ) ); u.setConfig( snappingConfig ); QgsPointLocator::Match m = u.snapToMap( QPoint( 100, 100 ) ); QVERIFY( m.isValid() ); QVERIFY( m.hasVertex() ); QCOMPARE( m.point(), QgsPoint( 1, 0 ) ); // test with filtering FilterExcludePoint myFilter( QgsPoint( 1, 0 ) ); QgsPointLocator::Match m2 = u.snapToMap( QPoint( 100, 100 ), &myFilter ); QVERIFY( !m2.isValid() ); }