Skip to content

Commit

Permalink
more fixes in python sextante algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
volaya committed Feb 7, 2013
1 parent a611905 commit c2b8ae0
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 58 deletions.
7 changes: 3 additions & 4 deletions python/plugins/sextante/algs/AddTableField.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from sextante.parameters.ParameterString import ParameterString
from sextante.parameters.ParameterSelection import ParameterSelection
from sextante.core.QGisLayers import QGisLayers
from PyQt4 import QtGui


class AddTableField(GeoAlgorithm):
Expand Down Expand Up @@ -64,7 +63,7 @@ def processAlgorithm(self, progress):
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
vprovider = vlayer.dataProvider()
fields = vprovider.fields()
fields[len(fields)] = QgsField(fieldname, self.TYPES[fieldtype])
fields.append(QgsField(fieldname, self.TYPES[fieldtype]))
writer = output.getVectorWriter(fields, vprovider.geometryType(), vprovider.crs() )
outFeat = QgsFeature()
inGeom = QgsGeometry()
Expand All @@ -76,9 +75,9 @@ def processAlgorithm(self, progress):
nElement += 1
inGeom = inFeat.geometry()
outFeat.setGeometry( inGeom )
atMap = inFeat.attributeMap()
atMap = inFeat.attributes()
atMap.append(QVariant())
outFeat.addAttribute( len(vprovider.fields()), QVariant() )
outFeat.setAttributes(atMap)
writer.addFeature( outFeat )
del writer

33 changes: 17 additions & 16 deletions python/plugins/sextante/algs/JoinAttributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ def processAlgorithm(self, progress):
# Layer 1
layer = QGisLayers.getObjectFromUri(input)
provider = layer.dataProvider()
join_field1_index = layer.fieldNameIndex(field)
joinField1Index = layer.fieldNameIndex(field)
# Layer 2
layer2 = QGisLayers.getObjectFromUri(input2)
provider2 = layer2.dataProvider()
fields2 = provider2.fields()
join_field2_index = layer2.fieldNameIndex(field2)

joinField2Index = layer2.fieldNameIndex(field2)

# Output
outFields = input.fields()
for f in fields2:
outFields.append(f)
outFields = []
outFields.extend(provider.fields())
outFields.extend(provider2.fields())

writer = output.getVectorWriter(outFields, provider.geometryType(), provider.crs())

Expand All @@ -82,18 +82,19 @@ def processAlgorithm(self, progress):
features = QGisLayers.features(layer);
for inFeat in features:
inGeom = inFeat.geometry()
atMap = inFeat.attributes()
join_value1 = atMap[join_field1_index].toString()
while provider2.nextFeature(inFeat2):
attrs = inFeat.attributes()
joinValue1 = attrs[joinField1Index].toString()
features2 = QGisLayers.features(layer2);
for inFeat2 in features2:
## Maybe it should cache this entries...
atMap2 = inFeat2.attributeMap()
join_value2 = atMap2[join_field2_index].toString()
if join_value1 == join_value2:
attrs2 = inFeat2.attributes()
joinValue2 = attrs2[joinField2Index].toString()
if joinValue1 == joinValue2:
# create the new feature
outFeat.setGeometry(inGeom)
atMap.extend(atMap2)
break;
outFeat.setAttributes(atMap)
outFeat.setGeometry(inGeom)
attrs.extend(attrs2)
break;
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)

