#-------------------------------------------------------------------------- #-------------------------------------------------------------------------- # # QGISLite - An Open Source GIS tool based on the QGIS core libraries # and written in Python # # Copyright (C) 2008 Aaron Racicot, Z-Pulley Inc. # # EMAIL: aaronr (at) z-pulley.com # WEB : http://www.reprojected.com # TRAC : http://trac.reprojected.com/qgislite # SVN : http://svn.reprojected.com/qgislite # #-------------------------------------------------------------------------- #-------------------------------------------------------------------------- # # licensed under the terms of GNU GPL 2 # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # #-------------------------------------------------------------------------- # PyQt4 from PyQt4.QtCore import QObject,QString,QFileInfo,SIGNAL,SLOT from PyQt4.QtGui import QFileDialog # QGIS bindings from qgis.core import QGis,QgsVectorLayer,QgsRasterLayer,QgsMapLayerRegistry from qgis.gui import QgsMapToolPan,QgsMapToolZoom,QgsMapCanvasLayer # General system includes import sys,string # The basic map tools class BasicMapTools(object): def __init__(self, mainwindow): self.mainwindow = mainwindow self.canvas = mainwindow.canvas self.statusbar = mainwindow.statusbar self.layers = mainwindow.layers # Actions QObject.connect(mainwindow.mpActionAddVectorLayer, SIGNAL("triggered()"), self.addVectorLayer) QObject.connect(mainwindow.mpActionAddRasterLayer, SIGNAL("triggered()"), self.addRasterLayer) QObject.connect(mainwindow.mpActionZoomIn, SIGNAL("triggered()"), self.zoomIn) QObject.connect(mainwindow.mpActionZoomOut, SIGNAL("triggered()"), self.zoomOut) QObject.connect(mainwindow.mpActionPan, SIGNAL("triggered()"), self.pan) # Create a little toolbar for basic map tools self.toolbar = mainwindow.addToolBar("File") self.toolbar.addAction(mainwindow.mpActionAddVectorLayer) self.toolbar.addAction(mainwindow.mpActionAddRasterLayer) self.toolbar.addAction(mainwindow.mpActionZoomIn) self.toolbar.addAction(mainwindow.mpActionZoomOut) self.toolbar.addAction(mainwindow.mpActionPan) # Create the basic map tools self.toolPan = QgsMapToolPan(self.canvas) self.toolPan.setAction(mainwindow.mpActionPan) self.toolZoomIn = QgsMapToolZoom(self.canvas, False) # false = in self.toolZoomIn.setAction(mainwindow.mpActionZoomIn) self.toolZoomOut = QgsMapToolZoom(self.canvas, True) # true = out self.toolZoomOut.setAction(mainwindow.mpActionZoomOut) # Default to zoom in tool self.canvas.setMapTool(self.toolZoomIn) self.canvas.setMapUnits(QGis.UnitType(0)) self.canvas.updateScale() # Signal handeler for zoom in button def zoomIn(self): self.canvas.setMapTool(self.toolZoomIn) # Signal handeler for zoom out button def zoomOut(self): self.canvas.setMapTool(self.toolZoomOut) # Signal handeler for pan button def pan(self): self.canvas.setMapTool(self.toolPan) # Signal handeler for add vector layer button def addVectorLayer(self): qd=QFileDialog() filter_str = QString("*.shp") f=qd.getOpenFileName(self.mainwindow,QString(),QString(),filter_str) # Check for cancel if len(f) == 0: return capture_string = QString(f) self.statusbar.showMessage(capture_string) info = QFileInfo(QString(f)) # create layer layer = QgsVectorLayer(QString(f), info.completeBaseName(), "ogr") if not layer.isValid(): capture_string = QString("ERROR reading file") self.statusbar.showMessage(capture_string) return if self.mainwindow.srs == None: self.mainwindow.srs = layer.srs() # set extent to the extent of our layer self.mainwindow.canvas.setExtent(layer.extent()) QgsMapLayerRegistry.instance().addMapLayer(layer) # set the map canvas layer set cl = QgsMapCanvasLayer(layer) self.layers.insert(0,cl) self.canvas.setLayerSet(self.layers) # Signal handeler for add raster layer button def addRasterLayer(self): qd=QFileDialog() filter_str = QString("*.tif") f=qd.getOpenFileName(self.mainwindow,QString(),QString(),filter_str) # Check for cancel if len(f) == 0: return capture_string = QString(f) self.statusbar.showMessage(capture_string) info = QFileInfo(QString(f)) # create layer layer = QgsRasterLayer(info.filePath(), info.completeBaseName()) if not layer.isValid(): capture_string = QString("ERROR reading file") self.statusbar.showMessage(capture_string) return if self.mainwindow.srs == None: self.mainwindow.srs = layer.srs() # set extent to the extent of our layer self.mainwindow.canvas.setExtent(layer.extent()) # add layer to the registry QgsMapLayerRegistry.instance().addMapLayer(layer) # set the map canvas layer set cl = QgsMapCanvasLayer(layer) self.layers.insert(0,cl) self.canvas.setLayerSet(self.layers)