Skip to content
Permalink
Browse files

[DBManager] get default pk, geom and srid values from input layer to …

…set default options in import vector dialog
  • Loading branch information
brushtyler committed Aug 25, 2015
1 parent a2ce73a commit fdb0d206771562229e1c030934878cddf8412e09
Showing with 43 additions and 25 deletions.
  1. +43 −25 python/plugins/db_manager/dlg_import_vector.py
@@ -71,31 +71,33 @@ def setupWorkingMode(self, mode):
QObject.connect(self.cboInputLayer, SIGNAL("editTextChanged(const QString &)"), self.inputPathChanged)
#QObject.connect( self.cboInputLayer, SIGNAL("currentIndexChanged(int)"), self.updateInputLayer )
QObject.connect(self.btnUpdateInputLayer, SIGNAL("clicked()"), self.updateInputLayer)

self.editPrimaryKey.setText(self.default_pk)
self.editGeomColumn.setText(self.default_geom)
else:
# set default values
pk = self.outUri.keyColumn()
self.editPrimaryKey.setText(pk if pk != "" else self.default_pk)
if self.inLayer.hasGeometryType():
geom = self.outUri.geometryColumn()
self.editGeomColumn.setText(geom if geom != "" else self.default_geom)

inCrs = self.inLayer.crs()
srid = inCrs.postgisSrid() if inCrs.isValid() else 4236
self.editSourceSrid.setText("%s" % srid)
self.editTargetSrid.setText("%s" % srid)

self.checkSupports()
self.updateInputLayer()

def checkSupports(self):
""" update options available for the current input layer """
allowSpatial = self.db.connector.hasSpatialSupport()
hasGeomType = self.inLayer and self.inLayer.hasGeometryType()
isShapefile = self.inLayer and self.inLayer.providerType() == "ogr" and self.inLayer.storageType() == "ESRI Shapefile"

self.chkGeomColumn.setEnabled(allowSpatial and hasGeomType)
if not self.chkGeomColumn.isEnabled(): self.chkGeomColumn.setChecked(False)

self.chkSourceSrid.setEnabled(allowSpatial and hasGeomType)
if not self.chkSourceSrid.isEnabled(): self.chkSourceSrid.setChecked(False)
self.chkTargetSrid.setEnabled(allowSpatial and hasGeomType)
if not self.chkTargetSrid.isEnabled(): self.chkTargetSrid.setChecked(False)

self.chkSinglePart.setEnabled(allowSpatial and hasGeomType and isShapefile)
if not self.chkSinglePart.isEnabled(): self.chkSinglePart.setChecked(False)

self.chkSpatialIndex.setEnabled(allowSpatial and hasGeomType)
if not self.chkSpatialIndex.isEnabled(): self.chkSpatialIndex.setChecked(False)

def populateLayers(self):
self.cboInputLayer.clear()
@@ -139,10 +141,10 @@ def inputPathChanged(self, path):
self.cboInputLayer.setEditText(path)
self.cboInputLayer.blockSignals(False)

def updateInputLayer(self):
def reloadInputLayer(self):
""" create the input layer and update available options """
if self.mode != self.ASK_FOR_INPUT_MODE:
return
return True

self.deleteInputLayer()

@@ -166,10 +168,27 @@ def updateInputLayer(self):
self.inLayer = iface.legendInterface().layers()[legendIndex]
self.inLayerMustBeDestroyed = False

# update the output table name
self.checkSupports()
return True

def updateInputLayer(self):
if not self.reloadInputLayer() or not self.inLayer:
return False

# update the output table name, pk and geom column
self.cboTable.setEditText(self.inLayer.name())

self.checkSupports()
srcUri = qgis.core.QgsDataSourceURI(self.inLayer.source())
pk = srcUri.keyColumn() if srcUri.keyColumn() else self.default_pk
self.editPrimaryKey.setText(pk)
geom = srcUri.geometryColumn() if srcUri.geometryColumn() else self.default_geom
self.editGeomColumn.setText(geom)

srcCrs = self.inLayer.crs()
srid = srcCrs.postgisSrid() if srcCrs.isValid() else 4326
self.editSourceSrid.setText("%s" % srid)
self.editTargetSrid.setText("%s" % srid)

return True

def populateSchemas(self):
@@ -221,13 +240,9 @@ def populateEncodings(self):
def accept(self):
if self.mode == self.ASK_FOR_INPUT_MODE:
# create the input layer (if not already done) and
# update available options w/o changing the tablename!
self.cboTable.blockSignals(True)
table = self.cboTable.currentText()
self.updateInputLayer()
self.cboTable.setEditText(table)
self.cboTable.blockSignals(False)

# update available options
self.reloadInputLayer()

# sanity checks
if self.inLayer is None:
QMessageBox.information(self, self.tr("Import to database"), self.tr("Input layer missing or not valid"))
@@ -265,11 +280,14 @@ def accept(self):

# get pk and geom field names from the source layer or use the
# ones defined by the user
pk = self.outUri.keyColumn() if not self.chkPrimaryKey.isChecked() else self.editPrimaryKey.text()
srcUri = qgis.core.QgsDataSourceURI(self.inLayer.source())

pk = srcUri.keyColumn() if not self.chkPrimaryKey.isChecked() else self.editPrimaryKey.text()
if not pk: pk = self.default_pk

if self.inLayer.hasGeometryType() and self.chkGeomColumn.isEnabled():
geom = self.outUri.geometryColumn() if not self.chkGeomColumn.isChecked() else self.editGeomColumn.text()
geom = geom if geom != "" else self.default_geom
geom = srcUri.geometryColumn() if not self.chkGeomColumn.isChecked() else self.editGeomColumn.text()
if not geom: geom = self.default_geom
else:
geom = None

0 comments on commit fdb0d20

Please sign in to comment.
You can’t perform that action at this time.