Skip to content

Commit

Permalink
Restore points layer from table algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 28, 2017
1 parent 9f5e33a commit d1a93e0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 58 deletions.
96 changes: 56 additions & 40 deletions python/plugins/processing/algs/qgis/PointsLayerFromTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@
from qgis.core import (QgsApplication,
QgsWkbTypes,
QgsPoint,
QgsFeatureSink,
QgsCoordinateReferenceSystem,
QgsFeatureRequest,
QgsGeometry,
QgsProcessingUtils)
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
Expand All @@ -50,6 +56,12 @@ class PointsLayerFromTable(QgisAlgorithm):
OUTPUT = 'OUTPUT'
TARGET_CRS = 'TARGET_CRS'

def icon(self):
return QgsApplication.getThemeIcon("/providerQgis.svg")

def svgIconPath(self):
return QgsApplication.iconPath("providerQgis.svg")

def tags(self):
return self.tr('points,create,values,attributes').split(',')

Expand All @@ -58,19 +70,22 @@ def group(self):

def __init__(self):
super().__init__()
self.addParameter(ParameterTable(self.INPUT,
self.tr('Input layer')))
self.addParameter(ParameterTableField(self.XFIELD,
self.tr('X field'), self.INPUT, ParameterTableField.DATA_TYPE_ANY))
self.addParameter(ParameterTableField(self.YFIELD,
self.tr('Y field'), self.INPUT, ParameterTableField.DATA_TYPE_ANY))
self.addParameter(ParameterTableField(self.ZFIELD,
self.tr('Z field'), self.INPUT, datatype=ParameterTableField.DATA_TYPE_ANY, optional=True))
self.addParameter(ParameterTableField(self.MFIELD,
self.tr('M field'), self.INPUT, datatype=ParameterTableField.DATA_TYPE_ANY, optional=True))
self.addParameter(ParameterCrs(self.TARGET_CRS,
self.tr('Target CRS'), 'EPSG:4326'))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Points from table'), datatype=[dataobjects.TYPE_VECTOR_POINT]))

self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT, self.tr('Input layer'), types=[QgsProcessingParameterField.TypeTable]))

self.addParameter(QgsProcessingParameterField(self.XFIELD,
self.tr('X field'), parentLayerParameterName=self.INPUT, type=QgsProcessingParameterField.Any))
self.addParameter(QgsProcessingParameterField(self.YFIELD,
self.tr('Y field'), parentLayerParameterName=self.INPUT, type=QgsProcessingParameterField.Any))
self.addParameter(QgsProcessingParameterField(self.ZFIELD,
self.tr('Z field'), parentLayerParameterName=self.INPUT, type=QgsProcessingParameterField.Any, optional=True))
self.addParameter(QgsProcessingParameterField(self.MFIELD,
self.tr('M field'), parentLayerParameterName=self.INPUT, type=QgsProcessingParameterField.Any, optional=True))
self.addParameter(QgsProcessingParameterCrs(self.TARGET_CRS,
self.tr('Target CRS'), defaultValue='EPSG:4326'))

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))

def name(self):
return 'createpointslayerfromtable'
Expand All @@ -79,36 +94,37 @@ def displayName(self):
return self.tr('Create points layer from table')

def processAlgorithm(self, parameters, context, feedback):
source = self.getParameterValue(self.INPUT)
vlayer = QgsProcessingUtils.mapLayerFromString(source, context)
output = self.getOutputFromName(self.OUTPUT)

fields = vlayer.fields()
x_field_index = fields.lookupField(self.getParameterValue(self.XFIELD))
y_field_index = fields.lookupField(self.getParameterValue(self.YFIELD))
z_field_index = None
if self.getParameterValue(self.ZFIELD):
z_field_index = fields.lookupField(self.getParameterValue(self.ZFIELD))
m_field_index = None
if self.getParameterValue(self.MFIELD):
m_field_index = fields.lookupField(self.getParameterValue(self.MFIELD))
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
if self.parameterAsString(parameters, self.ZFIELD, context):
z_field_index = fields.lookupField(self.parameterAsString(parameters, self.ZFIELD, context))
m_field_index = -1
if self.parameterAsString(parameters, self.MFIELD, context):
m_field_index = fields.lookupField(self.parameterAsString(parameters, self.MFIELD, context))

