2016-08-10 16:32:31 +10:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
Boundary.py
|
|
|
|
--------------
|
|
|
|
Date : July 2016
|
|
|
|
Copyright : (C) 2016 by Nyall Dawson
|
|
|
|
Email : nyall dot dawson 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__ = 'Nyall Dawson'
|
|
|
|
__date__ = 'July 2016'
|
|
|
|
__copyright__ = '(C) 2016, Nyall Dawson'
|
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive323
|
|
|
|
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
2017-05-22 15:46:50 +10:00
|
|
|
from qgis.core import (QgsGeometry,
|
|
|
|
QgsWkbTypes,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-05-22 15:46:50 +10:00
|
|
|
QgsProcessingUtils,
|
2017-05-22 16:14:58 +10:00
|
|
|
QgsProcessingParameterDefinition,
|
2017-06-05 11:15:17 +10:00
|
|
|
QgsProcessingParameterFeatureSource,
|
2017-06-05 11:19:46 +10:00
|
|
|
QgsProcessingParameterFeatureSink,
|
2017-05-22 15:46:50 +10:00
|
|
|
QgsProcessingOutputVectorLayer)
|
2016-08-10 16:32:31 +10:00
|
|
|
|
|
|
|
from qgis.PyQt.QtGui import QIcon
|
|
|
|
|
2017-05-22 15:46:50 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2016-08-10 16:32:31 +10:00
|
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.outputs import OutputVector
|
2017-05-02 14:47:58 +10:00
|
|
|
from processing.tools import dataobjects
|
2016-08-10 16:32:31 +10:00
|
|
|
|
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class Boundary(QgisAlgorithm):
|
2016-08-10 16:32:31 +10:00
|
|
|
|
|
|
|
INPUT_LAYER = 'INPUT_LAYER'
|
|
|
|
OUTPUT_LAYER = 'OUTPUT_LAYER'
|
|
|
|
|
2017-05-22 15:46:50 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-06-05 11:15:17 +10:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT_LAYER, self.tr('Input layer'), [QgsProcessingParameterDefinition.TypeVectorLine, QgsProcessingParameterDefinition.TypeVectorPolygon]))
|
2017-06-05 11:19:46 +10:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT_LAYER, self.tr('Boundary')))
|
2017-05-22 15:46:50 +10:00
|
|
|
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT_LAYER, self.tr("Boundaries")))
|
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
self.source = None
|
|
|
|
self.sink = None
|
|
|
|
self.dest_id = None
|
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2016-08-10 16:32:31 +10:00
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'convex_hull.png'))
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector geometry tools')
|
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
def name(self):
|
2017-03-29 17:09:39 +10:00
|
|
|
return 'boundary'
|
2017-03-29 12:51:59 +10:00
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Boundary')
|
2016-08-10 16:32:31 +10:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
def prepareAlgorithm(self, parameters, context, feedback):
|
|
|
|
self.source = self.parameterAsSource(parameters, self.INPUT_LAYER, context)
|
2016-08-10 16:32:31 +10:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
input_wkb = self.source.wkbType()
|
|
|
|
output_wkb = None
|
2016-08-10 16:32:31 +10:00
|
|
|
if QgsWkbTypes.geometryType(input_wkb) == QgsWkbTypes.LineGeometry:
|
|
|
|
output_wkb = QgsWkbTypes.MultiPoint
|
|
|
|
elif QgsWkbTypes.geometryType(input_wkb) == QgsWkbTypes.PolygonGeometry:
|
|
|
|
output_wkb = QgsWkbTypes.MultiLineString
|
|
|
|
if QgsWkbTypes.hasZ(input_wkb):
|
|
|
|
output_wkb = QgsWkbTypes.addZ(output_wkb)
|
|
|
|
if QgsWkbTypes.hasM(input_wkb):
|
|
|
|
output_wkb = QgsWkbTypes.addM(output_wkb)
|
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
(self.sink, self.dest_id) = self.parameterAsSink(parameters, self.OUTPUT_LAYER, context,
|
|
|
|
self.source.fields(), output_wkb, self.source.sourceCrs())
|
|
|
|
return True
|
2016-08-10 16:32:31 +10:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
def processAlgorithm(self, context, feedback):
|
|
|
|
features = self.source.getFeatures()
|
|
|
|
total = 100.0 / self.source.featureCount() if self.source.featureCount() else 0
|
2016-08-10 16:32:31 +10:00
|
|
|
|
|
|
|
for current, input_feature in enumerate(features):
|
2017-06-05 11:14:34 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
2016-08-10 16:32:31 +10:00
|
|
|
output_feature = input_feature
|
|
|
|
input_geometry = input_feature.geometry()
|
|
|
|
if input_geometry:
|
|
|
|
output_geometry = QgsGeometry(input_geometry.geometry().boundary())
|
|
|
|
if not output_geometry:
|
|
|
|
raise GeoAlgorithmExecutionException(
|
|
|
|
self.tr('Error calculating boundary'))
|
|
|
|
|
|
|
|
output_feature.setGeometry(output_geometry)
|
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
self.sink.addFeature(output_feature, QgsFeatureSink.FastInsert)
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2017-06-28 19:55:08 +10:00
|
|
|
return True
|
2016-08-10 16:32:31 +10:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
def postProcessAlgorithm(self, context, feedback):
|
|
|
|
return {self.OUTPUT_LAYER: self.dest_id}
|