mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-29 00:03:59 -04:00
Algorithms and other processing code should use this method (instead of dataobjects.getLayerFromString) to retrieve layers from a string, as it considers the processing context and allows resolving strings to temporarily stored layers. This permits processing models to function correctly when intermediate results are stored as memory layers. Subsequent model algorithms can then access these temporary layers as inputs. All temporary layers will be removed when the context object is destroyed after the model algorithm is run.
125 lines
5.1 KiB
Python
125 lines
5.1 KiB
Python
# -*- 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'
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
from qgis.PyQt.QtCore import QVariant
|
|
from qgis.core import (QgsApplication,
|
|
QgsFields,
|
|
QgsField,
|
|
QgsFeature,
|
|
QgsGeometry,
|
|
QgsWkbTypes,
|
|
QgsFeatureRequest,
|
|
QgsProcessingUtils)
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
|
from processing.core.parameters import ParameterVector
|
|
from processing.core.parameters import ParameterBoolean
|
|
from processing.core.outputs import OutputVector
|
|
from processing.tools import dataobjects, vector
|
|
|
|
|
|
class Polygonize(GeoAlgorithm):
|
|
|
|
INPUT = 'INPUT'
|
|
OUTPUT = 'OUTPUT'
|
|
FIELDS = 'FIELDS'
|
|
GEOMETRY = 'GEOMETRY'
|
|
|
|
def icon(self):
|
|
return QgsApplication.getThemeIcon("/providerQgis.svg")
|
|
|
|
def svgIconPath(self):
|
|
return QgsApplication.iconPath("providerQgis.svg")
|
|
|
|
def tags(self):
|
|
return self.tr('create,lines,polygons,convert').split(',')
|
|
|
|
def group(self):
|
|
return self.tr('Vector geometry tools')
|
|
|
|
def name(self):
|
|
return 'polygonize'
|
|
|
|
def displayName(self):
|
|
return self.tr('Polygonize')
|
|
|
|
def defineCharacteristics(self):
|
|
self.addParameter(ParameterVector(self.INPUT,
|
|
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_LINE]))
|
|
self.addParameter(ParameterBoolean(self.FIELDS,
|
|
self.tr('Keep table structure of line layer'), False))
|
|
self.addParameter(ParameterBoolean(self.GEOMETRY,
|
|
self.tr('Create geometry columns'), True))
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Polygons from lines'), datatype=[dataobjects.TYPE_VECTOR_POLYGON]))
|
|
|
|
def processAlgorithm(self, context, feedback):
|
|
vlayer = dataobjects.QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
|
|
output = self.getOutputFromName(self.OUTPUT)
|
|
if self.getParameterValue(self.FIELDS):
|
|
fields = vlayer.fields()
|
|
else:
|
|
fields = QgsFields()
|
|
if self.getParameterValue(self.GEOMETRY):
|
|
fieldsCount = fields.count()
|
|
fields.append(QgsField('area', QVariant.Double, 'double', 16, 2))
|
|
fields.append(QgsField('perimeter', QVariant.Double,
|
|
'double', 16, 2))
|
|
allLinesList = []
|
|
features = QgsProcessingUtils.getFeatures(vlayer, context, QgsFeatureRequest().setSubsetOfAttributes([]))
|
|
feedback.pushInfo(self.tr('Processing lines...'))
|
|
total = 40.0 / QgsProcessingUtils.featureCount(vlayer, context)
|
|
for current, inFeat in enumerate(features):
|
|
if inFeat.geometry():
|
|
allLinesList.append(inFeat.geometry())
|
|
feedback.setProgress(int(current * total))
|
|
|
|
feedback.setProgress(40)
|
|
|
|
feedback.pushInfo(self.tr('Noding lines...'))
|
|
allLines = QgsGeometry.unaryUnion(allLinesList)
|
|
|
|
feedback.setProgress(45)
|
|
feedback.pushInfo(self.tr('Polygonizing...'))
|
|
polygons = QgsGeometry.polygonize([allLines])
|
|
if polygons.isEmpty():
|
|
raise GeoAlgorithmExecutionException(self.tr('No polygons were created!'))
|
|
feedback.setProgress(50)
|
|
|
|
feedback.pushInfo('Saving polygons...')
|
|
writer = output.getVectorWriter(fields, QgsWkbTypes.Polygon, vlayer.crs(), context)
|
|
total = 50.0 / polygons.geometry().numGeometries()
|
|
for i in range(polygons.geometry().numGeometries()):
|
|
outFeat = QgsFeature()
|
|
geom = QgsGeometry(polygons.geometry().geometryN(i).clone())
|
|
outFeat.setGeometry(geom)
|
|
if self.getParameterValue(self.GEOMETRY):
|
|
outFeat.setAttributes([None] * fieldsCount + [geom.geometry().area(),
|
|
geom.geometry().perimeter()])
|
|
writer.addFeature(outFeat)
|
|
feedback.setProgress(50 + int(current * total))
|
|
del writer
|