125 lines
5.0 KiB
Python
Raw Normal View History

# -*- 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'
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,
QgsFeature,
QgsFeatureSink,
QgsGeometry,
QgsWkbTypes,
QgsFeatureRequest,
2017-07-27 14:56:39 +10:00
QgsProcessing,
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
class Polygonize(QgisAlgorithm):
INPUT = 'INPUT'
OUTPUT = 'OUTPUT'
2017-07-27 14:56:39 +10:00
KEEP_FIELDS = 'KEEP_FIELDS'
def tags(self):
return self.tr('create,lines,polygons,convert').split(',')
def group(self):
2017-08-22 23:36:42 +10:00
return self.tr('Vector geometry')
def groupId(self):
return 'vectorgeometry'
def __init__(self):
super().__init__()
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))
def name(self):
return 'polygonize'
def displayName(self):
return self.tr('Polygonize')
def processAlgorithm(self, parameters, context, feedback):
2017-07-27 14:56:39 +10:00
source = self.parameterAsSource(parameters, self.INPUT, context)
if source is None:
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
if self.parameterAsBoolean(parameters, self.KEEP_FIELDS, context):
2017-07-27 14:56:39 +10:00
fields = source.fields()
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())
if sink is None:
raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT))
2017-07-27 14:56:39 +10: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
for current, inFeat in enumerate(features):
2017-07-27 14:56:39 +10:00
if feedback.isCanceled():
break
if inFeat.geometry():
allLinesList.append(inFeat.geometry())
feedback.setProgress(int(current * total))
feedback.setProgress(40)
2018-02-15 22:30:52 +01:00
feedback.pushInfo(QCoreApplication.translate('Polygonize', 'Noding lines…'))
allLines = QgsGeometry.unaryUnion(allLinesList)
2017-07-27 14:56:39 +10:00
if feedback.isCanceled():
return {}
feedback.setProgress(45)
2018-02-15 22:30:52 +01:00
feedback.pushInfo(QCoreApplication.translate('Polygonize', 'Polygonizing…'))
polygons = QgsGeometry.polygonize([allLines])
if polygons.isEmpty():
2017-07-27 14:56:39 +10:00
feedback.reportError(self.tr('No polygons were created!'))
feedback.setProgress(50)
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}