class PostGISExecuteSQL(GeoAlgorithm): DATABASE = 'DATABASE' SQL = 'SQL' def getIcon(self): return QIcon(os.path.dirname(__file__) + '/../../images/postgis.png') def processAlgorithm(self, progress): connection = self.getParameterValue(self.DATABASE) settings = QSettings() mySettings = '/PostgreSQL/connections/' + connection try: database = settings.value(mySettings + '/database') username = settings.value(mySettings + '/username') host = settings.value(mySettings + '/host') port = settings.value(mySettings + '/port', type=int) password = settings.value(mySettings + '/password') except Exception, e: raise GeoAlgorithmExecutionException( 'Wrong database connection name: ' + connection) try: self.db = postgis_utils.GeoDB(host=host, port=port, dbname=database, user=username, passwd=password) except postgis_utils.DbError, e: raise GeoAlgorithmExecutionException( "Couldn't connect to database:\n" + e.message)
def __init__(self,connstring): parmlist=connstring.split(" ") self.db = postgis_utils.GeoDB(host=parmlist[2].split("=")[1],dbname=parmlist[1].split("=")[1],user=parmlist[3].split("=")[1],passwd=parmlist[4].split("=")[1],port=int(parmlist[5].split("=")[1])) if self.db==None: QtGui.QMessageBox.warning(None,"Error","Connection failed to "+connstring) return self.cursor = self.db.con.cursor()
class ImportIntoPostGIS(GeoAlgorithm): DATABASE = 'DATABASE' TABLENAME = 'TABLENAME' SCHEMA = 'SCHEMA' INPUT = 'INPUT' OVERWRITE = 'OVERWRITE' CREATEINDEX = 'CREATEINDEX' GEOMETRY_COLUMN = 'GEOMETRY_COLUMN' LOWERCASE_NAMES = 'LOWERCASE_NAMES' DROP_STRING_LENGTH = 'DROP_STRING_LENGTH' PRIMARY_KEY = 'PRIMARY_KEY' def getIcon(self): return QIcon(os.path.dirname(__file__) + '/../../images/postgis.png') def processAlgorithm(self, progress): connection = self.DB_CONNECTIONS[self.getParameterValue(self.DATABASE)] schema = self.getParameterValue(self.SCHEMA) overwrite = self.getParameterValue(self.OVERWRITE) createIndex = self.getParameterValue(self.CREATEINDEX) convertLowerCase = self.getParameterValue(self.LOWERCASE_NAMES) dropStringLength = self.getParameterValue(self.DROP_STRING_LENGTH) primaryKeyField = self.getParameterValue(self.PRIMARY_KEY) settings = QSettings() mySettings = '/PostgreSQL/connections/' + connection try: database = settings.value(mySettings + '/database') username = settings.value(mySettings + '/username') host = settings.value(mySettings + '/host') port = settings.value(mySettings + '/port', type=int) password = settings.value(mySettings + '/password') except Exception, e: raise GeoAlgorithmExecutionException( 'Wrong database connection name: ' + connection) table = self.getParameterValue(self.TABLENAME) table.replace(' ', '') providerName = 'postgres' try: db = postgis_utils.GeoDB(host=host, port=port, dbname=database, user=username, passwd=password) except postgis_utils.DbError, e: raise GeoAlgorithmExecutionException( "Couldn't connect to database:\n" + e.message)
class PostgisLayer: def __init__(self, iface, host, port, dbname, user, passwd): # Save reference to the QGIS interface self.iface = iface self.host = host self.port = port self.dbname = dbname self.user = user self.passwd = passwd def initGui(self): # Create action that will start plugin configuration self.action = QAction(QIcon(":/plugins/postgislayer/icon.png"), "Fast SQL Layer", self.iface.mainWindow()) #Add toolbar button and menu item self.iface.addToolBarIcon(self.action) #load the form path = os.path.dirname(os.path.abspath(__file__)) self.dock = uic.loadUi(os.path.join(path, "ui_postgislayer.ui")) self.iface.addDockWidget(Qt.BottomDockWidgetArea, self.dock) #connect the action to the run method QObject.connect(self.action, SIGNAL("triggered()"), self.show) QObject.connect(self.dock.buttonRun, SIGNAL('clicked()'), self.run) #populate the id and the_geom combos self.dock.uniqueCombo.addItem('id') self.dock.geomCombo.addItem('the_geom') #start the highlight engine self.higlight_text = hl.Highlighter(self.dock.textQuery.document(), "sql") def show(self): self.iface.addDockWidget(Qt.BottomDockWidgetArea, self.dock) def unload(self): # Remove the plugin menu item and icon self.iface.removeToolBarIcon(self.action) def run(self): try: import psycopg2 except ImportError, e: QMessageBox.information( self.iface.mainWindow(), "Warning", "Couldn't import Python module 'psycopg2' for communication with PostgreSQL database. Without it you won't be able to run this tool. Please install it." ) return uniqueFieldName = self.dock.uniqueCombo.currentText() geomFieldName = self.dock.geomCombo.currentText() try: db = postgis_utils.GeoDB(self.host, int(self.port), self.dbname, self.user, self.passwd) except postgis_utils.DbError, e: QMessageBox.critical(self.iface.mainWindow(), "error", "Couldn't connect to database:\n" + e.msg) return