/[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 2559 by nhueffme, Tue Feb 8 09:52:56 2005 UTC revision 2577 by nhueffme, Fri Mar 4 15:07:59 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 *  from wxPython.wx import wxFileDialog, wxOPEN, wxMULTIPLE, wxID_OK
 from wxPython.wx import wxFileDialog, wxOPEN, wxMULTIPLE, wxID_OK, \  
         wxOK, wxICON_HAND  
14    
15  # We need os.path  # We need os.path
16  import os  import os
# Line 21  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  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.
# Line 37  def open_with_ogr(context): Line 35  def open_with_ogr(context):
35    
36      # Get the file to be opened      # Get the file to be opened
37      dlg = wxFileDialog(canvas, _("Select a data file"),      dlg = wxFileDialog(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                               _("MapInfo files (*.tab)") + "|*.tab|" +
42                               _("DGN files (*.dgn)") + "|*.dgn|" +
43                               _("CSV files (*.csv)") + "|*.csv|" +
44                               _("All Files (*.*)") + "|*.*|",
45                             wxOPEN | wxMULTIPLE)                             wxOPEN | wxMULTIPLE)
46    
47      if dlg.ShowModal() == wxID_OK:      if dlg.ShowModal() == wxID_OK:
# Line 50  def open_with_ogr(context): Line 52  def open_with_ogr(context):
52              layerDlg = ogrdialog.ChooseLayer(canvas, filename)              layerDlg = ogrdialog.ChooseLayer(canvas, filename)
53              if layerDlg.ShowModal() == wxID_OK:              if layerDlg.ShowModal() == wxID_OK:
54                  layername = layerDlg.GetLayer()                  layername = layerDlg.GetLayer()
55              try:                  try:
56                  session = context.application.Session()                      session = context.application.Session()
57                  store = ogrshapes.OGRShapeStore(filename, layername)                      store = OpenFileShapestore(session, filename, layername)
58                  session.AddShapeStore(store)                      session.AddShapeStore(store)
59              except:                  except:
60                  # the layer couldn't be opened                      # the layer couldn't be opened
61                  context.mainwindow.RunMessageBox(("Add Layer"),                      context.mainwindow.RunMessageBox(("Add Layer"),
62                                  ("Can't open the file '%s'.")%filename)                                  ("Can't open the file '%s'.")%filename)
63              else:                  else:
64                  layer = Layer(title, store)                      if store is not None:
65                  map.AddLayer(layer)                          layer = Layer(title, store)
66                  if not has_layers:                          map.AddLayer(layer)
67                      # if we're adding a layer to an empty map, fit the                          if not has_layers:
68                      # new map to the window                              # if we're adding a layer to an empty map, fit the
69                      canvas.FitMapToWindow()                              # new map to the window
70                  context.application.SetPath("data",filename)                              canvas.FitMapToWindow()
71                        context.application.SetPath("data",filename)
72          dlg.Destroy()          dlg.Destroy()
73    
74  def select_file_format(context):  def select_file_format(context):
# Line 81  def select_file_format(context): Line 84  def select_file_format(context):
84      dlg = ogrdialog.ChooseFileFormat(canvas, session)      dlg = ogrdialog.ChooseFileFormat(canvas, session)
85    
86      if dlg.ShowModal() == wxID_OK:      if dlg.ShowModal() == wxID_OK:
87          context.mainwindow.RunMessageBox("dialog auf", "dialog auf")          pass
88      dlg.Destroy()      dlg.Destroy()
89    
90  def open_db(context):  def open_db(context):
# Line 91  def open_db(context): Line 94  def open_db(context):
94      canvas = context.mainwindow.canvas      canvas = context.mainwindow.canvas
95      map = canvas.Map()      map = canvas.Map()
96    
97      session = context.application.Session()      session = context.application.Session()
98      dlg = ChooseOGRDBTableDialog(canvas, session)      dlg = ChooseOGRDBTableDialog(canvas, session)
99    
100      if dlg.ShowModal() == wxID_OK:      if dlg.ShowModal() == wxID_OK:
101          dbconn, dbtable = dlg.GetTable()          dbconn, connString, dbtable, id_column = dlg.GetTable()
102          try:          try:
103              # Choose the correct Interface for the database type              store = OpenDBShapestore(session, dbconn, dbtable, id_column,
104              filename = ('PG: host=%s user=%s dbname=%s port=%s'                                              None)
                         %(dbconn.host, dbconn.user, dbconn.dbname, dbconn.port))  
   
             store = ogrshapes.OGRShapeStore(filename, dbtable)  