del writer
2 changes: 1 addition & 1 deletion python/plugins/sextante/algs/ftools/DensifyGeometries.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def defineCharacteristics(self):
self.addParameter(ParameterVector(self.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterNumber(self.VERTICES, "Vertices to add", 1, 10000000, 1))

self.addOutput(OutputVector(self.OUTPUT, "Simplified layer"))
self.addOutput(OutputVector(self.OUTPUT, "Densified layer"))

def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def defineCharacteristics(self):
self.addParameter(ParameterVector(self.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterNumber(self.INTERVAL, "Interval between Vertices to add", 1, 10000000, 1))

self.addOutput(OutputVector(self.OUTPUT, "Simplified layer"))
self.addOutput(OutputVector(self.OUTPUT, "Densified layer"))

def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/sextante/algs/ftools/Difference.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def processAlgorithm(self, progress):
atMap = inFeatA.attributes()
intersects = index.intersects( geom.boundingBox() )
for id in intersects:
vlayerB.featureAtId( int( id ), inFeatB , True, allAttrsB )
vlayerB.featureAtId( int( id ), inFeatB , True)
tmpGeom = QgsGeometry( inFeatB.geometry() )
try:
if diff_geom.intersects( tmpGeom ):
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/sextante/algs/ftools/ExtractNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ def processAlgorithm(self, progress):
total = 100.0 / float(len(features))
for inFeat in features:
inGeom = inFeat.geometry()
atMap = inFeat.attributeMap()
atMap = inFeat.attributes()

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

for i in points:
outFeat.setGeometry(outGeom.fromPoint(i))
Expand Down
41 changes: 20 additions & 21 deletions python/plugins/sextante/algs/ftools/FToolsUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from qgis.core import *

def createSpatialIndex(layer):
idx = QgsSpatialIndex()
idx = QgsSpatialIndex()
features = QGisLayers.features(layer)
for ft in features:
idx.insertFeature(ft)
Expand Down Expand Up @@ -61,12 +61,12 @@ def createUniqueFieldName(fieldName, fieldList):

def findOrCreateField(layer, fieldList, fieldName, fieldLen=24, fieldPrec=15):
idx = layer.fieldNameIndex(fieldName)
if idx == -1:
if idx == -1:
fn = createUniqueFieldName(fieldName, fieldList)
field = QgsField(fn, QVariant.Double, "", fieldLen, fieldPrec)
idx = len(fieldList)
fieldList.append(field)

return idx, fieldList

def extractPoints( geom ):
Expand All @@ -80,7 +80,7 @@ def extractPoints( geom ):
if geom.isMultipart():
lines = geom.asMultiPolyline()
for line in lines:
points.extend(line)
points.extend(line)
else:
points = geom.asPolyline()
elif geom.type() == QGis.Polygon:
Expand All @@ -96,38 +96,37 @@ def extractPoints( geom ):

return points

def getUniqueValues(layer, fieldIndex):
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:
values.append(feat.attributes()[fieldIndex])
values.append(feat.attributes()[fieldIndex])
return values

def getUniqueValuesCount(layer, fieldIndex):
return len(getUniqueValues(layer, fieldIndex))

# From two input field maps, create single field map
def combineVectorFields( layerA, layerB ):
fields = []
fieldsA = layerA.dataProvider().fields()
fields.extend(fieldsA)
namesA = [unicode(f.name()).lower() for f in fieldsA]
fieldsB = layerB.dataProvider().fields()
fieldsB = testForUniqueness( fieldsA, fieldsB )
for field in fieldsB:
fieldsA.append(field)
return fieldsA

# Check if two input field maps are unique, and resolve name issues if they aren't
def testForUniqueness( fieldList1, fieldList2 ):
changed = True
while changed:
changed = False
for i in fieldList1:
for j in fieldList2:
if j.name() == i.name():
j = createUniqueFieldNameFromName( j )
changed = True
return fieldList2
name = unicode(field.name()).lower()
if name in namesA:
idx=2
newName = name + "_" + unicode(idx)
while newName in namesA:
idx += 1
newName = name + "_" + unicode(idx)
field = QgsField(newName, field.type(), field.typeName())
fields.append(field)

return fields

# Create a unique field name based on input field name
def createUniqueFieldNameFromName( field ):
Expand Down
12 changes: 6 additions & 6 deletions python/plugins/sextante/algs/ftools/SelectByLocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SelectByLocation(GeoAlgorithm):

INPUT = "INPUT"
INTERSECT = "INTERSECT"
METHOD = "METHOD"
METHOD = "METHOD"
OUTPUT = "OUTPUT"

METHODS = ["creating new selection",
Expand All @@ -54,7 +54,7 @@ def defineCharacteristics(self):
self.group = "Vector selection tools"
self.addParameter(ParameterVector(self.INPUT, "Layer to select from", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterVector(self.INTERSECT, "Additional layer (intersection layer)", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterSelection(self.METHOD, "Modify current selection by", self.METHODS, 0))
self.addParameter(ParameterSelection(self.METHOD, "Modify current selection by", self.METHODS, 0))
self.addOutput(OutputVector(self.OUTPUT, "Selection", True))

def processAlgorithm(self, progress):
Expand All @@ -65,15 +65,15 @@ def processAlgorithm(self, progress):
selectLayer = QGisLayers.getObjectFromUri(filename)
inputProvider = inputLayer.dataProvider()

oldSelection = set(inputLayer.selectedFeaturesIds())
oldSelection = set(inputLayer.selectedFeaturesIds())
index = QgsSpatialIndex()
feat = QgsFeature()
while inputProvider.nextFeature(feat):
for feat in inputLayer.getFeatures():
index.insertFeature(feat)

infeat = QgsFeature()
geom = QgsGeometry()
selectedSet = []
selectedSet = []
current = 0
features = QGisLayers.features(selectLayer)
total = 100.0 / float(len(features))
Expand Down
4 changes: 3 additions & 1 deletion python/plugins/sextante/algs/ftools/Union.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def processAlgorithm(self, progress):
vproviderA = vlayerA.dataProvider()

fields = utils.combineVectorFields(vlayerA, vlayerB )
names = [field.name() for field in fields]
SextanteLog.addToLog(SextanteLog.LOG_INFO, str(names))
#longNames = ftools_utils.checkFieldNameLength( fields )
#if not longNames.isEmpty():
#raise GeoAlgorithmExecutionException("Following field names are longer than 10 characters:\n" + longNames.join('\n') )
Expand Down Expand Up @@ -83,7 +85,7 @@ def processAlgorithm(self, progress):
except:
# this really shouldn't happen, as we
# haven't edited the input geom at all
FEATURE_EXCEPT = False
raise GeoAlgorithmExecutionException("Feature exception while computing union")
else:
for id in intersects:
count += 1
Expand Down
1 change: 0 additions & 1 deletion python/plugins/sextante/grass/GrassUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* *
***************************************************************************
"""
from PyQt4 import QtGui

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
Expand Down
8 changes: 4 additions & 4 deletions python/plugins/sextante/gui/ParametersPanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ def getWidgetFromParameter(self, param):
item = QtGui.QComboBox()
layers = QGisLayers.getVectorLayers(param.shapetype)
if (param.optional):
item.addItem((self.NOT_SELECTED, None))
item.addItem(self.NOT_SELECTED, None)
for layer in layers:
item.addItem((self.getExtendedLayerName(layer), layer))
item.addItem(self.getExtendedLayerName(layer), layer)
item.currentIndexChanged.connect(self.updateDependentFields)
item.name = param.name
else:
Expand All @@ -230,9 +230,9 @@ def getWidgetFromParameter(self, param):
item = QtGui.QComboBox()
layers = QGisLayers.getTables()
if (param.optional):
item.addItem((self.NOT_SELECTED, None))
item.addItem(self.NOT_SELECTED, None)
for layer in layers:
item.addItem((layer.name(), layer))
item.addItem(layer.name(), layer)
item.currentIndexChanged.connect(self.updateDependentFields)
item.name = param.name
else:
Expand Down

0 comments on commit c2b8ae0

Please sign in to comment.