|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + ReprojectLayer.py |
| 6 | + --------------------- |
| 7 | + Date : October 2012 |
| 8 | + Copyright : (C) 2012 by Alexander Bruy |
| 9 | + Email : alexander dot bruy at gmail dot com |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | +__author__ = 'Alexander Bruy' |
| 21 | +__date__ = 'October 2012' |
| 22 | +__copyright__ = '(C) 2012, Alexander Bruy' |
| 23 | +# This will get replaced with a git SHA1 when you do a git archive |
| 24 | +__revision__ = '$Format:%H$' |
| 25 | + |
| 26 | +import os.path |
| 27 | + |
| 28 | +from PyQt4 import QtGui |
| 29 | +from PyQt4.QtCore import * |
| 30 | + |
| 31 | +from qgis.core import * |
| 32 | + |
| 33 | +from sextante.core.GeoAlgorithm import GeoAlgorithm |
| 34 | +from sextante.core.QGisLayers import QGisLayers |
| 35 | +from sextante.core.SextanteLog import SextanteLog |
| 36 | + |
| 37 | +from sextante.parameters.ParameterVector import ParameterVector |
| 38 | +from sextante.parameters.ParameterCrs import ParameterCrs |
| 39 | + |
| 40 | +from sextante.outputs.OutputVector import OutputVector |
| 41 | + |
| 42 | +class ReprojectLayer(GeoAlgorithm): |
| 43 | + |
| 44 | + INPUT = "INPUT" |
| 45 | + TARGET_CRS = "TARGET_CRS" |
| 46 | + OUTPUT = "OUTPUT" |
| 47 | + |
| 48 | + def getIcon(self): |
| 49 | + return QtGui.QIcon(os.path.dirname(__file__) + "/icons/reproject.png") |
| 50 | + |
| 51 | + def defineCharacteristics(self): |
| 52 | + self.name = "Reproject layer" |
| 53 | + self.group = "Data management tools" |
| 54 | + |
| 55 | + self.addParameter(ParameterVector(self.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY)) |
| 56 | + self.addParameter(ParameterCrs(self.TARGET_CRS, "Target CRS", "4326")) |
| 57 | + |
| 58 | + self.addOutput(OutputVector(self.OUTPUT, "Reprojected layer")) |
| 59 | + |
| 60 | + def processAlgorithm(self, progress): |
| 61 | + layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT)) |
| 62 | + crsId = self.getParameterValue(self.TARGET_CRS) |
| 63 | + targetCrs = QgsCoordinateReferenceSystem(int(crsId)) |
| 64 | + |
| 65 | + output = self.getOutputValue(self.OUTPUT) |
| 66 | + |
| 67 | + writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields(), |
| 68 | + layer.wkbType(), targetCrs) |
| 69 | + |
| 70 | + layer.select(layer.pendingAllAttributesList()) |
| 71 | + |
| 72 | + current = 0 |
| 73 | + total = 100.0 / float(layer.featureCount()) |
| 74 | + |
| 75 | + layerCrs = layer.crs() |
| 76 | + crsTransform = QgsCoordinateTransform(layerCrs, targetCrs) |
| 77 | + |
| 78 | + f = QgsFeature() |
| 79 | + outFeat = QgsFeature() |
| 80 | + while layer.nextFeature(f): |
| 81 | + geom = f.geometry() |
| 82 | + geom.transform(crsTransform) |
| 83 | + outFeat.setGeometry(geom) |
| 84 | + outFeat.setAttributeMap(f.attributeMap()) |
| 85 | + writer.addFeature(outFeat) |
| 86 | + |
| 87 | + current += 1 |
| 88 | + progress.setPercentage(int(current * total)) |
| 89 | + |
| 90 | + del writer |
0 commit comments