|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + Gridify.py |
| 6 | + --------------------- |
| 7 | + Date : May 2010 |
| 8 | + Copyright : (C) 2010 by Michael Minn |
| 9 | + Email : pyqgis at michaelminn dot com |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | +__author__ = 'Michael Minn' |
| 21 | +__date__ = 'May 2010' |
| 22 | +__copyright__ = '(C) 2010, Michael Minn' |
| 23 | + |
| 24 | +# This will get replaced with a git SHA1 when you do a git archive |
| 25 | + |
| 26 | +__revision__ = '$Format:%H$' |
| 27 | + |
| 28 | +from PyQt4.QtCore import * |
| 29 | +from qgis.core import * |
| 30 | +from processing.core.GeoAlgorithm import GeoAlgorithm |
| 31 | +from processing.core.GeoAlgorithmExecutionException import \ |
| 32 | + GeoAlgorithmExecutionException |
| 33 | +from processing.core.parameters import ParameterVector |
| 34 | +from processing.core.parameters import ParameterSelection |
| 35 | +from processing.core.outputs import OutputVector |
| 36 | + |
| 37 | +from processing.tools import dataobjects, vector |
| 38 | + |
| 39 | +class GeometryConvert(GeoAlgorithm): |
| 40 | + INPUT = 'INPUT' |
| 41 | + TYPE = 'TYPE' |
| 42 | + OUTPUT = 'OUTPUT' |
| 43 | + |
| 44 | + TYPES = ['Centroids', |
| 45 | + 'Nodes', |
| 46 | + 'Linestrings', |
| 47 | + 'Multilinestrings', |
| 48 | + 'Polygons' |
| 49 | + ] |
| 50 | + |
| 51 | + def defineCharacteristics(self): |
| 52 | + self.name = 'Convert geometry type' |
| 53 | + self.group = 'Vector geometry tools' |
| 54 | + |
| 55 | + self.addParameter(ParameterVector(self.INPUT, |
| 56 | + 'Input layer', [ParameterVector.VECTOR_TYPE_ANY])) |
| 57 | + self.addParameter(ParameterSelection(self.TYPE, |
| 58 | + 'New geometry type', self.TYPES)) |
| 59 | + |
| 60 | + self.addOutput(OutputVector(self.OUTPUT, 'Output')) |
| 61 | + |
| 62 | + def processAlgorithm(self, progress): |
| 63 | + layer = dataobjects.getObjectFromUri( |
| 64 | + self.getParameterValue(self.INPUT)) |
| 65 | + index = self.getParameterValue(self.TYPE) |
| 66 | + |
| 67 | + splitNodes = False |
| 68 | + if index == 0: |
| 69 | + newType = QGis.WKBPoint |
| 70 | + elif index == 1: |
| 71 | + newType = QGis.WKBPoint |
| 72 | + splitNodes = True |
| 73 | + elif index == 2: |
| 74 | + newType = QGis.WKBLineString |
| 75 | + elif index == 3: |
| 76 | + newType = QGis.WKBMultiLineString |
| 77 | + elif index == 4: |
| 78 | + newType = QGis.WKBPolygon |
| 79 | + else: |
| 80 | + newType = QGis.WKBPoint |
| 81 | + |
| 82 | + writer = self.getOutputFromName(self.OUTPUT).getVectorWriter( |
| 83 | + layer.pendingFields(), newType, layer.crs()) |
| 84 | + |
| 85 | + features = vector.features(layer) |
| 86 | + count = len(features) |
| 87 | + total = 100.0 / float(count) |
| 88 | + |
| 89 | + for count, f in enumerate(features): |
| 90 | + geom = f.geometry() |
| 91 | + geomType = geom.wkbType() |
| 92 | + |
| 93 | + if geomType in [QGis.WKBPoint, QGis.WKBPoint25D]: |
| 94 | + if newType == QGis.WKBPoint: |
| 95 | + writer.addFeature(f) |
| 96 | + else: |
| 97 | + raise GeoAlgorithmExecutionException( |
| 98 | + 'Cannot convert from %s to %s', geomType, newType) |
| 99 | + elif geomType in [QGis.WKBMultiPoint, QGis.WKBMultiPoint25D]: |
| 100 | + if newType == QGis.WKBPoint and splitNodes: |
| 101 | + points = geom.asMultiPoint() |
| 102 | + for p in points: |
| 103 | + feat = QgsFeature() |
| 104 | + feat.setAttributes(f.attributes()) |
| 105 | + feat.setGeometry(QgsGeometry.fromPoint(p)) |
| 106 | + writer.addFeature(feat) |
| 107 | + elif newType == QGis.WKBPoint: |
| 108 | + feat = QgsFeature() |
| 109 | + feat.setAttributes(f.attributes()) |
| 110 | + feat.setGeometry(geom.centroid()) |
| 111 | + writer.addFeature(feat) |
| 112 | + else: |
| 113 | + raise GeoAlgorithmExecutionException( |
| 114 | + 'Cannot convert from %s to %s', geomType, newType) |
| 115 | + elif geomType in [QGis.WKBLineString, QGis.WKBLineString25D]: |
| 116 | + if newType == QGis.WKBPoint and splitNodes: |
| 117 | + points = geom.asPolyline() |
| 118 | + for p in points: |
| 119 | + feat = QgsFeature() |
| 120 | + feat.setAttributes(f.attributes()) |
| 121 | + feat.setGeometry(QgsGeometry.fromPoint(p)) |
| 122 | + writer.addFeature(feat) |
| 123 | + elif newType == QGis.WKBPoint: |
| 124 | + feat = QgsFeature() |
| 125 | + feat.setAttributes(f.attributes()) |
| 126 | + feat.setGeometry(geom.centroid()) |
| 127 | + writer.addFeature(feat) |
| 128 | + elif newType == QGis.WKBLineString: |
| 129 | + writer.addFeature(f) |
| 130 | + else: |
| 131 | + raise GeoAlgorithmExecutionException( |
| 132 | + 'Cannot convert from %s to %s', geomType, newType) |
| 133 | + elif geomType in [QGis.WKBMultiLineString, QGis.WKBMultiLineString25D]: |
| 134 | + if newType == QGis.WKBPoint and splitNodes: |
| 135 | + lines = geom.asMultiPolyline() |
| 136 | + for line in lines: |
| 137 | + for p in line: |
| 138 | + feat = QgsFeature() |
| 139 | + feat.setAttributes(f.attributes()) |
| 140 | + feat.setGeometry(QgsGeometry.fromPoint(p)) |
| 141 | + writer.addFeature(feat) |
| 142 | + elif newType == QGis.WKBPoint: |
| 143 | + feat = QgsFeature() |
| 144 | + feat.setAttributes(f.attributes()) |
| 145 | + feat.setGeometry(geom.centroid()) |
| 146 | + writer.addFeature(feat) |
| 147 | + elif newType == QGis.WKBLineString: |
| 148 | + lines = geom.asMultiPolyline() |
| 149 | + for line in lines: |
| 150 | + feat = QgsFeature() |
| 151 | + feat.setAttributes(f.attributes()) |
| 152 | + feat.setGeometry(QgsGeometry.fromPolyline(line)) |
| 153 | + writer.addFeature(feat) |
| 154 | + elif newType == QGis.WKBMultiLineString: |
| 155 | + writer.addFeature(f) |
| 156 | + else: |
| 157 | + raise GeoAlgorithmExecutionException( |
| 158 | + 'Cannot convert from %s to %s', geomType, newType) |
| 159 | + elif geomType in [QGis.WKBPolygon, QGis.WKBPolygon25D]: |
| 160 | + if newType == QGis.WKBPoint and splitNodes: |
| 161 | + rings = geom.asPolygon() |
| 162 | + for ring in rings: |
| 163 | + for p in ring: |
| 164 | + feat = QgsFeature() |
| 165 | + feat.setAttributes(f.attributes()) |
| 166 | + feat.setGeometry(QgsGeometry.fromPoint(p)) |
| 167 | + writer.addFeature(feat) |
| 168 | + elif newType == QGis.WKBPoint: |
| 169 | + feat = QgsFeature() |
| 170 | + feat.setAttributes(f.attributes()) |
| 171 | + feat.setGeometry(geom.centroid()) |
| 172 | + writer.addFeature(feat) |
| 173 | + elif newType == QGis.WKBMultiLineString: |
| 174 | + rings = geom.asPolygon() |
| 175 | + feat = QgsFeature() |
| 176 | + feat.setAttributes(f.attributes()) |
| 177 | + feat.setGeometry(QgsGeometry.fromMultiPolyline(rings)) |
| 178 | + writer.addFeature(feat) |
| 179 | + elif newtype == QGis.WKBPolygon: |
| 180 | + writer.addFeature(f) |
| 181 | + else: |
| 182 | + raise GeoAlgorithmExecutionException( |
| 183 | + 'Cannot convert from %s to %s', geomType, newType) |
| 184 | + elif geomType in [QGis.WKBMultiPolygon, QGis.WKBMultiPolygon25D]: |
| 185 | + if newType == QGis.WKBPoint and splitNodes: |
| 186 | + polygons = geom.asMultiPolygon() |
| 187 | + for polygon in polygons: |
| 188 | + for line in polygon: |
| 189 | + for p in line: |
| 190 | + feat = QgsFeature() |
| 191 | + feat.setAttributes(f.attributes()) |
| 192 | + feat.setGeometry(QgsGeometry.fromPoint(p)) |
| 193 | + writer.addFeature(feat) |
| 194 | + elif newType == QGis.WKBPoint: |
| 195 | + feat = QgsFeature() |
| 196 | + feat.setAttributes(f.attributes()) |
| 197 | + feat.setGeometry(geom.centroid()) |
| 198 | + writer.addFeature(feat) |
| 199 | + elif newType == QGis.WKBLineString: |
| 200 | + polygons = geom.asMultiPolygon() |
| 201 | + for polygons in polygons: |
| 202 | + feat = QgsFeature() |
| 203 | + feat.setAttributes(f.attributes()) |
| 204 | + feat.setGeometry(QgsGeometry.fromPolyline(polygon)) |
| 205 | + writer.addFeature(feat) |
| 206 | + elif newType == QGis.WKBPolygon: |
| 207 | + polugons = geom.asMultiPolygon() |
| 208 | + for polygons in polygons: |
| 209 | + feat = QgsFeature() |
| 210 | + feat.setAttributes(f.attributes()) |
| 211 | + feat.setGeometry(QgsGeometry.fromPolygon(polygon)) |
| 212 | + writer.addFeature(feat) |
| 213 | + elif newType in [QGis.WKBMultiLineString, QGis.WKBMultiPolygon]: |
| 214 | + writer.addFeature(f) |
| 215 | + else: |
| 216 | + raise GeoAlgorithmExecutionException( |
| 217 | + 'Cannot convert from %s to %s', geomType, newType) |
| 218 | + |
| 219 | + progress.setPercentage(int(count * total)) |
| 220 | + |
| 221 | + del writer |
0 commit comments