wkb_type = QgsWkbTypes.Point
if z_field_index is not None:
if z_field_index >= 0:
wkb_type = QgsWkbTypes.addZ(wkb_type)
if m_field_index is not None:
if m_field_index >= 0:
wkb_type = QgsWkbTypes.addM(wkb_type)

crsId = self.getParameterValue(self.TARGET_CRS)
target_crs = QgsCoordinateReferenceSystem()
target_crs.createFromUserInput(crsId)
target_crs = self.parameterAsCrs(parameters, self.TARGET_CRS, context)

writer = output.getVectorWriter(fields, wkb_type, target_crs, context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, wkb_type, target_crs)

features = QgsProcessingUtils.getFeatures(vlayer, context)
total = 100.0 / vlayer.featureCount() if vlayer.featureCount() else 0
request = QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry)
features = source.getFeatures()
total = 100.0 / source.featureCount() if source.featureCount() else 0

for current, feature in enumerate(features):
if feedback.isCanceled():
break

feedback.setProgress(int(current * total))
attrs = feature.attributes()

Expand All @@ -118,13 +134,13 @@ def processAlgorithm(self, parameters, context, feedback):

point = QgsPoint(x, y)

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

if m_field_index is not None:
if m_field_index >= 0:
try:
point.addMValue(float(attrs[m_field_index]))
except:
Expand All @@ -134,6 +150,6 @@ def processAlgorithm(self, parameters, context, feedback):
except:
pass # no geometry

writer.addFeature(feature, QgsFeatureSink.FastInsert)
sink.addFeature(feature)

del writer
return {self.OUTPUT: dest_id}
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
from .ImportIntoPostGIS import ImportIntoPostGIS
from .ImportIntoSpatialite import ImportIntoSpatialite
from .Merge import Merge
from .PointsLayerFromTable import PointsLayerFromTable
from .PostGISExecuteSQL import PostGISExecuteSQL
from .RandomExtract import RandomExtract
from .RandomExtractWithinSubsets import RandomExtractWithinSubsets
Expand Down Expand Up @@ -117,7 +118,6 @@
# from .FieldPyculator import FieldsPyculator
# from .JoinAttributes import JoinAttributes
# from .CreateConstantRaster import CreateConstantRaster
# from .PointsLayerFromTable import PointsLayerFromTable
# from .PointsDisplacement import PointsDisplacement
# from .ZonalStatistics import ZonalStatistics
# from .PointsFromPolygons import PointsFromPolygons
Expand Down Expand Up @@ -207,7 +207,7 @@ def getAlgs(self):
# GeometryConvert(), FieldsCalculator(),
# JoinAttributes(),
# Explode(), FieldsPyculator(),
# EquivalentNumField(), PointsLayerFromTable(),
# EquivalentNumField(),
# StatisticsByCategories(), ConcaveHull(),
# RasterLayerStatistics(), PointsDisplacement(),
# ZonalStatistics(), PointsFromPolygons(),
Expand Down Expand Up @@ -260,6 +260,7 @@ def getAlgs(self):
ImportIntoPostGIS(),
ImportIntoSpatialite(),
Merge(),
PointsLayerFromTable(),
PostGISExecuteSQL(),
RandomExtract(),
RandomExtractWithinSubsets(),
Expand Down
32 changes: 16 additions & 16 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1600,22 +1600,22 @@ tests:
name: expected/delete_column.gml
type: vector

# - algorithm: qgis:createpointslayerfromtable
# name: Create points from table
# params:
# INPUT:
# name: create_points.gml
# type: table
# MFIELD: mcoord
# TARGET_CRS: EPSG:4326
# XFIELD: xcoord
# YFIELD: ycoord
# ZFIELD: zcoord
# results:
# OUTPUT:
# name: expected/create_points.gml
# type: vector
#
- algorithm: qgis:createpointslayerfromtable
name: Create points from table
params:
INPUT:
name: create_points.gml
type: table
MFIELD: mcoord
TARGET_CRS: EPSG:4326
XFIELD: xcoord
YFIELD: ycoord
ZFIELD: zcoord
results:
OUTPUT:
name: expected/create_points.gml
type: vector

# - algorithm: qgis:splitwithlines
# name: Split lines with lines (new alg)
# params:
Expand Down

0 comments on commit d1a93e0

Please sign in to comment.