mirror of
https://github.com/qgis/QGIS.git
synced 2025-06-17 00:03:34 -04:00
have specific icons, instead of generic qgis icon We consider these 'top level' algorithms, and using the standard algorithm icon should help reflect this and differentiate them from 3rd party algorithms.
105 lines
4.4 KiB
Python
105 lines
4.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
***************************************************************************
|
|
FixGeometry.py
|
|
-----------------------
|
|
Date : January 2017
|
|
Copyright : (C) 2017 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. *
|
|
* *
|
|
***************************************************************************
|
|
"""
|
|
|
|
__author__ = 'Alexander Bruy'
|
|
__date__ = 'January 2017'
|
|
__copyright__ = '(C) 2017, Alexander Bruy'
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
from qgis.core import (QgsWkbTypes,
|
|
QgsFeatureSink,
|
|
QgsGeometry,
|
|
QgsApplication,
|
|
QgsMessageLog,
|
|
QgsProcessingUtils)
|
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
|
from processing.core.parameters import ParameterVector
|
|
from processing.core.outputs import OutputVector
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
|
from processing.tools import dataobjects
|
|
|
|
|
|
class FixGeometry(QgisAlgorithm):
|
|
|
|
INPUT = 'INPUT'
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
def tags(self):
|
|
return self.tr('repair,invalid,geometry').split(',')
|
|
|
|
def group(self):
|
|
return self.tr('Vector geometry tools')
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.addParameter(ParameterVector(self.INPUT,
|
|
self.tr('Input Layer'),
|
|
[dataobjects.TYPE_VECTOR_POLYGON, dataobjects.TYPE_VECTOR_LINE]))
|
|
self.addOutput(OutputVector(self.OUTPUT,
|
|
self.tr('Layer with fixed geometries')))
|
|
|
|
def name(self):
|
|
return 'fixgeometries'
|
|
|
|
def displayName(self):
|
|
return self.tr('Fix geometries')
|
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
|
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
|
|
|
|
writer = self.getOutputFromName(
|
|
self.OUTPUT).getVectorWriter(layer.fields(), QgsWkbTypes.multiType(layer.wkbType()), layer.crs(), context)
|
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
|
if QgsProcessingUtils.featureCount(layer, context) == 0:
|
|
raise GeoAlgorithmExecutionException(self.tr('There are no features in the input layer'))
|
|
|
|
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
|
|
for current, inputFeature in enumerate(features):
|
|
outputFeature = inputFeature
|
|
if inputFeature.geometry():
|
|
outputGeometry = inputFeature.geometry().makeValid()
|
|
if not outputGeometry:
|
|
QgsMessageLog.logMessage('makeValid failed for feature {}'.format(inputFeature.id()), self.tr('Processing'), QgsMessageLog.WARNING)
|
|
|
|
if outputGeometry.wkbType() == QgsWkbTypes.Unknown or QgsWkbTypes.flatType(outputGeometry.geometry().wkbType()) == QgsWkbTypes.GeometryCollection:
|
|
tmpGeometries = outputGeometry.asGeometryCollection()
|
|
for g in tmpGeometries:
|
|
if g.type() == inputFeature.geometry().type():
|
|
try:
|
|
g.convertToMultiType()
|
|
outputFeature.setGeometry(QgsGeometry(g))
|
|
writer.addFeature(outputFeature, QgsFeatureSink.FastInsert)
|
|
except:
|
|
pass
|
|
feedback.setProgress(int(current * total))
|
|
continue
|
|
|
|
outputGeometry.convertToMultiType()
|
|
outputFeature.setGeometry(outputGeometry)
|
|
|
|
writer.addFeature(outputFeature, QgsFeatureSink.FastInsert)
|
|
feedback.setProgress(int(current * total))
|
|
|
|
del writer
|