~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to python/plugins/fTools/tools/doVectorGrid.py

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#-----------------------------------------------------------
 
2
 
3
# Generate Vector Grid
 
4
#
 
5
# A QGIS plugin for generating a line or polygon grid
 
6
#
 
7
# Copyright (C) 2008  Carson Farmer
 
8
#
 
9
# EMAIL: carson.farmer (at) gmail.com
 
10
# WEB  : www.geog.uvic.ca/spar/carson
 
11
#
 
12
#-----------------------------------------------------------
 
13
 
14
# licensed under the terms of GNU GPL 2
 
15
 
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.
 
20
 
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.
 
25
 
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.
 
29
 
30
#---------------------------------------------------------------------
 
31
 
 
32
from PyQt4.QtCore import *
 
33
from PyQt4.QtGui import *
 
34
import ftools_utils
 
35
from qgis.core import *
 
36
from ui_frmVectorGrid import Ui_Dialog
 
37
 
 
38
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("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() ) )
 
56
 
 
57
        def offset(self, value):
 
58
                if self.chkLock.isChecked():
 
59
                        self.spnY.setValue(value)
 
60
 
 
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() ) )
 
78
 
 
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, "Vector grid", "Please specify valid extent coordinates")
 
82
                elif self.outShape.text() == "":
 
83
                        QMessageBox.information(self, "Vector grid", "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, "Vector grid", "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, 
 
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 )
 
108
 
 
109
        def compute( self, bound, xOffset, yOffset, polygon ):
 
110
                if 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)
 
114
                        if check.exists():
 
115
                                if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
 
116
                                        return
 
117
                        writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fields, QGis.WKBPolygon, None)
 
118
                        #writer = QgsVectorFileWriter(outPath, "CP1250", fields, QGis.WKBPolygon, None)
 
119
                else:
 
120
                        fields = {0:QgsField("ID", QVariant.Int), 1:QgsField("COORD", QVariant.Double)}
 
121
                        check = QFile(self.shapefileName)
 
122
                        if check.exists():
 
123
                                if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
 
124
                                        return
 
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()
 
129
                idVar = 0
 
130
                self.progressBar.setRange( 0, 0 )
 
131
                if not polygon:
 
132
                        y = bound.yMaximum()
 
133
                        while y >= bound.yMinimum():
 
134
                                pt1 = QgsPoint(bound.xMinimum(), y)
 
135
                                pt2 = QgsPoint(bound.xMaximum(), y)
 
136
                                line = [pt1, pt2]
 
137
                                outFeat.setGeometry(outGeom.fromPolyline(line))
 
138
                                outFeat.addAttribute(0, QVariant(idVar))
 
139
                                outFeat.addAttribute(1, QVariant(y))
 
140
                                writer.addFeature(outFeat)
 
141
                                y = y - yOffset
 
142
                                idVar = idVar + 1
 
143
                        x = bound.xMinimum()
 
144
                        while x <= bound.xMaximum():
 
145
                                pt1 = QgsPoint(x, bound.yMaximum())
 
146
                                pt2 = QgsPoint(x, bound.yMinimum())
 
147
                                line = [pt1, pt2]
 
148
                                outFeat.setGeometry(outGeom.fromPolyline(line))
 
149
                                outFeat.addAttribute(0, QVariant(idVar))
 
150
                                outFeat.addAttribute(1, QVariant(x))
 
151
                                writer.addFeature(outFeat)
 
152
                                x = x + xOffset
 
153
                                idVar = idVar + 1
 
154
                else:
 
155
                        y = bound.yMaximum()
 
156
                        while y >= bound.yMinimum():
 
157
                                x = bound.xMinimum()
 
158
                                while x <= bound.xMaximum():
 
159
                                        pt1 = QgsPoint(x, y)
 
160
                                        pt2 = QgsPoint(x + xOffset, y)
 
161
                                        pt3 = QgsPoint(x + xOffset, y - yOffset)
 
162
                                        pt4 = QgsPoint(x, y - yOffset)
 
163
                                        pt5 = QgsPoint(x, y)
 
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)
 
172
                                        idVar = idVar + 1
 
173
                                        x = x + xOffset
 
174
                                y = y - yOffset
 
175
                self.progressBar.setRange( 0, 100 )
 
176
                del writer
 
177
 
 
178
        def outFile(self):
 
179
                self.outShape.clear()
 
180
                ( self.shapefileName, self.encoding ) = ftools_utils.saveDialog( self )
 
181
                if self.shapefileName is None or self.encoding is None:
 
182
                        return
 
183
                self.outShape.setText( QString( self.shapefileName ) )
 
184
 
 
185
        def getVectorLayerByName(self, myName):
 
186
                mc = self.iface.mapCanvas()
 
187
                nLayers = mc.layerCount()
 
188
                for l in range(nLayers):
 
189
                        layer = mc.layer(l)
 
190
                        if unicode(layer.name()) == unicode(myName):
 
191
                                vlayer = QgsVectorLayer(unicode(layer.source()),  unicode(myName),  unicode(layer.dataProvider().name()))
 
192
                                if vlayer.isValid():
 
193
                                        return vlayer
 
194
 
 
195
        def getMapLayerByName(self, myName):
 
196
                mc = self.iface.mapCanvas()
 
197
                nLayers = mc.layerCount()
 
198
                for l in range(nLayers):
 
199
                        layer = mc.layer(l)
 
200
                        if unicode(layer.name()) == unicode(myName):
 
201
                                if layer.isValid():
 
202
                                        return layer