Skip to content
Permalink
Browse files

review Mean coordinates tool

  • Loading branch information
alexbruy committed Oct 4, 2012
1 parent 97a432e commit b7cd5394156194f552a7d65bb820308d861d00f2
Showing with 94 additions and 42 deletions.
  1. +27 −0 python/plugins/sextante/ftools/FToolsUtils.py
  2. +67 −42 python/plugins/sextante/ftools/MeanCoords.py
@@ -47,3 +47,30 @@ def findOrCreateField(layer, fieldList, fieldName, fieldLen = 24, fieldPrec = 15
field = QgsField(fn, QVariant.Double, "", fieldLen, fieldPrec)
fieldList[idx] = field
return idx, fieldList

def extractPoints( geom ):
points = []
if geom.type() == QGis.Point:
if geom.isMultipart():
points = geom.asMultiPoint()
else:
points.append(geom.asPoint())
elif geom.type() == QGis.Line:
if geom.isMultipart():
lines = geom.asMultiPolyline()
for line in lines:
points.extend(line)
else:
points = geom.asPolyline()
elif geom.type() == QGis.Polygon:
if geom.isMultipart():
polygons = geom.asMultiPolygon()
for poly in polygons:
for line in poly:
points.extend(line)
else:
polygon = geom.asPolygon()
for line in polygon:
points.extend(line)

return points
@@ -1,15 +1,21 @@
from sextante.core.GeoAlgorithm import GeoAlgorithm
import os.path

from PyQt4 import QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *
from sextante.parameters.ParameterVector import ParameterVector

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.QGisLayers import QGisLayers
from sextante.ftools import ftools_utils

from sextante.parameters.ParameterTableField import ParameterTableField
from sextante.parameters.ParameterVector import ParameterVector

from sextante.outputs.OutputVector import OutputVector

from sextante.ftools import FToolsUtils as utils


class MeanCoords(GeoAlgorithm):

POINTS = "POINTS"
@@ -21,44 +27,67 @@ class MeanCoords(GeoAlgorithm):
def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/icons/mean.png")

def defineCharacteristics(self):
self.name = "Mean coordinate(s)"
self.group = "Analysis tools"

self.addParameter(ParameterVector(self.POINTS, "Input layer", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterTableField(self.WEIGHT, "Weight field", MeanCoords.POINTS, ParameterTableField.DATA_TYPE_NUMBER))
self.addParameter(ParameterTableField(self.UID, "Unique ID field", MeanCoords.POINTS, ParameterTableField.DATA_TYPE_NUMBER))

self.addOutput(OutputVector(MeanCoords.OUTPUT, "Result"))

def processAlgorithm(self, progress):
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(MeanCoords.POINTS))
weightField = self.getParameterValue(MeanCoords.WEIGHT)
uniqueField = self.getParameterValue(MeanCoords.UID)
provider = vlayer.dataProvider()
weightIndex = provider.fieldNameIndex(weightField)
uniqueIndex = provider.fieldNameIndex(uniqueField)
feat = QgsFeature()
allAttrs = provider.attributeIndexes()
provider.select(allAttrs)
sRs = provider.crs()
settings = QSettings()
encoding = settings.value( "/UI/encoding", "System" ).toString()

layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.POINTS))
weightField = self.getParameterValue(self.WEIGHT)
uniqueField = self.getParameterValue(self.UID)

output = self.getOutputValue(self.OUTPUT)

provider = layer.dataProvider()
weightIndex = layer.fieldNameIndex(weightField)
uniqueIndex = layer.fieldNameIndex(uniqueField)

if uniqueIndex <> -1:
uniqueValues = ftools_utils.getUniqueValues(provider, int( uniqueIndex ) )
uniqueValues = layer.uniqueValues(uniqueIndex)
single = False
else:
uniqueValues = [QVariant(1)]
single = True
fieldList = { 0 : QgsField("MEAN_X", QVariant.Double), 1 : QgsField("MEAN_Y", QVariant.Double), 2 : QgsField("UID", QVariant.String) }
writer = self.getOutputFromName(MeanCoords.OUTPUT).getVectorWriter(fieldList, QGis.WKBPoint, sRs)
outfeat = QgsFeature()
points = []
weights = []
nFeat = provider.featureCount() * len(uniqueValues)
nElement = 0

fieldList = {0 : QgsField("MEAN_X", QVariant.Double, "", 24, 15),
1 : QgsField("MEAN_Y", QVariant.Double, "", 24, 15),
2 : QgsField("UID", QVariant.String, "", 255)
}

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList,
QGis.WKBPoint, provider.crs())

current = 0
total = 100.0 / float(provider.featureCount() * len(uniqueValues))

feat = QgsFeature()
outFeat = QgsFeature()

for j in uniqueValues:
provider.rewind()
provider.select(allAttrs)
layer.select([weightIndex, uniqueIndex])
cx = 0.00
cy = 0.00
points = []
weights = []
while provider.nextFeature(feat):
nElement += 1
progress.setPercentage(nElement/nFeat * 100)
while layer.nextFeature(feat):
current += 1
progress.setPercentage(current * total)

if single:
check = j.toString().trimmed()
else:
check = feat.attributeMap()[uniqueIndex].toString().trimmed()

if check == j.toString().trimmed():
cx = 0.00
cy = 0.00
@@ -69,38 +98,34 @@ def processAlgorithm(self, progress):
weight = float(feat.attributeMap()[weightIndex].toDouble()[0])
except:
weight = 1.00

geom = QgsGeometry(feat.geometry())
geom = ftools_utils.extractPoints(geom)
geom = utils.extractPoints(geom)
for i in geom:
cx += i.x()
cy += i.y()
points.append(QgsPoint((cx / len(geom)), (cy / len(geom))))
weights.append(weight)

sumWeight = sum(weights)
cx = 0.00
cy = 0.00
item = 0
for item, i in enumerate(points):
cx += i.x() * weights[item]
cy += i.y() * weights[item]

cx = cx / sumWeight
cy = cy / sumWeight
meanPoint = QgsPoint(cx, cy)
outfeat.setGeometry(QgsGeometry.fromPoint(meanPoint))
outfeat.addAttribute(0, QVariant(cx))
outfeat.addAttribute(1, QVariant(cy))
outfeat.addAttribute(2, QVariant(j))
writer.addFeature(outfeat)
if single:
break
del writer

outFeat.setGeometry(QgsGeometry.fromPoint(meanPoint))
outFeat.addAttribute(0, QVariant(cx))
outFeat.addAttribute(1, QVariant(cy))
outFeat.addAttribute(2, QVariant(j))
writer.addFeature(outFeat)

if single:
break

def defineCharacteristics(self):
self.name = "Mean coordinate(s)"
self.group = "Analysis tools"
self.addParameter(ParameterVector(MeanCoords.POINTS, "Input layer", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterTableField(MeanCoords.WEIGHT, "Weight field", MeanCoords.POINTS))
self.addParameter(ParameterTableField(MeanCoords.UID, "Unique ID field", MeanCoords.POINTS))
self.addOutput(OutputVector(MeanCoords.OUTPUT, "Result"))
del writer

0 comments on commit b7cd539

Please sign in to comment.
You can’t perform that action at this time.