51 changes: 27 additions & 24 deletions python/plugins/sextante/algs/ftools/ExtentFromLayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
# 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 *

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

from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterBoolean import ParameterBoolean

from sextante.outputs.OutputVector import OutputVector

class ExtentFromLayer(GeoAlgorithm):
Expand Down Expand Up @@ -102,27 +105,26 @@ def layerExtent(self, layer, writer, progress):
feat = QgsFeature()
feat.setGeometry(geometry)
attrs = [QVariant(minx),
QVariant(miny),
QVariant(maxx),
QVariant(maxy),
QVariant(cntx),
QVariant(cnty),
QVariant(area),
QVariant(perim),
QVariant(height),
QVariant(width) ]
QVariant(miny),
QVariant(maxx),
QVariant(maxy),
QVariant(cntx),
QVariant(cnty),
QVariant(area),
QVariant(perim),
QVariant(height),
QVariant(width)
]
feat.setAttributes(attrs)
writer.addFeature(feat)

def featureExtent(self, layer, writer, progress):
current = 0
feat = QgsFeature()

provider = layer.dataProvider()
features = QGisLayers.features(layer)
total = 100.0 / float(len(features))
for inFeat in features:
rect = inFeat.geometry().boundingBox()
feat = QgsFeature()
for f in features:
rect = f.geometry().boundingBox()
minx = rect.xMinimum()
miny = rect.yMinimum()
maxx = rect.xMaximum()
Expand All @@ -143,15 +145,16 @@ def featureExtent(self, layer, writer, progress):
geometry = QgsGeometry().fromPolygon([rect])
feat.setGeometry(geometry)
attrs = [QVariant(minx),
QVariant(miny),
QVariant(maxx),
QVariant(maxy),
QVariant(cntx),
QVariant(cnty),
QVariant(area),
QVariant(perim),
QVariant(height),
QVariant(width) ]
QVariant(miny),
QVariant(maxx),
QVariant(maxy),
QVariant(cntx),
QVariant(cnty),
QVariant(area),
QVariant(perim),
QVariant(height),
QVariant(width)
]
feat.setAttributes(attrs)

writer.addFeature(feat)
Expand Down
15 changes: 7 additions & 8 deletions python/plugins/sextante/algs/ftools/ExtractNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
# 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 *

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.QGisLayers import QGisLayers
from sextante.parameters.ParameterVector import ParameterVector
from sextante.outputs.OutputVector import OutputVector

from sextante.algs.ftools import FToolsUtils as utils

class ExtractNodes(GeoAlgorithm):
Expand All @@ -53,9 +54,7 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))

provider = layer.dataProvider()

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

outFeat = QgsFeature()
Expand All @@ -65,12 +64,12 @@ def processAlgorithm(self, progress):
current = 0
features = QGisLayers.features(layer)
total = 100.0 / float(len(features))
for inFeat in features:
inGeom = inFeat.geometry()
atMap = inFeat.attributes()
for f in features:
inGeom = f.geometry()
attrs = f.attributes()

points = utils.extractPoints(inGeom)
outFeat.setAttributes(atMap)
outFeat.setAttributes(attrs)

for i in points:
outFeat.setGeometry(outGeom.fromPoint(i))
Expand Down
27 changes: 14 additions & 13 deletions python/plugins/sextante/algs/ftools/FToolsUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def createUniqueFieldName(fieldName, fieldList):
if len(fieldList) == 0:
return shortName

if shortName not in fieldList:
fieldNames = [f.name() for f in fieldList]

if shortName not in fieldNames:
return shortName

shortName = fieldName[:8] + "_1"
Expand All @@ -63,13 +65,13 @@ def findOrCreateField(layer, fieldList, fieldName, fieldLen=24, fieldPrec=15):
idx = layer.fieldNameIndex(fieldName)
if idx == -1:
fn = createUniqueFieldName(fieldName, fieldList)
field = QgsField(fn, QVariant.Double, "", fieldLen, fieldPrec)
field = QgsField(fn, QVariant.Double, "", fieldLen, fieldPrec)
idx = len(fieldList)
fieldList.append(field)

return idx, fieldList

