2015-05-22 13:18:00 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
CheckValidity.py
|
|
|
|
---------------------
|
|
|
|
Date : May 2015
|
|
|
|
Copyright : (C) 2015 by Arnaud Morvan
|
|
|
|
Email : arnaud dot morvan at camptocamp 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__ = 'Arnaud Morvan'
|
|
|
|
__date__ = 'May 2015'
|
|
|
|
__copyright__ = '(C) 2015, Arnaud Morvan'
|
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive323
|
|
|
|
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2017-03-03 20:39:17 +01:00
|
|
|
from qgis.PyQt.QtCore import QVariant
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-05-02 21:15:03 +10:00
|
|
|
from qgis.core import (QgsSettings,
|
|
|
|
QgsGeometry,
|
|
|
|
QgsFeature,
|
|
|
|
QgsField,
|
2017-06-26 10:53:12 +07:00
|
|
|
QgsFeatureRequest,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-05-02 21:15:03 +10:00
|
|
|
QgsWkbTypes,
|
|
|
|
QgsProcessingUtils,
|
2017-06-07 07:47:07 +10:00
|
|
|
QgsFields,
|
2017-06-26 10:53:12 +07:00
|
|
|
QgsProcessingFeatureSource,
|
2017-06-07 07:47:07 +10:00
|
|
|
QgsProcessingParameterFeatureSource,
|
|
|
|
QgsProcessingParameterEnum,
|
|
|
|
QgsProcessingParameterFeatureSink,
|
|
|
|
QgsProcessingOutputVectorLayer,
|
2017-06-11 19:51:46 +10:00
|
|
|
QgsProcessingParameterDefinition,
|
|
|
|
QgsProcessingOutputNumber
|
2017-06-07 07:47:07 +10:00
|
|
|
)
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2015-05-22 13:18:00 +02:00
|
|
|
|
|
|
|
settings_method_key = "/qgis/digitizing/validate_geometries"
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
2015-05-22 13:18:00 +02:00
|
|
|
|
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class CheckValidity(QgisAlgorithm):
|
2015-05-22 13:18:00 +02:00
|
|
|
|
|
|
|
INPUT_LAYER = 'INPUT_LAYER'
|
|
|
|
METHOD = 'METHOD'
|
|
|
|
VALID_OUTPUT = 'VALID_OUTPUT'
|
2017-06-11 19:51:46 +10:00
|
|
|
VALID_COUNT = 'VALID_COUNT'
|
2015-05-22 13:18:00 +02:00
|
|
|
INVALID_OUTPUT = 'INVALID_OUTPUT'
|
2017-06-11 19:51:46 +10:00
|
|
|
INVALID_COUNT = 'INVALID_COUNT'
|
2015-05-22 13:18:00 +02:00
|
|
|
ERROR_OUTPUT = 'ERROR_OUTPUT'
|
2017-06-11 19:51:46 +10:00
|
|
|
ERROR_COUNT = 'ERROR_COUNT'
|
2015-05-22 13:18:00 +02:00
|
|
|
|
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', 'check_geometry.png'))
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector geometry tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2015-08-31 16:59:11 +02:00
|
|
|
self.methods = [self.tr('The one selected in digitizing settings'),
|
|
|
|
'QGIS',
|
|
|
|
'GEOS']
|
|
|
|
|
2017-06-07 07:47:07 +10:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT_LAYER,
|
|
|
|
self.tr('Input layer')))
|
|
|
|
self.addParameter(QgsProcessingParameterEnum(self.METHOD,
|
|
|
|
self.tr('Method'), self.methods))
|
2015-05-22 13:18:00 +02:00
|
|
|
|
2017-06-07 07:47:07 +10:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSink(self.VALID_OUTPUT, self.tr('Valid output'), QgsProcessingParameterDefinition.TypeVectorAny, '', True))
|
|
|
|
self.addOutput(QgsProcessingOutputVectorLayer(self.VALID_OUTPUT, self.tr('Valid output')))
|
2017-06-11 19:51:46 +10:00
|
|
|
self.addOutput(QgsProcessingOutputNumber(self.VALID_COUNT, self.tr('Count of valid features')))
|
|
|
|
|
2017-06-07 07:47:07 +10:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSink(self.INVALID_OUTPUT, self.tr('Invalid output'), QgsProcessingParameterDefinition.TypeVectorAny, '', True))
|
|
|
|
self.addOutput(QgsProcessingOutputVectorLayer(self.INVALID_OUTPUT, self.tr('Invalid output')))
|
2017-06-11 19:51:46 +10:00
|
|
|
self.addOutput(QgsProcessingOutputNumber(self.INVALID_COUNT, self.tr('Count of invalid features')))
|
|
|
|
|
2017-06-07 07:47:07 +10:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSink(self.ERROR_OUTPUT, self.tr('Error output'), QgsProcessingParameterDefinition.TypeVectorAny, '', True))
|
|
|
|
self.addOutput(QgsProcessingOutputVectorLayer(self.ERROR_OUTPUT, self.tr('Error output')))
|
2017-06-11 19:51:46 +10:00
|
|
|
self.addOutput(QgsProcessingOutputNumber(self.ERROR_COUNT, self.tr('Count of errors')))
|
2015-05-22 13:18:00 +02:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
self.method = None
|
|
|
|
self.source = None
|
|
|
|
self.valid_output_sink = None
|
|
|
|
self.valid_output_dest_id = None
|
|
|
|
self.valid_count = 0
|
|
|
|
self.invalid_output_sink = None
|
|
|
|
self.invalid_output_dest_id = None
|
|
|
|
self.invalid_count = 0
|
|
|
|
self.error_output_sink = None
|
|
|
|
self.error_output_dest_id = None
|
|
|
|
self.error_count = 0
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'checkvalidity'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Check validity')
|
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
def prepareAlgorithm(self, parameters, context, feedback):
|
2017-06-07 07:47:07 +10:00
|
|
|
method_param = self.parameterAsEnum(parameters, self.METHOD, context)
|
|
|
|
if method_param == 0:
|
|
|
|
settings = QgsSettings()
|
2017-06-28 19:55:08 +10:00
|
|
|
self.method = int(settings.value(settings_method_key, 0)) - 1
|
|
|
|
if self.method < 0:
|
|
|
|
self.method = 0
|
2017-06-07 07:47:07 +10:00
|
|
|
else:
|
2017-06-28 19:55:08 +10:00
|
|
|
self.method = method_param - 1
|
2017-06-07 07:47:07 +10:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
self.source = self.parameterAsSource(parameters, self.INPUT_LAYER, context)
|
2017-06-07 07:47:07 +10:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
(self.valid_output_sink, self.valid_output_dest_id) = self.parameterAsSink(parameters, self.VALID_OUTPUT,
|
|
|
|
context,
|
|
|
|
self.source.fields(),
|
|
|
|
self.source.wkbType(),
|
|
|
|
self.source.sourceCrs())
|
2015-05-22 13:18:00 +02:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
invalid_fields = self.source.fields()
|
2017-06-07 07:47:07 +10:00
|
|
|
invalid_fields.append(QgsField('_errors', QVariant.String, 'string', 255))
|
2017-06-28 19:55:08 +10:00
|
|
|
(self.invalid_output_sink, self.invalid_output_dest_id) = self.parameterAsSink(parameters, self.INVALID_OUTPUT,
|
|
|
|
context,
|
|
|
|
invalid_fields,
|
|
|
|
self.source.wkbType(),
|
|
|
|
self.source.sourceCrs())
|
2015-05-22 13:18:00 +02:00
|
|
|
|
2017-05-02 21:15:03 +10:00
|
|
|
error_fields = QgsFields()
|
2017-06-07 07:47:07 +10:00
|
|
|
error_fields.append(QgsField('message', QVariant.String, 'string', 255))
|
2017-06-28 19:55:08 +10:00
|
|
|
(self.error_output_sink, self.error_output_dest_id) = self.parameterAsSink(parameters, self.ERROR_OUTPUT, context,
|
|
|
|
error_fields, QgsWkbTypes.Point,
|
|
|
|
self.source.sourceCrs())
|
|
|
|
return True
|
|
|
|
|
|
|
|
def processAlgorithm(self, context, feedback):
|
|
|
|
features = self.source.getFeatures(QgsFeatureRequest(), QgsProcessingFeatureSource.FlagSkipGeometryValidityChecks)
|
|
|
|
total = 100.0 / self.source.featureCount() if self.source.featureCount() else 0
|
2015-05-22 13:18:00 +02:00
|
|
|
for current, inFeat in enumerate(features):
|
2017-06-07 07:47:07 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
2016-08-01 16:25:46 +10:00
|
|
|
geom = inFeat.geometry()
|
2015-05-22 13:18:00 +02:00
|
|
|
attrs = inFeat.attributes()
|
|
|
|
|
|
|
|
valid = True
|
2017-01-30 22:22:09 +10:00
|
|
|
if not geom.isNull() and not geom.isEmpty():
|
2017-06-28 19:55:08 +10:00
|
|
|
errors = list(geom.validateGeometry(self.method))
|
2015-05-22 13:18:00 +02:00
|
|
|
if errors:
|
|
|
|
# QGIS method return a summary at the end
|
2017-06-28 19:55:08 +10:00
|
|
|
if self.method == 1:
|
2015-05-22 13:18:00 +02:00
|
|
|
errors.pop()
|
|
|
|
valid = False
|
|
|
|
reasons = []
|
|
|
|
for error in errors:
|
|
|
|
errFeat = QgsFeature()
|
|
|
|
error_geom = QgsGeometry.fromPoint(error.where())
|
|
|
|
errFeat.setGeometry(error_geom)
|
|
|
|
errFeat.setAttributes([error.what()])
|
2017-06-28 19:55:08 +10:00
|
|
|
if self.error_output_sink:
|
|
|
|
self.error_output_sink.addFeature(errFeat, QgsFeatureSink.FastInsert)
|
|
|
|
self.error_count += 1
|
2015-05-22 13:18:00 +02:00
|
|
|
|
|
|
|
reasons.append(error.what())
|
|
|
|
|
|
|
|
reason = "\n".join(reasons)
|
|
|
|
if len(reason) > 255:
|
|
|
|
reason = reason[:252] + '...'
|
|
|
|
attrs.append(reason)
|
|
|
|
|
|
|
|
outFeat = QgsFeature()
|
|
|
|
outFeat.setGeometry(geom)
|
|
|
|
outFeat.setAttributes(attrs)
|
|
|
|
|
|
|
|
if valid:
|
2017-06-28 19:55:08 +10:00
|
|
|
if self.valid_output_sink:
|
|
|
|
self.valid_output_sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
|
|
|
self.valid_count += 1
|
2015-05-22 13:18:00 +02:00
|
|
|
|
|
|
|
else:
|
2017-06-28 19:55:08 +10:00
|
|
|
if self.invalid_output_sink:
|
|
|
|
self.invalid_output_sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
|
|
|
self.invalid_count += 1
|
2015-05-22 13:18:00 +02:00
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2017-06-28 19:55:08 +10:00
|
|
|
return True
|
2015-05-22 13:18:00 +02:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
def postProcessAlgorithm(self, context, feedback):
|
2017-06-11 19:51:46 +10:00
|
|
|
results = {
|
2017-06-28 19:55:08 +10:00
|
|
|
self.VALID_COUNT: self.valid_count,
|
|
|
|
self.INVALID_COUNT: self.invalid_count,
|
|
|
|
self.ERROR_COUNT: self.error_count
|
2017-06-11 19:51:46 +10:00
|
|
|
}
|
2017-06-28 19:55:08 +10:00
|
|
|
if self.valid_output_sink:
|
|
|
|
results[self.VALID_OUTPUT] = self.valid_output_dest_id
|
|
|
|
if self.invalid_output_sink:
|
|
|
|
results[self.INVALID_OUTPUT] = self.invalid_output_dest_id
|
|
|
|
if self.error_output_sink:
|
|
|
|
results[self.ERROR_OUTPUT] = self.error_output_dest_id
|
2017-06-07 07:47:07 +10:00
|
|
|
return results
|