2013-02-04 23:29:55 +01:00
|
|
|
# -*- 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'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-02-04 23:29:55 +01:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-02-04 23:29:55 +01:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-08-04 09:10:08 +02:00
|
|
|
from qgis.core import Qgis, QgsWkbTypes
|
2016-03-23 00:55:36 +03:00
|
|
|
from qgis.core import QgsCoordinateReferenceSystem
|
|
|
|
from qgis.core import QgsFeature
|
|
|
|
from qgis.core import QgsGeometry
|
|
|
|
from qgis.core import QgsPoint
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterTable
|
|
|
|
from processing.core.parameters import ParameterTableField
|
|
|
|
from processing.core.parameters import ParameterCrs
|
|
|
|
from processing.core.outputs import OutputVector
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.tools import dataobjects, vector
|
|
|
|
|
2013-02-04 23:29:55 +01:00
|
|
|
|
|
|
|
class PointsLayerFromTable(GeoAlgorithm):
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
XFIELD = 'XFIELD'
|
|
|
|
YFIELD = 'YFIELD'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
TARGET_CRS = 'TARGET_CRS'
|
2013-02-04 23:29:55 +01:00
|
|
|
|
2016-02-17 09:36:59 +02:00
|
|
|
def defineCharacteristics(self):
|
|
|
|
self.name, self.i18n_name = self.trAlgorithm('Points layer from table')
|
|
|
|
self.group, self.i18n_group = self.trAlgorithm('Vector creation tools')
|
|
|
|
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(ParameterCrs(self.TARGET_CRS,
|
|
|
|
self.tr('Target CRS'), 'EPSG:4326'))
|
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Points from table')))
|
|
|
|
|
2013-02-04 23:29:55 +01:00
|
|
|
def processAlgorithm(self, progress):
|
|
|
|
source = self.getParameterValue(self.INPUT)
|
2013-09-12 13:19:00 +02:00
|
|
|
vlayer = dataobjects.getObjectFromUri(source)
|
2013-02-04 23:29:55 +01:00
|
|
|
output = self.getOutputFromName(self.OUTPUT)
|
|
|
|
vprovider = vlayer.dataProvider()
|
|
|
|
fields = vprovider.fields()
|
2016-08-04 09:10:08 +02:00
|
|
|
writer = output.getVectorWriter(fields, QgsWkbTypes.Point, self.crs)
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
xfieldindex = vlayer.fieldNameIndex(self.getParameterValue(self.XFIELD))
|
|
|
|
yfieldindex = vlayer.fieldNameIndex(self.getParameterValue(self.YFIELD))
|
2013-07-21 21:53:27 +02:00
|
|
|
|
2013-07-17 21:40:59 +02:00
|
|
|
crsId = self.getParameterValue(self.TARGET_CRS)
|
2015-01-22 14:38:47 +01:00
|
|
|
targetCrs = QgsCoordinateReferenceSystem()
|
|
|
|
targetCrs.createFromUserInput(crsId)
|
2013-07-17 21:40:59 +02:00
|
|
|
self.crs = targetCrs
|
2013-02-07 01:09:39 +01:00
|
|
|
|
|
|
|
outFeat = QgsFeature()
|
2013-09-12 13:19:00 +02:00
|
|
|
features = vector.features(vlayer)
|
2016-02-17 09:36:59 +02:00
|
|
|
total = 100.0 / len(features)
|
|
|
|
for current, feature in enumerate(features):
|
|
|
|
progress.setPercentage(int(current * total))
|
2013-02-04 23:29:55 +01:00
|
|
|
attrs = feature.attributes()
|
|
|
|
try:
|
2013-10-01 20:52:22 +03:00
|
|
|
x = float(attrs[xfieldindex])
|
|
|
|
y = float(attrs[yfieldindex])
|
2013-02-04 23:29:55 +01:00
|
|
|
except:
|
|
|
|
continue
|
|
|
|
pt = QgsPoint(x, y)
|
|
|
|
outFeat.setGeometry(QgsGeometry.fromPoint(pt))
|
2013-02-07 01:09:39 +01:00
|
|
|
outFeat.setAttributes(attrs)
|
2013-02-04 23:29:55 +01:00
|
|
|
writer.addFeature(outFeat)
|
2013-02-07 01:09:39 +01:00
|
|
|
|
2013-02-04 23:29:55 +01:00
|
|
|
del writer
|