Is there a way to calculate the XMIN, YMIN, XMAX and YMAX of a polygon and write it to a field in an attribute table?
The Geometry functions available in the Field Calculator are for points.
I am using 1.8.0 for Windows.
Is there a way to calculate the XMIN, YMIN, XMAX and YMAX of a polygon and write it to a field in an attribute table?
The Geometry functions available in the Field Calculator are for points.
I am using 1.8.0 for Windows.
The Field Calculator in QGIS now contains functions that will work fine for this.
You can now use:
x_min($geometry)
, x_max($geometry)
, y_min($geometry)
, or y_max($geometry)
Given a polygon shapefilename, the following python script should do the job:
layer = QgsVectorLayer(shapefilename,"","ogr")
iter = layer.getFeatures()
layer.startEditing()
for ft in iter:
geom=ft.geometry()
ft.setAttribute('XMAX',geom.boundingBox().xMaximum())
ft.setAttribute('XMIN',geom.boundingBox().xMinimum())
ft.setAttribute('YMAX',geom.boundingBox().yMaximum())
ft.setAttribute('YMIN',geom.boundingBox().yMinimum())
layer.updateFeature(ft)
layer.commitChanges()