mirror of
https://github.com/qgis/QGIS.git
synced 2025-03-04 00:30:59 -05:00
161 lines
6.7 KiB
Python
161 lines
6.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
***************************************************************************
|
|
PointsLayerFromTable.py
|
|
---------------------
|
|
Date : January 2013
|
|
Copyright : (C) 2013 by Victor Olaya
|
|
Email : volayaf at gmail dot com
|
|
***************************************************************************
|
|
* *
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
* it under the terms of the GNU General Public License as published by *
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
* (at your option) any later version. *
|
|
* *
|
|
***************************************************************************
|
|
"""
|
|
|
|
__author__ = 'Victor Olaya'
|
|
__date__ = 'August 2013'
|
|
__copyright__ = '(C) 2013, Victor Olaya'
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
from qgis.core import (QgsApplication,
|
|
QgsWkbTypes,
|
|
QgsPoint,
|
|
QgsFeatureRequest,
|
|
QgsGeometry,
|
|
QgsProcessingParameterDefinition,
|
|
QgsProcessingParameterFeatureSink,
|
|
QgsProcessingParameterFeatureSource,
|
|
QgsProcessingParameterCrs,
|
|
QgsProcessingOutputVectorLayer,
|
|
QgsProcessingParameterField)
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
|
|
|
|
|
class PointsLayerFromTable(QgisAlgorithm):
|
|
|
|
INPUT = 'INPUT'
|
|
XFIELD = 'XFIELD'
|
|
YFIELD = 'YFIELD'
|
|
ZFIELD = 'ZFIELD'
|
|
MFIELD = 'MFIELD'
|
|
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(',')
|
|
|
|
def group(self):
|
|
return self.tr('Vector creation tools')
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
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))
|
|
|
|
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 prepareAlgorithm(self, parameters, context, feedback):
|
|
self.source = self.parameterAsSource(parameters, self.INPUT, context)
|
|
|
|
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):
|
|
self.z_field_index = fields.lookupField(self.parameterAsString(parameters, self.ZFIELD, context))
|
|
self.m_field_index = -1
|
|
if self.parameterAsString(parameters, self.MFIELD, context):
|
|
self.m_field_index = fields.lookupField(self.parameterAsString(parameters, self.MFIELD, context))
|
|
|
|
wkb_type = QgsWkbTypes.Point
|
|
if self.z_field_index >= 0:
|
|
wkb_type = QgsWkbTypes.addZ(wkb_type)
|
|
if self.m_field_index >= 0:
|
|
wkb_type = QgsWkbTypes.addM(wkb_type)
|
|
|
|
target_crs = self.parameterAsCrs(parameters, self.TARGET_CRS, context)
|
|
|
|
(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 = 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():
|
|
break
|
|
|
|
feedback.setProgress(int(current * total))
|
|
attrs = feature.attributes()
|
|
|
|
try:
|
|
x = float(attrs[self.x_field_index])
|
|
y = float(attrs[self.y_field_index])
|
|
|
|
point = QgsPoint(x, y)
|
|
|
|
if self.z_field_index >= 0:
|
|
try:
|
|
point.addZValue(float(attrs[self.z_field_index]))
|
|
except:
|
|
point.addZValue(0.0)
|
|
|
|
if self.m_field_index >= 0:
|
|
try:
|
|
point.addMValue(float(attrs[self.m_field_index]))
|
|
except:
|
|
point.addMValue(0.0)
|
|
|
|
feature.setGeometry(QgsGeometry(point))
|
|
except:
|
|
pass # no geometry
|
|
|
|
self.sink.addFeature(feature)
|
|
return True
|
|
|
|
def postProcessAlgorithm(self, context, feedback):
|
|
return {self.OUTPUT: self.dest_id}
|