Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #2667 from SebDieBln/ZoomToFeatureIds
allow zooming to multiple features by their ID
  • Loading branch information
nyalldawson committed Jan 13, 2016
2 parents 7495d9e + e5f1d87 commit 21cdefa
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
6 changes: 3 additions & 3 deletions python/gui/qgsmapcanvas.sip
Expand Up @@ -168,10 +168,10 @@ class QgsMapCanvas : QGraphicsView
@param layer optionally specify different than current layer */
void zoomToSelected( QgsVectorLayer* layer = NULL );

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

/** Pan to the selected features of current (vector) layer keeping same extent. */
void panToSelected( QgsVectorLayer* layer = NULL );
Expand Down
5 changes: 3 additions & 2 deletions src/gui/attributetable/qgsdualview.cpp
Expand Up @@ -409,11 +409,12 @@ void QgsDualView::zoomToCurrentFeature()
return;
}

QgsFeatureId id = mFilterModel->rowToId( currentIndex );
QgsFeatureIds ids;
ids.insert( mFilterModel->rowToId( currentIndex ) );
QgsMapCanvas* canvas = mFilterModel->mapCanvas();
if ( canvas )
{
canvas->zoomToFeatureId( mLayerCache->layer(), id );
canvas->zoomToFeatureIds( mLayerCache->layer(), ids );
}
}

Expand Down
43 changes: 25 additions & 18 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -1071,38 +1071,45 @@ void QgsMapCanvas::zoomToFeatureExtent( QgsRectangle& rect )
refresh();
}

void QgsMapCanvas::zoomToFeatureId( QgsVectorLayer* layer, QgsFeatureId id )
void QgsMapCanvas::zoomToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids )
{
if ( !layer )
{
return;
}

QgsFeatureIterator it = layer->getFeatures( QgsFeatureRequest().setFilterFid( id ).setSubsetOfAttributes( QgsAttributeList() ) );
QgsFeatureIterator it = layer->getFeatures( QgsFeatureRequest().setFilterFids( ids ).setSubsetOfAttributes( QgsAttributeList() ) );
QgsRectangle rect;
rect.setMinimal();
QgsFeature fet;
if ( !it.nextFeature( fet ) )
int featureCount = 0;
while ( it.nextFeature( fet ) )
{
return;
QgsGeometry* geom = fet.geometry();
QString errorMessage;
if ( !geom || !geom->geometry() )
{
errorMessage = tr( "Feature does not have a geometry" );
}
else if ( geom->geometry()->isEmpty() )
{
errorMessage = tr( "Feature geometry is empty" );
}
if ( !errorMessage.isEmpty() )
{
emit messageEmitted( tr( "Zoom to feature id failed" ), errorMessage, QgsMessageBar::WARNING );
return;
}
QgsRectangle r = mapSettings().layerExtentToOutputExtent( layer, geom->boundingBox() );
rect.combineExtentWith( &r );
featureCount++;
}

QgsGeometry* geom = fet.geometry();

QString errorMessage;
if ( !geom || !geom->geometry() )
{
errorMessage = tr( "Feature does not have a geometry" );
}
else if ( geom->geometry()->isEmpty() )
{
errorMessage = tr( "Feature geometry is empty" );
}
if ( !errorMessage.isEmpty() )
if ( featureCount != ids.count() )
{
emit messageEmitted( tr( "Zoom to feature id failed" ), errorMessage, QgsMessageBar::WARNING );
return;
}

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

Expand Down
6 changes: 3 additions & 3 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -238,10 +238,10 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
@param layer optionally specify different than current layer */
void zoomToSelected( QgsVectorLayer* layer = nullptr );

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

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

0 comments on commit 21cdefa

Please sign in to comment.