Skip to content
Permalink
Browse files

Add method to seek atlas directly to a QgsFeature

  • Loading branch information
nyalldawson committed Jan 1, 2018
1 parent 7450c78 commit 39ae0eef7d3d0d65e092a1e239ec347535a3edc1
@@ -332,6 +332,19 @@ Seeks to the specified ``feature`` number.

.. seealso:: :py:func:`next()`

.. seealso:: :py:func:`last()`
%End

bool seekTo( const QgsFeature &feature );
%Docstring
Seeks to the specified ``feature``.

.. seealso:: :py:func:`first()`

.. seealso:: :py:func:`previous()`

.. seealso:: :py:func:`next()`

.. seealso:: :py:func:`last()`
%End

@@ -387,6 +387,28 @@ bool QgsLayoutAtlas::seekTo( int feature )
return prepareForFeature( feature );
}

bool QgsLayoutAtlas::seekTo( const QgsFeature &feature )
{
int i = -1;
auto it = mFeatureIds.constBegin();
for ( int currentIdx = 0; it != mFeatureIds.constEnd(); ++it, ++currentIdx )
{
if ( ( *it ).first == feature.id() )
{
i = currentIdx;
break;
}
}

if ( i < 0 )
{
//feature not found
return false;
}

return seekTo( i );
}

void QgsLayoutAtlas::refreshCurrentFeature()
{
prepareForFeature( mCurrentFeatureNo );
@@ -289,6 +289,15 @@ class CORE_EXPORT QgsLayoutAtlas : public QObject, public QgsAbstractLayoutItera
*/
bool seekTo( int feature );

/**
* Seeks to the specified \a feature.
* \see first()
* \see previous()
* \see next()
* \see last()
*/
bool seekTo( const QgsFeature &feature );

/**
* Refreshes the current atlas feature, by refetching its attributes from the vector layer provider
*/
@@ -20,6 +20,7 @@
import glob

from qgis.core import (QgsUnitTypes,
QgsFeature,
QgsLayout,
QgsPrintLayout,
QgsLayoutAtlas,
@@ -216,24 +217,28 @@ def testIteration(self):
self.assertEqual(atlas.currentFeatureNumber(), 0)
self.assertEqual(l.reportContext().feature()[4], 'Basse-Normandie')
self.assertEqual(l.reportContext().layer(), vector_layer)
f1 = l.reportContext().feature()

self.assertTrue(atlas.next())
self.assertEqual(len(atlas_feature_changed_spy), 2)
self.assertEqual(len(context_changed_spy), 2)
self.assertEqual(atlas.currentFeatureNumber(), 1)
self.assertEqual(l.reportContext().feature()[4], 'Bretagne')
f2 = l.reportContext().feature()

self.assertTrue(atlas.next())
self.assertEqual(len(atlas_feature_changed_spy), 3)
self.assertEqual(len(context_changed_spy), 3)
self.assertEqual(atlas.currentFeatureNumber(), 2)
self.assertEqual(l.reportContext().feature()[4], 'Pays de la Loire')
f3 = l.reportContext().feature()

self.assertTrue(atlas.next())
self.assertEqual(len(atlas_feature_changed_spy), 4)
self.assertEqual(len(context_changed_spy), 4)
self.assertEqual(atlas.currentFeatureNumber(), 3)
self.assertEqual(l.reportContext().feature()[4], 'Centre')
f4 = l.reportContext().feature()

self.assertFalse(atlas.next())
self.assertTrue(atlas.seekTo(2))
@@ -263,6 +268,16 @@ def testIteration(self):
self.assertTrue(atlas.endRender())
self.assertEqual(len(atlas_feature_changed_spy), 10)

self.assertTrue(atlas.seekTo(f1))
self.assertEqual(l.reportContext().feature()[4], 'Basse-Normandie')
self.assertTrue(atlas.seekTo(f4))
self.assertEqual(l.reportContext().feature()[4], 'Centre')
self.assertTrue(atlas.seekTo(f3))
self.assertEqual(l.reportContext().feature()[4], 'Pays de la Loire')
self.assertTrue(atlas.seekTo(f2))
self.assertEqual(l.reportContext().feature()[4], 'Bretagne')
self.assertFalse(atlas.seekTo(QgsFeature(5)))

def testUpdateFeature(self):
p = QgsProject()
vectorFileInfo = QFileInfo(unitTestDataPath() + "/france_parts.shp")

0 comments on commit 39ae0ee

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