def processAlgorithm(self, context, feedback): layer = dataobjects.getLayerFromString( self.getParameterValue(self.INPUT)) fieldName = self.getParameterValue(self.FIELD) directory = self.getOutputValue(self.OUTPUT) mkdir(directory) fieldIndex = layer.fields().lookupField(fieldName) uniqueValues = QgsProcessingUtils.uniqueValues(layer, fieldIndex, context) baseName = os.path.join(directory, '{0}_{1}'.format(layer.name(), fieldName)) fields = layer.fields() crs = layer.crs() geomType = layer.wkbType() total = 100.0 / len(uniqueValues) for current, i in enumerate(uniqueValues): fName = u'{0}_{1}.shp'.format(baseName, str(i).strip()) writer, dest, layer = vector.createVectorWriter( fName, None, fields, geomType, crs, context) for f in QgsProcessingUtils.getFeatures(layer, context): if f[fieldName] == i: writer.addFeature(f) del writer feedback.setProgress(int(current * total))
def getVectorWriter(self, fields, geomType, crs, context, options=None): """Returns a suitable writer to which features can be added as a result of the algorithm. Use this to transparently handle output values instead of creating your own method. Executing this method might modify the object, adding additional information to it, so the writer can be later accessed and processed within QGIS. It should be called just once, since a new call might result in previous data being replaced, thus rendering a previously obtained writer useless. @param fields a list of QgsField @param geomType a suitable geometry type, as it would be passed to a QgsVectorFileWriter constructor @param crs the crs of the layer to create @return writer instance of the vector writer class :param context: """ if self.encoding is None: settings = QgsSettings() self.encoding = settings.value('/Processing/encoding', 'System', str) w, w_dest, w_layer = createVectorWriter(self.value, self.encoding, fields, geomType, crs, context, options) self.layer = w_layer self.value = w_dest return w
##Centroids=name ##Geometry=group ##INPUT_LAYER=vector ##OUTPUT_LAYER=output vector from qgis.core import QgsWkbTypes, QgsGeometry, QgsProcessingUtils from processing.tools.vector import createVectorWriter from processing.tools import dataobjects layer = dataobjects.getLayerFromString(INPUT_LAYER) fields = layer.fields() writer, writer_dest, writer_layer = createVectorWriter(OUTPUT_LAYER, 'utf-8', fields, QgsWkbTypes.Point, layer.crs(), context) features = QgsProcessingUtils.getFeatures(layer, context) count = QgsProcessingUtils.featureCount(layer, context) if count == 0: raise GeoAlgorithmExecutionException('Input layer contains no features.') total = 100.0 / count for count, f in enumerate(features): outputFeature = f if f.hasGeometry(): outputGeometry = f.geometry().centroid() outputFeature.setGeometry(outputGeometry)
##Vector table tools=group ##input=vector ##class_field=field input ##value_field=field input ##N_unique_values=output vector from qgis.PyQt.QtCore import QVariant from qgis.core import QgsFeature, QgsField, QgsProcessingUtils from processing.tools.vector import createVectorWriter from processing.tools import dataobjects layer = dataobjects.getLayerFromString(input) fields = layer.fields() fields.append(QgsField('UNIQ_COUNT', QVariant.Int)) writer, writer_dest, writer_layer = createVectorWriter(N_unique_values, None, fields, layer.wkbType(), layer.crs(), context) class_field_index = layer.fields().lookupField(class_field) value_field_index = layer.fields().lookupField(value_field) outFeat = QgsFeature() classes = {} feats = QgsProcessingUtils.getFeatures(layer, context) nFeat = QgsProcessingUtils.featureCount(layer, context) for n, inFeat in enumerate(feats): feedback.setProgress(int(100 * n / nFeat)) attrs = inFeat.attributes() clazz = attrs[class_field_index] value = attrs[value_field_index] if clazz not in classes: