Skip to content

Commit

Permalink
Properly zoom to feature for single point selected
Browse files Browse the repository at this point in the history
fix #9160
this will fetch the canvas content and combine selected point with closest point and scale it 5 times
  • Loading branch information
3nids committed Oct 25, 2018
1 parent 0959e9f commit edc0303
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/gui/qgsmapcanvas.cpp
Expand Up @@ -990,8 +990,39 @@ void QgsMapCanvas::zoomToSelected( QgsVectorLayer *layer )
}

rect = mapSettings().layerExtentToOutputExtent( layer, rect );

// zoom in if point cannot be distinguish from others
// also check that rect is empty, as it might not in case of multi points
if ( layer->geometryType() == QgsWkbTypes::PointGeometry && rect.isEmpty() )
{
QgsPointXY center = mSettings.mapToLayerCoordinates( layer, rect.center() );
QgsRectangle extentRect = mSettings.mapToLayerCoordinates( layer, extent() );
QgsFeatureRequest req = QgsFeatureRequest().setFilterRect( extentRect ).setLimit( 1000 ).setNoAttributes();
QgsFeatureIterator fit = layer->getFeatures( req );
QgsFeature f;
QgsPointXY closestPoint;
double closestSquaredDistance = extentRect.width() + extentRect.height();
bool pointFound = false;
while ( fit.nextFeature( f ) )
{
QgsPointXY point = f.geometry().asPoint();
double sqrDist = point.sqrDist( center );
if ( sqrDist > closestSquaredDistance || sqrDist < 4 * std::numeric_limits<double>::epsilon() )
continue;
pointFound = true;
closestPoint = point;
closestSquaredDistance = sqrDist;
}
if ( pointFound )
{
// combine selected point with closest point and scale this rect
rect.combineExtentWith( mSettings.layerToMapCoordinates( layer, closestPoint ) );
rect.scale( 5, &center );
}
}

zoomToFeatureExtent( rect );
} // zoomToSelected
}

void QgsMapCanvas::zoomToFeatureExtent( QgsRectangle &rect )
{
Expand Down

0 comments on commit edc0303

Please sign in to comment.