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'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2018-05-15 11:24:29 +07:00
|
|
|
from qgis.core import (QgsApplication,
|
|
|
|
QgsFeature,
|
2017-07-14 14:44:38 +07:00
|
|
|
QgsGeometry,
|
|
|
|
QgsGeometryCollection,
|
2017-10-29 20:47:31 +01:00
|
|
|
QgsPolygon,
|
|
|
|
QgsMultiPolygon,
|
2017-07-14 14:44:38 +07:00
|
|
|
QgsMultiSurface,
|
|
|
|
QgsWkbTypes,
|
|
|
|
QgsFeatureSink,
|
|
|
|
QgsProcessing,
|
|
|
|
QgsProcessingParameterFeatureSource,
|
|
|
|
QgsProcessingParameterFeatureSink,
|
|
|
|
QgsProcessingUtils)
|
2013-02-28 21:43:59 +04:00
|
|
|
|
2017-07-20 13:06:18 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.tools import dataobjects, vector
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2013-02-28 21:43:59 +04:00
|
|
|
|
2017-07-20 13:06:18 +10:00
|
|
|
class LinesToPolygons(QgisFeatureBasedAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2018-05-15 11:24:29 +07:00
|
|
|
return QgsApplication.getThemeIcon("/algorithms/mAlgorithmLineToPolygon.svg")
|
|
|
|
|
|
|
|
def svgIconPath(self):
|
|
|
|
return QgsApplication.iconPath("/algorithms/mAlgorithmLineToPolygon.svg")
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-03-29 09:51:13 +10:00
|
|
|
def tags(self):
|
|
|
|
return self.tr('line,polygon,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
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
def name(self):
|
2017-03-29 17:09:39 +10:00
|
|
|
return 'linestopolygons'
|
2017-03-29 12:51:59 +10:00
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Lines to polygons')
|
2012-10-03 18:20:29 +03:00
|
|
|
|
2017-07-20 13:06:18 +10:00
|
|
|
def outputName(self):
|
|
|
|
return self.tr('Polygons')
|
2017-07-14 14:44:38 +07:00
|
|
|
|
2017-07-20 13:06:18 +10:00
|
|
|
def outputType(self):
|
|
|
|
return QgsProcessing.TypeVectorPolygon
|
2017-07-14 14:44:38 +07:00
|
|
|
|
2017-10-13 08:28:34 +10:00
|
|
|
def inputLayerTypes(self):
|
|
|
|
return [QgsProcessing.TypeVectorLine]
|
|
|
|
|
2017-07-20 13:06:18 +10:00
|
|
|
def outputWkbType(self, input_wkb_type):
|
|
|
|
return self.convertWkbToPolygons(input_wkb_type)
|
2017-07-14 14:44:38 +07:00
|
|
|
|
2017-11-27 07:55:01 +10:00
|
|
|
def processFeature(self, feature, context, feedback):
|
2017-07-20 13:06:18 +10:00
|
|
|
if feature.hasGeometry():
|
|
|
|
feature.setGeometry(QgsGeometry(self.convertToPolygons(feature.geometry())))
|
|
|
|
if feature.geometry().isEmpty():
|
|
|
|
feedback.reportError(self.tr("One or more line ignored due to geometry not having a minimum of three vertices."))
|
2018-02-21 07:02:01 +10:00
|
|
|
return [feature]
|
2017-07-14 14:44:38 +07:00
|
|
|
|
2018-09-21 13:39:27 +10:00
|
|
|
def supportInPlaceEdit(self, layer):
|
|
|
|
return False
|
|
|
|
|
2017-07-14 14:44:38 +07:00
|
|
|
def convertWkbToPolygons(self, wkb):
|
2018-09-21 13:39:27 +10:00
|
|
|
multi_wkb = QgsWkbTypes.NoGeometry
|
2017-07-14 14:44:38 +07:00
|
|
|
if QgsWkbTypes.singleType(QgsWkbTypes.flatType(wkb)) == QgsWkbTypes.LineString:
|
|
|
|
multi_wkb = QgsWkbTypes.MultiPolygon
|
|
|
|
elif QgsWkbTypes.singleType(QgsWkbTypes.flatType(wkb)) == QgsWkbTypes.CompoundCurve:
|
|
|
|
multi_wkb = QgsWkbTypes.MultiSurface
|
|
|
|
if QgsWkbTypes.hasM(wkb):
|
|
|
|
multi_wkb = QgsWkbTypes.addM(multi_wkb)
|
|
|
|
if QgsWkbTypes.hasZ(wkb):
|
|
|
|
multi_wkb = QgsWkbTypes.addZ(multi_wkb)
|
|
|
|
|
|
|
|
return multi_wkb
|
|
|
|
|
|
|
|
def convertToPolygons(self, geometry):
|
2017-10-25 17:48:19 +10:00
|
|
|
surfaces = self.getSurfaces(geometry.constGet())
|
2017-07-14 14:44:38 +07:00
|
|
|
output_wkb = self.convertWkbToPolygons(geometry.wkbType())
|
|
|
|
out_geom = None
|
|
|
|
if QgsWkbTypes.flatType(output_wkb) == QgsWkbTypes.MultiPolygon:
|
2017-10-29 12:10:43 +01:00
|
|
|
out_geom = QgsMultiPolygon()
|
2017-07-14 14:44:38 +07:00
|
|
|
else:
|
|
|
|
out_geom = QgsMultiSurface()
|
2012-10-03 18:20:29 +03:00
|
|
|
|
2017-07-14 14:44:38 +07:00
|
|
|
for surface in surfaces:
|
|
|
|
out_geom.addGeometry(surface)
|
2012-10-03 18:20:29 +03:00
|
|
|
|
2017-07-14 14:44:38 +07:00
|
|
|
return out_geom
|
2012-10-03 18:20:29 +03:00
|
|
|
|
2017-07-14 14:44:38 +07:00
|
|
|
def getSurfaces(self, geometry):
|
|
|
|
surfaces = []
|
|
|
|
if isinstance(geometry, QgsGeometryCollection):
|
|
|
|
# collection
|
|
|
|
for i in range(geometry.numGeometries()):
|
|
|
|
surfaces.extend(self.getSurfaces(geometry.geometryN(i)))
|
2012-10-03 18:20:29 +03:00
|
|
|
else:
|
2017-07-14 14:44:38 +07:00
|
|
|
# not collection
|
|
|
|
if geometry.vertexCount() > 2:
|
2017-10-29 12:08:07 +01:00
|
|
|
surface = QgsPolygon()
|
2017-07-14 14:44:38 +07:00
|
|
|
surface.setExteriorRing(geometry.clone())
|
|
|
|
surfaces.append(surface)
|
|
|
|
|
|
|
|
return surfaces
|