2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
Explode.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
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
from qgis.core import (QgsFeature,
|
|
|
|
QgsGeometry,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-03-29 10:42:42 +10:00
|
|
|
QgsWkbTypes,
|
2017-07-27 15:21:56 +10:00
|
|
|
QgsProcessing,
|
|
|
|
QgsLineString)
|
2018-02-21 07:21:30 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2018-02-21 07:21:30 +10:00
|
|
|
class Explode(QgisFeatureBasedAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
2012-09-15 18:25:25 +03:00
|
|
|
|
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
|
|
|
|
2018-02-21 07:21:30 +10:00
|
|
|
def inputLayerTypes(self):
|
|
|
|
return [QgsProcessing.TypeVectorLine]
|
|
|
|
|
|
|
|
def outputName(self):
|
|
|
|
return self.tr('Exploded')
|
|
|
|
|
|
|
|
def outputWkbType(self, inputWkb):
|
|
|
|
return QgsWkbTypes.singleType(inputWkb)
|
|
|
|
|
|
|
|
def outputLayerType(self):
|
|
|
|
return QgsProcessing.TypeVectorLine
|
2017-05-15 13:40:38 +10:00
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
def name(self):
|
2017-03-29 17:09:39 +10:00
|
|
|
return 'explodelines'
|
2017-03-29 12:51:59 +10:00
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Explode lines')
|
|
|
|
|
2018-02-21 07:21:30 +10:00
|
|
|
def processFeature(self, feature, context, feedback):
|
|
|
|
if not feature.hasGeometry():
|
|
|
|
return [feature]
|
|
|
|
|
|
|
|
segments = self.extractAsSingleSegments(feature.geometry())
|
|
|
|
output_features = []
|
|
|
|
for segment in segments:
|
|
|
|
output_feature = QgsFeature()
|
|
|
|
output_feature.setAttributes(feature.attributes())
|
|
|
|
output_feature.setGeometry(segment)
|
|
|
|
output_features.append(output_feature)
|
|
|
|
return output_features
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
def extractAsSingleSegments(self, geom):
|
2012-09-15 18:25:25 +03:00
|
|
|
segments = []
|
|
|
|
if geom.isMultipart():
|
2017-10-25 17:48:19 +10:00
|
|
|
for part in range(geom.constGet().numGeometries()):
|
|
|
|
segments.extend(self.getPolylineAsSingleSegments(geom.constGet().geometryN(part)))
|
2012-09-15 18:25:25 +03:00
|
|
|
else:
|
2013-10-01 20:52:22 +03:00
|
|
|
segments.extend(self.getPolylineAsSingleSegments(
|
2017-10-25 17:48:19 +10:00
|
|
|
geom.constGet()))
|
2012-09-15 18:25:25 +03:00
|
|
|
return segments
|
|
|
|
|
|
|
|
def getPolylineAsSingleSegments(self, polyline):
|
|
|
|
segments = []
|
2017-07-27 15:21:56 +10:00
|
|
|
for i in range(polyline.numPoints() - 1):
|
|
|
|
ptA = polyline.pointN(i)
|
|
|
|
ptB = polyline.pointN(i + 1)
|
|
|
|
segment = QgsGeometry(QgsLineString([ptA, ptB]))
|
2012-09-15 18:25:25 +03:00
|
|
|
segments.append(segment)
|
|
|
|
return segments
|