322 changes: 162 additions & 160 deletions python/plugins/processing/algs/mmqgisx/MMQGISXAlgorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ def defineCharacteristics(self):
self.LAYERNAME))
self.addOutput(OutputVector(self.SAVENAME, 'Output'))

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

def processAlgorithm(self, progress):

layer = dataobjects.getObjectFromUri(
Expand Down Expand Up @@ -100,12 +94,6 @@ def defineCharacteristics(self):
[ParameterVector.VECTOR_TYPE_ANY]))
self.addOutput(OutputVector(self.SAVENAME, 'Output'))

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

def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.LAYERNAME))
Expand Down Expand Up @@ -146,12 +134,6 @@ def defineCharacteristics(self):

self.addOutput(OutputVector(self.SAVENAME, 'Output'))

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

def processAlgorithm(self, progress):

layer = dataobjects.getObjectFromUri(
Expand Down Expand Up @@ -429,11 +411,6 @@ def defineCharacteristics(self):
self.addParameter(ParameterCrs(self.CRS, 'CRS'))
self.addOutput(OutputVector(self.SAVENAME, 'Output'))

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

def processAlgorithm(self, progress):
hspacing = self.getParameterValue(self.HSPACING)
vspacing = self.getParameterValue(self.VSPACING)
Expand Down Expand Up @@ -618,11 +595,6 @@ def defineCharacteristics(self):
default=0.1))
self.addOutput(OutputVector(self.SAVENAME, 'Output'))

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

def processAlgorithm(self, progress):

layer = dataobjects.getObjectFromUri(
Expand Down Expand Up @@ -817,12 +789,6 @@ def defineCharacteristics(self):

self.addOutput(OutputVector(self.SAVENAME, 'Output'))

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

def processAlgorithm(self, progress):

layersource = dataobjects.getObjectFromUri(
Expand Down Expand Up @@ -936,12 +902,6 @@ def defineCharacteristics(self):
'Spoke Hub ID Attribute', self.SPOKENAME))
self.addOutput(OutputVector(self.SAVENAME, 'Output'))

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

def processAlgorithm(self, progress):

hublayer = dataobjects.getObjectFromUri(
Expand Down Expand Up @@ -1020,11 +980,6 @@ def defineCharacteristics(self):
[ParameterVector.VECTOR_TYPE_ANY]))
self.addOutput(OutputVector(self.SAVENAME, 'Output'))

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

def processAlgorithm(self, progress):

