|
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 Centroids(GeoAlgorithm): |
13 |
| - |
14 |
| - INPUT = "INPUT" |
15 |
| - OUTPUT = "OUTPUT" |
16 |
| - |
17 |
| - def getIcon(self): |
18 |
| - return QtGui.QIcon(os.path.dirname(__file__) + "/icons/centroids.png") |
19 |
| - |
20 |
| - def processAlgorithm(self, progress): |
21 |
| - settings = QSettings() |
22 |
| - systemEncoding = settings.value( "/UI/encoding", "System" ).toString() |
23 |
| - output = self.getOutputValue(Centroids.OUTPUT) |
24 |
| - vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(Centroids.INPUT)) |
25 |
| - vprovider = vlayer.dataProvider() |
26 |
| - allAttrs = vprovider.attributeIndexes() |
27 |
| - vprovider.select( allAttrs ) |
28 |
| - fields = vprovider.fields() |
29 |
| - writer = QgsVectorFileWriter( output, systemEncoding, fields, QGis.WKBPoint, vprovider.crs() ) |
30 |
| - inFeat = QgsFeature() |
31 |
| - outFeat = QgsFeature() |
32 |
| - nFeat = vprovider.featureCount() |
33 |
| - nElement = 0 |
34 |
| - while vprovider.nextFeature( inFeat ): |
35 |
| - nElement += 1 |
36 |
| - progress.setPercentage(int(nElement/nFeat * 100)) |
37 |
| - inGeom = inFeat.geometry() |
38 |
| - atMap = inFeat.attributeMap() |
39 |
| - outGeom = QgsGeometry(inGeom.centroid()) |
40 |
| - if outGeom is None: |
41 |
| - raise GeoAlgorithmExecutionException("Error calculating centroid") |
42 |
| - outFeat.setAttributeMap( atMap ) |
43 |
| - outFeat.setGeometry( outGeom ) |
44 |
| - writer.addFeature( outFeat ) |
45 |
| - del writer |
46 |
| - |
47 |
| - def defineCharacteristics(self): |
48 |
| - self.name = "Centroids" |
49 |
| - self.group = "Geometry tools" |
50 |
| - self.addParameter(ParameterVector(Centroids.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_POLYGON)) |
51 |
| - self.addOutput(OutputVector(Centroids.OUTPUT, "Output layer")) |
52 |
| - #========================================================= |
| 1 | +import os.path |
| 2 | + |
| 3 | +from PyQt4 import QtGui |
| 4 | + |
| 5 | +from qgis.core import * |
| 6 | + |
| 7 | +from sextante.core.GeoAlgorithm import GeoAlgorithm |
| 8 | +from sextante.core.QGisLayers import QGisLayers |
| 9 | +from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException |
| 10 | + |
| 11 | +from sextante.parameters.ParameterVector import ParameterVector |
| 12 | +from sextante.outputs.OutputVector import OutputVector |
| 13 | + |
| 14 | +class Centroids(GeoAlgorithm): |
| 15 | + |
| 16 | + INPUT_LAYER = "INPUT_LAYER" |
| 17 | + OUTPUT_LAYER = "OUTPUT_LAYER" |
| 18 | + |
| 19 | + def getIcon(self): |
| 20 | + return QtGui.QIcon(os.path.dirname(__file__) + "/icons/centroids.png") |
| 21 | + |
| 22 | + def defineCharacteristics(self): |
| 23 | + self.name = "Polygon centroids" |
| 24 | + self.group = "Geometry tools" |
| 25 | + |
| 26 | + self.addParameter(ParameterVector(Centroids.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_POLYGON)) |
| 27 | + |
| 28 | + self.addOutput(OutputVector(Centroids.OUTPUT_LAYER, "Output layer")) |
| 29 | + |
| 30 | + def processAlgorithm(self, progress): |
| 31 | + layer = QGisLayers.getObjectFromUri(self.getParameterValue(Centroids.INPUT_LAYER)) |
| 32 | + |
| 33 | + outFileName = self.getOutputValue(Centroids.OUTPUT_LAYER) |
| 34 | + |
| 35 | + provider = layer.dataProvider() |
| 36 | + |
| 37 | + settings = QSettings() |
| 38 | + encoding = settings.value( "/UI/encoding", "System" ).toString() |
| 39 | + |
| 40 | + writer = QgsVectorFileWriter(outFileName, encoding, provider.fields(), |
| 41 | + QGis.WKBPoint, provider.crs()) |
| 42 | + |
| 43 | + allAttrs = provider.attributeIndexes() |
| 44 | + provider.select(allAttrs) |
| 45 | + |
| 46 | + inFeat = QgsFeature() |
| 47 | + outFeat = QgsFeature() |
| 48 | + total = provider.featureCount() |
| 49 | + current = 0 |
| 50 | + while provider.nextFeature(inFeat): |
| 51 | + inGeom = inFeat.geometry() |
| 52 | + attrMap = inFeat.attributeMap() |
| 53 | + |
| 54 | + outGeom = QgsGeometry(inGeom.centroid()) |
| 55 | + if outGeom is None: |
| 56 | + raise GeoAlgorithmExecutionException("Error calculating centroid") |
| 57 | + |
| 58 | + outFeat.setGeometry(outGeom) |
| 59 | + outFeat.setAttributeMap(attrMap) |
| 60 | + writer.addFeature(outFeat) |
| 61 | + current += 1 |
| 62 | + progress.setPercentage(int(current / total * 100)) |
| 63 | + |
| 64 | + del writer |
0 commit comments