dlg_add_geometry_column.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """
  2. /***************************************************************************
  3. Name : DB Manager
  4. Description : Database manager plugin for QGIS
  5. Date : Oct 13, 2011
  6. copyright : (C) 2011 by Giuseppe Sucameli
  7. email : brush.tyler@gmail.com
  8. The content of this file is based on
  9. - PG_Manager by Martin Dobias (GPLv2 license)
  10. ***************************************************************************/
  11. /***************************************************************************
  12. * *
  13. * This program is free software; you can redistribute it and/or modify *
  14. * it under the terms of the GNU General Public License as published by *
  15. * the Free Software Foundation; either version 2 of the License, or *
  16. * (at your option) any later version. *
  17. * *
  18. ***************************************************************************/
  19. """
  20. from qgis.PyQt.QtCore import Qt
  21. from qgis.PyQt.QtWidgets import QDialog, QMessageBox, QApplication
  22. from qgis.utils import OverrideCursor
  23. from .db_plugins.plugin import DbError
  24. from .dlg_db_error import DlgDbError
  25. from .ui.ui_DlgAddGeometryColumn import Ui_DbManagerDlgAddGeometryColumn as Ui_Dialog
  26. class DlgAddGeometryColumn(QDialog, Ui_Dialog):
  27. GEOM_TYPES = ["POINT", "LINESTRING", "POLYGON", "MULTIPOINT", "MULTILINESTRING", "MULTIPOLYGON",
  28. "GEOMETRYCOLLECTION"]
  29. def __init__(self, parent=None, table=None, db=None):
  30. QDialog.__init__(self, parent)
  31. self.table = table
  32. self.db = self.table.database() if self.table and self.table.database() else db
  33. self.setupUi(self)
  34. self.buttonBox.accepted.connect(self.createGeomColumn)
  35. def createGeomColumn(self):
  36. """ first check whether everything's fine """
  37. if self.editName.text() == "":
  38. QMessageBox.critical(self, self.tr("DB Manager"), self.tr("Field name must not be empty."))
  39. return
  40. name = self.editName.text()
  41. geom_type = self.GEOM_TYPES[self.cboType.currentIndex()]
  42. dim = self.spinDim.value()
  43. try:
  44. srid = int(self.editSrid.text())
  45. except ValueError:
  46. srid = -1
  47. createSpatialIndex = False
  48. # now create the geometry column
  49. with OverrideCursor(Qt.WaitCursor):
  50. try:
  51. self.table.addGeometryColumn(name, geom_type, srid, dim, createSpatialIndex)
  52. except DbError as e:
  53. DlgDbError.showError(e, self)
  54. return
  55. self.accept()