2012-11-10 00:31:01 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
ImportIntoPostGIS.py
|
|
|
|
---------------------
|
|
|
|
Date : October 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__ = 'October 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
|
|
|
import os
|
2013-09-12 20:06:34 +02:00
|
|
|
from processing.core.QGisLayers import QGisLayers
|
|
|
|
from processing.parameters.ParameterBoolean import ParameterBoolean
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.parameters.ParameterVector import ParameterVector
|
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
|
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
2012-11-10 00:31:01 +01:00
|
|
|
from qgis.core import *
|
|
|
|
from PyQt4.QtCore import *
|
|
|
|
from PyQt4.QtGui import *
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.parameters.ParameterString import ParameterString
|
|
|
|
from processing.admintools import postgis_utils
|
2012-11-10 00:31:01 +01:00
|
|
|
|
|
|
|
class ImportIntoPostGIS(GeoAlgorithm):
|
2012-12-10 00:12:07 +01:00
|
|
|
|
2012-11-10 00:31:01 +01:00
|
|
|
DATABASE = "DATABASE"
|
|
|
|
TABLENAME = "TABLENAME"
|
2013-09-12 20:06:34 +02:00
|
|
|
SCHEMA = "SCHEMA"
|
2012-11-10 00:31:01 +01:00
|
|
|
INPUT = "INPUT"
|
2012-12-15 18:49:32 +01:00
|
|
|
OVERWRITE = "OVERWRITE"
|
|
|
|
CREATEINDEX = "CREATEINDEX"
|
2012-12-10 00:12:07 +01:00
|
|
|
|
2012-11-10 00:31:01 +01:00
|
|
|
def getIcon(self):
|
|
|
|
return QIcon(os.path.dirname(__file__) + "/../images/postgis.png")
|
|
|
|
|
2012-12-10 00:12:07 +01:00
|
|
|
def processAlgorithm(self, progress):
|
2012-12-15 18:49:32 +01:00
|
|
|
connection = self.getParameterValue(self.DATABASE)
|
2013-09-12 20:06:34 +02:00
|
|
|
schema = self.getParameterValue(self.SCHEMA)
|
2012-12-15 18:49:32 +01:00
|
|
|
overwrite = self.getParameterValue(self.OVERWRITE)
|
2013-01-12 23:36:00 +01:00
|
|
|
createIndex = self.getParameterValue(self.CREATEINDEX)
|
2012-12-15 18:49:32 +01:00
|
|
|
settings = QSettings()
|
|
|
|
mySettings = "/PostgreSQL/connections/"+ connection
|
|
|
|
try:
|
2013-06-03 21:25:22 +02:00
|
|
|
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")
|
2012-12-15 18:49:32 +01:00
|
|
|
except Exception, e:
|
|
|
|
raise GeoAlgorithmExecutionException("Wrong database connection name: " + connection)
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2012-12-15 18:49:32 +01:00
|
|
|
table = self.getParameterValue(self.TABLENAME);
|
|
|
|
table.replace(" ", "")
|
|
|
|
providerName = "postgres"
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2012-12-15 18:49:32 +01:00
|
|
|
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)
|
|
|
|
|
|
|
|
uri = QgsDataSourceURI()
|
2013-01-12 23:36:00 +01:00
|
|
|
uri.setConnection(host, str(port), database, username, password)
|
2013-09-12 20:06:34 +02:00
|
|
|
uri.setDataSource(schema, table, "the_geom", "")
|
2012-12-15 18:49:32 +01:00
|
|
|
|
|
|
|
options = {}
|
|
|
|
if overwrite:
|
|
|
|
options['overwrite'] = True
|
|
|
|
|
2013-01-12 23:36:00 +01:00
|
|
|
layerUri = self.getParameterValue(self.INPUT);
|
2013-09-12 20:06:34 +02:00
|
|
|
layer = QGisLayers.getObjectFromUri(layerUri)
|
2012-12-15 18:49:32 +01:00
|
|
|
ret, errMsg = QgsVectorLayerImport.importLayer(layer, uri.uri(), providerName, self.crs, False, False, options)
|
|
|
|
if ret != 0:
|
2013-01-12 23:36:00 +01:00
|
|
|
raise GeoAlgorithmExecutionException(u"Error importing to PostGIS\n%s" % errMsg)
|
2012-12-15 18:49:32 +01:00
|
|
|
|
|
|
|
if createIndex:
|
2013-09-12 20:06:34 +02:00
|
|
|
db.create_spatial_index(table, schema, "the_geom")
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2013-09-12 20:06:34 +02:00
|
|
|
db.vacuum_analyze(table, schema)
|
2012-12-10 00:12:07 +01:00
|
|
|
|
|
|
|
def defineCharacteristics(self):
|
2012-11-10 00:31:01 +01:00
|
|
|
self.name = "Import into PostGIS"
|
2012-12-10 00:12:07 +01:00
|
|
|
self.group = "PostGIS management tools"
|
2012-11-10 00:31:01 +01:00
|
|
|
self.addParameter(ParameterVector(self.INPUT, "Layer to import"))
|
2012-12-15 18:49:32 +01:00
|
|
|
self.addParameter(ParameterString(self.DATABASE, "Database (connection name)"))
|
|
|
|
self.addParameter(ParameterString(self.TABLENAME, "Table to import to"))
|
2013-01-12 23:36:00 +01:00
|
|
|
self.addParameter(ParameterBoolean(self.OVERWRITE, "Overwrite", True))
|
|
|
|
self.addParameter(ParameterBoolean(self.CREATEINDEX, "Create spatial index", True))
|
|
|
|
|
|
|
|
|
2012-12-10 00:12:07 +01:00
|
|
|
|
|
|
|
|
2012-11-10 00:31:01 +01:00
|
|
|
|