nhueffme: thuban/Extensions/ogr ogrdialog.py, NONE, 1.1 __init__.py, 1.2, 1.3

cvs@intevation.de cvs at intevation.de
Mon Jan 31 10:18:29 CET 2005


Author: nhueffme

Update of /thubanrepository/thuban/Extensions/ogr
In directory doto:/tmp/cvs-serv18034/Extensions/ogr

Modified Files:
	__init__.py 
Added Files:
	ogrdialog.py 
Log Message:
Added class ogrdialog.py and changed some comments.




--- NEW FILE: ogrdialog.py ---
# Copyright (c) 2001, 2003, 2004 by Intevation GmbH
# Authors:
# Martin Mueller <mmueller at intevation.de>
# Bernhard Herzog <bh at intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with Thuban for details.


"""Dialogs to manage database connections"""

import sys, traceback

from wxPython.wx import *

try:
    import ogr
except ImportError:
    psycopg = None

from Thuban import _
from Thuban.UI.dialogs import NonModalDialog
from Thuban.Model.table import FIELDTYPE_INT
from Extensions.ogr import ogrshapes
from Thuban.Model.messages import DBCONN_ADDED, DBCONN_REMOVED
from Thuban.UI.messages import SESSION_REPLACED


ID_DB_ADD    = 9101
ID_DB_REMOVE = 9102

ID_DBCHOOSE_RETRIEVE = 9201
ID_DBCHOOSE_OK       = 9202
ID_DBCHOOSE_CANCEL   = 9203
ID_LB_DCLICK         = 9204


class ChooseFileFormat(wxDialog):

    def __init__(self, parent, session):
        wxDialog.__init__(self, parent, -1, _("Choose file format"),
                          style = wxDIALOG_MODAL|wxCAPTION)
        self.session = session
        self.tables = []

        #
        # Build the dialog
        #

        # Sizer for the entire dialog
        top = wxFlexGridSizer(2, 1, 0, 0)

        # Sizer for the main part with the list boxes
        main_sizer = wxBoxSizer(wxHORIZONTAL)
        top.Add(main_sizer, 1, wxEXPAND, 0)

        # The list box with the drivers
        static_box = wxStaticBoxSizer(wxStaticBox(self, -1, _("File formats")),
                                   wxHORIZONTAL)
        self.lb_drivers = wxListBox(self, -1)
        static_box.Add(self.lb_drivers, 0, wxEXPAND, 0)
        main_sizer.Add(static_box, 1, wxEXPAND, 0)

        for i in range(ogr.GetDriverCount()):
            self.lb_drivers.Append(ogr.GetDriver(i).GetName())
        if self.lb_drivers.GetCount() > 0:
            self.lb_drivers.SetSelection(0, True)

        # The button box between the connections list box and the table
        # list box
        buttons = wxFlexGridSizer(3, 1, 0, 0)
        buttons.Add(20, 80, 0, wxEXPAND, 0)
        retrieve_button = wxButton(self, ID_DBCHOOSE_RETRIEVE, _("Retrieve"))
        EVT_BUTTON(self, ID_DBCHOOSE_RETRIEVE, self.OnRetrieve)
        buttons.Add(retrieve_button, 0, wxALL
                    |wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4)
        buttons.Add(20, 80, 0, wxEXPAND, 0)
        main_sizer.Add(buttons, 0, wxEXPAND, 0)

        # The list box with the tables
        static_box = wxStaticBoxSizer(wxStaticBox(self, -1, _("Tables")),
                                   wxHORIZONTAL)
        self.lb_tables = wxListBox(self, ID_LB_DCLICK)
        EVT_LISTBOX(self, ID_LB_DCLICK, self.OnTableSelect)
        EVT_LISTBOX_DCLICK(self, ID_LB_DCLICK, self.OnLBDClick)
        static_box.Add(self.lb_tables, 0, wxEXPAND, 0)
        main_sizer.Add(static_box, 1, wxEXPAND, 0)

        # id column and geometry column selection
        box = wxBoxSizer(wxVERTICAL)
        box.Add(wxStaticText(self, -1, _("ID Column")), 0,
                wxALL|wxALIGN_CENTER_VERTICAL, 4)
        self.text_id_column = wxComboBox(self, -1, "")
        box.Add(self.text_id_column, 0,
                wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 4)

        box.Add(wxStaticText(self, -1, _("Geometry Column")), 0,
                wxALL|wxALIGN_CENTER_VERTICAL, 4)
        self.text_geo_column = wxComboBox(self, -1, "")
        box.Add(self.text_geo_column, 0,
                wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 4)
        main_sizer.Add(box, 1, wxEXPAND, 0)

        # The standard button box at the bottom of the dialog
        buttons = wxFlexGridSizer(1, 2, 0, 0)
        ok_button = wxButton(self, ID_DBCHOOSE_OK, _("OK"))
        EVT_BUTTON(self, ID_DBCHOOSE_OK, self.OnOK)
        buttons.Add(ok_button, 0, wxALL|wxALIGN_RIGHT, 4)
        cancel_button = wxButton(self, ID_DBCHOOSE_CANCEL, _("Cancel"))
        EVT_BUTTON(self, ID_DBCHOOSE_CANCEL, self.OnCancel)
        buttons.Add(cancel_button, 0, wxALL, 4)
        top.Add(buttons, 1, wxALL|wxALIGN_CENTER_HORIZONTAL, 4)

        # Autosizing
        self.SetAutoLayout(1)
        self.SetSizer(top)
        top.Fit(self)
        top.SetSizeHints(self)
        self.Layout()


    def GetTable(self):
        i = self.lb_tables.GetSelection()
        if i >= 0:
            return (self.selected_conn, self.tables[i],
                    self.text_id_column.GetValue(),
                    self.text_geo_column.GetValue())
        return None

    def OnRetrieve(self, event):
        i = self.lb_driver.GetSelection()
        if i >= 0:
            self.selected_conn = self.dbconns[i]
            self.tables = self.selected_conn.GeometryTables()
            self.lb_tables.Set(self.tables)

    def OnTableSelect(self, event):
        i = self.lb_tables.GetSelection()
        self.text_id_column.Clear()
        self.text_geo_column.Clear()
        if i >= 0:
            for name, typ in self.selected_conn.table_columns(self.tables[i]):
                if typ == "geometry":
                    self.text_geo_column.Append(name)
                elif typ == FIELDTYPE_INT:
                    self.text_id_column.Append(name)

    def OnLBDClick(self, event):
        if self.lb_tables.GetSelection() >= 0:
            self.EndModal(wxID_OK)
            self.Show(False)

    def OnOK(self, event):
        self.EndModal(wxID_OK)
        self.Show(False)

    def OnCancel(self, event):
        self.EndModal(wxID_CANCEL)
        self.Show(False)


