174 lines
7.0 KiB
Python
Raw Normal View History

2014-10-03 11:01:12 +03:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
Gridify.py
---------------------
Date : May 2010
Copyright : (C) 2010 by Michael Minn
Email : pyqgis at michaelminn 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__ = 'Michael Minn'
__date__ = 'May 2010'
__copyright__ = '(C) 2010, Michael Minn'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsFields,
QgsField,
QgsFeatureRequest,
QgsFeatureSink,
2017-06-09 15:47:40 +10:00
QgsProcessingUtils,
QgsProcessingParameterMultipleLayers,
QgsProcessingParameterDefinition,
QgsProcessingParameterFeatureSink,
QgsProcessingOutputVectorLayer,
QgsMapLayer)
2017-06-06 13:41:42 +10:00
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterMultipleInput
2014-10-03 11:01:12 +03:00
from processing.core.outputs import OutputVector
from processing.tools import dataobjects
2014-10-03 11:01:12 +03:00
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
class Merge(QgisAlgorithm):
LAYERS = 'LAYERS'
2014-10-03 11:01:12 +03:00
OUTPUT = 'OUTPUT'
def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'merge_shapes.png'))
def group(self):
return self.tr('Vector general tools')
def __init__(self):
super().__init__()
2017-06-09 15:47:40 +10:00
self.addParameter(QgsProcessingParameterMultipleLayers(self.LAYERS,
self.tr('Layers to merge'),
QgsProcessingParameterDefinition.TypeVectorAny))
2014-10-03 11:01:12 +03:00
2017-06-09 15:47:40 +10:00
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Merged')))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Merged')))
2014-10-03 11:01:12 +03:00
self.input_layers = []
self.fields = None
self.add_layer_field = None
self.add_path_field = None
self.sink = None
self.dest_id = None
self.dest_crs = None
def name(self):
return 'mergevectorlayers'
def displayName(self):
return self.tr('Merge vector layers')
def prepareAlgorithm(self, parameters, context, feedback):
self.input_layers = self.parameterAsLayerList(parameters, self.LAYERS, context)
layers = []
self.fields = QgsFields()
totalFeatureCount = 0
for layer in self.input_layers:
2017-06-09 15:47:40 +10:00
if layer.type() != QgsMapLayer.VectorLayer:
raise GeoAlgorithmExecutionException(
self.tr('All layers must be vector layers!'))
2014-10-03 11:01:12 +03:00
if (len(layers) > 0):
if (layer.wkbType() != layers[0].wkbType()):
raise GeoAlgorithmExecutionException(
self.tr('All layers must have same geometry type!'))
layers.append(layer)
totalFeatureCount += layer.featureCount()
for sindex, sfield in enumerate(layer.fields()):
2014-10-03 11:01:12 +03:00
found = None
for dfield in self.fields:
if (dfield.name().upper() == sfield.name().upper()):
2014-10-03 11:01:12 +03:00
found = dfield
if (dfield.type() != sfield.type()):
raise GeoAlgorithmExecutionException(
self.tr('{} field in layer {} has different '
2017-05-02 13:39:22 +02:00
'data type than in other layers.'.format(sfield.name(), layerSource)))
2014-10-03 11:01:12 +03:00
if not found:
self.fields.append(sfield)
2014-10-03 11:01:12 +03:00
self.add_layer_field = False
if self.fields.lookupField('layer') < 0:
self.fields.append(QgsField('layer', QVariant.String, '', 100))
self.add_layer_field = True
self.add_path_field = False
if self.fields.lookupField('path') < 0:
self.fields.append(QgsField('path', QVariant.String, '', 200))
self.add_path_field = True
total = 100.0 / totalFeatureCount if totalFeatureCount else 1
self.dest_crs = layers[0].crs()
(self.sink, self.dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
self.fields, layers[0].wkbType(), self.dest_crs)
return True
def processAlgorithm(self, context, feedback):
2014-10-03 11:01:12 +03:00
featureCount = 0
for layer in self.layers:
for feature in layer.getFeatures(QgsFeatureRequest().setDestinationCrs(self.dest_crs)):
2017-06-09 15:47:40 +10:00
if feedback.isCanceled():
break
sattributes = feature.attributes()
dattributes = []
for dindex, dfield in enumerate(self.fields):
if self.add_layer_field and dfield.name() == 'layer':
dattributes.append(layer.name())
continue
if self.add_path_field and dfield.name() == 'path':
dattributes.append(layer.publicSource())
continue
2016-06-27 14:17:51 +03:00
if (dfield.type() == QVariant.Int, QVariant.UInt, QVariant.LongLong, QVariant.ULongLong):
dattribute = 0
elif (dfield.type() == QVariant.Double):
dattribute = 0.0
2014-10-03 11:01:12 +03:00
else:
dattribute = ''
for sindex, sfield in enumerate(layer.fields()):
if (sfield.name().upper() == dfield.name().upper()):
if (sfield.type() != dfield.type()):
raise GeoAlgorithmExecutionException(
self.tr('Attribute type mismatch'))
dattribute = sattributes[sindex]
break
dattributes.append(dattribute)
feature.setAttributes(dattributes)
self.sink.addFeature(feature, QgsFeatureSink.FastInsert)
featureCount += 1
feedback.setProgress(int(featureCount * self.total))
return True
2014-10-03 11:01:12 +03:00
def postProcessAlgorithm(self, context, feedback):
return {self.OUTPUT: self.dest_id}