def extractPoints( geom ):
def extractPoints(geom):
points = []
if geom.type() == QGis.Point:
if geom.isMultipart():
Expand Down Expand Up @@ -121,13 +123,12 @@ def simpleMeasure(geom, method=0, ellips=None, crs=None):
if geom.type() == QGis.Polygon:
attr2 = measure.measurePerimeter(geom)
else:
attr2 = attr1
attr2 = None

return (attr1, attr2)

def getUniqueValues(layer, fieldIndex):
values = []
layer.select([fieldIndex], QgsRectangle(), False)
features = QGisLayers.features(layer)
for feat in features:
if feat.attributes()[fieldIndex] not in values:
Expand All @@ -138,7 +139,7 @@ def getUniqueValuesCount(layer, fieldIndex):
return len(getUniqueValues(layer, fieldIndex))

# From two input field maps, create single field map
def combineVectorFields( layerA, layerB ):
def combineVectorFields(layerA, layerB):
fields = []
fieldsA = layerA.dataProvider().fields()
fields.extend(fieldsA)
Expand All @@ -158,19 +159,19 @@ def combineVectorFields( layerA, layerB ):
return fields

# Create a unique field name based on input field name
def createUniqueFieldNameFromName( field ):
check = field.name().right( 2 )
shortName = field.name().left( 8 )
def createUniqueFieldNameFromName(field):
check = field.name().right(2)
shortName = field.name().left(8)
if check.startsWith("_"):
( val, test ) = check.right( 1 ).toInt()
(val, test) = check.right(1).toInt()
if test:
if val < 2:
val = 2
else:
val = val + 1
field.setName( shortName.left( len( shortName )-1 ) + unicode( val ) )
field.setName(shortName.left(len(shortName) - 1) + unicode(val))
else:
field.setName( shortName + "_2" )
field.setName(shortName + "_2")
else:
field.setName( shortName + "_2" )
field.setName(shortName + "_2")
return field
3 changes: 1 addition & 2 deletions python/plugins/sextante/algs/ftools/FixedDistanceBuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ def processAlgorithm(self, progress):
dissolve = self.getParameterValue(self.DISSOLVE)
segments = int(self.getParameterValue(self.SEGMENTS))

provider = layer.dataProvider()
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields(),
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields().toList(),
QGis.WKBPolygon, layer.crs())

buff.buffering(progress, writer, distance, None, False,
Expand Down
22 changes: 10 additions & 12 deletions python/plugins/sextante/algs/ftools/LinesIntersection.py
Original file line number Diff line number Diff line change
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 *
from sextante.core.GeoAlgorithm import GeoAlgorithm
Expand Down Expand Up @@ -64,9 +63,6 @@ def processAlgorithm(self, progress):
fieldA = self.getParameterValue(self.FIELD_A)
fieldB = self.getParameterValue(self.FIELD_B)

providerA = layerA.dataProvider()
providerB = layerB.dataProvider()

idxA = layerA.fieldNameIndex(fieldA)
idxB = layerB.fieldNameIndex(fieldB)

Expand All @@ -75,7 +71,7 @@ def processAlgorithm(self, progress):
]

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList,
QGis.WKBPoint, providerA.crs())
QGis.WKBPoint, layerA.dataProvider().crs())

spatialIndex = utils.createSpatialIndex(layerB)

Expand All @@ -85,11 +81,13 @@ def processAlgorithm(self, progress):
inGeom = QgsGeometry()
tmpGeom = QgsGeometry()

features = QGisLayers.features(layerA)

current = 0
total = 100.0 / float(providerA.featureCount())
total = 100.0 / float(len(features))
hasIntersections = False

while layerA.nextFeature(inFeatA):
for inFeatA in features:
inGeom = inFeatA.geometry()
hasIntersections = False
lines = spatialIndex.intersects(inGeom.boundingBox())
Expand All @@ -98,14 +96,14 @@ def processAlgorithm(self, progress):
hasIntersections = True

if hasIntersections:
layerB.select([idxB])
for i in lines:
layerB.featureAtId(int(i), inFeatB)
request = QgsFeatureRequest().setFilterFid(i)
inFeatB = layerB.getFeatures(request).next()
tmpGeom = QgsGeometry(inFeatB.geometry())

