Skip to content

Commit 5997c78

Browse files
committedMar 25, 2014
[processing] Allow choice of geometry column name, change database connection name to combo box for Import into PostGIS algorithm
1 parent 90f012d commit 5997c78

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed
 

‎python/plugins/processing/admintools/ImportIntoPostGIS.py

100644100755
Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from processing.parameters.ParameterBoolean import ParameterBoolean
3636
from processing.parameters.ParameterVector import ParameterVector
3737
from processing.parameters.ParameterString import ParameterString
38+
from processing.parameters.ParameterSelection import ParameterSelection
3839
from processing.tools import dataobjects
3940

4041
from processing.admintools import postgis_utils
@@ -48,12 +49,13 @@ class ImportIntoPostGIS(GeoAlgorithm):
4849
INPUT = 'INPUT'
4950
OVERWRITE = 'OVERWRITE'
5051
CREATEINDEX = 'CREATEINDEX'
52+
GEOMETRY_COLUMN = 'GEOMETRY_COLUMN'
5153

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

5557
def processAlgorithm(self, progress):
56-
connection = self.getParameterValue(self.DATABASE)
58+
connection = self.DB_CONNECTIONS[self.getParameterValue(self.DATABASE)]
5759
schema = self.getParameterValue(self.SCHEMA)
5860
overwrite = self.getParameterValue(self.OVERWRITE)
5961
createIndex = self.getParameterValue(self.CREATEINDEX)
@@ -80,9 +82,13 @@ def processAlgorithm(self, progress):
8082
raise GeoAlgorithmExecutionException(
8183
"Couldn't connect to database:\n" + e.message)
8284

85+
geomColumn = self.getParameterValue(self.GEOMETRY_COLUMN)
86+
if not geomColumn:
87+
geomColumn = 'the_geom'
88+
8389
uri = QgsDataSourceURI()
8490
uri.setConnection(host, str(port), database, username, password)
85-
uri.setDataSource(schema, table, 'the_geom', '')
91+
uri.setDataSource(schema, table, geomColumn, '')
8692

8793
options = {}
8894
if overwrite:
@@ -104,19 +110,29 @@ def processAlgorithm(self, progress):
104110
'Error importing to PostGIS\n%s' % errMsg)
105111

106112
if createIndex:
107-
db.create_spatial_index(table, schema, 'the_geom')
113+
db.create_spatial_index(table, schema, geomColumn)
108114

109115
db.vacuum_analyze(table, schema)
110116

117+
def dbConnectionNames(self):
118+
settings = QSettings()
119+
settings.beginGroup('/PostgreSQL/connections/')
120+
return settings.childGroups()
121+
111122
def defineCharacteristics(self):
112123
self.name = 'Import into PostGIS'
113124
self.group = 'PostGIS management tools'
114125
self.addParameter(ParameterVector(self.INPUT, 'Layer to import'))
115-
self.addParameter(ParameterString(self.DATABASE,
116-
'Database (connection name)'))
126+
127+
self.DB_CONNECTIONS = self.dbConnectionNames()
128+
self.addParameter(ParameterSelection(self.DATABASE, 'Database (connection name)',
129+
self.DB_CONNECTIONS))
130+
117131
self.addParameter(ParameterString(self.SCHEMA, 'Schema (schema name)'))
118132
self.addParameter(ParameterString(self.TABLENAME, 'Table to import to'
119133
))
134+
self.addParameter(ParameterString(self.GEOMETRY_COLUMN, 'Geometry column', 'the_geom'
135+
))
120136
self.addParameter(ParameterBoolean(self.OVERWRITE, 'Overwrite', True))
121137
self.addParameter(ParameterBoolean(self.CREATEINDEX,
122138
'Create spatial index', True))

0 commit comments

Comments
 (0)
Please sign in to comment.