|
1 | | -from sextante.core.GeoAlgorithm import GeoAlgorithm |
| 1 | +import os.path |
| 2 | +from sets import Set |
| 3 | + |
2 | 4 | from PyQt4 import QtGui |
3 | 5 | from PyQt4.QtCore import * |
4 | | -from PyQt4.QtGui import * |
| 6 | + |
5 | 7 | from qgis.core import * |
6 | | -from sextante.parameters.ParameterVector import ParameterVector |
| 8 | + |
| 9 | +from sextante.core.GeoAlgorithm import GeoAlgorithm |
7 | 10 | from sextante.core.QGisLayers import QGisLayers |
| 11 | + |
| 12 | +from sextante.parameters.ParameterVector import ParameterVector |
| 13 | + |
8 | 14 | from sextante.outputs.OutputVector import OutputVector |
9 | | -import voronoi |
10 | | -from sets import Set |
| 15 | + |
| 16 | +from sextante.ftools import voronoi |
11 | 17 |
|
12 | 18 | class VoronoiPolygons(GeoAlgorithm): |
13 | 19 |
|
14 | 20 | INPUT = "INPUT" |
15 | 21 | OUTPUT = "OUTPUT" |
16 | 22 |
|
| 23 | + def getIcon(self): |
| 24 | + return QtGui.QIcon(os.path.dirname(__file__) + "/icons/voronoi.png") |
| 25 | + |
| 26 | + def defineCharacteristics(self): |
| 27 | + self.name = "Voronoi polygons" |
| 28 | + self.group = "Geometry tools" |
| 29 | + |
| 30 | + self.addParameter(ParameterVector(self.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_POINT)) |
| 31 | + |
| 32 | + self.addOutput(OutputVector(self.OUTPUT, "Voronoi polygons")) |
| 33 | + |
17 | 34 | def processAlgorithm(self, progress): |
18 | | - vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(VoronoiPolygons.INPUT)) |
19 | | - vprovider = vlayer.dataProvider() |
20 | | - allAttrs = vprovider.attributeIndexes() |
21 | | - vprovider.select( allAttrs ) |
22 | | - writer = self.getOutputFromName(VoronoiPolygons.OUTPUT).getVectorWriter(vprovider.fields(), QGis.WKBPolygon, vprovider.crs() ) |
| 35 | + settings = QSettings() |
| 36 | + encoding = settings.value( "/UI/encoding", "System" ).toString() |
| 37 | + |
| 38 | + layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT)) |
| 39 | + |
| 40 | + output = self.getOutputValue(self.OUTPUT) |
| 41 | + |
| 42 | + provider = layer.dataProvider() |
| 43 | + layer.select(layer.pendingAllAttributesList()) |
| 44 | + |
| 45 | + writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields(), |
| 46 | + QGis.WKBPolygon, provider.crs()) |
| 47 | + |
23 | 48 | inFeat = QgsFeature() |
24 | 49 | outFeat = QgsFeature() |
25 | | - extent = vlayer.extent() |
| 50 | + extent = layer.extent() |
26 | 51 | height = extent.height() |
27 | 52 | width = extent.width() |
28 | 53 | c = voronoi.Context() |
29 | 54 | pts = [] |
30 | 55 | ptDict = {} |
31 | 56 | ptNdx = -1 |
32 | | - while vprovider.nextFeature(inFeat): |
33 | | - geom = QgsGeometry(inFeat.geometry()) |
34 | | - point = geom.asPoint() |
35 | | - x = point.x()-extent.xMinimum() |
36 | | - y = point.y()-extent.yMinimum() |
37 | | - pts.append((x, y)) |
38 | | - ptNdx +=1 |
39 | | - ptDict[ptNdx] = inFeat.id() |
40 | | - #self.vlayer = None |
| 57 | + |
| 58 | + while layer.nextFeature(inFeat): |
| 59 | + geom = QgsGeometry(inFeat.geometry()) |
| 60 | + point = geom.asPoint() |
| 61 | + x = point.x()-extent.xMinimum() |
| 62 | + y = point.y()-extent.yMinimum() |
| 63 | + pts.append((x, y)) |
| 64 | + ptNdx +=1 |
| 65 | + ptDict[ptNdx] = inFeat.id() |
| 66 | + |
41 | 67 | if len(pts) < 3: |
42 | | - return False |
| 68 | + raise GeoAlgorithmExecutionException("Input file should contain at least 3 points. Choose another file and try again.") |
| 69 | + |
43 | 70 | uniqueSet = Set(item for item in pts) |
44 | 71 | ids = [pts.index(item) for item in uniqueSet] |
45 | 72 | sl = voronoi.SiteList([voronoi.Site(i[0], i[1], sitenum=j) for j, i in enumerate(uniqueSet)]) |
46 | 73 | voronoi.voronoi(sl, c) |
47 | 74 | inFeat = QgsFeature() |
48 | | - nFeat = len(c.polygons) |
49 | | - nElement = 0 |
| 75 | + |
| 76 | + current = 0 |
| 77 | + total = 100.0 / float(len(c.polygons)) |
| 78 | + |
50 | 79 | for site, edges in c.polygons.iteritems(): |
51 | | - vprovider.featureAtId(ptDict[ids[site]], inFeat, True, allAttrs) |
52 | | - lines = self.clip_voronoi(edges, c, width, height, extent, 0, 0) |
53 | | - geom = QgsGeometry.fromMultiPoint(lines) |
54 | | - geom = QgsGeometry(geom.convexHull()) |
55 | | - outFeat.setGeometry(geom) |
56 | | - outFeat.setAttributeMap(inFeat.attributeMap()) |
57 | | - writer.addFeature(outFeat) |
58 | | - nElement += 1 |
59 | | - progress.setPercentage(nElement/nFeat * 100) |
60 | | - del writer |
61 | | - return True |
| 80 | + layer.featureAtId(ptDict[ids[site]], inFeat) |
| 81 | + lines = self.clip_voronoi(edges, c, width, height, extent, 0, 0) |
| 82 | + |
| 83 | + geom = QgsGeometry.fromMultiPoint(lines) |
| 84 | + geom = QgsGeometry(geom.convexHull()) |
| 85 | + outFeat.setGeometry(geom) |
| 86 | + outFeat.setAttributeMap(inFeat.attributeMap()) |
| 87 | + writer.addFeature(outFeat) |
62 | 88 |
|
| 89 | + current += 1 |
| 90 | + progress.setPercentage(int(current * total)) |
| 91 | + |
| 92 | + del writer |
63 | 93 |
|
64 | 94 | def clip_voronoi(self, edges, c, width, height, extent, exX, exY): |
65 | 95 | """ Clip voronoi function based on code written for Inkscape |
@@ -153,10 +183,3 @@ def clip_line(x1, y1, x2, y2, w, h, x, y): |
153 | 183 | if hasYMin: |
154 | 184 | lines.append(QgsPoint(width+extent.xMinimum()+exX, extent.yMinimum()-exY)) |
155 | 185 | return lines |
156 | | - |
157 | | - def defineCharacteristics(self): |
158 | | - self.name = "Voronoi polygons" |
159 | | - self.group = "Geometry tools" |
160 | | - self.addParameter(ParameterVector(VoronoiPolygons.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_POINT)) |
161 | | - self.addOutput(OutputVector(VoronoiPolygons.OUTPUT, "Voronoi polygons")) |
162 | | - |
|
0 commit comments