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.
The layer extents are available in the Layer Properties | Metadata section | Extent.
To get a bounding box in QGIS as a new vector layer use the "Extract layer extent":
Vector -> Research Tools -> Extract layer extent
From the (right-click) context menu of the desired layer, choose 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.
So for a standard bbox array of [minx,miny,maxx,maxy] your values would be [west,south,east,north].
There are several possibilities by means of the PyQGIS
.boundingBoxOfSelected()
methodlayer = QgsProject.instance().mapLayersByName('test')[0]
layer.selectAll()
extent = layer.boundingBoxOfSelected()
print(extent)
layer.removeSelection()
.extent()
methodlayer = 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
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
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
Also similar to @underdark's answer, you may see under the Information | Information from provider
The extent is provided in the following order: xmin,ymin : xmax,ymax