2013-03-28 23:55:34 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
Polygonize.py
|
|
|
|
---------------------
|
|
|
|
Date : March 2013
|
|
|
|
Copyright : (C) 2013 by Piotr Pociask
|
|
|
|
Email : ppociask at o2 dot pl
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* 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__ = 'Piotr Pociask'
|
|
|
|
__date__ = 'March 2013'
|
|
|
|
__copyright__ = '(C) 2013, Piotr Pociask'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2018-02-15 22:30:52 +01:00
|
|
|
from qgis.PyQt.QtCore import QCoreApplication
|
2017-07-27 14:56:39 +10:00
|
|
|
from qgis.core import (QgsFields,
|
2017-01-31 08:57:42 +10:00
|
|
|
QgsFeature,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-01-31 08:57:42 +10:00
|
|
|
QgsGeometry,
|
|
|
|
QgsWkbTypes,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsFeatureRequest,
|
2017-07-27 14:56:39 +10:00
|
|
|
QgsProcessing,
|
2018-04-27 12:31:56 +10:00
|
|
|
QgsProcessingException,
|
2017-07-27 14:56:39 +10:00
|
|
|
QgsProcessingParameterFeatureSource,
|
|
|
|
QgsProcessingParameterBoolean,
|
|
|
|
QgsProcessingParameterFeatureSink)
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-03-28 23:55:34 +01:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class Polygonize(QgisAlgorithm):
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
2017-07-27 14:56:39 +10:00
|
|
|
KEEP_FIELDS = 'KEEP_FIELDS'
|
2013-03-28 23:55:34 +01:00
|
|
|
|
2017-03-29 09:51:13 +10:00
|
|
|
def tags(self):
|
|
|
|
return self.tr('create,lines,polygons,convert').split(',')
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
2017-08-22 23:36:42 +10:00
|
|
|
return self.tr('Vector geometry')
|
2017-03-29 12:04:09 +10:00
|
|
|
|
2017-12-14 14:03:56 +02:00
|
|
|
def groupId(self):
|
|
|
|
return 'vectorgeometry'
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-07-10 16:31:14 +10:00
|
|
|
|
|
|
|
def initAlgorithm(self, config=None):
|
2017-07-27 14:56:39 +10:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
|
|
|
|
self.tr('Input layer'), types=[QgsProcessing.TypeVectorLine]))
|
|
|
|
self.addParameter(QgsProcessingParameterBoolean(self.KEEP_FIELDS,
|
|
|
|
self.tr('Keep table structure of line layer'), defaultValue=False, optional=True))
|
|
|
|
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Polygons from lines'), QgsProcessing.TypeVectorPolygon))
|
2016-02-17 09:36:59 +02:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'polygonize'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Polygonize')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-07-27 14:56:39 +10:00
|
|
|
source = self.parameterAsSource(parameters, self.INPUT, context)
|
2018-04-27 12:31:56 +10:00
|
|
|
if source is None:
|
|
|
|
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
|
|
|
|
|
2019-04-16 08:30:00 +02:00
|
|
|
if self.parameterAsBoolean(parameters, self.KEEP_FIELDS, context):
|
2017-07-27 14:56:39 +10:00
|
|
|
fields = source.fields()
|
2013-03-28 23:55:34 +01:00
|
|
|
else:
|
|
|
|
fields = QgsFields()
|
2017-07-27 14:56:39 +10:00
|
|
|
|
|
|
|
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
|
|
|
fields, QgsWkbTypes.Polygon, source.sourceCrs())
|
2018-04-27 12:52:02 +10:00
|
|
|
if sink is None:
|
|
|
|
raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT))
|
2017-07-27 14:56:39 +10:00
|
|
|
|
2013-03-28 23:55:34 +01:00
|
|
|
allLinesList = []
|
2017-07-27 14:56:39 +10:00
|
|
|
features = source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]))
|
2018-02-15 22:30:52 +01:00
|
|
|
feedback.pushInfo(QCoreApplication.translate('Polygonize', 'Processing lines…'))
|
2017-07-27 14:56:39 +10:00
|
|
|
total = (40.0 / source.featureCount()) if source.featureCount() else 1
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, inFeat in enumerate(features):
|
2017-07-27 14:56:39 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
|
2017-01-31 08:57:42 +10:00
|
|
|
if inFeat.geometry():
|
|
|
|
allLinesList.append(inFeat.geometry())
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2016-02-17 09:36:59 +02:00
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(40)
|
2016-02-17 09:36:59 +02:00
|
|
|
|
2018-02-15 22:30:52 +01:00
|
|
|
feedback.pushInfo(QCoreApplication.translate('Polygonize', 'Noding lines…'))
|
2017-01-31 08:57:42 +10:00
|
|
|
allLines = QgsGeometry.unaryUnion(allLinesList)
|
2017-07-27 14:56:39 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
return {}
|
2016-02-17 09:36:59 +02:00
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(45)
|
2018-02-15 22:30:52 +01:00
|
|
|
feedback.pushInfo(QCoreApplication.translate('Polygonize', 'Polygonizing…'))
|
2017-01-31 08:57:42 +10:00
|
|
|
polygons = QgsGeometry.polygonize([allLines])
|
|
|
|
if polygons.isEmpty():
|
2017-07-27 14:56:39 +10:00
|
|
|
feedback.reportError(self.tr('No polygons were created!'))
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(50)
|
2016-02-17 09:36:59 +02:00
|
|
|
|
2017-07-27 14:56:39 +10:00
|
|
|
if not polygons.isEmpty():
|
2018-02-15 22:30:52 +01:00
|
|
|
feedback.pushInfo(QCoreApplication.translate('Polygonize', 'Saving polygons…'))
|
2017-10-25 17:48:19 +10:00
|
|
|
total = 50.0 / polygons.constGet().numGeometries()
|
|
|
|
for i in range(polygons.constGet().numGeometries()):
|
2017-07-27 14:56:39 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
|
|
|
|
outFeat = QgsFeature()
|
2017-10-25 17:48:19 +10:00
|
|
|
geom = QgsGeometry(polygons.constGet().geometryN(i).clone())
|
2017-07-27 14:56:39 +10:00
|
|
|
outFeat.setGeometry(geom)
|
|
|
|
sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
|
|
|
feedback.setProgress(50 + int(current * total))
|
|
|
|
|
|
|
|
return {self.OUTPUT: dest_id}
|