Skip to content
Permalink
Browse files
refactor fTools algorithms
  • Loading branch information
volaya committed Jan 1, 2013
1 parent a385162 commit 287c9f367de11765a8ca358858d2c6f5cfcb1b69
Showing with 400 additions and 944 deletions.
  1. +9 −10 python/plugins/sextante/algs/FieldsCalculator.py
  2. +12 −11 python/plugins/sextante/algs/JoinAttributes.py
  3. +21 −53 python/plugins/sextante/algs/ftools/BasicStatisticsNumbers.py
  4. +24 −59 python/plugins/sextante/algs/ftools/BasicStatisticsStrings.py
  5. +57 −121 python/plugins/sextante/algs/ftools/Buffer.py
  6. +5 −7 python/plugins/sextante/algs/ftools/Centroids.py
  7. +0 −1 python/plugins/sextante/algs/ftools/Clip.py
  8. +62 −116 python/plugins/sextante/algs/ftools/ConvexHull.py
  9. +10 −18 python/plugins/sextante/algs/ftools/Delaunay.py
  10. +17 −38 python/plugins/sextante/algs/ftools/DensifyGeometries.py
  11. +17 −38 python/plugins/sextante/algs/ftools/DensifyGeometriesInterval.py
  12. +10 −78 python/plugins/sextante/algs/ftools/Difference.py
  13. +6 −9 python/plugins/sextante/algs/ftools/ExportGeometryInfo.py
  14. +41 −92 python/plugins/sextante/algs/ftools/ExtentFromLayer.py
  15. +3 −11 python/plugins/sextante/algs/ftools/ExtractNodes.py
  16. +12 −16 python/plugins/sextante/algs/ftools/FToolsUtils.py
  17. +1 −7 python/plugins/sextante/algs/ftools/FixedDistanceBuffer.py
  18. +9 −80 python/plugins/sextante/algs/ftools/Intersection.py
  19. +1 −8 python/plugins/sextante/algs/ftools/LinesIntersection.py
  20. +4 −17 python/plugins/sextante/algs/ftools/LinesToPolygons.py
  21. +0 −5 python/plugins/sextante/algs/ftools/MeanCoords.py
  22. +4 −9 python/plugins/sextante/algs/ftools/MultipartToSingleparts.py
  23. +6 −15 python/plugins/sextante/algs/ftools/NearestNeighbourAnalysis.py
  24. +0 −4 python/plugins/sextante/algs/ftools/PointDistance.py
  25. +4 −11 python/plugins/sextante/algs/ftools/PointsInPolygon.py
  26. +4 −6 python/plugins/sextante/algs/ftools/PointsInPolygonUnique.py
  27. +4 −4 python/plugins/sextante/algs/ftools/PointsInPolygonWeighted.py
  28. +5 −11 python/plugins/sextante/algs/ftools/PolygonsToLines.py
  29. +4 −5 python/plugins/sextante/algs/ftools/RandomSelection.py
  30. +0 −3 python/plugins/sextante/algs/ftools/RandomSelectionWithinSubsets.py
  31. +5 −14 python/plugins/sextante/algs/ftools/ReprojectLayer.py
  32. +1 −1 python/plugins/sextante/algs/ftools/SelectByLocation.py
  33. +5 −6 python/plugins/sextante/algs/ftools/SinglePartsToMultiparts.py
  34. +5 −7 python/plugins/sextante/algs/ftools/SumLines.py
  35. +1 −3 python/plugins/sextante/algs/ftools/Union.py
  36. +0 −5 python/plugins/sextante/algs/ftools/UniqueValues.py
  37. +1 −8 python/plugins/sextante/algs/ftools/VariableDistanceBuffer.py
  38. +2 −3 python/plugins/sextante/algs/ftools/VoronoiPolygons.py
  39. +26 −33 python/plugins/sextante/algs/ftools/ftools_utils.py
  40. +1 −1 python/plugins/sextante/algs/ftools/voronoi.py
  41. +1 −0 python/plugins/sextante/core/QGisLayers.py
@@ -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


@@ -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):
@@ -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


@@ -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())
@@ -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
@@ -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"
@@ -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"))

@@ -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
@@ -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"
@@ -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"))

@@ -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
@@ -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))

0 comments on commit 287c9f3

Please sign in to comment.