points = []
atMapA = inFeatA.attributes()
atMapB = inFeatB.attributes()
attrsA = inFeatA.attributes()
attrsB = inFeatB.attributes()

if inGeom.intersects(tmpGeom):
tempGeom = inGeom.intersection(tmpGeom)
Expand All @@ -117,7 +115,7 @@ def processAlgorithm(self, progress):

for j in points:
outFeat.setGeometry(tempGeom.fromPoint(j))
outFeat.setAttributes([atMapA[idxA], atMapB[idxB]])
outFeat.setAttributes([attrsA[idxA], attrsB[idxB]])
writer.addFeature(outFeat)

current += 1
Expand Down
18 changes: 9 additions & 9 deletions python/plugins/sextante/algs/ftools/LinesToPolygons.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@

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.outputs.OutputVector import OutputVector

class LinesToPolygons(GeoAlgorithm):

INPUT = "INPUT"
Expand All @@ -49,28 +51,26 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))

provider = layer.dataProvider()

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

outFeat = QgsFeature()

current = 0
features = QGisLayers.features(layer)
total = 100.0 / float(len(features))
for inFeat in features:
for f in features:
outGeomList = []
if inFeat.geometry().isMultipart():
outGeomList = inFeat.geometry().asMultiPolyline()
if f.geometry().isMultipart():
outGeomList = f.geometry().asMultiPolyline()
else:
outGeomList.append(inFeat.geometry().asPolyline())
outGeomList.append(f.geometry().asPolyline())

polyGeom = self.removeBadLines(outGeomList)
if len(polyGeom) <> 0:
outFeat.setGeometry(QgsGeometry.fromPolygon(polyGeom))
atMap = inFeat.attributes()
outFeat.setAttributes(atMap)
attrs = f.attributes()
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)

current += 1
Expand Down
6 changes: 2 additions & 4 deletions python/plugins/sextante/algs/ftools/MeanCoords.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from sextante.outputs.OutputVector import OutputVector
from sextante.algs.ftools import FToolsUtils as utils


class MeanCoords(GeoAlgorithm):

POINTS = "POINTS"
Expand Down Expand Up @@ -61,7 +60,6 @@ def processAlgorithm(self, progress):
weightField = self.getParameterValue(self.WEIGHT)
uniqueField = self.getParameterValue(self.UID)

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

Expand All @@ -81,7 +79,8 @@ def processAlgorithm(self, progress):
QGis.WKBPoint, layer.crs())

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

outFeat = QgsFeature()

Expand All @@ -90,7 +89,6 @@ def processAlgorithm(self, progress):
cy = 0.00
points = []
weights = []
features = QGisLayers.features(layer)
for feat in features:
current += 1
progress.setPercentage(current * total)
Expand Down
27 changes: 12 additions & 15 deletions python/plugins/sextante/algs/ftools/MultipartToSingleparts.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))

provider = layer.dataProvider()
geomType = self.multiToSingleGeom(layer.dataProvider().geometryType())

geomType = self.multiToSingleGeom(provider.geometryType())

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

outFeat = QgsFeature()
Expand All @@ -68,33 +66,32 @@ def processAlgorithm(self, progress):
current = 0
features = QGisLayers.features(layer)
total = 100.0 / float(len(features))
for inFeat in features:
inGeom = inFeat.geometry()
atMap = inFeat.attributes()
for f in features:
inGeom = f.geometry()
attrs = f.attributes()

features = self.extractAsSingle(inGeom)
outFeat.setAttributes(atMap)
geometries = self.extractAsSingle(inGeom)
outFeat.setAttributes(attrs)

for f in features:
outFeat.setGeometry(f)
for g in geometries:
outFeat.setGeometry(g)
writer.addFeature(outFeat)

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

del writer


