#--------------------------------------------------------------------- # # mac_noise - A QGIS plugin set to work with flight noise data # for the Metropolitan Airports Commission of Minneapolis, MN. # # Copyright (C) 2008 Aaron Racicot, Z-Pulley Inc. # Copyright (C) 2008 Metropolitan Airports Commission of Minneapolis # # EMAIL: aaronr (at) z-pulley.com # WEB : www.z-pulley.com # www.reprojected.com # #--------------------------------------------------------------------- # # 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. # #--------------------------------------------------------------------- from PyQt4.QtCore import * from PyQt4.QtGui import * from qgis.core import * from qgis.gui import * import resources from querybuilder.mac_querybuilder import QueryBuilderWindow from gate.mac_gate import GateWindow from elevation.mac_elevation import ElevationWindow from mac_dbconnection import MacNewConnectionBase import os import tempfile class MacNoisePlugin(QObject): def __init__(self, iface): QObject.__init__(self) # Save a reference to the QGIS iface self.iface = iface self.canvas = iface.mapCanvas() def initGui(self): # Default db params... # Setup the default URI and try to open the data layer... self.name = "MAC Query Layer" self.dbname = "test_db" self.host = "localhost" self.port = "5432" self.user = "aaronr" self.passwd = "" self.table="mac_flights2" self.schema="testing" self.geomcol="the_geom" self.macLayer = None # Create Query Builder Action self.querybuilder_action = QAction(QIcon(":/mac_querybuilder.png"), "MAC Query Builder", self.iface.mainWindow()) self.querybuilder_action.setWhatsThis("MAC Query Builder") QObject.connect(self.querybuilder_action, SIGNAL("activated()"), self.querybuilder_run) # Create Gate Action self.gate_action = QAction(QIcon(":/mac_gate.png"), "MAC Gate Analysis", self.iface.mainWindow()) self.gate_action.setWhatsThis("MAC Gate Analysis") QObject.connect(self.gate_action, SIGNAL("activated()"), self.gate_run) self.gate_action.setEnabled(False) # Create Elevation Action self.elevation_action = QAction(QIcon(":/mac_elevation.png"), "MAC Elevation Analysis", self.iface.mainWindow()) self.elevation_action.setWhatsThis("MAC Elevation Analysis") QObject.connect(self.elevation_action, SIGNAL("activated()"), self.elevation_run) self.helpaction = QAction(QIcon(":/mac_noisehelp.png"), "About", self.iface.mainWindow()) self.helpaction.setWhatsThis("MAC Flight Noise Help") QObject.connect(self.helpaction, SIGNAL("activated()"), self.helprun) # Add to the main toolbar self.iface.addToolBarIcon(self.querybuilder_action) self.iface.addPluginToMenu("MAC Flight Noise", self.querybuilder_action) self.iface.addToolBarIcon(self.gate_action) self.iface.addPluginToMenu("MAC Flight Noise", self.gate_action) self.iface.addToolBarIcon(self.elevation_action) self.iface.addPluginToMenu("MAC Flight Noise", self.elevation_action) self.iface.addPluginToMenu("MAC Flight Noise", self.helpaction) pluginMenu = self.iface.pluginMenu() for action in pluginMenu.actions(): if action.text() == QString("MAC Flight Noise"): # update the icon action.setIcon(QIcon(":/mac_noise.png")) # Set up the GUIS needed... just dont show them flags = Qt.WindowTitleHint | Qt.WindowSystemMenuHint | Qt.WindowMaximizeButtonHint self.querybuilder_gui = QueryBuilderWindow(self.iface,flags,self) flags = Qt.WindowTitleHint | Qt.WindowSystemMenuHint | Qt.WindowMaximizeButtonHint self.dbconnection_gui = MacNewConnectionBase(self.iface,flags,self) flags = Qt.WindowTitleHint | Qt.WindowSystemMenuHint | Qt.WindowMaximizeButtonHint self.gate_gui = GateWindow(self.iface.mainWindow(),flags) self.gate_gui.setModal(True) flags = Qt.WindowTitleHint | Qt.WindowSystemMenuHint | Qt.WindowMaximizeButtonHint self.elevation_gui = ElevationWindow(self.iface,flags,self) self.elevation_gui.setModal(True) def unload(self): # Remove the plugin self.iface.removePluginMenu("MAC Flight Noise",self.querybuilder_action) self.iface.removePluginMenu("MAC Flight Noise",self.gate_action) self.iface.removePluginMenu("MAC Flight Noise",self.elevation_action) self.iface.removePluginMenu("MAC Flight Noise",self.helpaction) self.iface.removeToolBarIcon(self.querybuilder_action) self.iface.removeToolBarIcon(self.gate_action) self.iface.removeToolBarIcon(self.elevation_action) def helprun(self): # print "Help pressed..." infoString = QString("Written by Aaron Racicot\naaronr@z-pulley.com\n") infoString = infoString.append("Company - http://www.z-pulley.com\n\n") infoString = infoString.append("Blog - http://www.reprojected.com\n\n") infoString = infoString.append("Source: http://svn.reprojected.com/") infoString = infoString.append("qgisplugins/trunk/mac_noise\n") infoString = infoString.append("TRAC: http://trac.reprojected.com/") infoString = infoString.append("qgisplugins\n") QMessageBox.information(self.iface.mainWindow(), "MAC Flight Noise",infoString) def getFieldList(self, vlayer): fProvider = vlayer.dataProvider() feat = QgsFeature() allAttrs = fProvider.attributeIndexes() fProvider.select(allAttrs) myFields = fProvider.fields() return myFields def createURI(self,uri = None): if uri == None: uri = QgsDataSourceURI() # set host name, port, database name, username and password uri.setConnection(self.host, self.port, self.dbname, self.user, self.passwd) uri.setDataSource(self.schema, self.table, self.geomcol, "") return uri def querybuilder_run(self): # Run the querybuilder print "Query Builder Pressed" #activeLayer = self.iface.activeLayer() #activeLayerIsValidForMAC = False #if activeLayer and (activeLayer.type() == activeLayer.VectorLayer): # fieldList = self.getFieldList(activeLayer) # for field in fieldList: # if fieldList[field].name() == QString("airport"): # activeLayerIsValidForMAC = True #psyUri=QString("dbname='%s' host='%s' port='%s' user='%s'" % (dbname,host,port,user)) #connection = psycopg2.connect(str(psyUri)) #mark = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) #self.connectionString.setText(psyUri) #uri = QgsDataSourceURI() # set host name, port, database name, username and password #uri.setConnection(self.host, self.port, self.dbname, self.user, self.passwd) # set database schema, table name, geometry column and optionaly subset (WHERE clause) #uri.setDataSource("public", self.table, "the_geom", "") if self.macLayer == None: uri = self.createURI() macLayer = QgsVectorLayer(uri.uri(), "MAC Noise Layer", "postgres") if macLayer.isValid(): self.macLayer = macLayer self.connect(QgsMapLayerRegistry.instance(), SIGNAL("layerWillBeRemoved(QString)"), self.layerWillBeRemoved) if self.macLayer != None and self.macLayer.isValid(): #print "Layer Valid..." # Now launch the querybuilder # flags = Qt.WindowTitleHint | Qt.WindowSystemMenuHint | Qt.WindowMaximizeButtonHint # self.querybuilder_gui = QueryBuilderWindow(self.iface,flags,macLayer) # self.querybuilder_gui.setModal(False) self.querybuilder_gui.setupMyUI(self.macLayer) self.querybuilder_gui.show() else: #print "Failed to load layer!" # Launch a window to have the user adjust the db params... # flags = Qt.WindowTitleHint | Qt.WindowSystemMenuHint | Qt.WindowMaximizeButtonHint # self.dbconnection_gui = MacNewConnectionBase(self.iface,flags) # Set the title to an error with connection... self.dbconnection_gui.setWindowTitle(QString("Error connecting... please modify connection params")) self.dbconnection_gui.show() def layerWillBeRemoved(self,layerID): print "Layer will be removed - " + str(layerID) if self.macLayer != None and self.macLayer.getLayerID() == layerID: # Our layer is going away and we need to clean up... self.macLayer = None # Let the querybuilder know... self.querybuilder_gui.layerWillBeRemoved(layerID) def gate_run(self): # Run the gate print "Gate Analysis Pressed" #flags = Qt.WindowTitleHint | Qt.WindowSystemMenuHint | Qt.WindowMaximizeButtonHint #self.gate_gui = GateWindow(self.iface.mainWindow(),flags) #self.gate_gui.setModal(True) self.gate_gui.show() def elevation_run(self): # Run the elevation #print "Elevation Analysis Pressed" #try: # import psycopg2 # import psycopg2.extras #except ImportError: # print "Unable to import psycopg2" # QMessageBox.warning(self.iface.mainWindow(), "Warning", "Unable to import psycopg2") # return #try: # import PyQt4.Qwt5 #except ImportError: # print "Unable to import PyQt4.Qwt5" # QMessageBox.warning(self.iface.mainWindow(), "Warning", "Unable to import PyQt4.Qwt5") # return if self.elevation_gui.setupMyUI(): self.elevation_gui.show() else: # pop up a warning about selecting a layer QMessageBox.warning(self.iface.mainWindow(), "Warning", "Please select a valid MAC layer")