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

Diff of /branches/WIP-pyshapelib-bramz/Extensions/ogr/ogrstart.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2493 by nhueffme, Mon Dec 20 14:13:57 2004 UTC revision 2559 by nhueffme, Tue Feb 8 09:52:56 2005 UTC
# Line 10  __version__ = "$Revision$" Line 10  __version__ = "$Revision$"
10  # $Id$  # $Id$
11    
12  # Needed wx-toolkit classes  # Needed wx-toolkit classes
13    #from wxPython.wx import *
14  from wxPython.wx import wxFileDialog, wxOPEN, wxMULTIPLE, wxID_OK, \  from wxPython.wx import wxFileDialog, wxOPEN, wxMULTIPLE, wxID_OK, \
15          wxOK, wxICON_HAND          wxOK, wxICON_HAND
16    
# Line 20  import os Line 21  import os
21  from Thuban import _  from Thuban import _
22    
23  from Thuban.Model.layer import Layer  from Thuban.Model.layer import Layer
24    from Thuban.UI.dbdialog import DBDialog
25    
26  # Import ogr related classes  # Import ogr related classes
27  from Extensions.ogr import ogrshapes, ogrdialog  from Extensions.ogr import ogrshapes, ogrdialog
28    from Extensions.ogr.ogrdialog import ChooseOGRDBTableDialog
29    
30  def open_with_ogr(context):  from Thuban.UI.menu import Menu
     '''Export data depending on the set properties.  
31    
32      This is the main export funcation.  def open_with_ogr(context):
33        '''Open a file supported by ogr.
34      '''      '''
35      canvas = context.mainwindow.canvas      canvas = context.mainwindow.canvas
     file = None  
36      map = canvas.Map()      map = canvas.Map()
37    
38      # Get the file to be opened      # Get the file to be opened
39      dlg = wxFileDialog(canvas, _("Select a data file"),      dlg = wxFileDialog(canvas, _("Select a data file"),
40                             ".", "",                             ".", "",
41                             _("Shapefiles (*.shp)") + "|*.shp;*.SHP|" +                             _("Shape/GML files (*.shp/*.gml)") + "|*.shp;*.gml|" +
42                             _("All Files (*.*)") + "|*.*",                             _("All Files (*.*)") + "|*.*",
43                             wxOPEN | wxMULTIPLE)                             wxOPEN | wxMULTIPLE)
44    
# Line 45  def open_with_ogr(context): Line 47  def open_with_ogr(context):
47          for filename in filenames:          for filename in filenames:
48              title = os.path.splitext(os.path.basename(filename))[0]              title = os.path.splitext(os.path.basename(filename))[0]
49              has_layers = map.HasLayers()              has_layers = map.HasLayers()
50              layername = title              layerDlg = ogrdialog.ChooseLayer(canvas, filename)
51                if layerDlg.ShowModal() == wxID_OK:
52                    layername = layerDlg.GetLayer()
53              try:              try:
54                  session = context.application.Session()                  session = context.application.Session()
55                  store = ogrshapes.OGRShapeStore(session, filename, layername)                  store = ogrshapes.OGRShapeStore(filename, layername)
56                  session.AddShapeStore(store)                  session.AddShapeStore(store)
57              except IOError:              except:
58                  # the layer couldn't be opened                  # the layer couldn't be opened
59                  self.RunMessageBox(("Add Layer"),                  context.mainwindow.RunMessageBox(("Add Layer"),
60                                  ("Can't open the file '%s'.")%filename)                                  ("Can't open the file '%s'.")%filename)
61              else:              else:
62                  layer = Layer(title, store)                  layer = Layer(title, store)
# Line 64  def open_with_ogr(context): Line 68  def open_with_ogr(context):
68                  context.application.SetPath("data",filename)                  context.application.SetPath("data",filename)
69          dlg.Destroy()          dlg.Destroy()
70    
71    def select_file_format(context):
72        ''' Display all available supported formats.
73        '''
74    
75        canvas = context.mainwindow.canvas
76        file = None
77        map = canvas.Map()
78    
79        session = context.application.Session()
80    
81        dlg = ogrdialog.ChooseFileFormat(canvas, session)
82    
83        if dlg.ShowModal() == wxID_OK:
84            context.mainwindow.RunMessageBox("dialog auf", "dialog auf")
85        dlg.Destroy()
86    
87    def open_db(context):
88        ''' Open a table in a database as a layer.
89        '''
90    
91        canvas = context.mainwindow.canvas
92        map = canvas.Map()
93    
94        session = context.application.Session()
95        dlg = ChooseOGRDBTableDialog(canvas, session)
96    
97        if dlg.ShowModal() == wxID_OK:
98            dbconn, dbtable = dlg.GetTable()
99            try:
100                # Choose the correct Interface for the database type
101                filename = ('PG: host=%s user=%s dbname=%s port=%s'
102                            %(dbconn.host, dbconn.user, dbconn.dbname, dbconn.port))
103    
104                store = ogrshapes.OGRShapeStore(filename, dbtable)
105                session.AddShapeStore(store)
106    
107                layer = Layer(dbtable, store)
108    
109                has_layers = map.HasLayers()
110                map.AddLayer(layer)
111                if not has_layers:
112                    canvas.FitMapToWindow()
113            except:
114                # Some error occured while initializing the layer
115                context.mainwindow.RunMessageBox(_("Add Layer from database"),
116                                   _("Can't open the database table '%s'")
117                                   % dbtable)
118        dlg.Destroy()
119    
120    
121    
122  # Thuban has named commands which can be registered in the central  # Thuban has named commands which can be registered in the central
# Line 74  from Thuban.UI.command import registry, Line 127  from Thuban.UI.command import registry,
127  # See Thuban/UI/menu.py for the API of the Menu class  # See Thuban/UI/menu.py for the API of the Menu class
128  from Thuban.UI.mainwindow import main_menu  from Thuban.UI.mainwindow import main_menu
129    
130    
131    # find the map menu (create it a new if not found)
132    map_menu = main_menu.FindOrInsertMenu('map', _('Map'))
133    ogr_menu = Menu("ogr", _("Open layer via OGR"),[])
134    
135    
136  # create new commands and register them  # create new commands and register them
137  registry.Add(Command('open_ogr_files', 'Open an ogr-file', open_with_ogr,  registry.Add(Command('open_ogr_files', 'Open an ogr-file', open_with_ogr,
138                       helptext = 'Open a file supported from ogr'))                       helptext = 'Open a file supported from ogr'))
139    
140  # find the ogr menu (create it a new if not found)  registry.Add(Command('select_file_format', 'Select a file format',
141  ogr_menu = main_menu.FindOrInsertMenu('ogr', _('OGR'))                       select_file_format,
142                         helptext = "Select a file format supported from ogr"))
143    
144    registry.Add(Command('open_db', 'Open a layer from a database',
145                         open_db,
146                         helptext = "Open a layer from a database, e.g. PostGIS"))
147    
148  # finally bind the new command with an entry in the extensions menu  # finally bind the new command with an entry in the extensions menu
149  ogr_menu.InsertItem('open_ogr_files')  ogr_menu.InsertItem("open_ogr_files")
150    ogr_menu.InsertItem('select_file_format')
151    ogr_menu.InsertItem('open_db')
152    
153    # Add ogr menu to map menu
154    map_menu.InsertItem(ogr_menu, after = "rasterlayer_add")
155    

Legend:
Removed from v.2493  
changed lines
  Added in v.2559

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26