|
1 | 1 | from PyQt4.QtCore import *
|
2 |
| -from PyQt4.QtGui import * |
| 2 | + |
3 | 3 | from qgis.core import *
|
4 |
| -from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException |
| 4 | + |
5 | 5 | from sextante.core.SextanteLog import SextanteLog
|
6 | 6 |
|
7 |
| -def buffering(progress, writer, distance, field, useSelection, useField, layer, dissolve, segments ): |
| 7 | +def buffering(progress, writer, distance, field, useSelection, useField, layer, dissolve, segments): |
8 | 8 | GEOS_EXCEPT = True
|
9 | 9 | FEATURE_EXCEPT = True
|
10 |
| - vproviderA = layer.dataProvider() |
11 |
| - allAttrs = vproviderA.attributeIndexes() |
12 |
| - vproviderA.select( allAttrs ) |
13 |
| - fields = vproviderA.fields() |
| 10 | + |
| 11 | + layer.select(layer.pendingAllAttributesList()) |
| 12 | + |
14 | 13 | if useField:
|
15 |
| - field = vproviderA.fieldNameIndex(field) |
| 14 | + field = layer.fieldNameIndex(field) |
| 15 | + |
16 | 16 | outFeat = QgsFeature()
|
17 | 17 | inFeat = QgsFeature()
|
18 | 18 | inGeom = QgsGeometry()
|
19 | 19 | outGeom = QgsGeometry()
|
20 |
| - nElement = 0 |
| 20 | + |
| 21 | + current = 0 |
| 22 | + |
21 | 23 | # there is selection in input layer
|
22 | 24 | if useSelection:
|
23 |
| - nFeat = layer.selectedFeatureCount() |
24 |
| - selectionA = layer.selectedFeatures() |
25 |
| - # with dissolve |
26 |
| - if dissolve: |
27 |
| - first = True |
28 |
| - for inFeat in selectionA: |
29 |
| - atMap = inFeat.attributeMap() |
30 |
| - if useField: |
31 |
| - value = atMap[ field ].doDouble()[ 0 ] |
32 |
| - else: |
33 |
| - value = distance |
34 |
| - inGeom = QgsGeometry( inFeat.geometry() ) |
35 |
| - try: |
36 |
| - outGeom = inGeom.buffer( float( value ), segments) |
37 |
| - if first: |
38 |
| - tempGeom = QgsGeometry( outGeom ) |
39 |
| - first = False |
40 |
| - else: |
41 |
| - try: |
42 |
| - tempGeom = tempGeom.combine( outGeom ) |
43 |
| - except: |
44 |
| - GEOS_EXCEPT = False |
45 |
| - continue |
46 |
| - except: |
47 |
| - GEOS_EXCEPT = False |
48 |
| - continue |
49 |
| - nElement += 1 |
50 |
| - progress.setPercentage(int(nElement/nFeat * 100)) |
51 |
| - try: |
52 |
| - outFeat.setGeometry( tempGeom ) |
53 |
| - writer.addFeature( outFeat ) |
54 |
| - except: |
55 |
| - FEATURE_EXCEPT = False |
56 |
| - # without dissolve |
57 |
| - else: |
58 |
| - for inFeat in selectionA: |
59 |
| - atMap = inFeat.attributeMap() |
60 |
| - if useField: |
61 |
| - value = atMap[ field ].toDouble()[ 0 ] |
62 |
| - else: |
63 |
| - value = distance |
64 |
| - inGeom = QgsGeometry( inFeat.geometry() ) |
65 |
| - try: |
66 |
| - outGeom = inGeom.buffer( float( value ), segments ) |
| 25 | + total = 100.0 / float(layer.selectedFeatureCount()) |
| 26 | + selection = layer.selectedFeatures() |
| 27 | + |
| 28 | + # with dissolve |
| 29 | + if dissolve: |
| 30 | + first = True |
| 31 | + for inFeat in selection: |
| 32 | + atMap = inFeat.attributeMap() |
| 33 | + if useField: |
| 34 | + value = atMap[field].doDouble()[0] |
| 35 | + else: |
| 36 | + value = distance |
| 37 | + |
| 38 | + inGeom = QgsGeometry(inFeat.geometry()) |
| 39 | + try: |
| 40 | + outGeom = inGeom.buffer(float(value), segments) |
| 41 | + if first: |
| 42 | + tempGeom = QgsGeometry(outGeom) |
| 43 | + first = False |
| 44 | + else: |
| 45 | + try: |
| 46 | + tempGeom = tempGeom.combine( outGeom ) |
| 47 | + except: |
| 48 | + GEOS_EXCEPT = False |
| 49 | + continue |
| 50 | + except: |
| 51 | + GEOS_EXCEPT = False |
| 52 | + continue |
| 53 | + |
| 54 | + current += 1 |
| 55 | + progress.setPercentage(int(current * total)) |
67 | 56 | try:
|
68 |
| - outFeat.setGeometry( outGeom ) |
69 |
| - outFeat.setAttributeMap( atMap ) |
70 |
| - writer.addFeature( outFeat ) |
| 57 | + outFeat.setGeometry(tempGeom) |
| 58 | + writer.addFeature(outFeat) |
71 | 59 | except:
|
72 |
| - FEATURE_EXCEPT = False |
73 |
| - continue |
74 |
| - except: |
75 |
| - GEOS_EXCEPT = False |
76 |
| - continue |
77 |
| - nElement += 1 |
78 |
| - progress.setPercentage(int(nElement/nFeat * 100)) |
| 60 | + FEATURE_EXCEPT = False |
| 61 | + # without dissolve |
| 62 | + else: |
| 63 | + for inFeat in selection: |
| 64 | + atMap = inFeat.attributeMap() |
| 65 | + if useField: |
| 66 | + value = atMap[field].toDouble()[0] |
| 67 | + else: |
| 68 | + value = distance |
| 69 | + |
| 70 | + inGeom = QgsGeometry(inFeat.geometry()) |
| 71 | + try: |
| 72 | + outGeom = inGeom.buffer(float(value), segments) |
| 73 | + try: |
| 74 | + outFeat.setGeometry(outGeom) |
| 75 | + outFeat.setAttributeMap(atMap) |
| 76 | + writer.addFeature(outFeat) |
| 77 | + except: |
| 78 | + FEATURE_EXCEPT = False |
| 79 | + continue |
| 80 | + except: |
| 81 | + GEOS_EXCEPT = False |
| 82 | + continue |
| 83 | + |
| 84 | + current += 1 |
| 85 | + progress.setPercentage(int(current * total)) |
79 | 86 | # there is no selection in input layer
|
80 | 87 | else:
|
81 |
| - nFeat = vproviderA.featureCount() |
| 88 | + total = 100.0 / float(layer.featureCount()) |
| 89 | + |
82 | 90 | # with dissolve
|
83 | 91 | if dissolve:
|
84 |
| - first = True |
85 |
| - while vproviderA.nextFeature( inFeat ): |
86 |
| - atMap = inFeat.attributeMap() |
87 |
| - if useField: |
88 |
| - value = atMap[ field ].toDouble()[ 0 ] |
89 |
| - else: |
90 |
| - value = distance |
91 |
| - inGeom = QgsGeometry( inFeat.geometry() ) |
92 |
| - try: |
93 |
| - outGeom = inGeom.buffer( float( value ), segments) |
94 |
| - if first: |
95 |
| - tempGeom = QgsGeometry( outGeom ) |
96 |
| - first = False |
97 |
| - else: |
| 92 | + first = True |
| 93 | + while layer.nextFeature(inFeat): |
| 94 | + atMap = inFeat.attributeMap() |
| 95 | + if useField: |
| 96 | + value = atMap[field].toDouble()[0] |
| 97 | + else: |
| 98 | + value = distance |
| 99 | + |
| 100 | + inGeom = QgsGeometry(inFeat.geometry()) |
98 | 101 | try:
|
99 |
| - tempGeom = tempGeom.combine( outGeom ) |
| 102 | + outGeom = inGeom.buffer(float(value), segments) |
| 103 | + if first: |
| 104 | + tempGeom = QgsGeometry(outGeom) |
| 105 | + first = False |
| 106 | + else: |
| 107 | + try: |
| 108 | + tempGeom = tempGeom.combine(outGeom) |
| 109 | + except: |
| 110 | + GEOS_EXCEPT = False |
| 111 | + continue |
100 | 112 | except:
|
101 |
| - GEOS_EXCEPT = False |
102 |
| - continue |
| 113 | + GEOS_EXCEPT = False |
| 114 | + continue |
| 115 | + |
| 116 | + current += 1 |
| 117 | + progress.setPercentage(int(current * total)) |
| 118 | + try: |
| 119 | + outFeat.setGeometry(tempGeom) |
| 120 | + writer.addFeature(outFeat) |
103 | 121 | except:
|
104 |
| - GEOS_EXCEPT = False |
105 |
| - continue |
106 |
| - nElement += 1 |
107 |
| - progress.setPercentage(int(nElement/nFeat * 100)) |
108 |
| - try: |
109 |
| - outFeat.setGeometry( tempGeom ) |
110 |
| - writer.addFeature( outFeat ) |
111 |
| - except: |
112 |
| - FEATURE_EXCEPT = False |
| 122 | + FEATURE_EXCEPT = False |
113 | 123 | # without dissolve
|
114 | 124 | else:
|
115 |
| - vproviderA.rewind() |
116 |
| - while vproviderA.nextFeature( inFeat ): |
117 |
| - atMap = inFeat.attributeMap() |
118 |
| - if useField: |
119 |
| - value = atMap[ field ].toDouble()[ 0 ] |
120 |
| - else: |
121 |
| - value = distance |
122 |
| - inGeom = QgsGeometry( inFeat.geometry() ) |
123 |
| - try: |
124 |
| - outGeom = inGeom.buffer( float( value ),segments ) |
125 |
| - try: |
126 |
| - outFeat.setGeometry( outGeom ) |
127 |
| - outFeat.setAttributeMap( atMap ) |
128 |
| - writer.addFeature( outFeat ) |
129 |
| - except: |
130 |
| - FEATURE_EXCEPT = False |
131 |
| - continue |
132 |
| - except: |
133 |
| - GEOS_EXCEPT = False |
134 |
| - continue |
135 |
| - nElement += 1 |
136 |
| - progress.setPercentage(int(nElement/nFeat * 100)) |
| 125 | + while layer.nextFeature(inFeat): |
| 126 | + atMap = inFeat.attributeMap() |
| 127 | + if useField: |
| 128 | + value = atMap[field].toDouble()[0] |
| 129 | + else: |
| 130 | + value = distance |
| 131 | + |
| 132 | + inGeom = QgsGeometry(inFeat.geometry()) |
| 133 | + try: |
| 134 | + outGeom = inGeom.buffer(float(value), segments) |
| 135 | + try: |
| 136 | + outFeat.setGeometry(outGeom) |
| 137 | + outFeat.setAttributeMap(atMap) |
| 138 | + writer.addFeature(outFeat) |
| 139 | + except: |
| 140 | + FEATURE_EXCEPT = False |
| 141 | + continue |
| 142 | + except: |
| 143 | + GEOS_EXCEPT = False |
| 144 | + continue |
| 145 | + |
| 146 | + current += 1 |
| 147 | + progress.setPercentage(int(current * total)) |
| 148 | + |
137 | 149 | del writer
|
| 150 | + |
138 | 151 | if not GEOS_EXCEPT:
|
139 | 152 | SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Geometry exception while computing buffer")
|
140 | 153 | if not FEATURE_EXCEPT:
|
141 | 154 | SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Feature exception while computing buffer")
|
142 |
| - |
143 |
| - |
144 |
| - |
|
0 commit comments