/[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 2549 by nhueffme, Wed Jan 26 09:17:01 2005 UTC revision 2721 by dpinte, Sat Jan 13 15:11:42 2007 UTC
# Line 1  Line 1 
1  # Copyright (c) 2004 by Intevation GmbH  # Copyright (c) 2004,2006 by Intevation GmbH    vim:encoding=latin-1:
2  # Authors:  # Authors:
3  # Nina H�ffmeyer <[email protected]>  # Nina Hüffmeyer <[email protected]>
4  #  #
5  # This program is free software under the GPL (>=v2)  # This program is free software under the GPL (>=v2)
6  # Read the file COPYING coming with Thuban for details.  # Read the file COPYING coming with Thuban for details.
# Line 9  __version__ = "$Revision$" Line 9  __version__ = "$Revision$"
9  # $Source$  # $Source$
10  # $Id$  # $Id$
11    
12  # Needed wx-toolkit classes  # Needed wx.-toolkit classes
13  from wxPython.wx import wxFileDialog, wxOPEN, wxMULTIPLE, wxID_OK, \  import wx
         wxOK, wxICON_HAND  
14    
15  # We need os.path  # We need os.path
16  import os  import os
# Line 20  import os Line 19  import os
19  from Thuban import _  from Thuban import _
20    
21  from Thuban.Model.layer import Layer  from Thuban.Model.layer import Layer
 from Thuban.UI.dbdialog import DBDialog  
22    
23  # Import ogr related classes  # Import ogr related classes
24  from Extensions.ogr import ogrshapes, ogrdialog  from Extensions.ogr import ogrshapes, ogrdialog
25  from Extensions.ogr.ogrdialog import ChooseOGRDBTableDialog  from Extensions.ogr.ogrdialog import ChooseOGRDBTableDialog
26    
27    from Thuban.UI.menu import Menu
28    from Thuban.UI.mainwindow import _has_dbconnections, _has_gdal_support
29    
30  def open_with_ogr(context):  def open_with_ogr(context):
31      '''Open a file supported by ogr.      '''Open a file supported by ogr.
32      '''      '''
# Line 33  def open_with_ogr(context): Line 34  def open_with_ogr(context):
34      map = canvas.Map()      map = canvas.Map()
35    
36      # Get the file to be opened      # Get the file to be opened
37      dlg = wxFileDialog(canvas, _("Select a data file"),      dlg = wx.FileDialog(canvas, _("Select a data file"),
38                             ".", "",                             context.application.Path("data"), "",
39                             _("Shape/GML files (*.shp/*.gml)") + "|*.shp;*.gml|" +                             _("Shapefiles (*.shp)") + "|*.shp|" +
40                             _("All Files (*.*)") + "|*.*",                             _("GML files (*.gml)") + "|*.gml|" +
41                             wxOPEN | wxMULTIPLE)                             _("MapInfo files (*.tab)") + "|*.tab|" +
42                               _("DGN files (*.dgn)") + "|*.dgn|" +
43                               _("CSV files (*.csv)") + "|*.csv|" +
44                               _("All Files (*.*)") + "|*.*|",
45                               wx.OPEN | wx.MULTIPLE)
46    
47      if dlg.ShowModal() == wxID_OK:      if dlg.ShowModal() == wx.ID_OK:
48          filenames = dlg.GetPaths()          filenames = dlg.GetPaths()
49          for filename in filenames:          for filename in filenames:
50              title = os.path.splitext(os.path.basename(filename))[0]              title = os.path.splitext(os.path.basename(filename))[0]
51              has_layers = map.HasLayers()              has_layers = map.HasLayers()
52              layerDlg = ogrdialog.ChooseLayer(canvas, filename)              layerDlg = ogrdialog.ChooseLayer(canvas, filename)
53              if layerDlg.ShowModal() == wxID_OK:              if layerDlg.ShowModal() == wx.ID_OK:
54                  layername = layerDlg.GetLayer()                  layername = layerDlg.GetLayer()
55              try:                  try:
56                  session = context.application.Session()                      session = context.application.Session()
57                  store = ogrshapes.OGRShapeStore(session, filename, layername)                      store = OpenFileShapestore(context.mainwindow, session,
58                  session.AddShapeStore(store)                                                 filename, layername)
59              except:                      session.AddShapeStore(store)
60                  # the layer couldn't be opened                  except:
61                  context.mainwindow.RunMessageBox(("Add Layer"),                      # the layer couldn't be opened
62                                  ("Can't open the file '%s'.")%filename)                      context.mainwindow.RunMessageBox(_("Add Layer"),
63              else:                                  _("Can't open the file '%s'.") % filename)
64                  layer = Layer(title, store)                  else:
65                  map.AddLayer(layer)                      if store is not None:
66                  if not has_layers:                          layer = Layer(title, store)
67                      # if we're adding a layer to an empty map, fit the                          map.AddLayer(layer)
68                      # new map to the window                          if not has_layers:
69                      canvas.FitMapToWindow()                              # if we're adding a layer to an empty map, fit the
70                  context.application.SetPath("data",filename)                              # new map to the window
71                                canvas.FitMapToWindow()
72                        context.application.SetPath("data",filename)
73          dlg.Destroy()          dlg.Destroy()
74    
75  def select_file_format(context):  def select_file_format(context):
# Line 77  def select_file_format(context): Line 84  def select_file_format(context):
84    
85      dlg = ogrdialog.ChooseFileFormat(canvas, session)      dlg = ogrdialog.ChooseFileFormat(canvas, session)
86    
87      if dlg.ShowModal() == wxID_OK:      if dlg.ShowModal() == wx.ID_OK:
88          context.mainwindow.RunMessageBox("dialog auf", "dialog auf")          pass
89      dlg.Destroy()      dlg.Destroy()
90    
91  def open_db(context):  def open_db(context):
# Line 87  def open_db(context): Line 94  def open_db(context):
94    
95      canvas = context.mainwindow.canvas      canvas = context.mainwindow.canvas
96      map = canvas.Map()      map = canvas.Map()
97        
98      session = context.application.Session()      session = context.application.Session()
99      dlg = ChooseOGRDBTableDialog(canvas, session)      dlg = ChooseOGRDBTableDialog(canvas, session)
100    
101      if dlg.ShowModal() == wxID_OK:      if dlg.ShowModal() == wx.ID_OK:
102          dbconn, dbtable = dlg.GetTable()          dbconn, connString, dbtable, id_column = dlg.GetTable()
103          try:          try:
104              # Choose the correct Interface for the database type              store = OpenDBShapestore(session, dbconn, dbtable, id_column,
105              filename = ('PG: host=%s user=%s dbname=%s port=%s'                                              None)
                         %(dbconn.host, dbconn.user, dbconn.dbname, dbconn.port))  
   
             store = ogrshapes.OGRShapeStore(session, filename, dbtable)  
