16

Is there a way to get the extent (bbox) of a vector layer in QGIS?

I see that I can update the extent, but I am looking for the actual coordinates of the extent.

Taras
  • 32,823
  • 4
  • 66
  • 137
ustroetz
  • 7,994
  • 10
  • 72
  • 118

7 Answers7

22

The layer extents are available in the Layer Properties | Metadata section | Extent.

enter image description here

Taras
  • 32,823
  • 4
  • 66
  • 137
underdark
  • 84,148
  • 21
  • 231
  • 413
13

To get a bounding box in QGIS as a new vector layer use the "Extract layer extent":
Vector -> Research Tools -> Extract layer extent

enter image description here

Taras
  • 32,823
  • 4
  • 66
  • 137
Curlew
  • 8,152
  • 5
  • 36
  • 72
  • I don't really want to create a new file. I am only interested in the four coordinates. – ustroetz Feb 03 '14 at 17:08
  • You could just use extract the nodes of the newly created polgygon and use the Export/Add geometry columns tool to get the corner coordinates. Put everything into a Processing model and it runs in no time...? – Curlew Feb 03 '14 at 17:15
  • Note: This works for raster layers too – prusswan Jun 25 '19 at 06:34
2

PostGIS has the ST_Extent() function which generates a bounding box for you. So my approach would be to export the layer to a database. if you want id, name, geom, bounding box just do

select id, name, geom, ST_Extent(geom)
from your_table group by id
Taras
  • 32,823
  • 4
  • 66
  • 137
2

From the (right-click) context menu of the desired layer, choose Export => Save Feature As...

Layer Context Menu: Export -> Save Feature As...

In the dialog that appears, you can ignore all inputs and just scroll down to the Extent section and enable the checkbox.

Save Feature As Dialog: Extent Section

So for a standard bbox array of [minx,miny,maxx,maxy] your values would be [west,south,east,north].

Erhhung
  • 121
  • 1
2

There are several possibilities by means of the PyQGIS

  1. Using the .boundingBoxOfSelected() method
layer = QgsProject.instance().mapLayersByName('test')[0]
layer.selectAll()
extent = layer.boundingBoxOfSelected()
print(extent)
layer.removeSelection()
  1. Using the .extent() method
layer = QgsProject.instance().mapLayersByName('test')[0]
extent = layer.extent()
print(extent)

In both cases, the result will end up in a form of:

<QgsRectangle: 435177.19075885589700192 5645765.28368342109024525, 435464.76195503829512745 5645985.34679672587662935>

To get a more visually pleasing result, please extend the code with an approach mentioned in this answer

xmin = extent.xMinimum()
xmax = extent.xMaximum()
ymin = extent.yMinimum()
ymax = extent.yMaximum()

print('XMin:{0}, XMax:{1}, YMin:{2}, YMax:{3}'.format(xmin, xmax, ymin, ymax))

this will lead in

XMin:435177.1907588559, XMax:435464.7619550383, YMin:5645765.283683421, YMax:5645985.346796726
Taras
  • 32,823
  • 4
  • 66
  • 137
2

It is also possible to get the extent within the Field Calculator by means of the layer_property() function and its extent parameter, and a bunch of: x_min, x_max, y_min, and y_max.

x_min(layer_property('test', 'extent')) || ' ' || x_max(layer_property('test', 'extent')) || ' ' || y_min(layer_property('test', 'extent')) || ' ' || y_max(layer_property('test', 'extent'))

This expression will lead to this result:

435177.1907588559 435464.7619550383 5645765.283683421 5645985.346796726
Taras
  • 32,823
  • 4
  • 66
  • 137
0

Also similar to @underdark's answer, you may see under the Information | Information from provider

extent

The extent is provided in the following order: xmin,ymin : xmax,ymax

Taras
  • 32,823
  • 4
  • 66
  • 137