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
|
|
|
|
2013-03-28 23:55:34 +01:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-03-28 23:55:34 +01:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtCore import QVariant
|
2017-03-29 10:42:42 +10:00
|
|
|
from qgis.core import (QgsApplication,
|
|
|
|
QgsFields,
|
2017-01-31 08:57:42 +10:00
|
|
|
QgsField,
|
|
|
|
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,
|
|
|
|
QgsProcessingUtils)
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterBoolean
|
|
|
|
from processing.core.outputs import OutputVector
|
2017-05-02 14:47:58 +10:00
|
|
|
from processing.tools import dataobjects
|
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'
|
|
|
|
FIELDS = 'FIELDS'
|
|
|
|
GEOMETRY = 'GEOMETRY'
|
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):
|
|
|
|
return self.tr('Vector geometry tools')
|
|
|
|
|
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):
|
2016-02-17 09:36:59 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_LINE]))
|
2016-02-17 09:36:59 +02:00
|
|
|
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))
|
2016-09-02 10:33:45 -04:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Polygons from lines'), datatype=[dataobjects.TYPE_VECTOR_POLYGON]))
|
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-05-02 13:40:49 +10:00
|
|
|
vlayer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
|
2013-03-28 23:55:34 +01:00
|
|
|
output = self.getOutputFromName(self.OUTPUT)
|
|
|
|
if self.getParameterValue(self.FIELDS):
|
2016-08-04 07:33:49 +10:00
|
|
|
fields = vlayer.fields()
|
2013-03-28 23:55:34 +01:00
|
|
|
else:
|
|
|
|
fields = QgsFields()
|
|
|
|
if self.getParameterValue(self.GEOMETRY):
|
|
|
|
fieldsCount = fields.count()
|
2013-10-01 20:52:22 +03:00
|
|
|
fields.append(QgsField('area', QVariant.Double, 'double', 16, 2))
|
|
|
|
fields.append(QgsField('perimeter', QVariant.Double,
|
|
|
|
'double', 16, 2))
|
2013-03-28 23:55:34 +01:00
|
|
|
allLinesList = []
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(vlayer, context, QgsFeatureRequest().setSubsetOfAttributes([]))
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.pushInfo(self.tr('Processing lines...'))
|
2017-04-25 17:53:32 +10:00
|
|
|
total = 40.0 / QgsProcessingUtils.featureCount(vlayer, context)
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, inFeat in enumerate(features):
|
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
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.pushInfo(self.tr('Noding lines...'))
|
2017-01-31 08:57:42 +10:00
|
|
|
allLines = QgsGeometry.unaryUnion(allLinesList)
|
2016-02-17 09:36:59 +02:00
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(45)
|
|
|
|
feedback.pushInfo(self.tr('Polygonizing...'))
|
2017-01-31 08:57:42 +10:00
|
|
|
polygons = QgsGeometry.polygonize([allLines])
|
|
|
|
if polygons.isEmpty():
|
2015-01-15 20:41:15 +02:00
|
|
|
raise GeoAlgorithmExecutionException(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-01-06 20:04:00 +10:00
|
|
|
feedback.pushInfo('Saving polygons...')
|
2017-04-26 14:33:53 +10:00
|
|
|
writer = output.getVectorWriter(fields, QgsWkbTypes.Polygon, vlayer.crs(), context)
|
2017-01-31 08:57:42 +10:00
|
|
|
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)
|
2013-03-28 23:55:34 +01:00
|
|
|
if self.getParameterValue(self.GEOMETRY):
|
2017-01-31 08:57:42 +10:00
|
|
|
outFeat.setAttributes([None] * fieldsCount + [geom.geometry().area(),
|
|
|
|
geom.geometry().perimeter()])
|
2017-06-23 14:34:38 +10:00
|
|
|
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(50 + int(current * total))
|
2013-03-28 23:55:34 +01:00
|
|
|
del writer
|