106              session.AddShapeStore(store)              session.AddShapeStore(store)
107    
108              layer = Layer(dbtable, store)              layer = Layer(dbtable, store)
# Line 114  def open_db(context): Line 118  def open_db(context):
118                                 % dbtable)                                 % dbtable)
119      dlg.Destroy()      dlg.Destroy()
120    
121    def open_OGRConnection(context):
122        """Open a datasource with an OGRConnection string."""
123        canvas = context.mainwindow.canvas
124        map = canvas.Map()
125    
126        session = context.application.Session()
127        dlg = ogrdialog.OGRConnectionDialog(canvas, session)
128    
129        if dlg.ShowModal() == wx.ID_OK:
130            dsname = dlg.GetDatasourceName()
131    
132            layerDlg = ogrdialog.ChooseLayer(canvas, dsname)
133            if layerDlg.ShowModal() == wx.ID_OK:
134                layername = layerDlg.GetLayer()
135                try:
136                    store = ogrshapes.OGRShapeStore(session, dsname, layername)
137                    session.AddShapeStore(store)
138                except:
139                    # the layer couldn't be opened
140                    context.mainwindow.RunMessageBox(("Add Layer"),
141                                ("Can't open the file '%s'.") % dsname)
142                else:
143                    layer = Layer(dsname, store)
144                    has_layers = map.HasLayers()
145                    map.AddLayer(layer)
146                    if not has_layers:
147                        # if we're adding a layer to an empty map, fit the
148                        # new map to the window
149                        canvas.FitMapToWindow()
150        dlg.Destroy()
151    
152    def OpenFileShapestore(mainwindow, session, filename, layername):
153        """Open a datasource and add the required layer.
154        """
155        try:
156            store = ogrshapes.OGRFileShapeStore(session, filename, layername)
157            return store
158        except:
159            # Some error occured while initializing the layer
160            mainwindow.RunMessageBox(_("Open datasource"),
161                               _("Can't open the datasource '%s'")
162                               % filename)
163            return None
164    
165    def OpenDBShapestore(session, dbconn, layername, id_column, geo_column):
166        """Open a datasource and add the required layer.
167    
168        dbconn     - shold be a DBConnection
169        layername  - the name of the table which should opened as layer
170        id_column  - the column name which should be used as ID column
171        geo_column - always None for ogr
172        """
173        try:
174            filename = "PG: dbname=%s" %dbconn.dbname
175            if dbconn.host is not "":
176                filename = filename + " host=%s" % dbconn.host
177            if dbconn.user is not "":
178                filename = filename + " user=%s" % dbconn.user
179            if dbconn.password is not "":
180                filename = filename + " password=%s" % dbconn.password
181            if dbconn.port is not "":
182                filename = filename + " port=%s" % dbconn.port
183            store = ogrshapes.OGRShapeStore(session, filename, layername,
184                                            id_column = id_column)
185            return store
186        except:
187            # Some error occured while initializing the layer
188            context.mainwindow.RunMessageBox(_("Open datasource"),
189                               _("Can't open the datasource '%s'")
190                               % filename)
191    
192  # Thuban has named commands which can be registered in the central  # Thuban has named commands which can be registered in the central
193  # instance registry.  # instance registry.
# Line 124  from Thuban.UI.command import registry, Line 197  from Thuban.UI.command import registry,
197  # See Thuban/UI/menu.py for the API of the Menu class  # See Thuban/UI/menu.py for the API of the Menu class
198  from Thuban.UI.mainwindow import main_menu  from Thuban.UI.mainwindow import main_menu
199    
200    
201    # find the map menu (create a new if not found)
202    #map_menu = main_menu.FindOrInsertMenu('map', _('Map'))
203    #ogr_menu = Menu("ogr", _("Open layer via OGR"),[])
204    # as long as there we are not stable, better add to "Extentions" marked "beta"
205    map_menu = main_menu.FindOrInsertMenu('extensions', _('E&xtensions'))
206    ogr_menu = Menu("ogr", _("(testing) Open layer via OGR"),[])
207    
208    
209    ogrsupport = ogrshapes.has_ogr_support()
210    
211  # create new commands and register them  # create new commands and register them
212  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,
213                         sensitive = _has_gdal_support,
214                       helptext = 'Open a file supported from ogr'))                       helptext = 'Open a file supported from ogr'))
215    
216  # find the ogr menu (create it a new if not found)  #registry.Add(Command('select_file_format', 'Select a file format',
217  ogr_menu = main_menu.FindOrInsertMenu('ogr', _('OGR'))  #                     select_file_format,
218  registry.Add(Command('select_file_format', 'Select a file format',  #                     helptext = "Select a file format supported from ogr"))
                      select_file_format,  
                      helptext = "Select a file format supported from ogr"))  
219    
220  registry.Add(Command('open_db', 'Open a layer from a database',  registry.Add(Command('open_db', 'Open a layer from a database',
221                       open_db,                       open_db,
222                         sensitive = _has_dbconnections,
223                       helptext = "Open a layer from a database, e.g. PostGIS"))                       helptext = "Open a layer from a database, e.g. PostGIS"))
224    
225    registry.Add(Command('open_OGRConnection',
226                         ("Open a datasource with an OGRConnection string"),
227                         open_OGRConnection,
228                         sensitive = _has_gdal_support, helptext =
229                         "Open a datasource with an OGRConnection string"))
230    
231  # finally bind the new command with an entry in the extensions menu  # finally bind the new command with an entry in the extensions menu
232  ogr_menu.InsertItem('open_ogr_files')  ogr_menu.InsertItem("open_ogr_files")
233  ogr_menu.InsertItem('select_file_format')  #ogr_menu.InsertItem('select_file_format')
234  ogr_menu.InsertItem('open_db')  ogr_menu.InsertItem('open_db')
235    ogr_menu.InsertItem('open_OGRConnection')
236    
237    # Add ogr menu to map menu
238    map_menu.InsertItem(ogr_menu, after = "rasterlayer_add")

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26