QGIS – Calculating Bounding Box Coordinates of a Selected Polygon

extentsqgis

I just manually (and somewhat inaccurately I'm sure) retrieved the bounding box coordinates for a county polygon, which is a selection from a larger layer of county shapes. I'd like to be able to use a QGIS plugin or other function to calculate this quickly and not-by-hand. This is almost certainly possible but I couldn't find anything with a Google search

Best Answer

The following little Python function will output the bounding box coordinates of the currently active feature:

def printBB():
    feature = iface.activeLayer().selectedFeatures()[0]
    print feature.geometry().boundingBox().toString()

To define the function, open the Python console from the Plugins menu, copy and paste the three lines into the console, and press enter. Then you can call the function by typing printBB() and pressing enter while the desired feature is selected.

Edit: For newer Python versions (Python 3.x) use this (print() with brackets):

def printBB():
    feature = iface.activeLayer().selectedFeatures()[0]
    print(feature.geometry().boundingBox().toString())