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

Legend:
Removed from v.2435  
changed lines
  Added in v.2549

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26