|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + ogr2ogrtabletopostgislist.py |
| 6 | + --------------------- |
| 7 | + Date : November 2012 |
| 8 | + Copyright : (C) 2012 by Victor Olaya |
| 9 | + Email : volayaf at gmail dot com |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | +__author__ = 'Victor Olaya' |
| 21 | +__date__ = 'November 2012' |
| 22 | +__copyright__ = '(C) 2012, Victor Olaya' |
| 23 | + |
| 24 | +# This will get replaced with a git SHA1 when you do a git archive |
| 25 | + |
| 26 | +__revision__ = '$Format:%H$' |
| 27 | + |
| 28 | +from PyQt4.QtCore import QSettings |
| 29 | + |
| 30 | +from processing.core.parameters import ParameterVector |
| 31 | +from processing.core.parameters import ParameterString |
| 32 | +from processing.core.parameters import ParameterTable |
| 33 | +from processing.core.parameters import ParameterCrs |
| 34 | +from processing.core.parameters import ParameterSelection |
| 35 | +from processing.core.parameters import ParameterBoolean |
| 36 | +from processing.core.parameters import ParameterExtent |
| 37 | +from processing.core.parameters import ParameterTableField |
| 38 | + |
| 39 | +from processing.tools.system import isWindows |
| 40 | + |
| 41 | +from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm |
| 42 | +from processing.algs.gdal.GdalUtils import GdalUtils |
| 43 | + |
| 44 | +class Ogr2OgrTableToPostGisList(OgrAlgorithm): |
| 45 | + |
| 46 | + DATABASE = 'DATABASE' |
| 47 | + INPUT_LAYER = 'INPUT_LAYER' |
| 48 | + HOST = 'HOST' |
| 49 | + PORT= 'PORT' |
| 50 | + USER = 'USER' |
| 51 | + DBNAME = 'DBNAME' |
| 52 | + PASSWORD = 'PASSWORD' |
| 53 | + SCHEMA = 'SCHEMA' |
| 54 | + TABLE = 'TABLE' |
| 55 | + PK = 'PK' |
| 56 | + PRIMARY_KEY = 'PRIMARY_KEY' |
| 57 | + WHERE = 'WHERE' |
| 58 | + GT = 'GT' |
| 59 | + OVERWRITE = 'OVERWRITE' |
| 60 | + APPEND = 'APPEND' |
| 61 | + ADDFIELDS = 'ADDFIELDS' |
| 62 | + LAUNDER = 'LAUNDER' |
| 63 | + SKIPFAILURES = 'SKIPFAILURES' |
| 64 | + PRECISION = 'PRECISION' |
| 65 | + OPTIONS = 'OPTIONS' |
| 66 | + |
| 67 | + def dbConnectionNames(self): |
| 68 | + settings = QSettings() |
| 69 | + settings.beginGroup('/PostgreSQL/connections/') |
| 70 | + return settings.childGroups() |
| 71 | + |
| 72 | + def defineCharacteristics(self): |
| 73 | + self.name = 'Import layer/table as table into PostGIS database (available connections)' |
| 74 | + self.group = '[OGR] Miscellaneous' |
| 75 | + self.DB_CONNECTIONS = self.dbConnectionNames() |
| 76 | + self.addParameter(ParameterSelection(self.DATABASE, |
| 77 | + self.tr('Database (connection name)'), self.DB_CONNECTIONS)) |
| 78 | + self.addParameter(ParameterTable(self.INPUT_LAYER, |
| 79 | + self.tr('Input layer'))) |
| 80 | + self.addParameter(ParameterString(self.SCHEMA, |
| 81 | + self.tr('Schema name'), 'public', optional=True)) |
| 82 | + self.addParameter(ParameterString(self.TABLE, |
| 83 | + self.tr('Table name, leave blank to use input name'), |
| 84 | + '', optional=True)) |
| 85 | + self.addParameter(ParameterString(self.PK, |
| 86 | + self.tr('Primary key'), 'id', optional=True)) |
| 87 | + self.addParameter(ParameterTableField(self.PRIMARY_KEY, |
| 88 | + self.tr('Primary key (existing field, used if the above option is left empty)'), |
| 89 | + self.INPUT_LAYER, optional=True)) |
| 90 | + self.addParameter(ParameterString(self.WHERE, |
| 91 | + self.tr('Select features using a SQL "WHERE" statement (Ex: column="value")'), |
| 92 | + '', optional=True)) |
| 93 | + self.addParameter(ParameterString(self.GT, |
| 94 | + self.tr('Group N features per transaction (Default: 20000)'), |
| 95 | + '', optional=True)) |
| 96 | + self.addParameter(ParameterBoolean(self.OVERWRITE, |
| 97 | + self.tr('Overwrite existing table'), True)) |
| 98 | + self.addParameter(ParameterBoolean(self.APPEND, |
| 99 | + self.tr('Append to existing table'), False)) |
| 100 | + self.addParameter(ParameterBoolean(self.ADDFIELDS, |
| 101 | + self.tr('Append and add new fields to existing table'), False)) |
| 102 | + self.addParameter(ParameterBoolean(self.LAUNDER, |
| 103 | + self.tr('Do not launder columns/table names'), False)) |
| 104 | + self.addParameter(ParameterBoolean(self.SKIPFAILURES, |
| 105 | + self.tr('Continue after a failure, skipping the failed record'), |
| 106 | + False)) |
| 107 | + self.addParameter(ParameterBoolean(self.PRECISION, |
| 108 | + self.tr('Keep width and precision of input attributes'), |
| 109 | + True)) |
| 110 | + self.addParameter(ParameterString(self.OPTIONS, |
| 111 | + self.tr('Additional creation options'), '', optional=True)) |
| 112 | + |
| 113 | + def processAlgorithm(self, progress): |
| 114 | + connection = self.DB_CONNECTIONS[self.getParameterValue(self.DATABASE)] |
| 115 | + settings = QSettings() |
| 116 | + mySettings = '/PostgreSQL/connections/' + connection |
| 117 | + dbname = settings.value(mySettings + '/database') |
| 118 | + user = settings.value(mySettings + '/username') |
| 119 | + host = settings.value(mySettings + '/host') |
| 120 | + port = settings.value(mySettings + '/port') |
| 121 | + password = settings.value(mySettings + '/password') |
| 122 | + inLayer = self.getParameterValue(self.INPUT_LAYER) |
| 123 | + ogrLayer = self.ogrConnectionString(inLayer)[1:-1] |
| 124 | + schema = unicode(self.getParameterValue(self.SCHEMA)) |
| 125 | + schemastring = "-lco SCHEMA="+schema |
| 126 | + table = unicode(self.getParameterValue(self.TABLE)) |
| 127 | + pk = unicode(self.getParameterValue(self.PK)) |
| 128 | + pkstring = "-lco FID="+pk |
| 129 | + primary_key = self.getParameterValue(self.PRIMARY_KEY) |
| 130 | + where = unicode(self.getParameterValue(self.WHERE)) |
| 131 | + wherestring = "-where '"+where+"'" |
| 132 | + gt = unicode(self.getParameterValue(self.GT)) |
| 133 | + overwrite = self.getParameterValue(self.OVERWRITE) |
| 134 | + append = self.getParameterValue(self.APPEND) |
| 135 | + addfields = self.getParameterValue(self.ADDFIELDS) |
| 136 | + launder = self.getParameterValue(self.LAUNDER) |
| 137 | + launderstring = "-lco LAUNDER=NO" |
| 138 | + skipfailures = self.getParameterValue(self.SKIPFAILURES) |
| 139 | + precision = self.getParameterValue(self.PRECISION) |
| 140 | + options = unicode(self.getParameterValue(self.OPTIONS)) |
| 141 | + |
| 142 | + arguments = [] |
| 143 | + arguments.append('-progress') |
| 144 | + arguments.append('--config PG_USE_COPY YES') |
| 145 | + arguments.append('-f') |
| 146 | + arguments.append('PostgreSQL') |
| 147 | + arguments.append('PG:"host=') |
| 148 | + arguments.append(host) |
| 149 | + arguments.append('port=') |
| 150 | + arguments.append(port) |
| 151 | + if len(dbname) > 0: |
| 152 | + arguments.append('dbname=' + dbname) |
| 153 | + if len(password) > 0: |
| 154 | + arguments.append('password=' + password) |
| 155 | + arguments.append('user=' + user + '"') |
| 156 | + arguments.append(ogrLayer) |
| 157 | + arguments.append('-nlt NONE') |
| 158 | + arguments.append(self.ogrLayerName(inLayer)) |
| 159 | + if launder: |
| 160 | + arguments.append(launderstring) |
| 161 | + if append: |
| 162 | + arguments.append('-append') |
| 163 | + if addfields: |
| 164 | + arguments.append('-addfields') |
| 165 | + if overwrite: |
| 166 | + arguments.append('-overwrite') |
| 167 | + if len(schema) > 0: |
| 168 | + arguments.append(schemastring) |
| 169 | + if len(pk) > 0: |
| 170 | + arguments.append(pkstring) |
| 171 | + elif primary_key is not None: |
| 172 | + arguments.append("-lco FID="+primary_key) |
| 173 | + if len(table) > 0: |
| 174 | + arguments.append('-nln') |
| 175 | + arguments.append(table) |
| 176 | + if skipfailures: |
| 177 | + arguments.append('-skipfailures') |
| 178 | + if where: |
| 179 | + arguments.append(wherestring) |
| 180 | + if len(gt) > 0: |
| 181 | + arguments.append('-gt') |
| 182 | + arguments.append(gt) |
| 183 | + if not precision: |
| 184 | + arguments.append('-lco PRECISION=NO') |
| 185 | + if len(options) > 0: |
| 186 | + arguments.append(options) |
| 187 | + |
| 188 | + commands = [] |
| 189 | + if isWindows(): |
| 190 | + commands = ['cmd.exe', '/C ', 'ogr2ogr.exe', |
| 191 | + GdalUtils.escapeAndJoin(arguments)] |
| 192 | + else: |
| 193 | + commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)] |
| 194 | + |
| 195 | + GdalUtils.runGdal(commands, progress) |
0 commit comments