Skip to content

Commit e5f1d87

Browse files
committed
allow zooming to multiple features by their ID
Changed QgsMapCanvas::zoomToFeatureId() to QgsMapCanvas::zoomToFeatureIds() accepting multiple IDs instead of just one.
1 parent 0ba089e commit e5f1d87

File tree

4 files changed

+34
-26
lines changed

4 files changed

+34
-26
lines changed

python/gui/qgsmapcanvas.sip

+3-3
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,10 @@ class QgsMapCanvas : QGraphicsView
168168
@param layer optionally specify different than current layer */
169169
void zoomToSelected( QgsVectorLayer* layer = NULL );
170170

171-
/** Set canvas extent to the bounding box of a feature
171+
/** Set canvas extent to the bounding box of a set of features
172172
@param layer the vector layer
173-
@param id the feature id*/
174-
void zoomToFeatureId( QgsVectorLayer* layer, QgsFeatureId id );
173+
@param ids the feature ids*/
174+
void zoomToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids );
175175

176176
/** Pan to the selected features of current (vector) layer keeping same extent. */
177177
void panToSelected( QgsVectorLayer* layer = NULL );

src/gui/attributetable/qgsdualview.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,12 @@ void QgsDualView::zoomToCurrentFeature()
409409
return;
410410
}
411411

412-
QgsFeatureId id = mFilterModel->rowToId( currentIndex );
412+
QgsFeatureIds ids;
413+
ids.insert( mFilterModel->rowToId( currentIndex ) );
413414
QgsMapCanvas* canvas = mFilterModel->mapCanvas();
414415
if ( canvas )
415416
{
416-
canvas->zoomToFeatureId( mLayerCache->layer(), id );
417+
canvas->zoomToFeatureIds( mLayerCache->layer(), ids );
417418
}
418419
}
419420

src/gui/qgsmapcanvas.cpp

+25-18
Original file line numberDiff line numberDiff line change
@@ -1071,38 +1071,45 @@ void QgsMapCanvas::zoomToFeatureExtent( QgsRectangle& rect )
10711071
refresh();
10721072
}
10731073

1074-
void QgsMapCanvas::zoomToFeatureId( QgsVectorLayer* layer, QgsFeatureId id )
1074+
void QgsMapCanvas::zoomToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids )
10751075
{
10761076
if ( !layer )
10771077
{
10781078
return;
10791079
}
10801080

1081-
QgsFeatureIterator it = layer->getFeatures( QgsFeatureRequest().setFilterFid( id ).setSubsetOfAttributes( QgsAttributeList() ) );
1081+
QgsFeatureIterator it = layer->getFeatures( QgsFeatureRequest().setFilterFids( ids ).setSubsetOfAttributes( QgsAttributeList() ) );
1082+
QgsRectangle rect;
1083+
rect.setMinimal();
10821084
QgsFeature fet;
1083-
if ( !it.nextFeature( fet ) )
1085+
int featureCount = 0;
1086+
while ( it.nextFeature( fet ) )
10841087
{
1085-
return;
1088+
QgsGeometry* geom = fet.geometry();
1089+
QString errorMessage;
1090+
if ( !geom || !geom->geometry() )
1091+
{
1092+
errorMessage = tr( "Feature does not have a geometry" );
1093+
}
1094+
else if ( geom->geometry()->isEmpty() )
1095+
{
1096+
errorMessage = tr( "Feature geometry is empty" );
1097+
}
1098+
if ( !errorMessage.isEmpty() )
1099+
{
1100+
emit messageEmitted( tr( "Zoom to feature id failed" ), errorMessage, QgsMessageBar::WARNING );
1101+
return;
1102+
}
1103+
QgsRectangle r = mapSettings().layerExtentToOutputExtent( layer, geom->boundingBox() );
1104+
rect.combineExtentWith( &r );
1105+
featureCount++;
10861106
}
10871107

1088-
QgsGeometry* geom = fet.geometry();
1089-
1090-
QString errorMessage;
1091-
if ( !geom || !geom->geometry() )
1092-
{
1093-
errorMessage = tr( "Feature does not have a geometry" );
1094-
}
1095-
else if ( geom->geometry()->isEmpty() )
1096-
{
1097-
errorMessage = tr( "Feature geometry is empty" );
1098-
}
1099-
if ( !errorMessage.isEmpty() )
1108+
if ( featureCount != ids.count() )
11001109
{
1101-
emit messageEmitted( tr( "Zoom to feature id failed" ), errorMessage, QgsMessageBar::WARNING );
11021110
return;
11031111
}
11041112

1105-
QgsRectangle rect = mapSettings().layerExtentToOutputExtent( layer, geom->boundingBox() );
11061113
zoomToFeatureExtent( rect );
11071114
}
11081115

src/gui/qgsmapcanvas.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,10 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
238238
@param layer optionally specify different than current layer */
239239
void zoomToSelected( QgsVectorLayer* layer = nullptr );
240240

241-
/** Set canvas extent to the bounding box of a feature
241+
/** Set canvas extent to the bounding box of a set of features
242242
@param layer the vector layer
243-
@param id the feature id*/
244-
void zoomToFeatureId( QgsVectorLayer* layer, QgsFeatureId id );
243+
@param ids the feature ids*/
244+
void zoomToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids );
245245

246246
/** Pan to the selected features of current (vector) layer keeping same extent. */
247247
void panToSelected( QgsVectorLayer* layer = nullptr );

0 commit comments

Comments
 (0)