2014-09-19 15:40:26 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
VectorSplit.py
|
|
|
|
---------------------
|
|
|
|
Date : September 2014
|
|
|
|
Copyright : (C) 2014 by Alexander Bruy
|
|
|
|
Email : alexander dot bruy 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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-09-21 18:24:26 +02:00
|
|
|
from builtins import str
|
2014-09-19 15:40:26 +03:00
|
|
|
|
|
|
|
__author__ = 'Alexander Bruy'
|
|
|
|
__date__ = 'September 2014'
|
|
|
|
__copyright__ = '(C) 2014, Alexander Bruy'
|
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2014-12-22 12:52:43 +01:00
|
|
|
import os
|
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
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2017-04-25 17:53:32 +10:00
|
|
|
from qgis.core import QgsProcessingUtils
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
from processing.algs.qgis import QgisAlgorithm
|
2014-09-19 15:40:26 +03:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterTableField
|
|
|
|
from processing.core.outputs import OutputDirectory
|
2017-05-02 14:47:58 +10:00
|
|
|
from processing.tools import vector
|
2014-12-22 12:52:43 +01:00
|
|
|
from processing.tools.system import mkdir
|
2014-09-19 15:40:26 +03:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class VectorSplit(QgisAlgorithm):
|
2014-09-19 15:40:26 +03:00
|
|
|
|
|
|
|
INPUT = 'INPUT'
|
|
|
|
FIELD = 'FIELD'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2016-01-25 15:42:11 +02:00
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'split_layer.png'))
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector general tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2014-09-19 15:40:26 +03:00
|
|
|
self.addParameter(ParameterVector(self.INPUT,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input layer')))
|
2014-09-19 15:40:26 +03:00
|
|
|
self.addParameter(ParameterTableField(self.FIELD,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Unique ID field'), self.INPUT))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addOutput(OutputDirectory(self.OUTPUT, self.tr('Output directory')))
|
2014-09-19 15:40:26 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'splitvectorlayer'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Split vector layer')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
|
2014-09-19 15:40:26 +03:00
|
|
|
fieldName = self.getParameterValue(self.FIELD)
|
|
|
|
directory = self.getOutputValue(self.OUTPUT)
|
|
|
|
|
2014-12-22 12:52:43 +01:00
|
|
|
mkdir(directory)
|
2014-09-19 15:40:26 +03:00
|
|
|
|
2016-09-22 12:09:13 +02:00
|
|
|
fieldIndex = layer.fields().lookupField(fieldName)
|
2017-04-26 09:39:40 +10:00
|
|
|
uniqueValues = QgsProcessingUtils.uniqueValues(layer, fieldIndex, context)
|
2014-12-22 12:52:43 +01:00
|
|
|
baseName = os.path.join(directory, '{0}_{1}'.format(layer.name(), fieldName))
|
2014-09-19 15:40:26 +03:00
|
|
|
|
2016-08-04 07:37:22 +10:00
|
|
|
fields = layer.fields()
|
2016-08-04 07:33:49 +10:00
|
|
|
crs = layer.crs()
|
2014-09-19 15:40:26 +03:00
|
|
|
geomType = layer.wkbType()
|
|
|
|
|
|
|
|
total = 100.0 / len(uniqueValues)
|
|
|
|
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, i in enumerate(uniqueValues):
|
2016-09-21 18:24:26 +02:00
|
|
|
fName = u'{0}_{1}.shp'.format(baseName, str(i).strip())
|
2014-09-19 15:40:26 +03:00
|
|
|
|
2017-05-09 15:03:51 +10:00
|
|
|
writer, dest = QgsProcessingUtils.createFeatureSink(fName, None, fields, geomType, crs, context)
|
2017-04-25 17:59:35 +10:00
|
|
|
for f in QgsProcessingUtils.getFeatures(layer, context):
|
2014-09-19 15:40:26 +03:00
|
|
|
if f[fieldName] == i:
|
|
|
|
writer.addFeature(f)
|
|
|
|
del writer
|
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|