QGIS/python/plugins/processing/algs/qgis/LinesToPolygons.py

106 lines
3.8 KiB
Python
Raw Normal View History

2012-10-04 19:33:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
LinesToPolygons.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf 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__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
2012-10-04 19:33:47 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2012-10-04 19:33:47 +02:00
__revision__ = '$Format:%H$'
import os
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtGui import QIcon
from qgis.core import QgsFeature, QgsGeometry, QgsWkbTypes, QgsFeatureSink, QgsProcessingUtils
2017-06-06 13:41:42 +10:00
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector
from processing.tools import dataobjects, vector
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
class LinesToPolygons(QgisAlgorithm):
2012-09-15 18:25:25 +03:00
INPUT = 'INPUT'
OUTPUT = 'OUTPUT'
2012-09-15 18:25:25 +03:00
def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'to_lines.png'))
def tags(self):
return self.tr('line,polygon,convert').split(',')
def group(self):
return self.tr('Vector geometry tools')
def __init__(self):
super().__init__()
self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer'),
[dataobjects.TYPE_VECTOR_LINE]))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Polygons from lines'), datatype=[dataobjects.TYPE_VECTOR_POLYGON]))
def name(self):
return 'linestopolygons'
def displayName(self):
return self.tr('Lines to polygons')
def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.fields(), QgsWkbTypes.Polygon,
layer.crs(), context)
2012-09-15 18:25:25 +03:00
outFeat = QgsFeature()
features = QgsProcessingUtils.getFeatures(layer, context)
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
for current, f in enumerate(features):
outGeomList = []
if f.geometry().isMultipart():
outGeomList = f.geometry().asMultiPolyline()
else:
outGeomList.append(f.geometry().asPolyline())
2012-09-15 18:25:25 +03:00
polyGeom = self.removeBadLines(outGeomList)
if len(polyGeom) != 0:
outFeat.setGeometry(QgsGeometry.fromPolygon(polyGeom))
attrs = f.attributes()
outFeat.setAttributes(attrs)
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
del writer
def removeBadLines(self, lines):
geom = []
if len(lines) == 1:
if len(lines[0]) > 2:
geom = lines
else:
geom = []
else:
geom = [elem for elem in lines if len(elem) > 2]
return geom