Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- # Import the PyQt and QGIS libraries
- from PyQt4.QtCore import *
- from PyQt4.QtGui import *
- from PyQt4.QtWebKit import *
- from qgis.core import *
- class forcedScale():
- # pre-defined scales
- predefinedScales = [
- 5000,
- 10000,
- 25000,
- 50000,
- 100000,
- 250000,
- 500000
- ]
- # mapCanvas
- mc = None
- # avoid loop
- replayed = False
- def __init__( self, *predefinedScales ):
- '''
- Initialize class instance
- '''
- mc = iface.mapCanvas()
- self.mc = mc
- self.mc.scaleChanged.connect(self.setScale)
- if predefinedScales:
- self.predefinedScales = predefinedScales
- def setScale( self, scale ):
- if self.replayed:
- return
- print "initial scale: %s" % scale
- targetScale = min(
- self.predefinedScales,
- key=lambda x:abs(x-scale)
- )
- if targetScale == scale:
- return
- self.replayed = True
- print "zoom to %s" % targetScale
- self.mc.zoomScale( targetScale )
- self.replayed = False
- # You can instanciate this class this way
- # fs = forcedScale()
- # which will use the scales defined in the property predefinedScales of the class,
- # or you can provide a list of scales like this
- # fs = forcedScale( 25000, 50000, 100000 )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement