Skip to content
Permalink
Browse files

Fix tests

  • Loading branch information
nyalldawson committed Jul 6, 2017
1 parent 7a1fd93 commit 8cfcf5754113fff862c4ca13f17aa7df2b2bfc0b
@@ -49,7 +49,7 @@ class Aspect(QgisAlgorithm):

INPUT = 'INPUT'
Z_FACTOR = 'Z_FACTOR'
OUTPUT_LAYER = 'OUTPUT_LAYER'
OUTPUT = 'OUTPUT'

def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'dem.png'))
@@ -28,22 +28,15 @@
from qgis.core import (QgsApplication,
QgsWkbTypes,
QgsPoint,
QgsCoordinateReferenceSystem,
QgsFeatureRequest,
QgsGeometry,
QgsProcessingUtils,
QgsProcessingParameterDefinition,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterCrs,
QgsProcessingOutputVectorLayer,
QgsProcessingParameterField)
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):
@@ -87,39 +80,49 @@ def __init__(self):
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.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):
return 'createpointslayerfromtable'

def displayName(self):
return self.tr('Create points layer from table')

def processAlgorithm(self, parameters, context, feedback):
source = self.parameterAsSource(parameters, self.INPUT, context)
def prepareAlgorithm(self, parameters, context, feedback):
self.source = self.parameterAsSource(parameters, self.INPUT, context)

fields = source.fields()
x_field_index = fields.lookupField(self.parameterAsString(parameters, self.XFIELD, context))
y_field_index = fields.lookupField(self.parameterAsString(parameters, self.YFIELD, context))
z_field_index = -1
fields = self.source.fields()
self.x_field_index = fields.lookupField(self.parameterAsString(parameters, self.XFIELD, context))
self.y_field_index = fields.lookupField(self.parameterAsString(parameters, self.YFIELD, context))
self.z_field_index = -1
if self.parameterAsString(parameters, self.ZFIELD, context):
z_field_index = fields.lookupField(self.parameterAsString(parameters, self.ZFIELD, context))
m_field_index = -1
self.z_field_index = fields.lookupField(self.parameterAsString(parameters, self.ZFIELD, context))
self.m_field_index = -1
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
if z_field_index >= 0:
if self.z_field_index >= 0:
wkb_type = QgsWkbTypes.addZ(wkb_type)
if m_field_index >= 0:
if self.m_field_index >= 0:
wkb_type = QgsWkbTypes.addM(wkb_type)

target_crs = self.parameterAsCrs(parameters, self.TARGET_CRS, context)

(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, wkb_type, target_crs)
(self.sink, self.dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, wkb_type, target_crs)
return True

def processAlgorithm(self, context, feedback):
request = QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry)
features = source.getFeatures()
total = 100.0 / source.featureCount() if source.featureCount() else 0
features = self.source.getFeatures(request)
total = 100.0 / self.source.featureCount() if self.source.featureCount() else 0

for current, feature in enumerate(features):
if feedback.isCanceled():
@@ -129,27 +132,29 @@ def processAlgorithm(self, parameters, context, feedback):
attrs = feature.attributes()

try:
x = float(attrs[x_field_index])
y = float(attrs[y_field_index])
x = float(attrs[self.x_field_index])
y = float(attrs[self.y_field_index])

point = QgsPoint(x, y)

if z_field_index >= 0:
if self.z_field_index >= 0:
try:
point.addZValue(float(attrs[z_field_index]))
point.addZValue(float(attrs[self.z_field_index]))
except:
point.addZValue(0.0)

if m_field_index >= 0:
if self.m_field_index >= 0:
try:
point.addMValue(float(attrs[m_field_index]))
point.addMValue(float(attrs[self.m_field_index]))
except:
point.addMValue(0.0)

feature.setGeometry(QgsGeometry(point))
except:
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}

0 comments on commit 8cfcf57

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