def multiToSingleGeom(self, wkbType):
try:
if wkbType in (QGis.WKBPoint, QGis.WKBMultiPoint,
QGis.WKBPoint25D, QGis.WKBMultiPoint25D):
QGis.WKBPoint25D, QGis.WKBMultiPoint25D):
return QGis.WKBPoint
elif wkbType in (QGis.WKBLineString, QGis.WKBMultiLineString,
QGis.WKBMultiLineString25D, QGis.WKBLineString25D):
QGis.WKBMultiLineString25D, QGis.WKBLineString25D):
return QGis.WKBLineString
elif wkbType in (QGis.WKBPolygon, QGis.WKBMultiPolygon,
QGis.WKBMultiPolygon25D, QGis.WKBPolygon25D):
QGis.WKBMultiPolygon25D, QGis.WKBPolygon25D):
return QGis.WKBPolygon
else:
return QGis.WKBUnknown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def defineCharacteristics(self):
self.addOutput(OutputNumber(self.POINT_COUNT, "Number of points"))
self.addOutput(OutputNumber(self.Z_SCORE, "Z-Score"))


def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.POINTS))
output = self.getOutputValue(self.OUTPUT)
Expand All @@ -83,7 +82,8 @@ def processAlgorithm(self, progress):
total = 100.0 / float(len(features))
for feat in features:
neighbourID = spatialIndex.nearestNeighbor(feat.geometry().asPoint(), 2)[1]
layer.featureAtId(neighbourID, neighbour, True)
request = QgsFeatureRequest().setFilterFid(neighbourID)
neighbour = layer.getFeatures(request).next()
sumDist += distance.measureLine(neighbour.geometry().asPoint(), feat.geometry().asPoint())

current += 1
Expand Down
24 changes: 15 additions & 9 deletions python/plugins/sextante/algs/ftools/PointDistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@
import math
import codecs
import cStringIO

from qgis.core import *

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.QGisLayers import QGisLayers
from sextante.algs.ftools import FToolsUtils as utils

from sextante.parameters.ParameterNumber import ParameterNumber
from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterSelection import ParameterSelection
from sextante.parameters.ParameterTableField import ParameterTableField

from sextante.outputs.OutputFile import OutputFile

from sextante.algs.ftools import FToolsUtils as utils

class PointDistance(GeoAlgorithm):

INPUT_LAYER = "INPUT_LAYER"
Expand Down Expand Up @@ -82,7 +86,7 @@ def processAlgorithm(self, progress):
outputFile = self.getOutputValue(self.DISTANCE_MATRIX)

if nPoints < 1:
nPoints = targetLayer.featureCount()
nPoints = len(QGisLayers.features(targetLayer))

