Skip to content
Permalink
Browse files

[sextante] more improvements in automated tests tools and some cleaning

  • Loading branch information
volaya committed Mar 24, 2013
1 parent 5e2ee4a commit 15df14430e123a08da71efd01b1286ecc72754cf
@@ -61,14 +61,7 @@ def processAlgorithm(self, progress):
uniqueField = self.getParameterValue(self.UID)

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

if uniqueIndex <> -1:
uniqueValues = utils.getUniqueValues(layer, uniqueIndex)
single = False
else:
uniqueValues = [QVariant(1)]
single = True
uniqueIndex = layer.fieldNameIndex(uniqueField)

fieldList = [QgsField("MEAN_X", QVariant.Double, "", 24, 15),
QgsField("MEAN_Y", QVariant.Double, "", 24, 15),
@@ -80,60 +73,41 @@ def processAlgorithm(self, progress):

current = 0
features = QGisLayers.features(layer)
total = 100.0 / float(len(features) * len(uniqueValues))

outFeat = QgsFeature()

for j in uniqueValues:
cx = 0.00
cy = 0.00
points = []
weights = []
for feat in features:
current += 1
progress.setPercentage(current * total)

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

if check == j.toString().trimmed():
cx = 0.00
cy = 0.00
if weightIndex == -1:
weight = 1.00
else:
try:
weight = float(feat.attributes()[weightIndex].toDouble()[0])
except:
weight = 1.00

geom = QgsGeometry(feat.geometry())
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
total = 100.0 / float(len(features))

means = {}
for feat in features:
current += 1
progress.setPercentage(current * total)
clazz = feat.attributes()[uniqueIndex].toString().trimmed()
if weightIndex == -1:
weight = 1.00
else:
try:
weight = float(feat.attributes()[weightIndex].toDouble()[0])
except:
weight = 1.00
if clazz not in means:
means[clazz] = (0,0,0)

cx,cy, totalweight = means[clazz]
geom = QgsGeometry(feat.geometry())
geom = utils.extractPoints(geom)
for i in geom:
cx += i.x() * weight
cy += i.y() * weight
totalweight += weight
means[clazz] = (cx, cy, totalweight)

for clazz, values in means.iteritems():
outFeat = QgsFeature()
cx = values[0] / values[2]
cy = values[1] / values[2]
meanPoint = QgsPoint(cx, cy)

outFeat.setGeometry(QgsGeometry.fromPoint(meanPoint))
outFeat.setAttributes([QVariant(cx), QVariant(cy), QVariant(j)])
outFeat.setAttributes([QVariant(cx), QVariant(cy), clazz])
writer.addFeature(outFeat)

if single:
break

del writer
@@ -70,8 +70,7 @@ def processAlgorithm(self, progress):
lengthFieldName = self.getParameterValue(self.LEN_FIELD)
countFieldName = self.getParameterValue(self.COUNT_FIELD)

polyProvider = polyLayer.dataProvider()
lineProvider = lineLayer.dataProvider()
polyProvider = polyLayer.dataProvider()

idxLength, fieldList = utils.findOrCreateField(polyLayer, polyLayer.pendingFields(), lengthFieldName)
idxCount, fieldList = utils.findOrCreateField(polyLayer, fieldList, countFieldName)
@@ -104,7 +103,8 @@ def processAlgorithm(self, progress):

if hasIntersections:
for i in lines:
lineLayer.featureAtId(int(i), ftLine)
request = QgsFeatureRequest().setFilterFid(i)
ftLine = lineLayer.getFeatures(request).next()
tmpGeom = QgsGeometry(ftLine.geometry())
if inGeom.intersects(tmpGeom):
outGeom = inGeom.intersection(tmpGeom)
@@ -732,6 +732,12 @@ def mmqgisx_gridify_points(hspacing, vspacing, points):
return newpoints, point_count, deleted_points


class mmqgisx_hub:
def __init__(self, point, newname):
self.point = point
self.name = newname


class mmqgisx_hub_distance_algorithm(GeoAlgorithm):

SOURCENAME = "SOURCENAME"

This file was deleted.

@@ -44,8 +44,8 @@ def createTest(text):
for token in tokens:
if i < alg.getVisibleParametersCount() + 1:
if os.path.exists(token[1:-1]):
token = os.path.basename(token[1:-1])[:-4]
execcommand += token + "(),"
token = os.path.basename(token[1:-1])[:-4] + "()"
execcommand += token + ","
else:
execcommand += "None,"
i+=1
@@ -61,9 +61,11 @@ def createTest(text):
s+="self.assertTrue(" + str(out) + ", output)\n"
if isinstance(out, OutputRaster):
dataset = gdal.Open(filename, GA_ReadOnly)
array = dataset.ReadAsArray(1)
strhash = hash(str(dataset.ReadAsArray(0).tolist()))
s+="\tself.assertTrue(os.path.isfile(output))\n"
s+="\tself.assertEqual(hashraster(output)," + str(hash(array)) + ")\n"
s+="\tdataset=gdal.Open(output, GA_ReadOnly)\n"
s+="\tstrhash=hash(str(dataset.ReadAsArray(0).tolist()))\n"
s+="\tself.assertEqual(strhash," + str(strhash) + ")\n"
if isinstance(out, OutputVector):
layer = Sextante.getObject(filename)
fields = layer.pendingFields()
@@ -87,6 +89,8 @@ def createTest(text):
s+="\texpectedvalues=[" + ",".join(['"' + str(attr.toString()) + '"' for attr in attrs]) + "]\n"
s+="\tvalues=[str(attr.toString()) for attr in attrs]\n"
s+="\tself.assertEqual(expectedvalues, values)\n"
s+="\twkt='" + str(feature.geometry().exportToWkt()) + "'\n"
s+="\tself.assertEqual(wkt, str(feature.geometry().exportToWkt()))"

dlg = ShowTestDialog(s)
dlg.exec_()
@@ -614,7 +614,7 @@ def getAsCommand(self):
return None

def commandLineName(self):
return "modeler:" + os.path.basename(self.descriptionFile)[:-5].lower()
return "modeler:" + os.path.basename(self.descriptionFile)[:-6].lower()

def setModelerView(self, dialog):
self.modelerdialog = dialog
@@ -1,5 +1,5 @@
NAME:Contours from points
GROUP:Tools
GROUP:[Example models]
PARAMETER:ParameterVector|VECTORLAYER_POINTS|Points|0|False
120.0,60.0
PARAMETER:ParameterNumber|NUMBER_INTERVAL|Interval|0.0|None|100.0
@@ -1,5 +1,5 @@
NAME:Habitat_selection_model
GROUP:Habitat models Sextante
GROUP:[Example models]
PARAMETER:ParameterVector|VECTORLAYER_LOCATIONS|Locations|0|False
120.0,60.0
PARAMETER:ParameterVector|VECTORLAYER_LINEFEATURES|Line features|1|False

This file was deleted.

0 comments on commit 15df144

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