# -*- coding: utf-8 -*- """ *************************************************************************** ogrsql.py --------------------- Date : November 2012 Copyright : (C) 2012 by Victor Olaya Email : volayaf at gmail dot com *************************************************************************** * * * 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. * * * *************************************************************************** """ __author__ = 'Victor Olaya' __date__ = 'November 2012' __copyright__ = '(C) 2012, Victor Olaya' # This will get replaced with a git SHA1 when you do a git archive __revision__ = '$Format:%H$' from sextante.outputs.OutputHTML import OutputHTML from sextante.parameters.ParameterVector import ParameterVector from sextante.parameters.ParameterString import ParameterString from sextante.core.SextanteLog import SextanteLog from qgis.core import * from PyQt4.QtCore import * from PyQt4.QtGui import * import string import re try: from osgeo import ogr ogrAvailable = True except: ogrAvailable = False from sextante.gdal.OgrAlgorithm import OgrAlgorithm class OgrSql(OgrAlgorithm): #constants used to refer to parameters and outputs. #They will be used when calling the algorithm from another algorithm, #or when calling SEXTANTE from the QGIS console. OUTPUT = "OUTPUT" INPUT_LAYER = "INPUT_LAYER" SQL = "SQL" def defineCharacteristics(self): self.name = "Execute SQL" self.group = "[OGR] Miscellaneous" #we add the input vector layer. It can have any kind of geometry #It is a mandatory (not optional) one, hence the False argument self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_ANY, False)) self.addParameter(ParameterString(self.SQL, "SQL", "")) self.addOutput(OutputHTML(self.OUTPUT, "SQL result")) def processAlgorithm(self, progress): if not ogrAvailable: SextanteLog.addToLog(SextanteLog.LOG_ERROR, "OGR bindings not installed" ) return input = self.getParameterValue(self.INPUT_LAYER) sql = self.getParameterValue(self.SQL) ogrLayer = self.ogrConnectionString(input) output = self.getOutputValue(self.OUTPUT) qDebug("Opening data source '%s'" % ogrLayer) poDS = ogr.Open( ogrLayer, False ) if poDS is None: SextanteLog.addToLog(SextanteLog.LOG_ERROR, self.failure(ogrLayer)) return result = self.select_values(poDS, sql) f = open(output, "w") f.write("
"+col+" | ") f.write("