/[thuban]/branches/WIP-pyshapelib-bramz/Extensions/ogr/ogrdialog.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/Extensions/ogr/ogrdialog.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2734 - (hide annotations)
Thu Mar 1 12:42:59 2007 UTC (18 years ago) by bramz
File MIME type: text/x-python
File size: 14286 byte(s)
made a copy
1 bernhard 2713 # Copyright (c) 2001, 2003, 2004 by Intevation GmbH vim:encoding=latin-1:
2 nhueffme 2554 # Authors:
3 dpinte 2721 # Martin Müller <[email protected]>
4 nhueffme 2554 # Bernhard Herzog <[email protected]>
5     #
6     # This program is free software under the GPL (>=v2)
7     # Read the file COPYING coming with Thuban for details.
8    
9    
10     """Dialogs to manage database connections"""
11    
12     import sys, traceback
13    
14 dpinte 2721 import wx
15 nhueffme 2554
16     try:
17     import ogr
18     except ImportError:
19 nhueffme 2577 ogr = None
20 nhueffme 2554
21     from Thuban import _
22     from Thuban.Model.table import FIELDTYPE_INT
23     from Extensions.ogr import ogrshapes
24    
25    
26     ID_DBCHOOSE_RETRIEVE = 9201
27     ID_DBCHOOSE_OK = 9202
28     ID_DBCHOOSE_CANCEL = 9203
29     ID_LB_DCLICK = 9204
30    
31    
32 dpinte 2721 class ChooseFileFormat(wx.Dialog):
33 nhueffme 2577 """This dialog lists all available drivers.
34     """
35 nhueffme 2554 def __init__(self, parent, session):
36 nhueffme 2577 """Initialize the dialog.
37     """
38 dpinte 2721 wx.Dialog.__init__(self, parent, -1, _("Choose file format"),
39     style = wx.DIALOG_MODAL|wx.CAPTION)
40 nhueffme 2554 self.session = session
41     self.tables = []
42    
43     #
44     # Build the dialog
45     #
46    
47     # Sizer for the entire dialog
48 dpinte 2721 top = wx.FlexGridSizer(2, 1, 0, 0)
49 nhueffme 2554
50 nhueffme 2577 # Sizer for the main part with the list box
51 dpinte 2721 main_sizer = wx.BoxSizer(wx.HORIZONTAL)
52     top.Add(main_sizer, 1, wx.EXPAND, 0)
53 nhueffme 2554
54     # The list box with the drivers
55 dpinte 2721 static_box = wx.StaticBoxSizer(wx.StaticBox(self, -1, "File formats"),
56     wx.HORIZONTAL)
57     self.lb_drivers = wx.ListBox(self, -1)
58     static_box.Add(self.lb_drivers, 0, wx.EXPAND, 0)
59     main_sizer.Add(static_box, 1, wx.EXPAND, 0)
60 nhueffme 2554
61     for i in range(ogr.GetDriverCount()):
62     self.lb_drivers.Append(ogr.GetDriver(i).GetName())
63     if self.lb_drivers.GetCount() > 0:
64     self.lb_drivers.SetSelection(0, True)
65    
66     # The standard button box at the bottom of the dialog
67 dpinte 2721 buttons = wx.FlexGridSizer(1, 2, 0, 0)
68     ok_button = wx.Button(self, ID_DBCHOOSE_OK, _("OK"))
69     wx.EVT_BUTTON(self, ID_DBCHOOSE_OK, self.OnOK)
70     buttons.Add(ok_button, 0, wx.ALL|wx.ALIGN_RIGHT, 4)
71     cancel_button = wx.Button(self, ID_DBCHOOSE_CANCEL, _("Cancel"))
72     wx.EVT_BUTTON(self, ID_DBCHOOSE_CANCEL, self.OnCancel)
73     buttons.Add(cancel_button, 0, wx.ALL, 4)
74     top.Add(buttons, 1, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 4)
75 nhueffme 2554
76     # Autosizing
77     self.SetAutoLayout(1)
78     self.SetSizer(top)
79     top.Fit(self)
80     top.SetSizeHints(self)
81     self.Layout()
82    
83    
84     def GetTable(self):
85 nhueffme 2577 """no functionality
86     """
87 nhueffme 2554 return None
88    
89     def OnLBDClick(self, event):
90 nhueffme 2577 """Close dialog.
91     """
92 nhueffme 2554 if self.lb_tables.GetSelection() >= 0:
93 dpinte 2721 self.EndModal(wx.ID_OK)
94 nhueffme 2554 self.Show(False)
95    
96     def OnOK(self, event):
97 nhueffme 2577 """Close dialog.
98     """
99 dpinte 2721 self.EndModal(wx.ID_OK)
100 nhueffme 2554 self.Show(False)
101    
102     def OnCancel(self, event):
103 nhueffme 2577 """Close dialog.
104     """
105 dpinte 2721 self.EndModal(wx.ID_CANCEL)
106 nhueffme 2554 self.Show(False)
107    
108    
109 dpinte 2721 class ChooseLayer(wx.Dialog):
110 nhueffme 2577 """This dialog lists all the layers contained in the given datasource.
111 nhueffme 2554
112 nhueffme 2577 One layer can be chosen, which is then opened.
113     """
114    
115 nhueffme 2554 def __init__(self, parent, filename):
116 nhueffme 2577 """Initialize the dialog.
117     """
118 dpinte 2721 wx.Dialog.__init__(self, parent, -1, _("Choose layer"),
119     style = wx.DIALOG_MODAL|wx.CAPTION)
120 nhueffme 2554 self.tables = []
121    
122     #
123     # Build the dialog
124     #
125    
126     # Sizer for the entire dialog
127 dpinte 2721 top = wx.FlexGridSizer(2, 1, 0, 0)
128 nhueffme 2554
129     # Sizer for the main part with the list boxes
130 dpinte 2721 main_sizer = wx.BoxSizer(wx.HORIZONTAL)
131     top.Add(main_sizer, 1, wx.EXPAND, 0)
132 nhueffme 2554
133     # The list box with the drivers
134 dpinte 2721 static_box = wx.StaticBoxSizer(wx.StaticBox(self, -1, _("Layers")),
135     wx.HORIZONTAL)
136     self.lb_drivers = wx.ListBox(self, -1)
137     static_box.Add(self.lb_drivers, 0, wx.EXPAND, 0)
138     main_sizer.Add(static_box, 1, wx.EXPAND, 0)
139 nhueffme 2554
140     datasource = ogr.Open(filename)
141     self.layer = []
142 nhueffme 2559 for i in range(datasource.GetLayerCount()):
143 nhueffme 2554 self.layer.append(datasource.GetLayer(i))
144     self.lb_drivers.Append(datasource.GetLayer(i).GetName())
145     if self.lb_drivers.GetCount() > 0:
146     self.lb_drivers.SetSelection(0, True)
147    
148     # The standard button box at the bottom of the dialog
149 dpinte 2721 buttons = wx.FlexGridSizer(1, 2, 0, 0)
150     ok_button = wx.Button(self, ID_DBCHOOSE_OK, _("OK"))
151     wx.EVT_BUTTON(self, ID_DBCHOOSE_OK, self.OnOK)
152     buttons.Add(ok_button, 0, wx.ALL|wx.ALIGN_RIGHT, 4)
153     cancel_button = wx.Button(self, ID_DBCHOOSE_CANCEL, _("Cancel"))
154     wx.EVT_BUTTON(self, ID_DBCHOOSE_CANCEL, self.OnCancel)
155     buttons.Add(cancel_button, 0, wx.ALL, 4)
156     top.Add(buttons, 1, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 4)
157 nhueffme 2554
158     # Autosizing
159     self.SetAutoLayout(1)
160     self.SetSizer(top)
161     top.Fit(self)
162     top.SetSizeHints(self)
163     self.Layout()
164    
165     def end_dialog(self, result):
166 nhueffme 2577 """If the dialog is closed with OK, set chosen layer as layer
167     to be opened.
168     """
169 nhueffme 2554 self.result = result
170     if result is not None:
171 dpinte 2721 self.EndModal(wx.ID_OK)
172 nhueffme 2554 else:
173 dpinte 2721 self.EndModal(wx.ID_CANCEL)
174 nhueffme 2554 self.Show(False)
175    
176     def OnOK(self, event):
177 nhueffme 2577 """Dialog closed with OK button.
178     """
179 nhueffme 2554 self.end_dialog(self.lb_drivers.GetSelection())
180    
181     def OnCancel(self, event):
182 nhueffme 2577 """Dialog closed with Cancel.
183     """
184 nhueffme 2554 self.end_dialog(None)
185    
186     def GetLayer(self):
187 nhueffme 2577 """Return the selected layer."""
188 nhueffme 2554 return self.layer[self.lb_drivers.GetSelection()].GetName()
189    
190    
191 dpinte 2721 class ChooseOGRDBTableDialog(wx.Dialog):
192 nhueffme 2577 """This dialog opens a datasource from an existing database connection.
193 nhueffme 2554
194 nhueffme 2577 A list of all available database connections is offered. If one connection
195     is selected and the button "Retrieve" is clicked, all layers are listed.
196     One of these layers can be chosen to be opened.
197     An ID column can be chosen, too."""
198    
199 nhueffme 2554 def __init__(self, parent, session):
200 nhueffme 2577 """Initialize the dialog.
201     """
202 dpinte 2721 wx.Dialog.__init__(self, parent, -1, _("Choose layer from database"),
203     style = wx.DIALOG_MODAL|wx.CAPTION)
204 nhueffme 2554 self.session = session
205     self.dbconns = self.session.DBConnections()
206     self.tables = []
207    
208     #
209     # Build the dialog
210     #
211    
212     # Sizer for the entire dialog
213 dpinte 2721 top = wx.FlexGridSizer(2, 1, 0, 0)
214 nhueffme 2554
215     # Sizer for the main part with the list boxes
216 dpinte 2721 main_sizer = wx.BoxSizer(wx.HORIZONTAL)
217     top.Add(main_sizer, 1, wx.EXPAND, 0)
218 nhueffme 2554
219     # The list box with the connections
220 dpinte 2721 static_box = wx.StaticBoxSizer(wx.StaticBox(self, -1, _("Databases")),
221     wx.HORIZONTAL)
222     self.lb_connections = wx.ListBox(self, -1)
223     static_box.Add(self.lb_connections, 0, wx.EXPAND, 0)
224     main_sizer.Add(static_box, 1, wx.EXPAND, 0)
225 nhueffme 2554
226     for i in range(len(self.dbconns)):
227     self.lb_connections.Append(self.dbconns[i].BriefDescription())
228     if self.lb_connections.GetCount() > 0:
229     self.lb_connections.SetSelection(0, True)
230    
231     # The button box between the connections list box and the table
232     # list box
233 dpinte 2721 buttons = wx.FlexGridSizer(3, 1, 0, 0)
234     buttons.Add(20, 80, 0, wx.EXPAND, 0)
235     retrieve_button = wx.Button(self, ID_DBCHOOSE_RETRIEVE, _("Retrieve"))
236     wx.EVT_BUTTON(self, ID_DBCHOOSE_RETRIEVE, self.OnRetrieve)
237     buttons.Add(retrieve_button, 0, wx.ALL
238     |wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 4)
239     buttons.Add(20, 80, 0, wx.EXPAND, 0)
240     main_sizer.Add(buttons, 0, wx.EXPAND, 0)
241 nhueffme 2554
242     # The list box with the tables
243 dpinte 2721 static_box = wx.StaticBoxSizer(wx.StaticBox(self, -1, _("Tables")),
244     wx.HORIZONTAL)
245     self.lb_tables = wx.ListBox(self, ID_LB_DCLICK)
246     wx.EVT_LISTBOX(self, ID_LB_DCLICK, self.OnTableSelect)
247     wx.EVT_LISTBOX_DCLICK(self, ID_LB_DCLICK, self.OnLBDClick)
248     static_box.Add(self.lb_tables, 0, wx.EXPAND, 0)
249     main_sizer.Add(static_box, 1, wx.EXPAND, 0)
250 nhueffme 2554
251 nhueffme 2577 # id column and geometry column selection
252 dpinte 2721 box = wx.BoxSizer(wx.VERTICAL)
253     box.Add(wx.StaticText(self, -1, _("ID Column")), 0,
254     wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
255     self.text_id_column = wx.ComboBox(self, -1, "")
256 nhueffme 2577 box.Add(self.text_id_column, 0,
257 dpinte 2721 wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 4)
258     main_sizer.Add(box, 1, wx.EXPAND, 0)
259 nhueffme 2577
260 nhueffme 2554 # The standard button box at the bottom of the dialog
261 dpinte 2721 buttons = wx.FlexGridSizer(1, 2, 0, 0)
262     ok_button = wx.Button(self, ID_DBCHOOSE_OK, _("OK"))
263     wx.EVT_BUTTON(self, ID_DBCHOOSE_OK, self.OnOK)
264     buttons.Add(ok_button, 0, wx.ALL|wx.ALIGN_RIGHT, 4)
265     cancel_button = wx.Button(self, ID_DBCHOOSE_CANCEL, _("Cancel"))
266     wx.EVT_BUTTON(self, ID_DBCHOOSE_CANCEL, self.OnCancel)
267     buttons.Add(cancel_button, 0, wx.ALL, 4)
268     top.Add(buttons, 1, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 4)
269 nhueffme 2554
270     # Autosizing
271     self.SetAutoLayout(1)
272     self.SetSizer(top)
273     top.Fit(self)
274     top.SetSizeHints(self)
275     self.Layout()
276    
277    
278     def GetTable(self):
279 nhueffme 2577 """Return the chosen DB connection, the table to be opened,
280     the connection string for ogr and the ID column.
281     """
282 nhueffme 2554 i = self.lb_tables.GetSelection()
283     if i >= 0:
284 nhueffme 2577 return (self.selected_conn, self.connString, self.tables[i],
285     self.text_id_column.GetValue())
286 nhueffme 2554 return None
287    
288     def OnRetrieve(self, event):
289 nhueffme 2577 """Provide a list of all available layers in the selected datasource.
290     """
291 nhueffme 2554 i = self.lb_connections.GetSelection()
292     if i >= 0:
293     self.selected_conn = self.dbconns[i]
294 nhueffme 2577 self.connString = "PG: dbname=%s" %self.selected_conn.dbname
295     if self.selected_conn.host is not "":
296     self.connString = (self.connString + " host=%s"
297     %self.selected_conn.host)
298     if self.selected_conn.user is not "":
299     self.connString = (self.connString + " user=%s"
300     %self.selected_conn.user)
301     if self.selected_conn.password is not "":
302     self.connString = (self.connString + " password=%s"
303     %self.selected_conn.password)
304     if self.selected_conn.port is not "":
305     self.connString = (self.connString + " port= %s"
306     %self.selected_conn.port)
307     ds = ogr.Open(self.connString)
308 nhueffme 2559 if ds:
309     for i in range(ds.GetLayerCount()):
310     self.tables.append(ds.GetLayer(i).GetName())
311     self.lb_tables.Set(self.tables)
312 nhueffme 2554
313     def OnTableSelect(self, event):
314 nhueffme 2577 """If a table is selected, list all possible ID columns.
315     """
316 nhueffme 2554 i = self.lb_tables.GetSelection()
317 nhueffme 2577 self.text_id_column.Clear()
318     if i >= 0:
319     for name, typ in self.selected_conn.table_columns(self.tables[i]):
320     if typ == FIELDTYPE_INT:
321     self.text_id_column.Append(name)
322 nhueffme 2554
323     def OnLBDClick(self, event):
324 nhueffme 2577 """Close dialog.
325     """
326 nhueffme 2554 if self.lb_tables.GetSelection() >= 0:
327 dpinte 2721 self.EndModal(wx.ID_OK)
328 nhueffme 2554 self.Show(False)
329    
330     def OnOK(self, event):
331 nhueffme 2577 """Dialog closed with OK button.
332     """
333 dpinte 2721 self.EndModal(wx.ID_OK)
334 nhueffme 2554 self.Show(False)
335    
336     def OnCancel(self, event):
337 nhueffme 2577 """Dialog closed with Cancel.
338     """
339 dpinte 2721 self.EndModal(wx.ID_CANCEL)
340 nhueffme 2554 self.Show(False)
341    
342    
343 dpinte 2721 class OGRConnectionDialog(wx.Dialog):
344 nhueffme 2577 """A string can be enteres, which is directly passed to ogr to open a
345     datasource.
346     """
347     def __init__(self, parent, session):
348     """Initialize the dialog.
349     """
350 dpinte 2721 wx.Dialog.__init__(self, parent, -1, "Enter string for OGRConnection",
351     style = wx.DIALOG_MODAL|wx.CAPTION)
352 nhueffme 2577 self.session = session
353    
354     # Sizer for the entire dialog
355 dpinte 2721 top = wx.BoxSizer(wx.VERTICAL)
356 nhueffme 2577
357     # The list box with the drivers
358 dpinte 2721 box = wx.BoxSizer(wx.HORIZONTAL)#wx.Box(self, -1, _("OGRConnection")),
359     # wx.HORIZONTAL)
360     box.Add(wx.StaticText(self, -1, _("URL:")), 0,
361     wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
362     self.text_string = wx.TextCtrl(self, -1, "")
363     box.Add(self.text_string, 0, wx.EXPAND, 0)
364     top.Add(box, 0, wx.EXPAND)
365 nhueffme 2577
366     # The standard button box at the bottom of the dialog
367 dpinte 2721 buttons = wx.FlexGridSizer(1, 2, 0, 0)
368     ok_button = wx.Button(self, ID_DBCHOOSE_OK, _("OK"))
369     wx.EVT_BUTTON(self, ID_DBCHOOSE_OK, self.OnOK)
370     buttons.Add(ok_button, 0, wx.ALL|wx.ALIGN_RIGHT, 4)
371     cancel_button = wx.Button(self, ID_DBCHOOSE_CANCEL, _("Cancel"))
372     wx.EVT_BUTTON(self, ID_DBCHOOSE_CANCEL, self.OnCancel)
373     buttons.Add(cancel_button, 0, wx.ALL, 4)
374     top.Add(buttons, 1, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 4)
375 nhueffme 2577
376     # Autosizing
377     self.SetAutoLayout(1)
378     self.SetSizer(top)
379     top.Fit(self)
380     top.SetSizeHints(self)
381     self.Layout()
382    
383     def RunDialog(self):
384     """Run dialog
385     """
386     self.ShowModal()
387     self.Destroy()
388     return self.result
389    
390     def end_dialog(self, result):
391     """Close dialog
392     """
393     self.result = result
394     if result is not None:
395 dpinte 2721 self.EndModal(wx.ID_OK)
396 nhueffme 2577 else:
397 dpinte 2721 self.EndModal(wx.ID_CANCEL)
398 nhueffme 2577 self.Show(False)
399    
400     def OnOK(self, event):
401     """Dialog closed with OK
402     """
403     result = {}
404     result["string"] = getattr(self, "text_string").GetValue()
405     self.end_dialog(result)
406    
407     def OnCancel(self, event):
408     """Dialog closed with Cancel.
409     """
410     self.end_dialog(None)
411    
412     def GetDatasourceName(self):
413     """Return the string to be used for opening the database
414     """
415     return self.result["string"]
416    

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26