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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2609 - (hide annotations)
Thu Apr 28 09:39:58 2005 UTC (19 years, 10 months ago) by jan
Original Path: trunk/thuban/Extensions/ogr/ogrstart.py
File MIME type: text/x-python
File size: 8703 byte(s)
(open_with_ogr): Added forgotten _ for i18n.
Adapted call OpenFileShapestore regarding new parameter.
(OpenFileShapestore): Use OGRFileShapeStore instead of
OGRShapeStore. Fix return value (None instead of null).
Add new parameter "mainwindow" to avoid accessing global
context.

1 nhueffme 2435 # Copyright (c) 2004 by Intevation GmbH
2     # Authors:
3     # Nina Hüffmeyer <[email protected]>
4     #
5     # This program is free software under the GPL (>=v2)
6     # Read the file COPYING coming with Thuban for details.
7    
8     __version__ = "$Revision$"
9     # $Source$
10     # $Id$
11    
12     # Needed wx-toolkit classes
13 nhueffme 2577 from wxPython.wx import wxFileDialog, wxOPEN, wxMULTIPLE, wxID_OK
14 nhueffme 2435
15     # We need os.path
16     import os
17    
18     # use _() already now for all strings that may later be translated
19     from Thuban import _
20    
21     from Thuban.Model.layer import Layer
22    
23     # Import ogr related classes
24 nhueffme 2493 from Extensions.ogr import ogrshapes, ogrdialog
25 nhueffme 2549 from Extensions.ogr.ogrdialog import ChooseOGRDBTableDialog
26 nhueffme 2435
27 nhueffme 2559 from Thuban.UI.menu import Menu
28 nhueffme 2577 from Thuban.UI.mainwindow import _has_dbconnections, _has_gdal_support
29 nhueffme 2559
30 nhueffme 2435 def open_with_ogr(context):
31 nhueffme 2549 '''Open a file supported by ogr.
32 nhueffme 2493 '''
33     canvas = context.mainwindow.canvas
34     map = canvas.Map()
35 nhueffme 2435
36 nhueffme 2493 # Get the file to be opened
37     dlg = wxFileDialog(canvas, _("Select a data file"),
38 nhueffme 2577 context.application.Path("data"), "",
39     _("Shapefiles (*.shp)") + "|*.shp|" +
40     _("GML files (*.gml)") + "|*.gml|" +
41     _("MapInfo files (*.tab)") + "|*.tab|" +
42     _("DGN files (*.dgn)") + "|*.dgn|" +
43     _("CSV files (*.csv)") + "|*.csv|" +
44     _("All Files (*.*)") + "|*.*|",
45 nhueffme 2435 wxOPEN | wxMULTIPLE)
46    
47 nhueffme 2493 if dlg.ShowModal() == wxID_OK:
48     filenames = dlg.GetPaths()
49     for filename in filenames:
50     title = os.path.splitext(os.path.basename(filename))[0]
51     has_layers = map.HasLayers()
52 nhueffme 2549 layerDlg = ogrdialog.ChooseLayer(canvas, filename)
53     if layerDlg.ShowModal() == wxID_OK:
54     layername = layerDlg.GetLayer()
55 nhueffme 2577 try:
56     session = context.application.Session()
57 jan 2609 store = OpenFileShapestore(context.mainwindow, session,
58     filename, layername)
59 nhueffme 2577 session.AddShapeStore(store)
60     except:
61     # the layer couldn't be opened
62 jan 2609 context.mainwindow.RunMessageBox(_("Add Layer"),
63     _("Can't open the file '%s'.") % filename)
64 nhueffme 2577 else:
65     if store is not None:
66     layer = Layer(title, store)
67     map.AddLayer(layer)
68     if not has_layers:
69     # if we're adding a layer to an empty map, fit the
70     # new map to the window
71     canvas.FitMapToWindow()
72     context.application.SetPath("data",filename)
73 nhueffme 2435 dlg.Destroy()
74    
75 nhueffme 2549 def select_file_format(context):
76     ''' Display all available supported formats.
77     '''
78 nhueffme 2435
79 nhueffme 2549 canvas = context.mainwindow.canvas
80     file = None
81     map = canvas.Map()
82 nhueffme 2435
83 nhueffme 2549 session = context.application.Session()
84    
85     dlg = ogrdialog.ChooseFileFormat(canvas, session)
86    
87     if dlg.ShowModal() == wxID_OK:
88 nhueffme 2577 pass
89 nhueffme 2549 dlg.Destroy()
90    
91     def open_db(context):
92     ''' Open a table in a database as a layer.
93     '''
94    
95     canvas = context.mainwindow.canvas
96     map = canvas.Map()
97 nhueffme 2559
98 nhueffme 2577 session = context.application.Session()
99 nhueffme 2549 dlg = ChooseOGRDBTableDialog(canvas, session)
100    
101     if dlg.ShowModal() == wxID_OK:
102 nhueffme 2577 dbconn, connString, dbtable, id_column = dlg.GetTable()
103 nhueffme 2549 try:
104 nhueffme 2577 store = OpenDBShapestore(session, dbconn, dbtable, id_column,
105     None)
106 nhueffme 2549 session.AddShapeStore(store)
107    
108     layer = Layer(dbtable, store)
109    
110     has_layers = map.HasLayers()
111     map.AddLayer(layer)
112     if not has_layers:
113     canvas.FitMapToWindow()
114     except:
115     # Some error occured while initializing the layer
116     context.mainwindow.RunMessageBox(_("Add Layer from database"),
117     _("Can't open the database table '%s'")
118     % dbtable)
119     dlg.Destroy()
120    
121 nhueffme 2577 def open_OGRConnection(context):
122     """Open a datasource with an OGRConnection string."""
123     canvas = context.mainwindow.canvas
124     map = canvas.Map()
125 nhueffme 2549
126 nhueffme 2577 session = context.application.Session()
127     dlg = ogrdialog.OGRConnectionDialog(canvas, session)
128 nhueffme 2549
129 nhueffme 2577 if dlg.ShowModal() == wxID_OK:
130     dsname = dlg.GetDatasourceName()
131    
132     layerDlg = ogrdialog.ChooseLayer(canvas, dsname)
133     if layerDlg.ShowModal() == wxID_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 jan 2609 def OpenFileShapestore(mainwindow, session, filename, layername):
153 nhueffme 2577 """Open a datasource and add the required layer.
154     """
155     try:
156 jan 2609 store = ogrshapes.OGRFileShapeStore(session, filename, layername)
157 nhueffme 2577 return store
158     except:
159     # Some error occured while initializing the layer
160 jan 2609 mainwindow.RunMessageBox(_("Open datasource"),
161 nhueffme 2577 _("Can't open the datasource '%s'")
162     % filename)
163 jan 2609 return None
164 nhueffme 2577
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 nhueffme 2435 # Thuban has named commands which can be registered in the central
193     # instance registry.
194     from Thuban.UI.command import registry, Command
195    
196     # The instance of the main menu of the Thuban application
197     # See Thuban/UI/menu.py for the API of the Menu class
198     from Thuban.UI.mainwindow import main_menu
199    
200 nhueffme 2559
201 nhueffme 2577 # find the map menu (create a new if not found)
202 nhueffme 2559 map_menu = main_menu.FindOrInsertMenu('map', _('Map'))
203     ogr_menu = Menu("ogr", _("Open layer via OGR"),[])
204    
205 nhueffme 2577 ogrsupport = ogrshapes.has_ogr_support()
206 nhueffme 2559
207 nhueffme 2493 # create new commands and register them
208     registry.Add(Command('open_ogr_files', 'Open an ogr-file', open_with_ogr,
209 nhueffme 2577 sensitive = _has_gdal_support,
210 nhueffme 2493 helptext = 'Open a file supported from ogr'))
211 nhueffme 2435
212 nhueffme 2577 #registry.Add(Command('select_file_format', 'Select a file format',
213     # select_file_format,
214     # helptext = "Select a file format supported from ogr"))
215 nhueffme 2435
216 nhueffme 2549 registry.Add(Command('open_db', 'Open a layer from a database',
217     open_db,
218 nhueffme 2577 sensitive = _has_dbconnections,
219 nhueffme 2549 helptext = "Open a layer from a database, e.g. PostGIS"))
220    
221 nhueffme 2577 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 nhueffme 2435 # finally bind the new command with an entry in the extensions menu
228 nhueffme 2559 ogr_menu.InsertItem("open_ogr_files")
229 nhueffme 2577 #ogr_menu.InsertItem('select_file_format')
230 nhueffme 2549 ogr_menu.InsertItem('open_db')
231 nhueffme 2577 ogr_menu.InsertItem('open_OGRConnection')
232 nhueffme 2493
233 nhueffme 2559 # Add ogr menu to map menu
234     map_menu.InsertItem(ogr_menu, after = "rasterlayer_add")

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26