From fc189a2112b25491f0ccfa556e717e2da72e55dd Mon Sep 17 00:00:00 2001 From: Caio Hamamura Date: Wed, 13 Jul 2016 03:14:46 -0300 Subject: [PATCH] Create Max_distance_from_centroid Creates a field within polygon vector layer with the calculation of maximum distance from polygon border to centroid. --- scripts/Max_distance_from_centroid | 42 ++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 scripts/Max_distance_from_centroid diff --git a/scripts/Max_distance_from_centroid b/scripts/Max_distance_from_centroid new file mode 100644 index 0000000..dbaca45 --- /dev/null +++ b/scripts/Max_distance_from_centroid @@ -0,0 +1,42 @@ +##Polygons=vector +##Field_name=string maxDistCen + +from PyQt4.QtCore import QVariant +from qgis.core import * +from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException + +input_features = processing.getObject(Polygons) +provider = input_features.dataProvider() +fieldName = Field_name + +if len(fieldName) > 10: raise GeoAlgorithmExecutionException('Field name length should be 10 or less') + +field = QgsField(fieldName, QVariant.Double) +if provider.fieldNameIndex(fieldName)==-1: + provider.addAttributes([field]) + +attrIndex = provider.fieldNameIndex(fieldName) + +n = input_features.featureCount()+0.0 +features = input_features.getFeatures() +input_features.startEditing() +pos = 0 + + +for feat in features: + geom = feat.geometry() + centroid = geom.centroid().asPoint() + points = [] + i = 0 + point = geom.vertexAt(i) + while not point==QgsPoint(): + points.append(point) + i += 1 + point = geom.vertexAt(i) + if len(points) > 0: + maxDist = max([centroid.sqrDist(x) for x in points]) + out = input_features.changeAttributeValue(feat.id(), attrIndex, maxDist) + pos += 1 + progress.setPercentage(int(100*pos/n)) + +input_features.commitChanges()