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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2559 - (show annotations)
Tue Feb 8 09:52:56 2005 UTC (20 years ago) by nhueffme
Original Path: trunk/thuban/Extensions/ogr/ogrstart.py
File MIME type: text/x-python
File size: 5127 byte(s)
Changed the GUI. OGR support can now be accessed via the Map menu.
Removed some print commands.

1 # 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 #from wxPython.wx import *
14 from wxPython.wx import wxFileDialog, wxOPEN, wxMULTIPLE, wxID_OK, \
15 wxOK, wxICON_HAND
16
17 # We need os.path
18 import os
19
20 # use _() already now for all strings that may later be translated
21 from Thuban import _
22
23 from Thuban.Model.layer import Layer
24 from Thuban.UI.dbdialog import DBDialog
25
26 # Import ogr related classes
27 from Extensions.ogr import ogrshapes, ogrdialog
28 from Extensions.ogr.ogrdialog import ChooseOGRDBTableDialog
29
30 from Thuban.UI.menu import Menu
31
32 def open_with_ogr(context):
33 '''Open a file supported by ogr.
34 '''
35 canvas = context.mainwindow.canvas
36 map = canvas.Map()
37
38 # Get the file to be opened
39 dlg = wxFileDialog(canvas, _("Select a data file"),
40 ".", "",
41 _("Shape/GML files (*.shp/*.gml)") + "|*.shp;*.gml|" +
42 _("All Files (*.*)") + "|*.*",
43 wxOPEN | wxMULTIPLE)
44
45 if dlg.ShowModal() == wxID_OK:
46 filenames = dlg.GetPaths()
47 for filename in filenames:
48 title = os.path.splitext(os.path.basename(filename))[0]
49 has_layers = map.HasLayers()
50 layerDlg = ogrdialog.ChooseLayer(canvas, filename)
51 if layerDlg.ShowModal() == wxID_OK:
52 layername = layerDlg.GetLayer()
53 try:
54 session = context.application.Session()
55 store = ogrshapes.OGRShapeStore(filename, layername)
56 session.AddShapeStore(store)
57 except:
58 # the layer couldn't be opened
59 context.mainwindow.RunMessageBox(("Add Layer"),
60 ("Can't open the file '%s'.")%filename)
61 else:
62 layer = Layer(title, store)
63 map.AddLayer(layer)
64 if not has_layers:
65 # if we're adding a layer to an empty map, fit the
66 # new map to the window
67 canvas.FitMapToWindow()
68 context.application.SetPath("data",filename)
69 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
123 # instance registry.
124 from Thuban.UI.command import registry, Command
125
126 # The instance of the main menu of the Thuban application
127 # See Thuban/UI/menu.py for the API of the Menu class
128 from Thuban.UI.mainwindow import main_menu
129
130
131 # find the map menu (create it a new if not found)
132 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
149 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

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26