Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refactor fTools algorithms
  • Loading branch information
volaya committed Jan 1, 2013
1 parent a385162 commit 287c9f3
Show file tree
Hide file tree
Showing 41 changed files with 400 additions and 944 deletions.
19 changes: 9 additions & 10 deletions python/plugins/sextante/algs/FieldsCalculator.py
Expand Up @@ -31,7 +31,6 @@
from PyQt4.QtGui import *
from sextante.parameters.ParameterString import ParameterString
from sextante.core.QGisLayers import QGisLayers
from PyQt4 import QtGui
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException


Expand All @@ -40,7 +39,7 @@ class FieldsCalculator(GeoAlgorithm):
INPUT_LAYER = "INPUT_LAYER"
FIELD_NAME = "FIELD_NAME"
FORMULA = "FORMULA"
OUTPUT_LAYER ="OUTPUT_LAYER"
OUTPUT_LAYER = "OUTPUT_LAYER"

#===========================================================================
# def getIcon(self):
Expand All @@ -62,32 +61,32 @@ def processAlgorithm(self, progress):
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
vprovider = vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
vprovider.select(allAttrs)
fields = vprovider.fields()
fields[len(fields)] = QgsField(fieldname, QVariant.Double)
writer = output.getVectorWriter(fields, vprovider.geometryType(), vprovider.crs() )
writer = output.getVectorWriter(fields, vprovider.geometryType(), vprovider.crs())
outFeat = QgsFeature()
inGeom = QgsGeometry()
nFeat = vprovider.featureCount()
nElement = 0
features = QGisLayers.features(vlayer)
for inFeat in features:
progress.setPercentage(int((100 * nElement)/nFeat))
progress.setPercentage(int((100 * nElement) / nFeat))
attrs = inFeat.attributeMap()
expression = formula
for (k,attr) in attrs.iteritems():
for (k, attr) in attrs.iteritems():
expression = expression.replace(str(fields[k].name()), str(attr.toString()))
try:
result = eval(expression)
except Exception:
raise GeoAlgorithmExecutionException("Problem evaluation formula: Wrong field values or formula")
nElement += 1
inGeom = inFeat.geometry()
outFeat.setGeometry( inGeom )
outFeat.setGeometry(inGeom)
atMap = inFeat.attributeMap()
outFeat.setAttributeMap( atMap )
outFeat.addAttribute( len(vprovider.fields()), QVariant(result) )
writer.addFeature( outFeat )
outFeat.setAttributeMap(atMap)
outFeat.addAttribute(len(vprovider.fields()), QVariant(result))
writer.addFeature(outFeat)
del writer


Expand Down
23 changes: 12 additions & 11 deletions python/plugins/sextante/algs/JoinAttributes.py
Expand Up @@ -62,20 +62,20 @@ def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(input)
provider = layer.dataProvider()
allAttrs = provider.attributeIndexes()
provider.select( allAttrs )
provider.select(allAttrs)
join_field1_index = provider.fieldNameIndex(field)
# Layer 2
layer2 = QGisLayers.getObjectFromUri(input2)
provider2 = layer2.dataProvider()
allAttrs = provider2.attributeIndexes()
provider2.select( allAttrs )
provider2.select(allAttrs)
fields2 = provider2.fields()
join_field2_index = provider2.fieldNameIndex(field2)

# Output
outFields = provider.fields()
for (i,f) in fields2.iteritems():
f.setName("x_"+f.name())
for (i, f) in fields2.iteritems():
f.setName("x_" + f.name())
outFields[len(outFields)] = f

writer = output.getVectorWriter(outFields, provider.geometryType(), provider.crs())
Expand All @@ -88,18 +88,19 @@ def processAlgorithm(self, progress):
while provider.nextFeature(inFeat):
inGeom = inFeat.geometry()
atMap = inFeat.attributeMap()
join_value1 = atMap[join_field1_index].toString()
join_value1 = atMap[join_field1_index].toString()
provider2.rewind()
while provider2.nextFeature(inFeat2):
atMap2 = inFeat2.attributeMap()
join_value2 = atMap2[join_field2_index].toString()
if join_value1 == join_value2:
# create the new feature
outFeat.setGeometry( inGeom )
outFeat.setAttributeMap( atMap )
outFeat.setGeometry(inGeom)
outFeat.setAttributeMap(atMap)
l = len(provider.fields())
for (i,a) in atMap2.iteritems():
outFeat.addAttribute( l+i, a )
for (i, a) in atMap2.iteritems():
outFeat.addAttribute(l + i, a)

writer.addFeature( outFeat )
writer.addFeature(outFeat)

del writer
del writer
74 changes: 21 additions & 53 deletions python/plugins/sextante/algs/ftools/BasicStatisticsNumbers.py
Expand Up @@ -47,7 +47,6 @@ class BasicStatisticsNumbers(GeoAlgorithm):

INPUT_LAYER = "INPUT_LAYER"
FIELD_NAME = "FIELD_NAME"
USE_SELECTION = "USE_SELECTION"
OUTPUT_HTML_FILE = "OUTPUT_HTML_FILE"

CV = "CV"
Expand All @@ -72,7 +71,6 @@ def defineCharacteristics(self):

self.addParameter(ParameterVector(self.INPUT_LAYER, "Input vector layer", ParameterVector.VECTOR_TYPE_ANY, False))
self.addParameter(ParameterTableField(self.FIELD_NAME, "Field to calculate statistics on", self.INPUT_LAYER, ParameterTableField.DATA_TYPE_NUMBER))
self.addParameter(ParameterBoolean(self.USE_SELECTION, "Use selection", False))

