17

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

26

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.

  • 1
    I get this error when I try to run this script: File "<input>", line 4 printBB() ^ SyntaxError: invalid syntax – cbunn Aug 4 '15 at 22:01
  • @cbunn make sure you have the layer selected in the layers / table of contents - it's very sensitive to this! – DPSSpatial Sep 21 '15 at 18:56
  • thanks for the help. I was also forgetting to press enter to fully define the function printBB() before calling it. – cbunn Sep 22 '15 at 20:18
  • 2
    I had to use print(feature.geometry().boundingBox().toString()) for line 3. – cm1 Jun 25 '18 at 16:08
15

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

  • 1
    You have to check Use only selected obects and calculate for every object to get the desired result. – AndreJ Dec 3 '13 at 5:12
  • hmm that didn't work for me, the outputted shapefile was blank essentially – boulder_ruby Dec 3 '13 at 19:24
  • 1
    You only want to check 'Use only selected objects' if you have objects selected. I left both those fields unchecked and achieved a fine result. – Johanna Mar 8 '16 at 10:03
4

You can also do this using PyShp plugin in Python.. Its a lot simpler...

import shapefile

sf = shapefile.Reader("Path to shapefile...") 
shapes = sf.shapes() 
bbox = shapes[0].bbox # Retrieves the bounding box of the first shape

print bbox # Will print the bounding box coordinates

More information on PyShp and other functionalities can be found here.

3

If you need many polygon's bounding boxes, you can "Save As..." your layer to GeoJSON with WRITE_BBOX=YES option and there will be a single "bbox" attribute before your "geometry" attribute, even for multipolygons.

..., "bbox": [ -70.062408006999874, 12.417669989000046, -69.876820441999939, 12.632147528000104 ], "geometry": ...
1

I had to do this to generate a field I could import as a list elsewhere.

  1. Install QGIS plugin FieldPyculator
  2. Toggle editing of layer
  3. Create new column in attribute table as string e.g. bbox (save edits)
  4. Open FieldPyculator
  5. Set Update Field to bbox
  6. In Field expression type something like:

    bb = $geom.boundingBox().toString()

    bb = bb.replace(' ','')

    bb = bb.replace(':',',')

    value = bb

  7. Run

  8. Turn off editing and save layer
1

For quick acces, an option is to keep bbox coordinates in the attribute table.

  • Toogle edit mode
  • Launch Field calculator
  • Set create new field, set name (e.g xmin), type (decimal), length, (precision)
  • Expression: x_min( $geometry)
  • You will get a new field in the attribute table with LEFT coordinate.

    Repeat this for RIGHT, BOTTOM, TOP with expressions:

  • x_max($geometry)
  • y_min($geometry)
  • y_max($geometry)
  • Example of the result enter image description here

    You can then set AutoFields plugin to automatically update these fields when feature is changed.

    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.