105              session.AddShapeStore(store)              session.AddShapeStore(store)
106    
107              layer = Layer(dbtable, store)              layer = Layer(dbtable, store)
# Line 117  def open_db(context): Line 117  def open_db(context):
117                                 % dbtable)                                 % dbtable)
118      dlg.Destroy()      dlg.Destroy()
119    
120    def open_OGRConnection(context):
121        """Open a datasource with an OGRConnection string."""
122        canvas = context.mainwindow.canvas
123        map = canvas.Map()
124    
125        session = context.application.Session()
126        dlg = ogrdialog.OGRConnectionDialog(canvas, session)
127    
128        if dlg.ShowModal() == wxID_OK:
129            dsname = dlg.GetDatasourceName()
130    
131            layerDlg = ogrdialog.ChooseLayer(canvas, dsname)
132            if layerDlg.ShowModal() == wxID_OK:
133                layername = layerDlg.GetLayer()
134                try:
135                    store = ogrshapes.OGRShapeStore(session, dsname, layername)
136                    session.AddShapeStore(store)
137                except:
138                    # the layer couldn't be opened
139                    context.mainwindow.RunMessageBox(("Add Layer"),
140                                ("Can't open the file '%s'.") % dsname)
141                else:
142                    layer = Layer(dsname, store)
143                    has_layers = map.HasLayers()
144                    map.AddLayer(layer)
145                    if not has_layers:
146                        # if we're adding a layer to an empty map, fit the
147                        # new map to the window
148                        canvas.FitMapToWindow()
149        dlg.Destroy()
150    
151    def OpenFileShapestore(session, filename, layername):
152        """Open a datasource and add the required layer.
153        """
154        try:
155            store = ogrshapes.OGRShapeStore(session, filename, layername)
156            return store
157        except:
158            # Some error occured while initializing the layer
159            context.mainwindow.RunMessageBox(_("Open datasource"),
160                               _("Can't open the datasource '%s'")
161                               % filename)
162        else:
163            return null
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 128  from Thuban.UI.command import registry, Line 198  from Thuban.UI.command import registry,
198  from Thuban.UI.mainwindow import main_menu  from Thuban.UI.mainwindow import main_menu
199    
200    
201  # find the map menu (create it a new if not found)  # find the map menu (create a new if not found)
202  map_menu = main_menu.FindOrInsertMenu('map', _('Map'))  map_menu = main_menu.FindOrInsertMenu('map', _('Map'))
203  ogr_menu = Menu("ogr", _("Open layer via OGR"),[])  ogr_menu = Menu("ogr", _("Open layer via OGR"),[])
204    
205    ogrsupport = ogrshapes.has_ogr_support()
206    
207  # create new commands and register them  # create new commands and register them
208  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,
209                         sensitive = _has_gdal_support,
210                       helptext = 'Open a file supported from ogr'))                       helptext = 'Open a file supported from ogr'))
211    
212  registry.Add(Command('select_file_format', 'Select a file format',  #registry.Add(Command('select_file_format', 'Select a file format',
213                       select_file_format,  #                     select_file_format,
214                       helptext = "Select a file format supported from ogr"))  #                     helptext = "Select a file format supported from ogr"))
215    
216  registry.Add(Command('open_db', 'Open a layer from a database',  registry.Add(Command('open_db', 'Open a layer from a database',
217                       open_db,                       open_db,
218                         sensitive = _has_dbconnections,
219                       helptext = "Open a layer from a database, e.g. PostGIS"))                       helptext = "Open a layer from a database, e.g. PostGIS"))
220    
221    registry.Add(Command('open_OGRConnection',
222                         ("Open a datasource with an OGRConnection string"),
223                         open_OGRConnection,
224                         sensitive = _has_gdal_support, helptext =
225                         "Open a datasource with an OGRConnection string"))
226    
227  # finally bind the new command with an entry in the extensions menu  # finally bind the new command with an entry in the extensions menu
228  ogr_menu.InsertItem("open_ogr_files")  ogr_menu.InsertItem("open_ogr_files")
229  ogr_menu.InsertItem('select_file_format')  #ogr_menu.InsertItem('select_file_format')
230  ogr_menu.InsertItem('open_db')  ogr_menu.InsertItem('open_db')
231    ogr_menu.InsertItem('open_OGRConnection')
232    
233  # Add ogr menu to map menu  # Add ogr menu to map menu
234  map_menu.InsertItem(ogr_menu, after = "rasterlayer_add")  map_menu.InsertItem(ogr_menu, after = "rasterlayer_add")
   

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26