class ChooseLayer(wxDialog):

    def __init__(self, parent, filename):
        wxDialog.__init__(self, parent, -1, _("Choose layer"),
                          style = wxDIALOG_MODAL|wxCAPTION)
        self.tables = []

        #
        # Build the dialog
        #

        # Sizer for the entire dialog
        top = wxFlexGridSizer(2, 1, 0, 0)

        # Sizer for the main part with the list boxes
        main_sizer = wxBoxSizer(wxHORIZONTAL)
        top.Add(main_sizer, 1, wxEXPAND, 0)

        # The list box with the drivers
        static_box = wxStaticBoxSizer(wxStaticBox(self, -1, _("Layers")),
                                   wxHORIZONTAL)
        self.lb_drivers = wxListBox(self, -1)
        static_box.Add(self.lb_drivers, 0, wxEXPAND, 0)
        main_sizer.Add(static_box, 1, wxEXPAND, 0)

        datasource = ogr.Open(filename)
        self.layer = []
	for i in range(datasource.GetLayerCount()):
            self.layer.append(datasource.GetLayer(i))
            self.lb_drivers.Append(datasource.GetLayer(i).GetName())
        if self.lb_drivers.GetCount() > 0:
            self.lb_drivers.SetSelection(0, True)

        # The standard button box at the bottom of the dialog
        buttons = wxFlexGridSizer(1, 2, 0, 0)
        ok_button = wxButton(self, ID_DBCHOOSE_OK, _("OK"))
        EVT_BUTTON(self, ID_DBCHOOSE_OK, self.OnOK)
        buttons.Add(ok_button, 0, wxALL|wxALIGN_RIGHT, 4)
        cancel_button = wxButton(self, ID_DBCHOOSE_CANCEL, _("Cancel"))
        EVT_BUTTON(self, ID_DBCHOOSE_CANCEL, self.OnCancel)
        buttons.Add(cancel_button, 0, wxALL, 4)
        top.Add(buttons, 1, wxALL|wxALIGN_CENTER_HORIZONTAL, 4)

        # Autosizing
        self.SetAutoLayout(1)
        self.SetSizer(top)
        top.Fit(self)
        top.SetSizeHints(self)
        self.Layout()

    def end_dialog(self, result):
        self.result = result
        if result is not None:
            self.EndModal(wxID_OK)
        else:
            self.EndModal(wxID_CANCEL)
        self.Show(False)

    def OnOK(self, event):
        self.end_dialog(self.lb_drivers.GetSelection())

    def OnCancel(self, event):
        self.end_dialog(None)

    def GetLayer(self):
        return self.layer[self.lb_drivers.GetSelection()].GetName()


