Skip to content

Commit

Permalink
[sextante] more improvements in automated tests tools and some cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
volaya committed Mar 24, 2013
1 parent 5e2ee4a commit 15df144
Show file tree
Hide file tree
Showing 22 changed files with 338 additions and 145 deletions.
92 changes: 33 additions & 59 deletions python/plugins/sextante/algs/ftools/MeanCoords.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
6 changes: 3 additions & 3 deletions python/plugins/sextante/algs/ftools/SumLines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions python/plugins/sextante/algs/mmqgisx/MMQGISXAlgorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
53 changes: 0 additions & 53 deletions python/plugins/sextante/gdalwarp.patch

This file was deleted.

12 changes: 8 additions & 4 deletions python/plugins/sextante/gui/TestTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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_()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/sextante/modeler/ModelerAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 0 additions & 12 deletions python/plugins/sextante/r_local_library.patch

This file was deleted.

Loading

0 comments on commit 15df144

Please sign in to comment.