Skip to content

Commit

Permalink
housekeeping: remove obsolete code, completely switch to new vector API
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Mar 21, 2013
1 parent 6df3540 commit 9afe76d
Show file tree
Hide file tree
Showing 33 changed files with 235 additions and 259 deletions.
Expand Up @@ -24,12 +24,17 @@
__revision__ = '$Format:%H$'

import math

from PyQt4.QtCore import *

from qgis.core import *

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.QGisLayers import QGisLayers

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

from sextante.outputs.OutputHTML import OutputHTML
from sextante.outputs.OutputNumber import OutputNumber

Expand Down Expand Up @@ -86,7 +91,6 @@ def processAlgorithm(self, progress):
outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE)

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

cvValue = 0
minValue = 0
Expand Down
8 changes: 5 additions & 3 deletions python/plugins/sextante/algs/ftools/BasicStatisticsStrings.py
Expand Up @@ -23,8 +23,8 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import codecs

from PyQt4 import QtGui
from PyQt4.QtCore import *

from qgis.core import *
Expand Down Expand Up @@ -84,7 +84,6 @@ def processAlgorithm(self, progress):
outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE)

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

sumValue = 0
minValue = 0
Expand Down Expand Up @@ -150,7 +149,10 @@ def processAlgorithm(self, progress):
self.setOutputValue(self.UNIQUE, uniqueValues)

def createHTML(self, outputFile, algData):
f = open(outputFile, "w")
f = codecs.open(outputFile, "w", encoding="utf-8")
f.write('<html><head>')
f.write('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>')
for s in algData:
f.write("<p>" + str(s) + "</p>")
f.write("</body></html>")
f.close()
13 changes: 6 additions & 7 deletions python/plugins/sextante/algs/ftools/Buffer.py
Expand Up @@ -34,8 +34,6 @@ def buffering(progress, writer, distance, field, useField, layer, dissolve, segm
GEOS_EXCEPT = True
FEATURE_EXCEPT = True

layer.select(layer.pendingAllAttributesList())

if useField:
field = layer.fieldNameIndex(field)

Expand All @@ -52,9 +50,9 @@ def buffering(progress, writer, distance, field, useField, layer, dissolve, segm
if dissolve:
first = True
for inFeat in features:
atMap = inFeat.attributes()
attrs = inFeat.attributes()
if useField:
value = atMap[field].toDouble()[0]
value = attrs[field].toDouble()[0]
else:
value = distance

Expand All @@ -78,15 +76,16 @@ def buffering(progress, writer, distance, field, useField, layer, dissolve, segm
progress.setPercentage(int(current * total))
try:
outFeat.setGeometry(tempGeom)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
except:
FEATURE_EXCEPT = False
# without dissolve
else:
for inFeat in features:
atMap = inFeat.attributes()
attrs = inFeat.attributes()
if useField:
value = atMap[field].toDouble()[0]
value = attrs[field].toDouble()[0]
else:
value = distance

Expand All @@ -95,7 +94,7 @@ def buffering(progress, writer, distance, field, useField, layer, dissolve, segm
outGeom = inGeom.buffer(float(value), segments)
try:
outFeat.setGeometry(outGeom)
outFeat.setAttributes(atMap)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
except:
FEATURE_EXCEPT = False
Expand Down
4 changes: 1 addition & 3 deletions python/plugins/sextante/algs/ftools/Centroids.py
Expand Up @@ -55,11 +55,9 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))

writer = self.getOutputFromName(self.OUTPUT_LAYER).getVectorWriter(layer.pendingFields(),
writer = self.getOutputFromName(self.OUTPUT_LAYER).getVectorWriter(layer.pendingFields().toList(),
QGis.WKBPoint, layer.crs())

#layer.select(layer.pendingAllAttributesList())

outFeat = QgsFeature()

features = QGisLayers.features(layer)
Expand Down
10 changes: 1 addition & 9 deletions python/plugins/sextante/algs/ftools/ConvexHull.py
Expand Up @@ -113,7 +113,7 @@ def processAlgorithm(self, progress):
first = True
features = QGisLayers.features(layer)
for f in features:
idVar = f.attribute(fieldName)
idVar = f[fieldName]
if idVar.toString().trimmed() == i.toString().trimmed():
if first:
val = idVar
Expand All @@ -135,10 +135,6 @@ def processAlgorithm(self, progress):
QVariant(area),
QVariant(perim)
])
#~ outFeat.setAttribute("id", QVariant(fid))
#~ outFeat.setAttribute("value", QVariant(val))
#~ outFeat.setAttribute("area", QVariant(area))
#~ outFeat.setAttribute("perim", QVariant(perim))
writer.addFeature(outFeat)
except:
GEOS_EXCEPT = False
Expand All @@ -165,10 +161,6 @@ def processAlgorithm(self, progress):
QVariant(area),
QVariant(perim)
])
#print outFeat.setAttribute("id", QVariant(0))
#print outFeat.setAttribute("value", QVariant("all"))
#print outFeat.setAttribute("area", QVariant(area))
#print outFeat.setAttribute("perim", QVariant(perim))
writer.addFeature(outFeat)
except:
GEOS_EXCEPT = False
Expand Down
24 changes: 15 additions & 9 deletions python/plugins/sextante/algs/ftools/Delaunay.py
Expand Up @@ -23,17 +23,22 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from sets import Set

