Skip to content
This repository has been archived by the owner on Jun 11, 2019. It is now read-only.

Create Max_distance_from_centroid #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions scripts/Max_distance_from_centroid
Original file line number Diff line number Diff line change
@@ -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()