Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1262 from nyalldawson/processing_postgis
Improvements to processing "Import into PostGIS" algorithm
  • Loading branch information
jef-n committed Mar 25, 2014
2 parents 839c3e7 + 748d261 commit 44a054d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
33 changes: 27 additions & 6 deletions python/plugins/processing/admintools/ImportIntoPostGIS.py 100644 → 100755
Expand Up @@ -35,6 +35,7 @@
from processing.parameters.ParameterBoolean import ParameterBoolean
from processing.parameters.ParameterVector import ParameterVector
from processing.parameters.ParameterString import ParameterString
from processing.parameters.ParameterSelection import ParameterSelection
from processing.tools import dataobjects

from processing.admintools import postgis_utils
Expand All @@ -48,15 +49,18 @@ class ImportIntoPostGIS(GeoAlgorithm):
INPUT = 'INPUT'
OVERWRITE = 'OVERWRITE'
CREATEINDEX = 'CREATEINDEX'
GEOMETRY_COLUMN = 'GEOMETRY_COLUMN'
LOWERCASE_NAMES = 'LOWERCASE_NAMES'

def getIcon(self):
return QIcon(os.path.dirname(__file__) + '/../images/postgis.png')

def processAlgorithm(self, progress):
connection = self.getParameterValue(self.DATABASE)
connection = self.DB_CONNECTIONS[self.getParameterValue(self.DATABASE)]
schema = self.getParameterValue(self.SCHEMA)
overwrite = self.getParameterValue(self.OVERWRITE)
createIndex = self.getParameterValue(self.CREATEINDEX)
convertLowerCase = self.getParameterValue(self.LOWERCASE_NAMES)
settings = QSettings()
mySettings = '/PostgreSQL/connections/' + connection
try:
Expand All @@ -80,14 +84,19 @@ def processAlgorithm(self, progress):
raise GeoAlgorithmExecutionException(
"Couldn't connect to database:\n" + e.message)

geomColumn = self.getParameterValue(self.GEOMETRY_COLUMN)
if not geomColumn:
geomColumn = 'the_geom'

uri = QgsDataSourceURI()
uri.setConnection(host, str(port), database, username, password)
uri.setDataSource(schema, table, 'the_geom', '')
uri.setDataSource(schema, table, geomColumn, '')

options = {}
if overwrite:
options['overwrite'] = True

if convertLowerCase:
options['lowercaseFieldNames'] = True
layerUri = self.getParameterValue(self.INPUT)
layer = dataobjects.getObjectFromUri(layerUri)
(ret, errMsg) = QgsVectorLayerImport.importLayer(
Expand All @@ -104,19 +113,31 @@ def processAlgorithm(self, progress):
'Error importing to PostGIS\n%s' % errMsg)

if createIndex:
db.create_spatial_index(table, schema, 'the_geom')
db.create_spatial_index(table, schema, geomColumn)

db.vacuum_analyze(table, schema)

def dbConnectionNames(self):
settings = QSettings()
settings.beginGroup('/PostgreSQL/connections/')
return settings.childGroups()

def defineCharacteristics(self):
self.name = 'Import into PostGIS'
self.group = 'PostGIS management tools'
self.addParameter(ParameterVector(self.INPUT, 'Layer to import'))
self.addParameter(ParameterString(self.DATABASE,
'Database (connection name)'))

self.DB_CONNECTIONS = self.dbConnectionNames()
self.addParameter(ParameterSelection(self.DATABASE, 'Database (connection name)',
self.DB_CONNECTIONS))

self.addParameter(ParameterString(self.SCHEMA, 'Schema (schema name)'))
self.addParameter(ParameterString(self.TABLENAME, 'Table to import to'
))
self.addParameter(ParameterString(self.GEOMETRY_COLUMN, 'Geometry column', 'the_geom'
))
self.addParameter(ParameterBoolean(self.OVERWRITE, 'Overwrite', True))
self.addParameter(ParameterBoolean(self.CREATEINDEX,
'Create spatial index', True))
self.addParameter(ParameterBoolean(self.LOWERCASE_NAMES,
'Convert field names to lowercase', False))
8 changes: 6 additions & 2 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -2844,8 +2844,6 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
QString *errorMessage,
const QMap<QString, QVariant> *options )
{
Q_UNUSED( options );

// populate members from the uri structure
QgsDataSourceURI dsUri( uri );
QString schemaName = dsUri.schema();
Expand Down Expand Up @@ -3048,6 +3046,12 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
continue;
}

if ( options->contains( "lowercaseFieldNames" ) && options->value( "lowercaseFieldNames" ).toBool() )
{
//convert field name to lowercase
fld.setName( fld.name().toLower() );
}

if ( !convertField( fld ) )
{
if ( errorMessage )
Expand Down

0 comments on commit 44a054d

Please sign in to comment.