I need to identify in QGIS the area of a Qgsvectorlayer .
Anyone have an idea how to do that in python ?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.Sign up to join this community
A Polygon vectorlayer is composed of many features (geometries), each with an area.
If the geometries don't overlap, you can iterate over the vector layer and sum the areas:
layer = iface.activeLayer() areatot = 0 for elem in layer.getFeatures() geom = elem.geometry() areatot += geom.area()
or in one line:
aeratot = [sum(elem.geometry().area() for elem in layer.getFeatures())]
But if some geometries overlap, this result is wrong:
You can then Union all the geometries and use the area of the resulting geometry:
# creation of a empty geometry for unioning geomtot = QgsGeometry.fromWkt('GEOMETRYCOLLECTION EMPTY') # union for elem in layer.getFeatures(): geomtot = geomtot.combine(elem.geometry()) area = geomtot.area()
layer = qgis.utils.iface.activeLayer() areatot = 0 layer.select() for elem in layer: geom = elem.geometry() areatot += geom.area()
geomtot = QgsGeometry.fromWkt('GEOMETRYCOLLECTION EMPTY') layer.select() for elem in layer: geomtot = geomtot.combine(elem.geometry()) area = geomtot.area()
In Qgis, go to plugins > Python console
Declare your layer
vlayer = QgsVectorLayer("/path/to/shapefile/file.shp", "layer_name_you_like", "ogr")
and to get extent, do
vlayer.extent() return a qgis.core.QgsRectangle, so if you want, you can use different methods depending of you use case (see the doc for this component at http://qgis.org/api/2.0/classQgsRectangle.html)
Is it what you wanted?