|
| 1 | +from sextante.core.GeoAlgorithm import GeoAlgorithm |
| 2 | +import os.path |
| 3 | +from PyQt4 import QtGui |
| 4 | +from PyQt4.QtCore import * |
| 5 | +from PyQt4.QtGui import * |
| 6 | +from qgis.core import * |
| 7 | +from sextante.parameters.ParameterVector import ParameterVector |
| 8 | +from sextante.core.QGisLayers import QGisLayers |
| 9 | +from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException |
| 10 | +from sextante.outputs.OutputVector import OutputVector |
| 11 | + |
| 12 | +class MultipartTosingleparts(GeoAlgorithm): |
| 13 | + |
| 14 | + INPUT = "INPUT" |
| 15 | + OUTPUT = "OUTPUT" |
| 16 | + |
| 17 | + def getIcon(self): |
| 18 | + return QtGui.QIcon(os.path.dirname(__file__) + "/icons/multi_to_single.png") |
| 19 | + |
| 20 | + def processAlgorithm(self, progress): |
| 21 | + settings = QSettings() |
| 22 | + systemEncoding = settings.value( "/UI/encoding", "System" ).toString() |
| 23 | + vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT)) |
| 24 | + output = self.getOutputValue(self.OUTPUT) |
| 25 | + vprovider = vlayer.dataProvider() |
| 26 | + allAttrs = vprovider.attributeIndexes() |
| 27 | + vprovider.select( allAttrs ) |
| 28 | + fields = vprovider.fields() |
| 29 | + geomType = self.multiToSingleGeom(vprovider.geometryType()) |
| 30 | + writer = QgsVectorFileWriter( output, systemEncoding, |
| 31 | + fields, geomType, vprovider.crs() ) |
| 32 | + inFeat = QgsFeature() |
| 33 | + outFeat = QgsFeature() |
| 34 | + inGeom = QgsGeometry() |
| 35 | + nFeat = vprovider.featureCount() |
| 36 | + nElement = 0 |
| 37 | + while vprovider.nextFeature( inFeat ): |
| 38 | + nElement += 1 |
| 39 | + progress.setPercentage((nElement*100)/nFeat) |
| 40 | + inGeom = inFeat.geometry() |
| 41 | + atMap = inFeat.attributeMap() |
| 42 | + featList = self.extractAsSingle( inGeom ) |
| 43 | + outFeat.setAttributeMap( atMap ) |
| 44 | + for i in featList: |
| 45 | + outFeat.setGeometry( i ) |
| 46 | + writer.addFeature( outFeat ) |
| 47 | + del writer |
| 48 | + |
| 49 | + |
| 50 | + def multiToSingleGeom(self, wkbType): |
| 51 | + try: |
| 52 | + if wkbType in (QGis.WKBPoint, QGis.WKBMultiPoint, |
| 53 | + QGis.WKBPoint25D, QGis.WKBMultiPoint25D): |
| 54 | + return QGis.WKBPoint |
| 55 | + elif wkbType in (QGis.WKBLineString, QGis.WKBMultiLineString, |
| 56 | + QGis.WKBMultiLineString25D, QGis.WKBLineString25D): |
| 57 | + return QGis.WKBLineString |
| 58 | + elif wkbType in (QGis.WKBPolygon, QGis.WKBMultiPolygon, |
| 59 | + QGis.WKBMultiPolygon25D, QGis.WKBPolygon25D): |
| 60 | + return QGis.WKBPolygon |
| 61 | + else: |
| 62 | + return QGis.WKBUnknown |
| 63 | + except Exception, err: |
| 64 | + raise GeoAlgorithmExecutionException(str(err)) |
| 65 | + |
| 66 | + |
| 67 | + def extractAsSingle( self, geom ): |
| 68 | + multi_geom = QgsGeometry() |
| 69 | + temp_geom = [] |
| 70 | + if geom.type() == 0: |
| 71 | + if geom.isMultipart(): |
| 72 | + multi_geom = geom.asMultiPoint() |
| 73 | + for i in multi_geom: |
| 74 | + temp_geom.append( QgsGeometry().fromPoint ( i ) ) |
| 75 | + else: |
| 76 | + temp_geom.append( geom ) |
| 77 | + elif geom.type() == 1: |
| 78 | + if geom.isMultipart(): |
| 79 | + multi_geom = geom.asMultiPolyline() |
| 80 | + for i in multi_geom: |
| 81 | + temp_geom.append( QgsGeometry().fromPolyline( i ) ) |
| 82 | + else: |
| 83 | + temp_geom.append( geom ) |
| 84 | + elif geom.type() == 2: |
| 85 | + if geom.isMultipart(): |
| 86 | + multi_geom = geom.asMultiPolygon() |
| 87 | + for i in multi_geom: |
| 88 | + temp_geom.append( QgsGeometry().fromPolygon( i ) ) |
| 89 | + else: |
| 90 | + temp_geom.append( geom ) |
| 91 | + return temp_geom |
| 92 | + |
| 93 | + |
| 94 | + def defineCharacteristics(self): |
| 95 | + self.name = "Multipart to singleparts" |
| 96 | + self.group = "Geometry tools" |
| 97 | + self.addParameter(ParameterVector(self.INPUT, "Input layer")) |
| 98 | + self.addOutput(OutputVector(self.OUTPUT, "Output layer")) |
| 99 | + |
0 commit comments