|
| 1 | +# -*- coding: utf-8 -*- |
1 | 2 | #-----------------------------------------------------------
|
2 | 3 | #
|
3 | 4 | # Generate Vector Grid
|
|
36 | 37 | from ui_frmVectorGrid import Ui_Dialog
|
37 | 38 |
|
38 | 39 | class Dialog(QDialog, Ui_Dialog):
|
39 |
| - def __init__(self, iface): |
40 |
| - QDialog.__init__(self) |
41 |
| - self.iface = iface |
42 |
| - self.setupUi(self) |
43 |
| - QObject.connect(self.toolOut, SIGNAL("clicked()"), self.outFile) |
44 |
| - QObject.connect(self.spnX, SIGNAL("valueChanged(double)"), self.offset) |
45 |
| - #QObject.connect(self.inShape, SIGNAL("currentIndexChanged(QString)"), self.updateInput) |
46 |
| - QObject.connect(self.btnUpdate, SIGNAL("clicked()"), self.updateLayer) |
47 |
| - QObject.connect(self.btnCanvas, SIGNAL("clicked()"), self.updateCanvas) |
48 |
| - self.setWindowTitle(self.tr("Vector grid")) |
49 |
| - self.xMin.setValidator(QDoubleValidator(self.xMin)) |
50 |
| - self.xMax.setValidator(QDoubleValidator(self.xMax)) |
51 |
| - self.yMin.setValidator(QDoubleValidator(self.yMin)) |
52 |
| - self.yMax.setValidator(QDoubleValidator(self.yMax)) |
53 |
| - layermap = QgsMapLayerRegistry.instance().mapLayers() |
54 |
| - for name, layer in layermap.iteritems(): |
55 |
| - self.inShape.addItem( unicode( layer.name() ) ) |
| 40 | + def __init__(self, iface): |
| 41 | + QDialog.__init__(self) |
| 42 | + self.iface = iface |
| 43 | + self.setupUi(self) |
| 44 | + QObject.connect(self.toolOut, SIGNAL("clicked()"), self.outFile) |
| 45 | + QObject.connect(self.spnX, SIGNAL("valueChanged(double)"), self.offset) |
| 46 | + #QObject.connect(self.inShape, SIGNAL("currentIndexChanged(QString)"), self.updateInput) |
| 47 | + QObject.connect(self.btnUpdate, SIGNAL("clicked()"), self.updateLayer) |
| 48 | + QObject.connect(self.btnCanvas, SIGNAL("clicked()"), self.updateCanvas) |
| 49 | + self.setWindowTitle(self.tr("Vector grid")) |
| 50 | + self.xMin.setValidator(QDoubleValidator(self.xMin)) |
| 51 | + self.xMax.setValidator(QDoubleValidator(self.xMax)) |
| 52 | + self.yMin.setValidator(QDoubleValidator(self.yMin)) |
| 53 | + self.yMax.setValidator(QDoubleValidator(self.yMax)) |
| 54 | + layermap = QgsMapLayerRegistry.instance().mapLayers() |
| 55 | + for name, layer in layermap.iteritems(): |
| 56 | + self.inShape.addItem( unicode( layer.name() ) ) |
56 | 57 |
|
57 |
| - def offset(self, value): |
58 |
| - if self.chkLock.isChecked(): |
59 |
| - self.spnY.setValue(value) |
| 58 | + def offset(self, value): |
| 59 | + if self.chkLock.isChecked(): |
| 60 | + self.spnY.setValue(value) |
60 | 61 |
|
61 |
| - def updateLayer( self ): |
62 |
| - mLayerName = self.inShape.currentText() |
63 |
| - if not mLayerName == "": |
64 |
| - mLayer = ftools_utils.getMapLayerByName( unicode( mLayerName ) ) |
65 |
| - boundBox = mLayer.extent() |
66 |
| - self.updateExtents( boundBox ) |
67 |
| - |
68 |
| - def updateCanvas( self ): |
69 |
| - canvas = self.iface.mapCanvas() |
70 |
| - boundBox = canvas.extent() |
71 |
| - self.updateExtents( boundBox ) |
72 |
| - |
73 |
| - def updateExtents( self, boundBox ): |
74 |
| - self.xMin.setText( unicode( boundBox.xMinimum() ) ) |
75 |
| - self.yMin.setText( unicode( boundBox.yMinimum() ) ) |
76 |
| - self.xMax.setText( unicode( boundBox.xMaximum() ) ) |
77 |
| - self.yMax.setText( unicode( boundBox.yMaximum() ) ) |
| 62 | + def updateLayer( self ): |
| 63 | + mLayerName = self.inShape.currentText() |
| 64 | + if not mLayerName == "": |
| 65 | + mLayer = ftools_utils.getMapLayerByName( unicode( mLayerName ) ) |
| 66 | + boundBox = mLayer.extent() |
| 67 | + self.updateExtents( boundBox ) |
| 68 | + |
| 69 | + def updateCanvas( self ): |
| 70 | + canvas = self.iface.mapCanvas() |
| 71 | + boundBox = canvas.extent() |
| 72 | + self.updateExtents( boundBox ) |
| 73 | + |
| 74 | + def updateExtents( self, boundBox ): |
| 75 | + self.xMin.setText( unicode( boundBox.xMinimum() ) ) |
| 76 | + self.yMin.setText( unicode( boundBox.yMinimum() ) ) |
| 77 | + self.xMax.setText( unicode( boundBox.xMaximum() ) ) |
| 78 | + self.yMax.setText( unicode( boundBox.yMaximum() ) ) |
78 | 79 |
|
79 |
| - def accept(self): |
80 |
| - if self.xMin.text() == "" or self.xMax.text() == "" or self.yMin.text() == "" or self.yMax.text() == "": |
81 |
| - QMessageBox.information(self, self.tr("Vector grid"), self.tr("Please specify valid extent coordinates")) |
82 |
| - elif self.outShape.text() == "": |
83 |
| - QMessageBox.information(self, self.tr("Vector grid"), self.tr("Please specify output shapefile")) |
84 |
| - else: |
85 |
| - try: |
86 |
| - boundBox = QgsRectangle( |
87 |
| - float( self.xMin.text() ), |
88 |
| - float( self.yMin.text() ), |
89 |
| - float( self.xMax.text() ), |
90 |
| - float( self.yMax.text() ) ) |
91 |
| - except: |
92 |
| - QMessageBox.information(self, self.tr("Vector grid"), self.tr("Invalid extent coordinates entered")) |
93 |
| - xSpace = self.spnX.value() |
94 |
| - ySpace = self.spnY.value() |
95 |
| - if self.rdoPolygons.isChecked(): polygon = True |
96 |
| - else: polygon = False |
97 |
| - self.outShape.clear() |
98 |
| - self.compute( boundBox, xSpace, ySpace, polygon ) |
99 |
| - addToTOC = QMessageBox.question(self, self.tr("Generate Vector Grid"), self.tr("Created output shapefile:\n%1\n\nNote: Layer has no associated coordinate system, please use the Projection Management Tool to specify spatial reference system.\n\nWould you like to add the new layer to the TOC?").arg(unicode(self.shapefileName)), QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton) |
100 |
| - if addToTOC == QMessageBox.Yes: |
101 |
| - ftools_utils.addShapeToCanvas( self.shapefileName ) |
102 |
| - self.progressBar.setValue( 0 ) |
| 80 | + def accept(self): |
| 81 | + if self.xMin.text() == "" or self.xMax.text() == "" or self.yMin.text() == "" or self.yMax.text() == "": |
| 82 | + QMessageBox.information(self, self.tr("Vector grid"), self.tr("Please specify valid extent coordinates")) |
| 83 | + elif self.outShape.text() == "": |
| 84 | + QMessageBox.information(self, self.tr("Vector grid"), self.tr("Please specify output shapefile")) |
| 85 | + else: |
| 86 | + try: |
| 87 | + boundBox = QgsRectangle( |
| 88 | + float( self.xMin.text() ), |
| 89 | + float( self.yMin.text() ), |
| 90 | + float( self.xMax.text() ), |
| 91 | + float( self.yMax.text() ) ) |
| 92 | + except: |
| 93 | + QMessageBox.information(self, self.tr("Vector grid"), self.tr("Invalid extent coordinates entered")) |
| 94 | + xSpace = self.spnX.value() |
| 95 | + ySpace = self.spnY.value() |
| 96 | + if self.rdoPolygons.isChecked(): polygon = True |
| 97 | + else: polygon = False |
| 98 | + self.outShape.clear() |
| 99 | + self.compute( boundBox, xSpace, ySpace, polygon ) |
| 100 | + addToTOC = QMessageBox.question(self, self.tr("Generate Vector Grid"), self.tr("Created output shapefile:\n%1\n\nWould you like to add the new layer to the TOC?").arg(unicode(self.shapefileName)), QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton) |
| 101 | + if addToTOC == QMessageBox.Yes: |
| 102 | + ftools_utils.addShapeToCanvas( self.shapefileName ) |
| 103 | + self.progressBar.setValue( 0 ) |
103 | 104 |
|
104 |
| - def compute( self, bound, xOffset, yOffset, polygon ): |
105 |
| - if polygon: |
106 |
| - fields = {0:QgsField("ID", QVariant.Int), 1:QgsField("XMIN", QVariant.Double), 2:QgsField("XMAX", QVariant.Double), |
107 |
| - 3:QgsField("YMIN", QVariant.Double), 4:QgsField("YMAX", QVariant.Double)} |
108 |
| - check = QFile(self.shapefileName) |
109 |
| - if check.exists(): |
110 |
| - if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName): |
111 |
| - return |
112 |
| - writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fields, QGis.WKBPolygon, None) |
113 |
| - #writer = QgsVectorFileWriter(outPath, "CP1250", fields, QGis.WKBPolygon, None) |
114 |
| - else: |
115 |
| - fields = {0:QgsField("ID", QVariant.Int), 1:QgsField("COORD", QVariant.Double)} |
116 |
| - check = QFile(self.shapefileName) |
117 |
| - if check.exists(): |
118 |
| - if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName): |
119 |
| - return |
120 |
| - writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fields, QGis.WKBLineString, None) |
121 |
| - #writer = QgsVectorFileWriter(unicode(outPath), "CP1250", fields, QGis.WKBLineString, None) |
122 |
| - outFeat = QgsFeature() |
123 |
| - outGeom = QgsGeometry() |
124 |
| - idVar = 0 |
125 |
| - self.progressBar.setRange( 0, 0 ) |
126 |
| - if not polygon: |
127 |
| - y = bound.yMaximum() |
128 |
| - while y >= bound.yMinimum(): |
129 |
| - pt1 = QgsPoint(bound.xMinimum(), y) |
130 |
| - pt2 = QgsPoint(bound.xMaximum(), y) |
131 |
| - line = [pt1, pt2] |
132 |
| - outFeat.setGeometry(outGeom.fromPolyline(line)) |
133 |
| - outFeat.addAttribute(0, QVariant(idVar)) |
134 |
| - outFeat.addAttribute(1, QVariant(y)) |
135 |
| - writer.addFeature(outFeat) |
136 |
| - y = y - yOffset |
137 |
| - idVar = idVar + 1 |
138 |
| - x = bound.xMinimum() |
139 |
| - while x <= bound.xMaximum(): |
140 |
| - pt1 = QgsPoint(x, bound.yMaximum()) |
141 |
| - pt2 = QgsPoint(x, bound.yMinimum()) |
142 |
| - line = [pt1, pt2] |
143 |
| - outFeat.setGeometry(outGeom.fromPolyline(line)) |
144 |
| - outFeat.addAttribute(0, QVariant(idVar)) |
145 |
| - outFeat.addAttribute(1, QVariant(x)) |
146 |
| - writer.addFeature(outFeat) |
147 |
| - x = x + xOffset |
148 |
| - idVar = idVar + 1 |
149 |
| - else: |
150 |
| - y = bound.yMaximum() |
151 |
| - while y >= bound.yMinimum(): |
152 |
| - x = bound.xMinimum() |
153 |
| - while x <= bound.xMaximum(): |
154 |
| - pt1 = QgsPoint(x, y) |
155 |
| - pt2 = QgsPoint(x + xOffset, y) |
156 |
| - pt3 = QgsPoint(x + xOffset, y - yOffset) |
157 |
| - pt4 = QgsPoint(x, y - yOffset) |
158 |
| - pt5 = QgsPoint(x, y) |
159 |
| - polygon = [[pt1, pt2, pt3, pt4, pt5]] |
160 |
| - outFeat.setGeometry(outGeom.fromPolygon(polygon)) |
161 |
| - outFeat.addAttribute(0, QVariant(idVar)) |
162 |
| - outFeat.addAttribute(1, QVariant(x)) |
163 |
| - outFeat.addAttribute(2, QVariant(x + xOffset)) |
164 |
| - outFeat.addAttribute(3, QVariant(y - yOffset)) |
165 |
| - outFeat.addAttribute(4, QVariant(y)) |
166 |
| - writer.addFeature(outFeat) |
167 |
| - idVar = idVar + 1 |
168 |
| - x = x + xOffset |
169 |
| - y = y - yOffset |
170 |
| - self.progressBar.setRange( 0, 100 ) |
171 |
| - del writer |
| 105 | + def compute( self, bound, xOffset, yOffset, polygon ): |
| 106 | + crs = self.canvas.mapRenderer().destinationSrs() |
| 107 | + if not crs.isValid(): crs = None |
| 108 | + if polygon: |
| 109 | + fields = {0:QgsField("ID", QVariant.Int), 1:QgsField("XMIN", QVariant.Double), 2:QgsField("XMAX", QVariant.Double), |
| 110 | + 3:QgsField("YMIN", QVariant.Double), 4:QgsField("YMAX", QVariant.Double)} |
| 111 | + check = QFile(self.shapefileName) |
| 112 | + if check.exists(): |
| 113 | + if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName): |
| 114 | + return |
| 115 | + writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fields, QGis.WKBPolygon, crs) |
| 116 | + #writer = QgsVectorFileWriter(outPath, "CP1250", fields, QGis.WKBPolygon, None) |
| 117 | + else: |
| 118 | + fields = {0:QgsField("ID", QVariant.Int), 1:QgsField("COORD", QVariant.Double)} |
| 119 | + check = QFile(self.shapefileName) |
| 120 | + if check.exists(): |
| 121 | + if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName): |
| 122 | + return |
| 123 | + writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fields, QGis.WKBLineString, crs) |
| 124 | + #writer = QgsVectorFileWriter(unicode(outPath), "CP1250", fields, QGis.WKBLineString, None) |
| 125 | + outFeat = QgsFeature() |
| 126 | + outGeom = QgsGeometry() |
| 127 | + idVar = 0 |
| 128 | + self.progressBar.setRange( 0, 0 ) |
| 129 | + if not polygon: |
| 130 | + y = bound.yMaximum() |
| 131 | + while y >= bound.yMinimum(): |
| 132 | + pt1 = QgsPoint(bound.xMinimum(), y) |
| 133 | + pt2 = QgsPoint(bound.xMaximum(), y) |
| 134 | + line = [pt1, pt2] |
| 135 | + outFeat.setGeometry(outGeom.fromPolyline(line)) |
| 136 | + outFeat.addAttribute(0, QVariant(idVar)) |
| 137 | + outFeat.addAttribute(1, QVariant(y)) |
| 138 | + writer.addFeature(outFeat) |
| 139 | + y = y - yOffset |
| 140 | + idVar = idVar + 1 |
| 141 | + x = bound.xMinimum() |
| 142 | + while x <= bound.xMaximum(): |
| 143 | + pt1 = QgsPoint(x, bound.yMaximum()) |
| 144 | + pt2 = QgsPoint(x, bound.yMinimum()) |
| 145 | + line = [pt1, pt2] |
| 146 | + outFeat.setGeometry(outGeom.fromPolyline(line)) |
| 147 | + outFeat.addAttribute(0, QVariant(idVar)) |
| 148 | + outFeat.addAttribute(1, QVariant(x)) |
| 149 | + writer.addFeature(outFeat) |
| 150 | + x = x + xOffset |
| 151 | + idVar = idVar + 1 |
| 152 | + else: |
| 153 | + y = bound.yMaximum() |
| 154 | + while y >= bound.yMinimum(): |
| 155 | + x = bound.xMinimum() |
| 156 | + while x <= bound.xMaximum(): |
| 157 | + pt1 = QgsPoint(x, y) |
| 158 | + pt2 = QgsPoint(x + xOffset, y) |
| 159 | + pt3 = QgsPoint(x + xOffset, y - yOffset) |
| 160 | + pt4 = QgsPoint(x, y - yOffset) |
| 161 | + pt5 = QgsPoint(x, y) |
| 162 | + polygon = [[pt1, pt2, pt3, pt4, pt5]] |
| 163 | + outFeat.setGeometry(outGeom.fromPolygon(polygon)) |
| 164 | + outFeat.addAttribute(0, QVariant(idVar)) |
| 165 | + outFeat.addAttribute(1, QVariant(x)) |
| 166 | + outFeat.addAttribute(2, QVariant(x + xOffset)) |
| 167 | + outFeat.addAttribute(3, QVariant(y - yOffset)) |
| 168 | + outFeat.addAttribute(4, QVariant(y)) |
| 169 | + writer.addFeature(outFeat) |
| 170 | + idVar = idVar + 1 |
| 171 | + x = x + xOffset |
| 172 | + y = y - yOffset |
| 173 | + self.progressBar.setRange( 0, 100 ) |
| 174 | + del writer |
172 | 175 |
|
173 |
| - def outFile(self): |
174 |
| - self.outShape.clear() |
175 |
| - ( self.shapefileName, self.encoding ) = ftools_utils.saveDialog( self ) |
176 |
| - if self.shapefileName is None or self.encoding is None: |
177 |
| - return |
178 |
| - self.outShape.setText( QString( self.shapefileName ) ) |
| 176 | + def outFile(self): |
| 177 | + self.outShape.clear() |
| 178 | + ( self.shapefileName, self.encoding ) = ftools_utils.saveDialog( self ) |
| 179 | + if self.shapefileName is None or self.encoding is None: |
| 180 | + return |
| 181 | + self.outShape.setText( QString( self.shapefileName ) ) |
179 | 182 |
|
180 |
| - def getVectorLayerByName(self, myName): |
181 |
| - mc = self.iface.mapCanvas() |
182 |
| - nLayers = mc.layerCount() |
183 |
| - for l in range(nLayers): |
184 |
| - layer = mc.layer(l) |
185 |
| - if unicode(layer.name()) == unicode(myName): |
186 |
| - vlayer = QgsVectorLayer(unicode(layer.source()), unicode(myName), unicode(layer.dataProvider().name())) |
187 |
| - if vlayer.isValid(): |
188 |
| - return vlayer |
| 183 | + def getVectorLayerByName(self, myName): |
| 184 | + mc = self.iface.mapCanvas() |
| 185 | + nLayers = mc.layerCount() |
| 186 | + for l in range(nLayers): |
| 187 | + layer = mc.layer(l) |
| 188 | + if unicode(layer.name()) == unicode(myName): |
| 189 | + vlayer = QgsVectorLayer(unicode(layer.source()), unicode(myName), unicode(layer.dataProvider().name())) |
| 190 | + if vlayer.isValid(): |
| 191 | + return vlayer |
189 | 192 |
|
190 |
| - def getMapLayerByName(self, myName): |
191 |
| - mc = self.iface.mapCanvas() |
192 |
| - nLayers = mc.layerCount() |
193 |
| - for l in range(nLayers): |
194 |
| - layer = mc.layer(l) |
195 |
| - if unicode(layer.name()) == unicode(myName): |
196 |
| - if layer.isValid(): |
197 |
| - return layer |
| 193 | + def getMapLayerByName(self, myName): |
| 194 | + mc = self.iface.mapCanvas() |
| 195 | + nLayers = mc.layerCount() |
| 196 | + for l in range(nLayers): |
| 197 | + layer = mc.layer(l) |
| 198 | + if unicode(layer.name()) == unicode(myName): |
| 199 | + if layer.isValid(): |
| 200 | + return layer |
0 commit comments