# prepare CSV file writer
csvFile = open(outputFile, "wb")
Expand Down Expand Up @@ -125,7 +129,8 @@ def linearMatrix(self, inLayer, inField, targetLayer, targetField, matType, nPoi
distList = []
vari = 0.0
for i in featList:
targetLayer.featureAtId(i, outFeat)
request = QgsFeatureRequest().setFilterFid(i)
outFeat = targetLayer.getFeatures(request).next()
outID = outFeat.attributes()[outIdx].toString()
outGeom = outFeat.geometry()
dist = distArea.measureLine(inGeom.asPoint(), outGeom.asPoint())
Expand All @@ -145,7 +150,6 @@ def linearMatrix(self, inLayer, inField, targetLayer, targetField, matType, nPoi
progress.setPercentage(int(current * total))

def regularMatrix(self, inLayer, inField, targetLayer, targetField, nPoints, progress):

index = utils.createSpatialIndex(targetLayer)

inIdx = inLayer.fieldNameIndex(inField)
Expand All @@ -158,9 +162,9 @@ def regularMatrix(self, inLayer, inField, targetLayer, targetField, nPoints, pro

first = True
current = 0
total = 100.0 / float(inLayer.featureCount())

features = QGisLayers.features(inLayer)
total = 100.0 / float(features)

for inFeat in features:
inGeom = inFeat.geometry()
inID = inFeat.attributes()[inIdx].toString()
Expand All @@ -169,13 +173,15 @@ def regularMatrix(self, inLayer, inField, targetLayer, targetField, nPoints, pro
first = False
data = ["ID"]
for i in featList:
targetLayer.featureAtId(i, outFeat)
data.append(unicode(outFeat.attributeMap()[outIdx].toString()))
request = QgsFeatureRequest().setFilterFid(i)
outFeat = targetLayer.getFeatures(request).next()
data.append(unicode(outFeat.attributes[outIdx].toString()))
self.writer.writerow(data)

data = [unicode(inID)]
for i in featList:
targetLayer.featureAtId(i, outFeat)
request = QgsFeatureRequest().setFilterFid(i)
outFeat = targetLayer.getFeatures(request).next()
outGeom = outFeat.geometry()
dist = distArea.measureLine(inGeom.asPoint(), outGeom.asPoint())
data.append(unicode(float(dist)))
Expand Down
23 changes: 11 additions & 12 deletions python/plugins/sextante/algs/ftools/PointsInPolygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.QGisLayers import QGisLayers
from sextante.core.SextanteLog import SextanteLog

from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterString import ParameterString

from sextante.outputs.OutputVector import OutputVector
from sextante.algs.ftools import FToolsUtils as utils

from sextante.algs.ftools import FToolsUtils as utils

class PointsInPolygon(GeoAlgorithm):

Expand Down Expand Up @@ -63,14 +65,10 @@ def processAlgorithm(self, progress):
fieldName = self.getParameterValue(self.FIELD)

polyProvider = polyLayer.dataProvider()
pointProvider = pointLayer.dataProvider()
if polyProvider.crs() != pointProvider.crs():
SextanteLog.addToLog(SextanteLog.LOG_WARNING,
"CRS warning: Input layers have non-matching CRS. This may cause unexpected results.")

idxCount, fieldList = utils.findOrCreateField(polyLayer, polyLayer.pendingFields(), fieldName)

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList,
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList.toList(),
polyProvider.geometryType(), polyProvider.crs())

spatialIndex = utils.createSpatialIndex(pointLayer)
Expand All @@ -87,7 +85,7 @@ def processAlgorithm(self, progress):
total = 100.0 / float(len(features))
for ftPoly in features:
geom = ftPoly.geometry()
atMap = ftPoly.attributes()
attrs = ftPoly.attributes()

count = 0
hasIntersections = False
Expand All @@ -97,17 +95,18 @@ def processAlgorithm(self, progress):

if hasIntersections:
for i in points:
pointLayer.featureAtId(int(i), ftPoint, True, False)
request = QgsFeatureRequest().setFilterFid(i)
ftPoint = pointLayer.getFeatures(request).next()
tmpGeom = QgsGeometry(ftPoint.geometry())
if geom.contains(tmpGeom):
count += 1

outFeat.setGeometry(geom)
if idxCount == len(atMap):
atMap.append(QVariant(count))
if idxCount == len(attrs):
attrs.append(QVariant(count))
else:
atMap[idxCount] = QVariant(count)
outFeat.setAttributes(atMap)
attrs[idxCount] = QVariant(count)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)

current += 1
Expand Down
25 changes: 9 additions & 16 deletions python/plugins/sextante/algs/ftools/PointsInPolygonUnique.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,13 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os.path

from PyQt4 import QtGui
from PyQt4.QtCore import *

from qgis.core import *

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

from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterString import ParameterString
Expand All @@ -42,7 +39,6 @@

from sextante.algs.ftools import FToolsUtils as utils


class PointsInPolygonUnique(GeoAlgorithm):

POLYGONS = "POLYGONS"
Expand Down Expand Up @@ -72,15 +68,11 @@ def processAlgorithm(self, progress):
classFieldName = self.getParameterValue(self.CLASSFIELD)

polyProvider = polyLayer.dataProvider()
pointProvider = pointLayer.dataProvider()
if polyProvider.crs() != pointProvider.crs():
SextanteLog.addToLog(SextanteLog.LOG_WARNING,
"CRS warning: Input layers have non-matching CRS. This may cause unexpected results.")

classFieldIndex = pointProvider.fieldNameIndex(classFieldName)
classFieldIndex = pointLayer.fieldNameIndex(classFieldName)
idxCount, fieldList = utils.findOrCreateField(polyLayer, polyLayer.pendingFields(), fieldName)

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList,
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList.toList(),
polyProvider.geometryType(), polyProvider.crs())

spatialIndex = utils.createSpatialIndex(pointLayer)
Expand All @@ -96,7 +88,7 @@ def processAlgorithm(self, progress):
total = 100.0 / float(len(features))
for ftPoly in features:
geom = ftPoly.geometry()
atMap = ftPoly.attributes()
attrs = ftPoly.attributes()

classes = []
hasIntersections = False
Expand All @@ -106,19 +98,20 @@ def processAlgorithm(self, progress):

if hasIntersections:
for i in points:
pointLayer.featureAtId(int(i), ftPoint, True, True)
request = QgsFeatureRequest().setFilterFid(i)
ftPoint = pointLayer.getFeatures(request).next()
tmpGeom = QgsGeometry(ftPoint.geometry())
if geom.contains(tmpGeom):
clazz = ftPoint.attributes()[classFieldIndex].toString()
if not clazz in classes:
classes.append(clazz)

outFeat.setGeometry(geom)
if idxCount == len(atMap):
atMap.append(QVariant(len(classes)))
if idxCount == len(attrs):
attrs.append(QVariant(len(classes)))
else:
atMap[idxCount] = QVariant(len(classes))
outFeat.setAttributes(atMap)
attrs[idxCount] = QVariant(len(classes))
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)