layer1 = dataobjects.getObjectFromUri(
Expand Down Expand Up @@ -1130,11 +1085,52 @@ def defineCharacteristics(self):

self.addOutput(OutputVector(self.RESULT, 'Output', True))

# ===========================================================================
# def getIcon(self):
# return QIcon(os.path.dirname(__file__) +
# "/icons/mmqgis_attribute_export.png")
# ===========================================================================
def processAlgorithm(self, progress):

filename = self.getParameterValue(self.LAYERNAME)
layer = dataobjects.getObjectFromUri(filename)

attribute = self.getParameterValue(self.ATTRIBUTE)
comparison = self.comparisons[self.getParameterValue(self.COMPARISON)]
comparisonvalue = self.getParameterValue(self.COMPARISONVALUE)

selected = select(layer, attribute, comparison, comparisonvalue, progress)

layer.setSelectedFeatures(selected)
self.setOutputValue(self.RESULT, filename)

class mmqgisx_extract_algorithm(GeoAlgorithm):

LAYERNAME = 'LAYERNAME'
ATTRIBUTE = 'ATTRIBUTE'
COMPARISONVALUE = 'COMPARISONVALUE'
COMPARISON = 'COMPARISON'
RESULT = 'RESULT'

def defineCharacteristics(self):
self.name = 'Extract by attribute'
self.group = 'Vector selection tools'

self.addParameter(ParameterVector(self.LAYERNAME, 'Input Layer',
[ParameterVector.VECTOR_TYPE_ANY]))
self.addParameter(ParameterTableField(self.ATTRIBUTE,
'Selection attribute', self.LAYERNAME))
self.comparisons = [
'==',
'!=',
'>',
'>=',
'<',
'<=',
'begins with',
'contains',
]
self.addParameter(ParameterSelection(self.COMPARISON, 'Comparison',
self.comparisons, default=0))
self.addParameter(ParameterString(self.COMPARISONVALUE, 'Value',
default='0'))

self.addOutput(OutputVector(self.RESULT, 'Output'))

def processAlgorithm(self, progress):

Expand All @@ -1145,118 +1141,130 @@ def processAlgorithm(self, progress):
comparison = self.comparisons[self.getParameterValue(self.COMPARISON)]
comparisonvalue = self.getParameterValue(self.COMPARISONVALUE)

selectindex = layer.dataProvider().fieldNameIndex(attribute)
selected = select(layer, attribute, comparison, comparisonvalue, progress)

features = vector.features(layer)
featureCount = len(features)
output = self.getOutputFromName(self.OUTPUT)
writer = output.getVectorWriter(layer.fields(),
layer.geometryType(), layer.crs())
for (i, feat) in enumerate(features):
if feat.id() in selected:
writer.addFeature(feat)
progress.setPercentage(100 * i / float(featureCount))
del writer

def select(layer, attribute, comparison, comparisonvalue, 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:
# string, boolean
try:
y = unicode(comparisonvalue)
except ValueError:
selectionError = True
msg = 'Cannot convert "' + unicode(comparisonvalue) \
+ '" to unicode'
elif selectType == 14:
# Date
dateAndFormat = comparisonvalue.split(' ')

selectType = layer.dataProvider().fields()[selectindex].type()
selectionError = False
if len(dateAndFormat) == 1:
# QtCore.QDate object
y = QLocale.system().toDate(dateAndFormat[0])

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:
# 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:
# QtCore.QDate object
y = QLocale.system().toDate(dateAndFormat[0])

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():
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():
# Conversion was unsuccessfull
selectionError = True
msg += 'Enter the date and the date format, e.g. \
"07.26.2011" "MM.dd.yyyy".'
msg = 'Cannot convert "' + unicode(dateAndFormat[0]) \
+ '" to date with format string "' \
+ unicode(dateAndFormat[1] + '". ')
else:
y = QDate()
msg = ''

if (comparison == 'begins with' or comparison == 'contains') \
and selectType != 10:
if y.isNull():
# Conversion was unsuccessfull
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():
aValue = feature[selectindex]

if aValue is None:
continue

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

match = False
if comparison == '==':
match = x == y
elif comparison == '!=':
match = x != y
elif comparison == '>':
match = x > y
elif comparison == '>=':
match = x >= y
elif comparison == '<':
match = x < y
elif comparison == '<=':
match = x <= y
elif comparison == 'begins with':
match = x.startswith(y)
elif comparison == 'contains':
match = x.find(y) >= 0

readcount += 1
if match:
selected.append(feature.id())

progress.setPercentage(float(readcount) / totalcount * 100)
msg += 'Enter the date and the date format, e.g. \
"07.26.2011" "MM.dd.yyyy".'

layer.setSelectedFeatures(selected)
self.setOutputValue(self.RESULT, filename)
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 = []
features = vector.features(layer)
totalcount = len(features)
for feature in features:
aValue = feature[selectindex]

if aValue is None:
continue

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

match = False
if comparison == '==':
match = x == y
elif comparison == '!=':
match = x != y
elif comparison == '>':
match = x > y
elif comparison == '>=':
match = x >= y
elif comparison == '<':
match = x < y
elif comparison == '<=':
match = x <= y
elif comparison == 'begins with':
match = x.startswith(y)
elif comparison == 'contains':
match = x.find(y) >= 0

readcount += 1
if match:
selected.append(feature.id())

progress.setPercentage(float(readcount) / totalcount * 100)

return selected

class mmqgisx_text_to_float_algorithm(GeoAlgorithm):

Expand All @@ -1275,12 +1283,6 @@ def defineCharacteristics(self):
self.LAYERNAME))
self.addOutput(OutputVector(self.SAVENAME, 'Output'))

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

def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.LAYERNAME))
Expand Down