Fix tests

This commit is contained in:
Nyall Dawson 2017-06-30 12:42:18 +10:00
parent 7a1fd93ace
commit 8cfcf57541
2 changed files with 36 additions and 31 deletions

View File

@ -49,7 +49,7 @@ class Aspect(QgisAlgorithm):
INPUT = 'INPUT' INPUT = 'INPUT'
Z_FACTOR = 'Z_FACTOR' Z_FACTOR = 'Z_FACTOR'
OUTPUT_LAYER = 'OUTPUT_LAYER' OUTPUT = 'OUTPUT'
def icon(self): def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'dem.png')) return QIcon(os.path.join(pluginPath, 'images', 'dem.png'))

View File

@ -28,10 +28,8 @@ __revision__ = '$Format:%H$'
from qgis.core import (QgsApplication, from qgis.core import (QgsApplication,
QgsWkbTypes, QgsWkbTypes,
QgsPoint, QgsPoint,
QgsCoordinateReferenceSystem,
QgsFeatureRequest, QgsFeatureRequest,
QgsGeometry, QgsGeometry,
QgsProcessingUtils,
QgsProcessingParameterDefinition, QgsProcessingParameterDefinition,
QgsProcessingParameterFeatureSink, QgsProcessingParameterFeatureSink,
QgsProcessingParameterFeatureSource, QgsProcessingParameterFeatureSource,
@ -39,11 +37,6 @@ from qgis.core import (QgsApplication,
QgsProcessingOutputVectorLayer, QgsProcessingOutputVectorLayer,
QgsProcessingParameterField) QgsProcessingParameterField)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterTable
from processing.core.parameters import ParameterTableField
from processing.core.parameters import ParameterCrs
from processing.core.outputs import OutputVector
from processing.tools import dataobjects
class PointsLayerFromTable(QgisAlgorithm): class PointsLayerFromTable(QgisAlgorithm):
@ -87,39 +80,49 @@ class PointsLayerFromTable(QgisAlgorithm):
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Points from table'), type=QgsProcessingParameterDefinition.TypeVectorPoint)) self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Points from table'), type=QgsProcessingParameterDefinition.TypeVectorPoint))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Points from table'), type=QgsProcessingParameterDefinition.TypeVectorPoint)) self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Points from table'), type=QgsProcessingParameterDefinition.TypeVectorPoint))
self.source = None
self.x_field_index = None
self.y_field_index = None
self.z_field_index = None
self.m_field_index = None
self.sink = None
self.dest_id = None
def name(self): def name(self):
return 'createpointslayerfromtable' return 'createpointslayerfromtable'
def displayName(self): def displayName(self):
return self.tr('Create points layer from table') return self.tr('Create points layer from table')
def processAlgorithm(self, parameters, context, feedback): def prepareAlgorithm(self, parameters, context, feedback):
source = self.parameterAsSource(parameters, self.INPUT, context) self.source = self.parameterAsSource(parameters, self.INPUT, context)
fields = source.fields() fields = self.source.fields()
x_field_index = fields.lookupField(self.parameterAsString(parameters, self.XFIELD, context)) self.x_field_index = fields.lookupField(self.parameterAsString(parameters, self.XFIELD, context))
y_field_index = fields.lookupField(self.parameterAsString(parameters, self.YFIELD, context)) self.y_field_index = fields.lookupField(self.parameterAsString(parameters, self.YFIELD, context))
z_field_index = -1 self.z_field_index = -1
if self.parameterAsString(parameters, self.ZFIELD, context): if self.parameterAsString(parameters, self.ZFIELD, context):
z_field_index = fields.lookupField(self.parameterAsString(parameters, self.ZFIELD, context)) self.z_field_index = fields.lookupField(self.parameterAsString(parameters, self.ZFIELD, context))
m_field_index = -1 self.m_field_index = -1
if self.parameterAsString(parameters, self.MFIELD, context): if self.parameterAsString(parameters, self.MFIELD, context):
m_field_index = fields.lookupField(self.parameterAsString(parameters, self.MFIELD, context)) self.m_field_index = fields.lookupField(self.parameterAsString(parameters, self.MFIELD, context))
wkb_type = QgsWkbTypes.Point wkb_type = QgsWkbTypes.Point
if z_field_index >= 0: if self.z_field_index >= 0:
wkb_type = QgsWkbTypes.addZ(wkb_type) wkb_type = QgsWkbTypes.addZ(wkb_type)
if m_field_index >= 0: if self.m_field_index >= 0:
wkb_type = QgsWkbTypes.addM(wkb_type) wkb_type = QgsWkbTypes.addM(wkb_type)
target_crs = self.parameterAsCrs(parameters, self.TARGET_CRS, context) target_crs = self.parameterAsCrs(parameters, self.TARGET_CRS, context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context, (self.sink, self.dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, wkb_type, target_crs) fields, wkb_type, target_crs)
return True
def processAlgorithm(self, context, feedback):
request = QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry) request = QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry)
features = source.getFeatures() features = self.source.getFeatures(request)
total = 100.0 / source.featureCount() if source.featureCount() else 0 total = 100.0 / self.source.featureCount() if self.source.featureCount() else 0
for current, feature in enumerate(features): for current, feature in enumerate(features):
if feedback.isCanceled(): if feedback.isCanceled():
@ -129,20 +132,20 @@ class PointsLayerFromTable(QgisAlgorithm):
attrs = feature.attributes() attrs = feature.attributes()
try: try:
x = float(attrs[x_field_index]) x = float(attrs[self.x_field_index])
y = float(attrs[y_field_index]) y = float(attrs[self.y_field_index])
point = QgsPoint(x, y) point = QgsPoint(x, y)
if z_field_index >= 0: if self.z_field_index >= 0:
try: try:
point.addZValue(float(attrs[z_field_index])) point.addZValue(float(attrs[self.z_field_index]))
except: except:
point.addZValue(0.0) point.addZValue(0.0)
if m_field_index >= 0: if self.m_field_index >= 0:
try: try:
point.addMValue(float(attrs[m_field_index])) point.addMValue(float(attrs[self.m_field_index]))
except: except:
point.addMValue(0.0) point.addMValue(0.0)
@ -150,6 +153,8 @@ class PointsLayerFromTable(QgisAlgorithm):
except: except:
pass # no geometry pass # no geometry
sink.addFeature(feature) self.sink.addFeature(feature)
return True
return {self.OUTPUT: dest_id} def postProcessAlgorithm(self, context, feedback):
return {self.OUTPUT: self.dest_id}