Calculate bounding box coordinates of a selected polygon with QGIS
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())
QGIS can do it via Polygon from Layer Extent
Vector - Research tools - Polygon From Layer Extent
Will produce a new shapefile with attributes like XMIN XMAX YMIN YMAX AREA WIDTH HEIGHT
If you need many polygon's bounding boxes, on the left hand side,
- Right click on your layer
- Click "Export"
- Click "Save Feature As"
The in the dialog that pops up,
- Set "Format" to "GeoJSON"
- Under the "Layer Options" set WRITE_BBOX to YES
Now when you export look inside the file for "bbox" (should be before the feature declaration), like this:
..., "bbox": [ -70.062408006999874, 12.417669989000046, -69.876820441999939, 12.632147528000104 ], "geometry": ...