33
33
import ftools_utils
34
34
from qgis .core import *
35
35
from ui_frmVectorGrid import Ui_Dialog
36
+ import math
36
37
37
38
class Dialog (QDialog , Ui_Dialog ):
38
39
def __init__ (self , iface ):
@@ -44,6 +45,7 @@ def __init__(self, iface):
44
45
#QObject.connect(self.inShape, SIGNAL("currentIndexChanged(QString)"), self.updateInput)
45
46
QObject .connect (self .btnUpdate , SIGNAL ("clicked()" ), self .updateLayer )
46
47
QObject .connect (self .btnCanvas , SIGNAL ("clicked()" ), self .updateCanvas )
48
+ QObject .connect (self .chkAlign , SIGNAL ("toggled(bool)" ), self .chkAlignToggled )
47
49
self .buttonOk = self .buttonBox_2 .button ( QDialogButtonBox .Ok )
48
50
self .setWindowTitle (self .tr ("Vector grid" ))
49
51
self .xMin .setValidator (QDoubleValidator (self .xMin ))
@@ -66,12 +68,50 @@ def updateLayer( self ):
66
68
mLayerName = self .inShape .currentText ()
67
69
if not mLayerName == "" :
68
70
mLayer = ftools_utils .getMapLayerByName ( unicode ( mLayerName ) )
71
+ # get layer extents
69
72
boundBox = mLayer .extent ()
73
+ # if "align extents and resolution..." button is checked
74
+ if self .chkAlign .isChecked ():
75
+ if not mLayer .type () == QgsMapLayer .RasterLayer :
76
+ QMessageBox .information (self , self .tr ("Vector grid" ), self .tr ("Please select a raster layer" ))
77
+ else :
78
+ dx = math .fabs (boundBox .xMaximum ()- boundBox .xMinimum ()) / mLayer .width ()
79
+ dy = math .fabs (boundBox .yMaximum ()- boundBox .yMinimum ()) / mLayer .height ()
80
+ self .spnX .setValue (dx )
81
+ self .spnY .setValue (dy )
70
82
self .updateExtents ( boundBox )
71
83
72
84
def updateCanvas ( self ):
73
85
canvas = self .iface .mapCanvas ()
74
86
boundBox = canvas .extent ()
87
+ # if "align extents and resolution..." button is checked
88
+ if self .chkAlign .isChecked ():
89
+ mLayerName = self .inShape .currentText ()
90
+ if not mLayerName == "" :
91
+ mLayer = ftools_utils .getMapLayerByName ( unicode ( mLayerName ) )
92
+ if not mLayer .type () == QgsMapLayer .RasterLayer :
93
+ QMessageBox .information (self , self .tr ("Vector grid" ), self .tr ("Please select a raster layer" ))
94
+ else :
95
+ # get extents and pixel size
96
+ xMin = boundBox .xMinimum ()
97
+ yMin = boundBox .yMinimum ()
98
+ xMax = boundBox .xMaximum ()
99
+ yMax = boundBox .yMaximum ()
100
+ boundBox2 = mLayer .extent ()
101
+ dx = math .fabs (boundBox2 .xMaximum ()- boundBox2 .xMinimum ()) / mLayer .width ()
102
+ dy = math .fabs (boundBox2 .yMaximum ()- boundBox2 .yMinimum ()) / mLayer .height ()
103
+ # get pixels from the raster that are closest to the desired extent
104
+ newXMin = self .getClosestPixel ( boundBox2 .xMinimum (), boundBox .xMinimum (), dx , True )
105
+ newXMax = self .getClosestPixel ( boundBox2 .xMaximum (), boundBox .xMaximum (), dx , False )
106
+ newYMin = self .getClosestPixel ( boundBox2 .yMinimum (), boundBox .yMinimum (), dy , True )
107
+ newYMax = self .getClosestPixel ( boundBox2 .yMaximum (), boundBox .yMaximum (), dy , False )
108
+ # apply new values if found all min/max
109
+ if newXMin is not None and newXMax is not None and newYMin is not None and newYMax is not None :
110
+ boundBox .set ( newXMin , newYMin , newXMax , newYMax )
111
+ self .spnX .setValue (dx )
112
+ self .spnY .setValue (dy )
113
+ else :
114
+ QMessageBox .information (self , self .tr ("Vector grid" ), self .tr ("Unable to compute extents aligned on selected raster layer" ))
75
115
self .updateExtents ( boundBox )
76
116
77
117
def updateExtents ( self , boundBox ):
@@ -185,3 +225,39 @@ def outFile(self):
185
225
if self .shapefileName is None or self .encoding is None :
186
226
return
187
227
self .outShape .setText ( QString ( self .shapefileName ) )
228
+
229
+ def chkAlignToggled (self ):
230
+ if self .chkAlign .isChecked ():
231
+ self .spnX .setEnabled ( False )
232
+ self .lblX .setEnabled ( False )
233
+ self .spnY .setEnabled ( False )
234
+ self .lblY .setEnabled ( False )
235
+ else :
236
+ self .spnX .setEnabled ( True )
237
+ self .lblX .setEnabled ( True )
238
+ self .spnY .setEnabled ( not self .chkLock .isChecked () )
239
+ self .lblY .setEnabled ( not self .chkLock .isChecked () )
240
+
241
+ def getClosestPixel (self , startVal , targetVal , step , isMin ):
242
+ foundVal = None
243
+ tmpVal = startVal
244
+ # find pixels covering the extent - slighlyt inneficient b/c loop on all elements before xMin
245
+ if targetVal < startVal :
246
+ backOneStep = not isMin
247
+ step = - step
248
+ while foundVal is None :
249
+ if tmpVal <= targetVal :
250
+ if backOneStep :
251
+ tmpVal -= step
252
+ foundVal = tmpVal
253
+ tmpVal += step
254
+ else :
255
+ backOneStep = isMin
256
+ while foundVal is None :
257
+ if tmpVal >= targetVal :
258
+ if backOneStep :
259
+ tmpVal -= step
260
+ foundVal = tmpVal
261
+ tmpVal += step
262
+ return foundVal
263
+
0 commit comments