current += 1
Expand Down
28 changes: 10 additions & 18 deletions python/plugins/sextante/algs/ftools/PointsInPolygonWeighted.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,13 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os.path

from PyQt4 import QtGui
from PyQt4.QtCore import *

from qgis.core import *

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

from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterString import ParameterString
Expand All @@ -42,7 +39,6 @@

from sextante.algs.ftools import FToolsUtils as utils


class PointsInPolygonWeighted(GeoAlgorithm):

POLYGONS = "POLYGONS"
Expand Down Expand Up @@ -71,17 +67,13 @@ def processAlgorithm(self, progress):
polyLayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.POLYGONS))
pointLayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.POINTS))
fieldName = self.getParameterValue(self.FIELD)
fieldidx = pointLayer.dataProvider().fieldNameIndex(self.getParameterValue(self.WEIGHT))
fieldIdx = pointLayer.fieldNameIndex(self.getParameterValue(self.WEIGHT))

polyProvider = polyLayer.dataProvider()
pointProvider = pointLayer.dataProvider()
if polyProvider.crs() != pointProvider.crs():
SextanteLog.addToLog(SextanteLog.LOG_WARNING,
"CRS warning: Input layers have non-matching CRS. This may cause unexpected results.")

idxCount, fieldList = utils.findOrCreateField(polyLayer, polyLayer.pendingFields(), fieldName)

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList,
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList.toList(),
polyProvider.geometryType(), polyProvider.crs())

spatialIndex = utils.createSpatialIndex(pointLayer)
Expand All @@ -97,7 +89,7 @@ def processAlgorithm(self, progress):
total = 100.0 / float(len(features))
for ftPoly in features:
geom = ftPoly.geometry()
atMap = ftPoly.attributes()
attrs = ftPoly.attributes()

count = 0
hasIntersections = False
Expand All @@ -108,20 +100,20 @@ def processAlgorithm(self, progress):
if hasIntersections:
progress.setText(str(len(points)))
for i in points:
pointLayer.featureAtId(int(i), ftPoint, True, True)
request = QgsFeatureRequest().setFilterFid(i)
ftPoint = pointLayer.getFeatures(request).next()
tmpGeom = QgsGeometry(ftPoint.geometry())
if geom.contains(tmpGeom):
#try:
weight, ok = ftPoint.attributes()[fieldidx].toDouble()
weight, ok = ftPoint.attributes()[fieldIdx].toDouble()
if ok:
count += weight

outFeat.setGeometry(geom)
if idxCount == len(atMap):
atMap.append(QVariant(count))
if idxCount == len(attrs):
attrs.append(QVariant(count))
else:
atMap[idxCount] = QVariant(count)
outFeat.setAttributes(atMap)
attrs[idxCount] = QVariant(count)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)

current += 1
Expand Down
11 changes: 5 additions & 6 deletions python/plugins/sextante/algs/ftools/PolygonsToLines.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,21 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))

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

inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
outGeom = QgsGeometry()

current = 0
features = QGisLayers.features(layer)
total = 100.0 / float(len(features))
for inFeat in features:
inGeom = inFeat.geometry()
atMap = inFeat.attributes()
for f in features:
inGeom = f.geometry()
attrs = f.attributes()
lineList = self.extractAsLine(inGeom)
outFeat.setAttributes(atMap)
outFeat.setAttributes(attrs)
for h in lineList:
outFeat.setGeometry(outGeom.fromPolyline(h))
writer.addFeature(outFeat)
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/sextante/algs/ftools/RandomSelection.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ def processAlgorithm(self, progress):
method = self.getParameterValue(self.METHOD)

