1
#-----------------------------------------------------------
5
# A QGIS plugin for generating a line or polygon grid
7
# Copyright (C) 2008 Carson Farmer
9
# EMAIL: carson.farmer (at) gmail.com
10
# WEB : www.geog.uvic.ca/spar/carson
12
#-----------------------------------------------------------
14
# licensed under the terms of GNU GPL 2
16
# This program is free software; you can redistribute it and/or modify
17
# it under the terms of the GNU General Public License as published by
18
# the Free Software Foundation; either version 2 of the License, or
19
# (at your option) any later version.
21
# This program is distributed in the hope that it will be useful,
22
# but WITHOUT ANY WARRANTY; without even the implied warranty of
23
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
# GNU General Public License for more details.
26
# You should have received a copy of the GNU General Public License along
27
# with this program; if not, write to the Free Software Foundation, Inc.,
28
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30
#---------------------------------------------------------------------
32
from PyQt4.QtCore import *
33
from PyQt4.QtGui import *
35
from qgis.core import *
36
from ui_frmVectorGrid import Ui_Dialog
38
class Dialog(QDialog, Ui_Dialog):
39
def __init__(self, iface):
40
QDialog.__init__(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("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() ) )
57
def offset(self, value):
58
if self.chkLock.isChecked():
59
self.spnY.setValue(value)
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 )
68
def updateCanvas( self ):
69
canvas = self.iface.mapCanvas()
70
boundBox = canvas.extent()
71
self.updateExtents( boundBox )
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() ) )
80
if self.xMin.text() == "" or self.xMax.text() == "" or self.yMin.text() == "" or self.yMax.text() == "":
81
QMessageBox.information(self, "Vector grid", "Please specify valid extent coordinates")
82
elif self.outShape.text() == "":
83
QMessageBox.information(self, "Vector grid", "Please specify output shapefile")
86
boundBox = QgsRectangle(
87
float( self.xMin.text() ),
88
float( self.yMin.text() ),
89
float( self.xMax.text() ),
90
float( self.yMax.text() ) )
92
QMessageBox.information(self, "Vector grid", "Invalid extent coordinates entered")
93
xSpace = self.spnX.value()
94
ySpace = self.spnY.value()
95
if self.rdoPolygons.isChecked(): polygon = True
98
self.compute( boundBox, xSpace, ySpace, polygon )
99
addToTOC = QMessageBox.question(self,
100
"Generate Vector Grid", "Created output Shapefile:\n" + self.shapefileName
101
+ "\nNote: Layer has no associated coordinate system, please use "
102
+ "the Projection Management Tool to specify spatial reference system."
103
+ "\n\nWould you like to add the new layer to the TOC?",
104
QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton)
105
if addToTOC == QMessageBox.Yes:
106
ftools_utils.addShapeToCanvas( self.shapefileName )
107
self.progressBar.setValue( 0 )
109
def compute( self, bound, xOffset, yOffset, polygon ):
111
fields = {0:QgsField("ID", QVariant.Int), 1:QgsField("XMIN", QVariant.Double), 2:QgsField("XMAX", QVariant.Double),
112
3:QgsField("YMIN", QVariant.Double), 4:QgsField("YMAX", QVariant.Double)}
113
check = QFile(self.shapefileName)
115
if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
117
writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fields, QGis.WKBPolygon, None)
118
#writer = QgsVectorFileWriter(outPath, "CP1250", fields, QGis.WKBPolygon, None)
120
fields = {0:QgsField("ID", QVariant.Int), 1:QgsField("COORD", QVariant.Double)}
121
check = QFile(self.shapefileName)
123
if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
125
writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fields, QGis.WKBLineString, None)
126
#writer = QgsVectorFileWriter(unicode(outPath), "CP1250", fields, QGis.WKBLineString, None)
127
outFeat = QgsFeature()
128
outGeom = QgsGeometry()
130
self.progressBar.setRange( 0, 0 )
133
while y >= bound.yMinimum():
134
pt1 = QgsPoint(bound.xMinimum(), y)
135
pt2 = QgsPoint(bound.xMaximum(), y)
137
outFeat.setGeometry(outGeom.fromPolyline(line))
138
outFeat.addAttribute(0, QVariant(idVar))
139
outFeat.addAttribute(1, QVariant(y))
140
writer.addFeature(outFeat)
144
while x <= bound.xMaximum():
145
pt1 = QgsPoint(x, bound.yMaximum())
146
pt2 = QgsPoint(x, bound.yMinimum())
148
outFeat.setGeometry(outGeom.fromPolyline(line))
149
outFeat.addAttribute(0, QVariant(idVar))
150
outFeat.addAttribute(1, QVariant(x))
151
writer.addFeature(outFeat)
156
while y >= bound.yMinimum():
158
while x <= bound.xMaximum():
160
pt2 = QgsPoint(x + xOffset, y)
161
pt3 = QgsPoint(x + xOffset, y - yOffset)
162
pt4 = QgsPoint(x, y - yOffset)
164
polygon = [[pt1, pt2, pt3, pt4, pt5]]
165
outFeat.setGeometry(outGeom.fromPolygon(polygon))
166
outFeat.addAttribute(0, QVariant(idVar))
167
outFeat.addAttribute(1, QVariant(x))
168
outFeat.addAttribute(2, QVariant(x + xOffset))
169
outFeat.addAttribute(3, QVariant(y - yOffset))
170
outFeat.addAttribute(4, QVariant(y))
171
writer.addFeature(outFeat)
175
self.progressBar.setRange( 0, 100 )
179
self.outShape.clear()
180
( self.shapefileName, self.encoding ) = ftools_utils.saveDialog( self )
181
if self.shapefileName is None or self.encoding is None:
183
self.outShape.setText( QString( self.shapefileName ) )
185
def getVectorLayerByName(self, myName):
186
mc = self.iface.mapCanvas()
187
nLayers = mc.layerCount()
188
for l in range(nLayers):
190
if unicode(layer.name()) == unicode(myName):
191
vlayer = QgsVectorLayer(unicode(layer.source()), unicode(myName), unicode(layer.dataProvider().name()))
195
def getMapLayerByName(self, myName):
196
mc = self.iface.mapCanvas()
197
nLayers = mc.layerCount()
198
for l in range(nLayers):
200
if unicode(layer.name()) == unicode(myName):