98 lines
3.1 KiB
Python

# -*- 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 processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterTable
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterSelection
from processing.core.outputs import OutputVector
from processing.algs.gdal.GdalUtils import GdalUtils
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
DIALECTS = [None, 'ogrsql', 'sqlite']
class OgrSql(GdalAlgorithm):
INPUT = 'INPUT'
OUTPUT = 'OUTPUT'
SQL = 'SQL'
DIALECT = 'DIALECT'
def __init__(self):
super().__init__()
def initAlgorithm(self, config=None):
self.addParameter(ParameterTable(self.INPUT, self.tr('Input layer or table')))
self.addParameter(ParameterString(self.SQL, self.tr('SQL'), ''))
self.addParameter(ParameterSelection(
self.DIALECT,
self.tr('Dialect'),
DIALECTS)
)
self.addOutput(OutputVector(self.OUTPUT, self.tr('SQL result')))
def name(self):
return 'executesql'
def displayName(self):
return self.tr('Execute SQL')
def group(self):
return self.tr('Vector miscellaneous')
def getConsoleCommands(self, parameters, context, feedback):
sql = self.getParameterValue(self.SQL)
if sql == '':
raise GeoAlgorithmExecutionException(
self.tr('Empty SQL. Please enter valid SQL expression and try again.'))
arguments = []
arguments.append('-sql')
arguments.append(sql)
dialectIdx = self.getParameterValue(self.DIALECT)
dialect = DIALECTS[dialectIdx]
if dialect:
arguments.append("-dialect")
arguments.append(dialect)
output = self.getOutputFromName(self.OUTPUT)
outFile = output.value
arguments.append(outFile)
layer = self.getParameterValue(self.INPUT)
conn = GdalUtils.ogrConnectionString(layer, context)[1:-1]
arguments.append(conn)
return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]