Skip to content
Permalink
Browse files

allow zooming to multiple features by their ID

Changed QgsMapCanvas::zoomToFeatureId() to QgsMapCanvas::zoomToFeatureIds() accepting multiple IDs instead of just one.
  • Loading branch information
SebDieBln committed Jan 12, 2016
1 parent 0ba089e commit e5f1d875066bfc06179de193d69bfcc3e590ec66
Showing with 34 additions and 26 deletions.
  1. +3 −3 python/gui/qgsmapcanvas.sip
  2. +3 −2 src/gui/attributetable/qgsdualview.cpp
  3. +25 −18 src/gui/qgsmapcanvas.cpp
  4. +3 −3 src/gui/qgsmapcanvas.h
@@ -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 );
@@ -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 );
}
}

@@ -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 );
}

@@ -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 );

0 comments on commit e5f1d87

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