1

Does anybody know where i can find implementation of feature called "Zoom to Feature".

To use this function from GUI you have to right click on layer then choose "Open Attribute Table" then right click on feature and there should be option called "Zoom to Feature" in context menu.

It looks like this:

enter image description here

[IMPORTANT]

Feature in Qgis is a set of attributes from table.

enter image description here

  • Witch context menu are you referring to ? Also if you are not selecting a feature in the table to witch feature do you want to zoom ? – J.R Oct 17 '19 at 13:24
  • Are you wanting the QgsMapCanvas::zoomToSelected() method? – Joseph Oct 17 '19 at 13:42
  • @Joseph Nope i dont want this method because it assumes that feature was previously selected. I dont want to select any feature. I just want to zoom my map view to some feature on some layer. – BannanaFunTime Oct 17 '19 at 13:45
  • @J.R Im reffering to context menu presented in the attached picture. There is a feature selected in the attached picture. As far as i know Feature in Qgis is set of attributes in table. – BannanaFunTime Oct 17 '19 at 13:48
2

It is what i was looking 4.

    void QgsIdentifyResultsDialog::zoomToFeature()
{
  QTreeWidgetItem *item = lstResults->currentItem();

  QgsVectorLayer *vlayer = vectorLayer( item );
  QgsRasterLayer *rlayer = rasterLayer( item );
  if ( !vlayer && !rlayer )
    return;

  QgsMapLayer *layer = nullptr;
  if ( vlayer )
    layer = vlayer;
  else
    layer = rlayer;

  QgsIdentifyResultsFeatureItem *featItem = dynamic_cast<QgsIdentifyResultsFeatureItem *>( featureItem( item ) );
  if ( !featItem )
    return;

  QgsFeature feat = featItem->feature();
  if ( !feat.hasGeometry() )
    return;

  // TODO: verify CRS for raster WMS features
  QgsRectangle rect = mCanvas->mapSettings().layerExtentToOutputExtent( layer, feat.geometry().boundingBox() );

  if ( rect.isEmpty() )
  {
    QgsPointXY c = rect.center();
    rect = mCanvas->extent();
    rect.scale( 0.5, &c );
  }

  mCanvas->setExtent( rect );
  mCanvas->refresh();
}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.