Skip to content
Permalink
Browse files

Fixes #33449 Snapping empty layer when adding new feature

  • Loading branch information
Julien Cabieces authored and nyalldawson committed Jan 13, 2020
1 parent e2004c7 commit 6b967949e3cec93fbff86e1076c3c3c9c535a0cb
Showing with 48 additions and 1 deletion.
  1. +5 −1 src/core/qgspointlocator.cpp
  2. +43 −0 tests/src/core/testqgspointlocator.cpp
@@ -834,7 +834,11 @@ void QgsPointLocator::onFeatureAdded( QgsFeatureId fid )
if ( !mRTree )
{
if ( mIsEmptyLayer )
init(); // first feature - let's built the index
{
// layer is not empty any more, let's build the index
mIsEmptyLayer = false;
init();
}
return; // nothing to do if we are not initialized yet
}

@@ -474,6 +474,49 @@ class TestQgsPointLocator : public QObject
delete loc;
}


void testEmptyLayer()
{
// Issue https://github.com/qgis/QGIS/issues/33449


// Create an empty layer, add one feature and check that we can snap on this feature
QgsVectorLayer layer( QStringLiteral( "Polygon" ), QStringLiteral( "x" ), QStringLiteral( "memory" ) );
QgsProject::instance()->addMapLayer( &layer );

QgsPointLocator loc( &layer );

// init locator (no rtree in locator because there is no feature in layer)
QgsPointXY pt( 2, 2 );
QgsPointLocator::Match m = loc.nearestVertex( pt, 999 );
QVERIFY( loc.mIsEmptyLayer );
QVERIFY( !loc.mRTree );

layer.startEditing();
QgsFeature ff( 0 );
QgsPolygonXY polygon;
QgsPolylineXY polyline;
polyline << QgsPointXY( 0, 1 ) << QgsPointXY( 1, 0 ) << QgsPointXY( 1, 1 ) << QgsPointXY( 0, 1 );
polygon << polyline;
QgsGeometry ffGeom = QgsGeometry::fromPolygonXY( polygon );
ff.setGeometry( ffGeom );

layer.addFeature( ff );

QVERIFY( !loc.mIsEmptyLayer );
QVERIFY( loc.mRTree );

// Check is inserted feature is well known from the locator (even in relaxed mode,
// no need to index and wait for finished)
m = loc.nearestVertex( pt, 999, nullptr, true );
QVERIFY( m.isValid() );
QVERIFY( m.hasVertex() );
QCOMPARE( m.layer(), &layer );
QCOMPARE( m.point(), QgsPointXY( 1, 1 ) );
QCOMPARE( m.distance(), std::sqrt( 2.0 ) );
QCOMPARE( m.vertexIndex(), 2 );
}

};

QGSTEST_MAIN( TestQgsPointLocator )

1 comment on commit 6b96794

@borysiasty

This comment has been minimized.

Copy link
Member

borysiasty commented on 6b96794 Jan 14, 2020

Thanks!

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