/[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 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  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          canvas = context.mainwindow.canvas      '''
35          file = None      canvas = context.mainwindow.canvas
36          map = canvas.Map()      map = canvas.Map()
37    
38          if hasattr(canvas, "export_path"):      # Get the file to be opened
39                  export_path = canvas.export_path      dlg = wxFileDialog(canvas, _("Select a data file"),
40          else:                             ".", "",
41                  export_path="."                             _("Shape/GML files (*.shp/*.gml)") + "|*.shp;*.gml|" +
         # Get the file to be opened  
         dlg = wxFileDialog(canvas, _("Select a data file"),  
                            export_path, "",  
                            _("Shapefiles (*.shp)") + "|*.shp;*.SHP|" +  
42                             _("All Files (*.*)") + "|*.*",                             _("All Files (*.*)") + "|*.*",
43                             wxOPEN | wxMULTIPLE)                             wxOPEN | wxMULTIPLE)
44    
45          if dlg.ShowModal() == wxID_OK:      if dlg.ShowModal() == wxID_OK:
46              filenames = dlg.GetPaths()          filenames = dlg.GetPaths()
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                  try:              if layerDlg.ShowModal() == wxID_OK:
52                      session = context.application.Session()                  layername = layerDlg.GetLayer()
53                      store = ogrshapes.OGRShapeStore(session, filename, layername)              try:
54                      session.AddShapeStore(store)                  session = context.application.Session()
55                  except IOError:                  store = ogrshapes.OGRShapeStore(filename, layername)
56                      # the layer couldn't be opened                  session.AddShapeStore(store)
57                      self.RunMessageBox(_("Add Layer"),              except:
58                                         _("Can't open the file '%s'.")%filename)                  # the layer couldn't be opened
59                  else:                  context.mainwindow.RunMessageBox(("Add Layer"),
60                      layer = Layer(title, store)                                  ("Can't open the file '%s'.")%filename)
61                      map.AddLayer(layer)              else:
62                      if not has_layers:                  layer = Layer(title, store)
63                          # if we're adding a layer to an empty map, fit the                  map.AddLayer(layer)
64                          # new map to the window                  if not has_layers:
65                          canvas.FitMapToWindow()                      # if we're adding a layer to an empty map, fit the
66                      context.application.SetPath("data",filename)                      # new map to the window
67                        canvas.FitMapToWindow()
68                    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 78  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    
 # create a new command and register it  
 registry.Add(Command('open_ogr_files', _('Open an ogr-file'), open_with_ogr,  
                      helptext = _('Open a file supported from ogr')))  
130    
131  # find the ogr menu (create it a new if not found)  # find the map menu (create it a new if not found)
132  ogr_menu = main_menu.FindOrInsertMenu('ogr', _('OGR'))  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
137    registry.Add(Command('open_ogr_files', 'Open an ogr-file', open_with_ogr,
138                         helptext = 'Open a file supported from ogr'))
139    
140    registry.Add(Command('select_file_format', 'Select a file format',
141                         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.2435  
changed lines
  Added in v.2559

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26