from PyQt4.QtCore import *

from qgis.core import *
from sets import Set
from sextante.algs.ftools import voronoi

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.GeoAlgorithmExecutionException import \
GeoAlgorithmExecutionException
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from sextante.core.QGisLayers import QGisLayers
from sextante.outputs.OutputVector import OutputVector

from sextante.parameters.ParameterVector import ParameterVector

from sextante.outputs.OutputVector import OutputVector

from sextante.algs.ftools import voronoi


class Delaunay(GeoAlgorithm):

Expand All @@ -57,9 +62,9 @@ def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))


fields = [ QgsField("POINTA", QVariant.Double, "", 24, 15),
QgsField("POINTB", QVariant.Double, "", 24, 15),
QgsField("POINTC", QVariant.Double, "", 24, 15)
fields = [QgsField("POINTA", QVariant.Double, "", 24, 15),
QgsField("POINTB", QVariant.Double, "", 24, 15),
QgsField("POINTC", QVariant.Double, "", 24, 15)
]

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields,
Expand Down Expand Up @@ -100,7 +105,8 @@ def processAlgorithm(self, progress):
attrs = []
step = 0
for index in indicies:
layer.featureAtId(ptDict[ids[index]], inFeat, True)
request = QgsFeatureRequest().setFilterFid(ptDict[ids[index]])
inFeat = layer.getFeatures(request).next()
geom = QgsGeometry(inFeat.geometry())
point = QgsPoint(geom.asPoint())
polygon.append(point)
Expand Down
10 changes: 3 additions & 7 deletions python/plugins/sextante/algs/ftools/DensifyGeometries.py
Expand Up @@ -23,7 +23,6 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'


from PyQt4.QtCore import *

from qgis.core import *
Expand All @@ -42,9 +41,6 @@ class DensifyGeometries(GeoAlgorithm):
VERTICES = "VERTICES"
OUTPUT = "OUTPUT"

#def getIcon(self):
# return QtGui.QIcon(os.path.dirname(__file__) + "/icons/de.png")

def defineCharacteristics(self):
self.name = "Densify geometries"
self.group = "Vector geometry tools"
Expand All @@ -60,19 +56,19 @@ def processAlgorithm(self, progress):

isPolygon = layer.geometryType() == QGis.Polygon

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields(),
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields().toList(),
layer.wkbType(), layer.crs())

