Skip to content

Commit

Permalink
Fixes #33449 Snapping empty layer when adding new feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Cabieces authored and nyalldawson committed Jan 13, 2020
1 parent e2004c7 commit 6b96794
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/core/qgspointlocator.cpp
Expand Up @@ -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
}

Expand Down
43 changes: 43 additions & 0 deletions tests/src/core/testqgspointlocator.cpp
Expand Up @@ -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 )
Expand Down

1 comment on commit 6b96794

@borysiasty
Copy link
Member

Choose a reason for hiding this comment

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

Thanks!

Please sign in to comment.