Calculate total area of polygons in shapefile using GDAL?
There's a special field in OGR SQL called OGR_GEOM_AREA which returns the area of the feature's geometry:
ogrinfo -sql "SELECT SUM(OGR_GEOM_AREA) AS TOTAL_AREA FROM myshapefile" myshapefile.shp
where TOTAL_AREA
unit of measure depends by the layer SRS (read the comments below).
Yes, it is possible, but you need to use the OGR SQLite dialect as follows:
ogrinfo -dialect SQLite -sql 'SELECT SUM(ST_Area(geometry))/10000 FROM myshapefile' myshapefile.shp
Also, ensure that myshapefile
is the layer name in myshapefile.shp
. You can do this as follows:
ogrinfo myshapefile.shp
INFO: Open of `myshapefile.shp`
using driver `ESRI Shapefile' successful.
1: myshapefile (Polygon)
Using QGIS, you may run this simple code for printing the total area of the shapefile (I assume you are evaluating the area in a projected reference system):
from qgis.core import *
filepath = 'C:/Users/path_to_the_shapefile/shapefile.shp'
layer = QgsVectorLayer(filepath, 'layer' , 'ogr')
area = 0
for feat in layer.getFeatures():
area += feat.geometry().area()
print area # total area in square meters
print area/10000 # total area in hectares