|
1 |
| -from sextante.core.GeoAlgorithm import GeoAlgorithm |
2 | 1 | import os.path
|
| 2 | + |
3 | 3 | from PyQt4 import QtGui
|
4 | 4 | from PyQt4.QtCore import *
|
5 |
| -from PyQt4.QtGui import * |
| 5 | + |
6 | 6 | from qgis.core import *
|
7 |
| -from sextante.parameters.ParameterVector import ParameterVector |
| 7 | + |
| 8 | +from sextante.core.GeoAlgorithm import GeoAlgorithm |
8 | 9 | from sextante.core.QGisLayers import QGisLayers
|
| 10 | + |
| 11 | +from sextante.parameters.ParameterVector import ParameterVector |
| 12 | +from sextante.parameters.ParameterBoolean import ParameterBoolean |
| 13 | + |
9 | 14 | from sextante.outputs.OutputVector import OutputVector
|
10 | 15 |
|
11 | 16 | class ExtentFromLayer(GeoAlgorithm):
|
12 | 17 |
|
13 |
| - INPUT = "INPUT" |
| 18 | + INPUT_LAYER = "INPUT_LAYER" |
| 19 | + USE_SELECTION = "USE_SELECTION" |
| 20 | + BY_FEATURE = "BY_FEATURE" |
| 21 | + |
14 | 22 | OUTPUT = "OUTPUT"
|
15 | 23 |
|
16 | 24 | def getIcon(self):
|
17 | 25 | return QtGui.QIcon(os.path.dirname(__file__) + "/icons/layer_extent.png")
|
18 | 26 |
|
| 27 | + def defineCharacteristics(self): |
| 28 | + self.name = "Polygon from layer extent" |
| 29 | + self.group = "Research tools" |
| 30 | + |
| 31 | + self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_ANY)) |
| 32 | + self.addParameter(ParameterBoolean(self.USE_SELECTION, "Use selection", False)) |
| 33 | + self.addParameter(ParameterBoolean(self.BY_FEATURE, "Calculate extent for each feature separately", False)) |
| 34 | + |
| 35 | + self.addOutput(OutputVector(self.OUTPUT, "Output layer")) |
| 36 | + |
19 | 37 | def processAlgorithm(self, progress):
|
20 |
| - vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(ExtentFromLayer.INPUT)) |
21 |
| - fields = { |
22 |
| - 0 : QgsField( "MINX", QVariant.Double ), |
23 |
| - 1 : QgsField( "MINY", QVariant.Double ), |
24 |
| - 2 : QgsField( "MAXX", QVariant.Double ), |
25 |
| - 3 : QgsField( "MAXY", QVariant.Double ), |
26 |
| - 4 : QgsField( "CNTX", QVariant.Double ), |
27 |
| - 5 : QgsField( "CNTY", QVariant.Double ), |
28 |
| - 6 : QgsField( "AREA", QVariant.Double ), |
29 |
| - 7 : QgsField( "PERIM", QVariant.Double ), |
30 |
| - 8 : QgsField( "HEIGHT", QVariant.Double ), |
31 |
| - 9 : QgsField( "WIDTH", QVariant.Double ) } |
32 |
| - |
33 |
| - writer = self.getOutputFromName(ExtentFromLayer.OUTPUT).getVectorWriter(fields, QGis.WKBPolygon, vlayer.crs() ) |
34 |
| - rect = vlayer.extent() |
| 38 | + settings = QSettings() |
| 39 | + |
| 40 | + layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER)) |
| 41 | + useSelection = self.getParameterValue(self.USE_SELECTION) |
| 42 | + byFeature = self.getParameterValue(self.BY_FEATURE) |
| 43 | + |
| 44 | + output = self.getOutputValue(self.OUTPUT) |
| 45 | + |
| 46 | + fields = {0 : QgsField("MINX", QVariant.Double), |
| 47 | + 1 : QgsField("MINY", QVariant.Double), |
| 48 | + 2 : QgsField("MAXX", QVariant.Double), |
| 49 | + 3 : QgsField("MAXY", QVariant.Double), |
| 50 | + 4 : QgsField("CNTX", QVariant.Double), |
| 51 | + 5 : QgsField("CNTY", QVariant.Double), |
| 52 | + 6 : QgsField("AREA", QVariant.Double), |
| 53 | + 7 : QgsField("PERIM", QVariant.Double), |
| 54 | + 8 : QgsField("HEIGHT", QVariant.Double), |
| 55 | + 9 : QgsField("WIDTH", QVariant.Double) |
| 56 | + } |
| 57 | + |
| 58 | + writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, |
| 59 | + QGis.WKBPolygon, layer.crs()) |
| 60 | + |
| 61 | + if byFeature: |
| 62 | + self.featureExtent(layer, writer, useSelection, progress) |
| 63 | + else: |
| 64 | + self.layerExtent(layer, writer, progress) |
| 65 | + |
| 66 | + del writer |
| 67 | + |
| 68 | + def layerExtent(self, layer, writer, progress): |
| 69 | + rect = layer.extent() |
35 | 70 | minx = rect.xMinimum()
|
36 | 71 | miny = rect.yMinimum()
|
37 | 72 | maxx = rect.xMaximum()
|
38 | 73 | maxy = rect.yMaximum()
|
39 | 74 | height = rect.height()
|
40 | 75 | width = rect.width()
|
41 |
| - cntx = minx + ( width / 2.0 ) |
42 |
| - cnty = miny + ( height / 2.0 ) |
| 76 | + cntx = minx + (width / 2.0) |
| 77 | + cnty = miny + (height / 2.0) |
43 | 78 | area = width * height
|
44 |
| - perim = ( 2 * width ) + (2 * height ) |
45 |
| - rect = [ |
46 |
| - QgsPoint( minx, miny ), |
47 |
| - QgsPoint( minx, maxy ), |
48 |
| - QgsPoint( maxx, maxy ), |
49 |
| - QgsPoint( maxx, miny ), |
50 |
| - QgsPoint( minx, miny ) ] |
51 |
| - geometry = QgsGeometry().fromPolygon( [ rect ] ) |
| 79 | + perim = (2 * width) + (2 * height) |
| 80 | + |
| 81 | + rect = [QgsPoint(minx, miny), |
| 82 | + QgsPoint(minx, maxy), |
| 83 | + QgsPoint(maxx, maxy), |
| 84 | + QgsPoint(maxx, miny), |
| 85 | + QgsPoint(minx, miny) |
| 86 | + ] |
| 87 | + |
| 88 | + geometry = QgsGeometry().fromPolygon([rect]) |
52 | 89 | feat = QgsFeature()
|
53 |
| - feat.setGeometry( geometry ) |
54 |
| - feat.setAttributeMap( { |
55 |
| - 0 : QVariant( minx ), |
56 |
| - 1 : QVariant( miny ), |
57 |
| - 2 : QVariant( maxx ), |
58 |
| - 3 : QVariant( maxy ), |
59 |
| - 4 : QVariant( cntx ), |
60 |
| - 5 : QVariant( cnty ), |
61 |
| - 6 : QVariant( area ), |
62 |
| - 7 : QVariant( perim ), |
63 |
| - 8 : QVariant( height ), |
64 |
| - 9 : QVariant( width ) } ) |
65 |
| - writer.addFeature( feat ) |
66 |
| - del writer |
| 90 | + feat.setGeometry(geometry) |
| 91 | + feat.setAttributeMap({0 : QVariant(minx), |
| 92 | + 1 : QVariant(miny), |
| 93 | + 2 : QVariant(maxx), |
| 94 | + 3 : QVariant(maxy), |
| 95 | + 4 : QVariant(cntx), |
| 96 | + 5 : QVariant(cnty), |
| 97 | + 6 : QVariant(area), |
| 98 | + 7 : QVariant(perim), |
| 99 | + 8 : QVariant(height), |
| 100 | + 9 : QVariant(width) |
| 101 | + }) |
| 102 | + writer.addFeature(feat) |
67 | 103 |
|
| 104 | + def featureExtent(self, layer, writer, useSelection, progress): |
| 105 | + current = 0 |
68 | 106 |
|
69 |
| - def defineCharacteristics(self): |
70 |
| - self.name = "Extent from layer" |
71 |
| - self.group = "Research tools" |
72 |
| - self.addParameter(ParameterVector(ExtentFromLayer.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY)) |
73 |
| - self.addOutput(OutputVector(ExtentFromLayer.OUTPUT, "Extent layer")) |
74 |
| - #========================================================= |
| 107 | + inFeat = QgsFeature() |
| 108 | + outFeat = QgsFeature() |
| 109 | + |
| 110 | + provider = layer.dataProvider() |
| 111 | + provider.select() |
| 112 | + |
| 113 | + if useSelection: |
| 114 | + total = 100.0 / float(layer.selectedFeatureCount()) |
| 115 | + for inFeat in layer.selectedFeatures(): |
| 116 | + rect = inFeat.geometry().boundingBox() |
| 117 | + minx = rect.xMinimum() |
| 118 | + miny = rect.yMinimum() |
| 119 | + maxx = rect.xMaximum() |
| 120 | + maxy = rect.yMaximum() |
| 121 | + height = rect.height() |
| 122 | + width = rect.width() |
| 123 | + cntx = minx + (width / 2.0) |
| 124 | + cnty = miny + (height / 2.0) |
| 125 | + area = width * height |
| 126 | + perim = (2 * width) + (2 * height) |
| 127 | + rect = [QgsPoint(minx, miny), |
| 128 | + QgsPoint(minx, maxy), |
| 129 | + QgsPoint(maxx, maxy), |
| 130 | + QgsPoint(maxx, miny), |
| 131 | + QgsPoint(minx, miny) |
| 132 | + ] |
| 133 | + geometry = QgsGeometry().fromPolygon([rect]) |
| 134 | + |
| 135 | + outFeat.setGeometry(geometry) |
| 136 | + outFeat.setAttributeMap({0 : QVariant(minx), |
| 137 | + 1 : QVariant(miny), |
| 138 | + 2 : QVariant(maxx), |
| 139 | + 3 : QVariant(maxy), |
| 140 | + 4 : QVariant(cntx), |
| 141 | + 5 : QVariant(cnty), |
| 142 | + 6 : QVariant(area), |
| 143 | + 7 : QVariant(perim), |
| 144 | + 8 : QVariant(height), |
| 145 | + 9 : QVariant(width) |
| 146 | + }) |
| 147 | + writer.addFeature(outFeat) |
| 148 | + current += 1 |
| 149 | + progress.setPercentage(int(current * total)) |
| 150 | + else: |
| 151 | + total = 100.0 / float(provider.featureCount()) |
| 152 | + while provider.nextFeature(inFeat): |
| 153 | + rect = inFeat.geometry().boundingBox() |
| 154 | + minx = rect.xMinimum() |
| 155 | + miny = rect.yMinimum() |
| 156 | + maxx = rect.xMaximum() |
| 157 | + maxy = rect.yMaximum() |
| 158 | + height = rect.height() |
| 159 | + width = rect.width() |
| 160 | + cntx = minx + (width / 2.0) |
| 161 | + cnty = miny + (height / 2.0) |
| 162 | + area = width * height |
| 163 | + perim = (2 * width) + (2 * height) |
| 164 | + rect = [QgsPoint(minx, miny), |
| 165 | + QgsPoint(minx, maxy), |
| 166 | + QgsPoint(maxx, maxy), |
| 167 | + QgsPoint(maxx, miny), |
| 168 | + QgsPoint(minx, miny) |
| 169 | + ] |
| 170 | + |
| 171 | + geometry = QgsGeometry().fromPolygon([rect]) |
| 172 | + |
| 173 | + outFeat.setGeometry(geometry) |
| 174 | + outFeat.setAttributeMap({0 : QVariant(minx), |
| 175 | + 1 : QVariant(miny), |
| 176 | + 2 : QVariant(maxx), |
| 177 | + 3 : QVariant(maxy), |
| 178 | + 4 : QVariant(cntx), |
| 179 | + 5 : QVariant(cnty), |
| 180 | + 6 : QVariant(area), |
| 181 | + 7 : QVariant(perim), |
| 182 | + 8 : QVariant(height), |
| 183 | + 9 : QVariant(width) |
| 184 | + }) |
| 185 | + writer.addFeature(outFeat) |
| 186 | + current += 1 |
| 187 | + progress.setPercentage(int(current * total)) |
0 commit comments