Geography Reference
In-Depth Information
dlg.show()
5
result = dlg.exec_()
-
# See if OK was pressed
-
if result == 1:
-
# Get the coordinates and scale factor from the dialog
-
x = dlg.ui.xCoord.text()
10
y = dlg.ui.yCoord.text()
-
scale = dlg.ui.spinBoxScale.value()
-
# Create a rectangle to cover the new extent
-
rect = QgsRect(float(x)-scale,float(y)-scale,float(x)+scale,float(y)+scale)
-
# Get the map canvas
15
mc=self.iface.getMapCanvas()
-
# Set the extent to our new rectangle
-
mc.setExtent(rect)
-
# Refresh the map
-
mc.refresh()
20
Let's step through the run ( ) method to see how it works. On line 3, we
print a message to the terminal to let us know what's happening. If you
start QGIS from a command shell in either Linux or OS X, you can see
messages that are sent to the terminal. In this case, we use it just to
ease our paranoia about whether the run ( ) method is getting called.
The next step is to create the dialog box (line 4) and then display it
using exec_ ( ). This causes the dialog box to show itself and then wait
for some user interaction. The dialog box remains up until either the
OK or Cancel button is clicked. Once a button is clicked, we test to see
whether it was the OK button on line 8. If so, we are then ready to zoom
the map.
First we have to retrieve the x and y coordinates and the scale that you
entered on the dialog box (lines 10 through 12). We store these in local
variables just to make the next step a bit more readable in the code.
Once we have the user inputs, we need to create a rectangle that we
can use in setting the map extent. The QGIS API has a QgsRect class
that is used for this purpose. On line 14, we create the rectangle by
simply expanding the x and y values by the scale value. Once we have
the rectangle, we are ready to zoom the map. First we get a reference
to the QGIS map canvas on line 16, using the iface reference we saved
in the __init__ ( ) method. Then it's simply a matter of calling the map
canvas setExtent ( ) method. To actually get the map to zoom, we call
the map canvas refresh ( ) method and the map zooms to the rectangle
we specified. Once complete, our plugin stands by ready for the next
request.
 
Search WWH ::




Custom Search