featureCount = layer.featureCount()

value = int(self.getParameterValue(self.NUMBER))

layer.removeSelection(True)

if method == 0:
if value > featureCount:
raise GeoAlgorithmExecutionException("Selected number is greater than feature count. Choose a lower value and try again.")
Expand All @@ -85,4 +86,3 @@ def processAlgorithm(self, progress):

layer.setSelectedFeatures(selran)
self.setOutputValue(self.OUTPUT, filename)

Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,14 @@ def processAlgorithm(self, progress):
current = 0
total = 100.0 / float(featureCount * len(unique))

features = QGisLayers.features(layer)

if not len(unique) == featureCount:
for i in unique:
FIDs= []
layer.select([index])
while layer.nextFeature(inFeat):
atMap = inFeat.attributes()
if atMap[index] == QVariant(i):
for inFeat in features:
attrs = inFeat.attributes()
if attrs[index] == QVariant(i):
FIDs.append(inFeat.id())
current += 1
progress.setPercentage(int(current * total))
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/sextante/algs/ftools/ReprojectLayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def processAlgorithm(self, progress):
crsId = self.getParameterValue(self.TARGET_CRS)
targetCrs = QgsCoordinateReferenceSystem(crsId)

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

layerCrs = layer.crs()
Expand Down
22 changes: 10 additions & 12 deletions python/plugins/sextante/algs/ftools/SelectByLocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
from sextante.parameters.ParameterSelection import ParameterSelection
from sextante.parameters.ParameterVector import ParameterVector


from sextante.outputs.OutputVector import OutputVector

from sextante.algs.ftools import FToolsUtils as utils

class SelectByLocation(GeoAlgorithm):

INPUT = "INPUT"
Expand Down Expand Up @@ -63,28 +64,25 @@ def processAlgorithm(self, progress):
method = self.getParameterValue(self.METHOD)
filename = self.getParameterValue(self.INTERSECT)
selectLayer = QGisLayers.getObjectFromUri(filename)
inputProvider = inputLayer.dataProvider()

oldSelection = set(inputLayer.selectedFeaturesIds())
index = QgsSpatialIndex()
feat = QgsFeature()
for feat in inputLayer.getFeatures():
index.insertFeature(feat)
index = spatialIndex = utils.createSpatialIndex(inputLayer)

infeat = QgsFeature()
feat = QgsFeature()
geom = QgsGeometry()
selectedSet = []
current = 0
features = QGisLayers.features(selectLayer)
total = 100.0 / float(len(features))
for feat in features:
geom = QgsGeometry(feat.geometry())
for f in features:
geom = QgsGeometry(f.geometry())
intersects = index.intersects(geom.boundingBox())
for i in intersects:
inputLayer.featureAtId(i, infeat, True)
tmpGeom = QgsGeometry( infeat.geometry() )
request = QgsFeatureRequest().setFilterFid(i)
feat = inputLayer.getFeatures(request).next()
tmpGeom = QgsGeometry(feat.geometry())
if geom.intersects(tmpGeom):
selectedSet.append(infeat.id())
selectedSet.append(feat.id())
current += 1
progress.setPercentage(int(current * total))

Expand Down
9 changes: 3 additions & 6 deletions python/plugins/sextante/algs/ftools/SimplifyGeometries.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class SimplifyGeometries(GeoAlgorithm):

INPUT = "INPUT"
TOLERANCE = "TOLERANCE"
USE_SELECTION = "USE_SELECTION"
OUTPUT = "OUTPUT"

#===========================================================================
Expand All @@ -62,23 +61,21 @@ def processAlgorithm(self, progress):
pointsBefore = 0
pointsAfter = 0

provider = layer.dataProvider()

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