features = QGisLayers.features(layer)
total = 100.0 / float(len(features))
current = 0
for f in features:
featGeometry = QgsGeometry(f.geometry())
attrMap = f.attributes()
attrs = f.attributes()
newGeometry = self.densifyGeometry(featGeometry, int(vertices), isPolygon)
feature = QgsFeature()
feature.setGeometry(newGeometry)
feature.setAttributes(attrMap)
feature.setAttributes(attrs)
writer.addFeature(feature)
current += 1
progress.setPercentage(int(current * total))
Expand Down
10 changes: 4 additions & 6 deletions python/plugins/sextante/algs/ftools/DensifyGeometriesInterval.py
Expand Up @@ -3,8 +3,7 @@
"""
***************************************************************************
DensifyGeometriesInterval.py by Anita Graser, Dec 2012
based on
DensifyGeometries.py
based on DensifyGeometries.py
---------------------
Date : October 2012
Copyright : (C) 2012 by Victor Olaya
Expand Down Expand Up @@ -58,20 +57,19 @@ def processAlgorithm(self, progress):

isPolygon = layer.geometryType() == QGis.Polygon

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields(),
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields().toList(),
layer.wkbType(), layer.crs())


features = QGisLayers.features(layer)
total = 100.0 / float(len(features))
current = 0
for f in features:
featGeometry = QgsGeometry(f.geometry())
attrMap = f.attributes()
attrs = f.attributes()
newGeometry = self.densifyGeometry(featGeometry, interval, isPolygon)
feature = QgsFeature()
feature.setGeometry(newGeometry)
feature.setAttributes(attrMap)
feature.setAttributes(attrs)
writer.addFeature(feature)

current += 1
Expand Down
39 changes: 22 additions & 17 deletions python/plugins/sextante/algs/ftools/ExportGeometryInfo.py
Expand Up @@ -66,24 +66,26 @@ def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
method = self.getParameterValue(self.METHOD)

provider = layer.dataProvider()
geometryType = layer.geometryType()

layer.select(layer.pendingAllAttributesList())

fields = provider.fields()
idx1 = -1
idx2 = -1
fields = layer.pendingFields()

if geometryType == QGis.Polygon:
fields.append(QgsField(QString("area"), QVariant.Double))
fields.append(QgsField(QString("perimeter"), QVariant.Double))
idx1, fields = utils.findOrCreateField(layer, fields, "area", 21, 6)
idx2, fields = utils.findOrCreateField(layer, fields, "perimeter", 21, 6)
elif geometryType == QGis.Line:
fields.append(QgsField(QString("length"), QVariant.Double))
idx1, fields = utils.findOrCreateField(layer, fields, "length", 21, 6)
idx2 = idx1
else:
fields.append(QgsField(QString("xcoords"), QVariant.Double))
fields.append(QgsField(QString("ycoords"), QVariant.Double))
idx1, fields = utils.findOrCreateField(layer, fields, "xcoord", 21, 6)
idx2, fields = utils.findOrCreateField(layer, fields, "ycoord", 21, 6)

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields.toList(),
layer.dataProvider().geometryType(), layer.crs())

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields,
provider.geometryType(), layer.crs())
print idx1, idx2

ellips = None
crs = None
Expand All @@ -105,23 +107,26 @@ def processAlgorithm(self, progress):
outFeat = QgsFeature()
inGeom = QgsGeometry()

outFeat.initAttributes(len(fields))
outFeat.setFields(fields)

current = 0
features = QGisLayers.features(layer)
total = 100.0 / float(len(features))
for inFeat in features:
inGeom = inFeat.geometry()
for f in features:
inGeom = f.geometry()

if method == 1:
inGeom.transform(coordTransform)

(attr1, attr2) = utils.simpleMeasure(inGeom, method, ellips, crs)

outFeat.setGeometry(inGeom)
atMap = inFeat.attributes()
atMap.append(QVariant(attr1))
attrs = f.attributes()
attrs.insert(idx1, QVariant(attr1))
if attr2 is not None:
atMap.append(QVariant(attr2))
outFeat.setAttributes(atMap)
attrs.insert(idx2, QVariant(attr2))
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)

current += 1
Expand Down

4 comments on commit 9afe76d

@nirvn
Copy link
Contributor

@nirvn nirvn commented on 9afe76d Mar 22, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexbruy , this possibly fixes http://hub.qgis.org/issues/7360 ; let me have a look at confirming it.

@nirvn
Copy link
Contributor

@nirvn nirvn commented on 9afe76d Mar 22, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexbruy issue still there; might not be entirely related to new vector API then. with a set of 655 points, QGIS chokes and dies around 50% of the process. With a >13,000 points set, it dies at 0%.

@alexbruy
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit only fixes issues introduced by further API cleanup. Don't know why you decide that this related to #7360

@nirvn
Copy link
Contributor

@nirvn nirvn commented on 9afe76d Mar 22, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexbruy , my apologies then, I thought this commit was migrating some ftools functions to new vector API and would fix ftools' delaunay triangulation function, which is currently broken on QGIS master.

Please sign in to comment.