Skip to content

Commit

Permalink
move the optimal extent calculation for a point layer to its own method
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Sep 21, 2020
1 parent dd19040 commit b47346b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
58 changes: 33 additions & 25 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -1240,33 +1240,13 @@ void QgsMapCanvas::clearExtentHistory()
emit zoomNextStatusChanged( mLastExtentIndex < mLastExtent.size() - 1 );
}// clearExtentHistory

void QgsMapCanvas::zoomToSelected( QgsVectorLayer *layer )
QgsRectangle QgsMapCanvas::optimalExtentForPointLayer( QgsVectorLayer *layer, const QgsPointXY &center, int scaleFactor )
{
if ( !layer )
{
// use current layer by default
layer = qobject_cast<QgsVectorLayer *>( mCurrentLayer );
}

if ( !layer || !layer->isSpatial() || layer->selectedFeatureCount() == 0 )
return;

QgsRectangle rect = layer->boundingBoxOfSelected();
if ( rect.isNull() )
{
emit messageEmitted( tr( "Cannot zoom to selected feature(s)" ), tr( "No extent could be determined." ), Qgis::Warning );
return;
}

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

// zoom in if point cannot be distinguished from others
// also check that rect is empty, as it might not in case of multi points
if ( layer->geometryType() == QgsWkbTypes::PointGeometry && rect.isEmpty() )
if ( layer->geometryType() == QgsWkbTypes::PointGeometry )
{
int scaleFactor = 5;
QgsPointXY centerMapCoordinates = rect.center();
QgsPointXY centerLayerCoordinates = mSettings.mapToLayerCoordinates( layer, centerMapCoordinates );
QgsPointXY centerLayerCoordinates = mSettings.mapToLayerCoordinates( layer, center );
QgsRectangle extentRect = mSettings.mapToLayerCoordinates( layer, extent() ).scaled( 1.0 / scaleFactor, &centerLayerCoordinates );
QgsFeatureRequest req = QgsFeatureRequest().setFilterRect( extentRect ).setLimit( 1000 ).setNoAttributes();
QgsFeatureIterator fit = layer->getFeatures( req );
Expand All @@ -1288,10 +1268,38 @@ void QgsMapCanvas::zoomToSelected( QgsVectorLayer *layer )
{
// combine selected point with closest point and scale this rect
rect.combineExtentWith( mSettings.layerToMapCoordinates( layer, closestPoint ) );
rect.scale( scaleFactor, &centerMapCoordinates );
rect.scale( scaleFactor, &center );
}
}
return rect;
}

void QgsMapCanvas::zoomToSelected( QgsVectorLayer *layer )
{
if ( !layer )
{
// use current layer by default
layer = qobject_cast<QgsVectorLayer *>( mCurrentLayer );
}

if ( !layer || !layer->isSpatial() || layer->selectedFeatureCount() == 0 )
return;

QgsRectangle rect = layer->boundingBoxOfSelected();
if ( rect.isNull() )
{
emit messageEmitted( tr( "Cannot zoom to selected feature(s)" ), tr( "No extent could be determined." ), Qgis::Warning );
return;
}

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

// zoom in if point cannot be distinguished from others
// also check that rect is empty, as it might not in case of multi points
if ( layer->geometryType() == QgsWkbTypes::PointGeometry && rect.isEmpty() )
{
rect = optimalExtentForPointLayer( layer, rect.center() );
}
zoomToFeatureExtent( rect );
}

Expand Down
8 changes: 8 additions & 0 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -1265,6 +1265,14 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
*/
bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;

/**
* Rerturns the optimal extent for a point \a layer and a given \a center point in canvas CRS.
* This will return an extent combined of the center and the closest point in the layer.
* The extent can be scaled with a \a scale factor.
* The returned extent might be an empty rect if it cannot be determnined.
*/
QgsRectangle optimalExtentForPointLayer( QgsVectorLayer *layer, const QgsPointXY &center, int scaleFactor = 5 );

void setLayersPrivate( const QList<QgsMapLayer *> &layers );

void startPreviewJobs();
Expand Down

0 comments on commit b47346b

Please sign in to comment.