self.addOutput(OutputHTML(self.OUTPUT_HTML_FILE, "Statistics for numeric field"))

Expand All @@ -90,79 +88,49 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
fieldName = self.getParameterValue(self.FIELD_NAME)
useSelection = self.getParameterValue(self.USE_SELECTION)

outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE)

index = layer.fieldNameIndex(fieldName)
layer.select([index], QgsRectangle(), False)

count = 0
rValue = 0

cvValue = 0
minValue = 0
maxValue = 0
sumValue = 0
meanValue = 0
medianValue = 0
stdDevValue = 0
uniqueValue = 0

isFirst = True
values = []

if useSelection:
selection = layer.selectedFeatures()
count = layer.selectedFeatureCount()
total = 100.0 / float(count)
current = 0

for f in selection:
value = float(f.attributeMap()[index].toDouble()[0])

if isFirst:
minValue = value
maxValue = value
isFirst = False
else:
if value < minValue:
minValue = value
if value > maxValue:
maxValue = value

values.append(value)
sumValue += value

current += 1
progress.setPercentage(int(current * total))
else:
count = layer.featureCount()
total = 100.0 / float(count)
current = 0

ft = QgsFeature()
while layer.nextFeature(ft):
value = float(ft.attributeMap()[index].toDouble()[0])

if isFirst:

features = QGisLayers.features(layer)
count = len(features)
total = 100.0 / float(count)
current = 0
for ft in features:
value = float(ft.attributeMap()[index].toDouble()[0])

if isFirst:
minValue = value
maxValue = value
isFirst = False
else:
if value < minValue:
minValue = value
if value > maxValue:
maxValue = value
isFirst = False
else:
if value < minValue:
minValue = value
if value > maxValue:
maxValue = value

values.append( value )
sumValue += value
values.append( value )
sumValue += value

current += 1
progress.setPercentage(int(current * total))
current += 1
progress.setPercentage(int(current * total))

# calculate additional values
rValue = maxValue - minValue
uniqueValue = utils.getUniqueValuesCount(layer, index, useSelection)
uniqueValue = utils.getUniqueValuesCount(layer, index)

if count > 0:
meanValue = sumValue / count
Expand Down
83 changes: 24 additions & 59 deletions python/plugins/sextante/algs/ftools/BasicStatisticsStrings.py
Expand Up @@ -45,7 +45,6 @@ class BasicStatisticsStrings(GeoAlgorithm):

INPUT_LAYER = "INPUT_LAYER"
FIELD_NAME = "FIELD_NAME"
USE_SELECTION = "USE_SELECTION"
OUTPUT_HTML_FILE = "OUTPUT_HTML_FILE"

MIN_LEN = "MIN_LEN"
Expand All @@ -67,7 +66,6 @@ def defineCharacteristics(self):

self.addParameter(ParameterVector(self.INPUT_LAYER, "Input vector layer", ParameterVector.VECTOR_TYPE_ANY, False))
self.addParameter(ParameterTableField(self.FIELD_NAME, "Field to calculate statistics on", self.INPUT_LAYER, ParameterTableField.DATA_TYPE_STRING))
self.addParameter(ParameterBoolean(self.USE_SELECTION, "Use selection", False))

self.addOutput(OutputHTML(self.OUTPUT_HTML_FILE, "Statistics for text field"))

Expand All @@ -82,14 +80,12 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
fieldName = self.getParameterValue(self.FIELD_NAME)
useSelection = self.getParameterValue(self.USE_SELECTION)

outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE)

index = layer.fieldNameIndex(fieldName)
layer.select([index], QgsRectangle(), False)

count = 0
sumValue = 0
minValue = 0
maxValue = 0
Expand All @@ -100,70 +96,39 @@ def processAlgorithm(self, progress):
isFirst = True
values = []

if useSelection:
selection = layer.selectedFeatures()
count = layer.selectedFeatureCount()
total = 100.0 / float(count)
current = 0

for f in selection:
length = float(len(f.attributeMap()[index].toString()))

if isFirst:
minValue = length
maxValue = length
isFirst = False
else:
if length < minValue:
minValue = length
if length > maxValue:
maxValue = length

if length != 0.00:
countFilled += 1
else:
countEmpty += 1

values.append(length)
sumValue += length

current += 1
progress.setPercentage(int(current * total))
else:
count = layer.featureCount()
total = 100.0 / float(count)
current = 0

ft = QgsFeature()
while layer.nextFeature(ft):
length = float(len(ft.attributeMap()[index].toString()))

if isFirst:
features = QGisLayers.features(layer)
count = len(features)
total = 100.0 / float(count)
current = 0
for ft in features:
length = float(len(ft.attributeMap()[index].toString()))

if isFirst:
minValue = length
maxValue = length
isFirst = False
else:
if length < minValue:
minValue = length
if length > maxValue:
maxValue = length
isFirst = False
else:
if length < minValue:
minValue = length
if length > maxValue:
maxValue = length

if length != 0.00:
countFilled += 1
else:
countEmpty += 1
if length != 0.00:
countFilled += 1
else:
countEmpty += 1

values.append(length)
sumValue += length
values.append(length)
sumValue += length

current += 1
progress.setPercentage(int(current * total))
current += 1
progress.setPercentage(int(current * total))

n = float(len(values))
if n > 0:
meanValue = sumValue / n
meanValue = sumValue / n

uniqueValues = utils.getUniqueValuesCount(layer, index, useSelection)
uniqueValues = utils.getUniqueValuesCount(layer, index)

data = []
data.append("Minimum length: " + unicode(minValue))
Expand Down

0 comments on commit 287c9f3

Please sign in to comment.