|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + VectorSplit.py |
| 6 | + --------------------- |
| 7 | + Date : September 2014 |
| 8 | + Copyright : (C) 2014 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__ = 'September 2014' |
| 22 | +__copyright__ = '(C) 2014, Alexander Bruy' |
| 23 | + |
| 24 | +# This will get replaced with a git SHA1 when you do a git archive |
| 25 | + |
| 26 | +__revision__ = '$Format:%H$' |
| 27 | + |
| 28 | +from PyQt4.QtCore import * |
| 29 | +from qgis.core import * |
| 30 | +from processing.core.GeoAlgorithm import GeoAlgorithm |
| 31 | +from processing.core.parameters import ParameterVector |
| 32 | +from processing.core.parameters import ParameterTableField |
| 33 | +from processing.core.outputs import OutputDirectory |
| 34 | +from processing.tools import dataobjects, vector |
| 35 | + |
| 36 | + |
| 37 | +class VectorSplit(GeoAlgorithm): |
| 38 | + |
| 39 | + INPUT = 'INPUT' |
| 40 | + FIELD = 'FIELD' |
| 41 | + OUTPUT = 'OUTPUT' |
| 42 | + |
| 43 | + def defineCharacteristics(self): |
| 44 | + self.name = 'Split vector layer' |
| 45 | + self.group = 'Vector general tools' |
| 46 | + self.addParameter(ParameterVector(self.INPUT, |
| 47 | + 'Input layer', [ParameterVector.VECTOR_TYPE_ANY])) |
| 48 | + self.addParameter(ParameterTableField(self.FIELD, |
| 49 | + 'Unique ID field', self.INPUT)) |
| 50 | + |
| 51 | + self.addOutput(OutputDirectory(self.OUTPUT, 'Output directory')) |
| 52 | + |
| 53 | + def processAlgorithm(self, progress): |
| 54 | + layer = dataobjects.getObjectFromUri( |
| 55 | + self.getParameterValue(self.INPUT)) |
| 56 | + fieldName = self.getParameterValue(self.FIELD) |
| 57 | + |
| 58 | + directory = self.getOutputValue(self.OUTPUT) |
| 59 | + |
| 60 | + if directory.find('\\') != -1: |
| 61 | + directory.replace('\\', '/') |
| 62 | + |
| 63 | + if not directory.endswith("/"): |
| 64 | + directory += '/' |
| 65 | + |
| 66 | + fieldIndex = layer.fieldNameIndex(fieldName) |
| 67 | + uniqueValues = vector.uniqueValues(layer, fieldIndex) |
| 68 | + baseName = '{0}{1}_{2}'.format(directory, layer.name(), fieldName) |
| 69 | + |
| 70 | + fields = layer.pendingFields() |
| 71 | + crs = layer.dataProvider().crs() |
| 72 | + geomType = layer.wkbType() |
| 73 | + |
| 74 | + total = 100.0 / len(uniqueValues) |
| 75 | + features = vector.features(layer) |
| 76 | + |
| 77 | + for count, i in enumerate(uniqueValues): |
| 78 | + fName = '{0}_{1}.shp'.format(baseName, unicode(i).strip()) |
| 79 | + |
| 80 | + writer = vector.VectorWriter(fName, None, fields, geomType, crs) |
| 81 | + for f in features: |
| 82 | + if f[fieldName] == i: |
| 83 | + writer.addFeature(f) |
| 84 | + del writer |
| 85 | + |
| 86 | + progress.setPercentage(int(count * total)) |
0 commit comments