class ChooseOGRDBTableDialog(wxDialog):

    def __init__(self, parent, session):
        wxDialog.__init__(self, parent, -1, _("Choose layer from database"),
                          style = wxDIALOG_MODAL|wxCAPTION)
        self.session = session
        self.dbconns = self.session.DBConnections()
        self.tables = []

        #
        # Build the dialog
        #

        # Sizer for the entire dialog
        top = wxFlexGridSizer(2, 1, 0, 0)

        # Sizer for the main part with the list boxes
        main_sizer = wxBoxSizer(wxHORIZONTAL)
        top.Add(main_sizer, 1, wxEXPAND, 0)

        # The list box with the connections
        static_box = wxStaticBoxSizer(wxStaticBox(self, -1, _("Databases")),
                                   wxHORIZONTAL)
        self.lb_connections = wxListBox(self, -1)
        static_box.Add(self.lb_connections, 0, wxEXPAND, 0)
        main_sizer.Add(static_box, 1, wxEXPAND, 0)

        for i in range(len(self.dbconns)):
            self.lb_connections.Append(self.dbconns[i].BriefDescription())
        if self.lb_connections.GetCount() > 0:
            self.lb_connections.SetSelection(0, True)

        # The button box between the connections list box and the table
        # list box
        buttons = wxFlexGridSizer(3, 1, 0, 0)
        buttons.Add(20, 80, 0, wxEXPAND, 0)
        retrieve_button = wxButton(self, ID_DBCHOOSE_RETRIEVE, _("Retrieve"))
        EVT_BUTTON(self, ID_DBCHOOSE_RETRIEVE, self.OnRetrieve)
        buttons.Add(retrieve_button, 0, wxALL
                    |wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4)
        buttons.Add(20, 80, 0, wxEXPAND, 0)
        main_sizer.Add(buttons, 0, wxEXPAND, 0)

        # The list box with the tables
        static_box = wxStaticBoxSizer(wxStaticBox(self, -1, _("Tables")),
                                   wxHORIZONTAL)
        self.lb_tables = wxListBox(self, ID_LB_DCLICK)
        EVT_LISTBOX(self, ID_LB_DCLICK, self.OnTableSelect)
        EVT_LISTBOX_DCLICK(self, ID_LB_DCLICK, self.OnLBDClick)
        static_box.Add(self.lb_tables, 0, wxEXPAND, 0)
        main_sizer.Add(static_box, 1, wxEXPAND, 0)

        # The standard button box at the bottom of the dialog
        buttons = wxFlexGridSizer(1, 2, 0, 0)
        ok_button = wxButton(self, ID_DBCHOOSE_OK, _("OK"))
        EVT_BUTTON(self, ID_DBCHOOSE_OK, self.OnOK)
        buttons.Add(ok_button, 0, wxALL|wxALIGN_RIGHT, 4)
        cancel_button = wxButton(self, ID_DBCHOOSE_CANCEL, _("Cancel"))
        EVT_BUTTON(self, ID_DBCHOOSE_CANCEL, self.OnCancel)
        buttons.Add(cancel_button, 0, wxALL, 4)
        top.Add(buttons, 1, wxALL|wxALIGN_CENTER_HORIZONTAL, 4)

        # Autosizing
        self.SetAutoLayout(1)
        self.SetSizer(top)
        top.Fit(self)
        top.SetSizeHints(self)
        self.Layout()


    def GetTable(self):
        i = self.lb_tables.GetSelection()
        if i >= 0:
            return (self.selected_conn, self.tables[i])
        return None

    def OnRetrieve(self, event):
        i = self.lb_connections.GetSelection()
        if i >= 0:
            self.selected_conn = self.dbconns[i]
            self.tables = self.selected_conn.GeometryTables()
            self.lb_tables.Set(self.tables)

    def OnTableSelect(self, event):
        i = self.lb_tables.GetSelection()
 #       self.text_id_column.Clear()
  #      self.text_geo_column.Clear()
   #     if i >= 0:
    #        for name, typ in self.selected_conn.table_columns(self.tables[i]):
     #           if typ == "geometry":
      #              self.text_geo_column.Append(name)
       #         elif typ == FIELDTYPE_INT:
        #            self.text_id_column.Append(name)

    def OnLBDClick(self, event):
        if self.lb_tables.GetSelection() >= 0:
            self.EndModal(wxID_OK)
            self.Show(False)

    def OnOK(self, event):
        self.EndModal(wxID_OK)
        self.Show(False)

    def OnCancel(self, event):
        self.EndModal(wxID_CANCEL)
        self.Show(False)



Index: __init__.py
===================================================================
RCS file: /thubanrepository/thuban/Extensions/ogr/__init__.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- __init__.py	26 Jan 2005 09:17:01 -0000	1.2
+++ __init__.py	31 Jan 2005 09:18:27 -0000	1.3
@@ -28,6 +28,6 @@
 ext_registry.add(ExtensionDesc(
     name = 'OGRstart',
     version = '0.9.0',
-    authors= [ 'Nina Hüffmeyer' ],
+    authors= [ 'Nina Hueffmeyer' ],
     copyright = '2004 Intevation GmbH',
     desc = _("Open a file supported by ogr.")))





More information about the Thuban-devel mailing list

This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)