Skip to content
Permalink
Browse files

[processing] Make select by attribute work

  • Loading branch information
bstroebl committed Sep 26, 2013
1 parent 770e52b commit a5ee91a33148b6ce2c09bf5eb3dd3efa8e5ff0fe
Showing with 62 additions and 8 deletions.
  1. +62 −8 python/plugins/processing/algs/mmqgisx/MMQGISXAlgorithms.py
@@ -1061,18 +1061,72 @@ def processAlgorithm(self, progress):

selectindex = layer.dataProvider().fieldNameIndex(attribute)

selectType = layer.dataProvider().fields()[selectindex].type()
selectionError = False

if selectType == 2:
try:
y = int(comparisonvalue)
except ValueError:
selectionError = True
msg = "Cannot convert \"" + unicode(comparisonvalue) + "\" to integer"
elif selectType == 6:
try:
y = float(comparisonvalue)
except ValueError:
selectionError = True
msg = "Cannot convert \"" + unicode(comparisonvalue) + "\" to float"
elif selectType == 10: # 10: string, boolean
try:
y = unicode(comparisonvalue)
except ValueError:
selectionError = True
msg = "Cannot convert \"" + unicode(comparisonvalue) + "\" to unicode"
elif selectType == 14: # date
dateAndFormat = comparisonvalue.split(" ")

if len(dateAndFormat) == 1:
y = QLocale.system().toDate(dateAndFormat[0]) # QtCore.QDate object

if y.isNull():
msg = "Cannot convert \"" + unicode(dateAndFormat) + "\" to date with system date format " + QLocale.system().dateFormat()
elif len(dateAndFormat) == 2:
y = QDate.fromString(dateAndFormat[0], dateAndFormat[1])

if y.isNull():
msg = "Cannot convert \"" + unicode(dateAndFormat[0]) + "\" to date with format string \"" + unicode(dateAndFormat[1] + "\". ")
else:
y = QDate()
msg = ""

if y.isNull(): # conversion was unsuccessfull
selectionError = True
msg += "Enter the date and the date format, e.g. \"07.26.2011\" \"MM.dd.yyyy\"."

if ((comparison == 'begins with') or (comparison == 'contains')) and selectType != 10:
selectionError = True
msg = "\"" + comparison + "\" can only be used with string fields"

if selectionError:
raise GeoAlgorithmExecutionException("Error in selection input: " + msg)

readcount = 0
selected = []
totalcount = layer.featureCount()
for feature in layer.getFeatures():
if (comparison == 'begins with') or (comparison == 'contains') or \
isinstance(feature.attributes()[selectindex], basestring) or \
isinstance(comparisonvalue, basestring):
x = unicode(feature.attributes()[selectindex])
y = unicode(comparisonvalue)
else:
x = float(feature.attributes()[selectindex])
y = float(comparisonvalue)
aValue = feature[selectindex]

if aValue == None:
continue

if selectType == 2:
x = int(aValue)
elif selectType == 6:
x = float(aValue)
elif selectType == 10: # 10: string, boolean
x = unicode(aValue)
elif selectType == 14: # date
x = aValue # should be date

match = False
if (comparison == '=='):

0 comments on commit a5ee91a

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