mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-27 00:33:48 -05:00
98 lines
3.1 KiB
Python
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
|
|
|
|
from processing.tools.vector import ogrConnectionString
|
|
|
|
|
|
DIALECTS = [None, 'ogrsql', 'sqlite']
|
|
|
|
|
|
class OgrSql(GdalAlgorithm):
|
|
|
|
INPUT = 'INPUT'
|
|
OUTPUT = 'OUTPUT'
|
|
SQL = 'SQL'
|
|
DIALECT = 'DIALECT'
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
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):
|
|
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 = ogrConnectionString(layer)[1:-1]
|
|
arguments.append(conn)
|
|
|
|
return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
|