current = 0
selection = QGisLayers.features(layer)
total = 100.0 / float(len(selection))
for f in selection:
featGeometry = QgsGeometry(f.geometry())
attrMap = f.attributes()
attrs = f.attributes()
pointsBefore += self.geomVertexCount(featGeometry)
newGeometry = featGeometry.simplify(tolerance)
pointsAfter += self.geomVertexCount(newGeometry)
feature = QgsFeature()
feature.setGeometry(newGeometry)
feature.setAttributes(attrMap)
feature.setAttributes(attrs)
writer.addFeature(feature)
current += 1
progress.setPercentage(int(current * total))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,9 @@ def processAlgorithm(self, progress):
output = self.getOutputValue(self.OUTPUT)
fieldName = self.getParameterValue(self.FIELD)

provider = layer.dataProvider()
allAttrs = layer.pendingAllAttributesList()
layer.select(allAttrs)
geomType = self.singleToMultiGeom(provider.geometryType())
geomType = self.singleToMultiGeom(layer.dataProvider().geometryType())

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

inFeat = QgsFeature()
Expand All @@ -81,10 +78,8 @@ def processAlgorithm(self, progress):

if not len(unique) == layer.featureCount():
for i in unique:
#provider.rewind()
multi_feature= []
first = True
layer.select(allAttrs)
features = QGisLayers.features(layer)
for inFeat in features:
atMap = inFeat.attributes()
Expand Down
21 changes: 9 additions & 12 deletions python/plugins/sextante/algs/ftools/SumLines.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,11 @@ def processAlgorithm(self, progress):

polyProvider = polyLayer.dataProvider()
lineProvider = lineLayer.dataProvider()
if polyProvider.crs() != lineProvider.crs():
SextanteLog.addToLog(SextanteLog.LOG_WARNING,
"CRS warning: Input layers have non-matching CRS. This may cause unexpected results.")

idxLength, fieldList = utils.findOrCreateField(polyLayer, polyLayer.pendingFields(), lengthFieldName)
idxCount, fieldList = utils.findOrCreateField(polyLayer, fieldList, countFieldName)

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList,
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList.toList(),
polyProvider.geometryType(), polyProvider.crs())

spatialIndex = utils.createSpatialIndex(lineLayer)
Expand All @@ -97,7 +94,7 @@ def processAlgorithm(self, progress):
hasIntersections = False
for ftPoly in features:
inGeom = QgsGeometry(ftPoly.geometry())
atMap = ftPoly.attributes()
attrs = ftPoly.attributes()
count = 0
length = 0
hasIntersections = False
Expand All @@ -115,15 +112,15 @@ def processAlgorithm(self, progress):
count += 1

outFeat.setGeometry(inGeom)
if idxLength == len(atMap):
atMap.append(QVariant(length))
if idxLength == len(attrs):
attrs.append(QVariant(length))
else:
atMap[idxLength] = QVariant(length)
if idxCount == len(atMap):
atMap.append(QVariant(count))
attrs[idxLength] = QVariant(length)
if idxCount == len(attrs):
attrs.append(QVariant(count))
else:
atMap[idxCount] = QVariant(count)
outFeat.setAttributes(atMap)
attrs[idxCount] = QVariant(count)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)

current += 1
Expand Down
3 changes: 3 additions & 0 deletions python/plugins/sextante/algs/ftools/UniqueValues.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@

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

from sextante.algs.ftools import FToolsUtils as utils

class UniqueValues(GeoAlgorithm):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def processAlgorithm(self, progress):
field = self.getParameterValue(self.FIELD)
segments = int(self.getParameterValue(self.SEGMENTS))

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

buff.buffering(progress, writer, 0, field, True,
Expand Down
5 changes: 3 additions & 2 deletions python/plugins/sextante/algs/ftools/VoronoiPolygons.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))

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

inFeat = QgsFeature()
Expand Down Expand Up @@ -95,7 +95,8 @@ def processAlgorithm(self, progress):
total = 100.0 / float(len(c.polygons))

for site, edges in c.polygons.iteritems():
layer.featureAtId(ptDict[ids[site]], inFeat)
request = QgsFeatureRequest().setFilterFid(ptDict[ids[site]])
inFeat = layer.getFeatures(request).next()
lines = self.clip_voronoi(edges, c, width, height, extent, 0, 0)

geom = QgsGeometry.fromMultiPoint(lines)
Expand Down