20 |
from Thuban import _ |
from Thuban import _ |
21 |
|
|
22 |
from Thuban.Model.layer import Layer |
from Thuban.Model.layer import Layer |
23 |
|
from Thuban.UI.dbdialog import DBDialog |
24 |
|
|
25 |
# Import ogr related classes |
# Import ogr related classes |
26 |
from Extensions.ogr import ogrshapes, ogrdialog |
from Extensions.ogr import ogrshapes, ogrdialog |
27 |
|
from Extensions.ogr.ogrdialog import ChooseOGRDBTableDialog |
28 |
|
|
29 |
def open_with_ogr(context): |
def open_with_ogr(context): |
30 |
'''Export data depending on the set properties. |
'''Open a file supported by ogr. |
|
|
|
|
This is the main export funcation. |
|
31 |
''' |
''' |
32 |
canvas = context.mainwindow.canvas |
canvas = context.mainwindow.canvas |
|
file = None |
|
33 |
map = canvas.Map() |
map = canvas.Map() |
34 |
|
|
35 |
# Get the file to be opened |
# Get the file to be opened |
36 |
dlg = wxFileDialog(canvas, _("Select a data file"), |
dlg = wxFileDialog(canvas, _("Select a data file"), |
37 |
".", "", |
".", "", |
38 |
_("Shapefiles (*.shp)") + "|*.shp;*.SHP|" + |
_("Shape/GML files (*.shp/*.gml)") + "|*.shp;*.gml|" + |
39 |
_("All Files (*.*)") + "|*.*", |
_("All Files (*.*)") + "|*.*", |
40 |
wxOPEN | wxMULTIPLE) |
wxOPEN | wxMULTIPLE) |
41 |
|
|
44 |
for filename in filenames: |
for filename in filenames: |
45 |
title = os.path.splitext(os.path.basename(filename))[0] |
title = os.path.splitext(os.path.basename(filename))[0] |
46 |
has_layers = map.HasLayers() |
has_layers = map.HasLayers() |
47 |
layername = title |
layerDlg = ogrdialog.ChooseLayer(canvas, filename) |
48 |
|
if layerDlg.ShowModal() == wxID_OK: |
49 |
|
layername = layerDlg.GetLayer() |
50 |
try: |
try: |
51 |
session = context.application.Session() |
session = context.application.Session() |
52 |
store = ogrshapes.OGRShapeStore(session, filename, layername) |
store = ogrshapes.OGRShapeStore(session, filename, layername) |
53 |
session.AddShapeStore(store) |
session.AddShapeStore(store) |
54 |
except IOError: |
except: |
55 |
# the layer couldn't be opened |
# the layer couldn't be opened |
56 |
self.RunMessageBox(("Add Layer"), |
context.mainwindow.RunMessageBox(("Add Layer"), |
57 |
("Can't open the file '%s'.")%filename) |
("Can't open the file '%s'.")%filename) |
58 |
else: |
else: |
59 |
layer = Layer(title, store) |
layer = Layer(title, store) |
65 |
context.application.SetPath("data",filename) |
context.application.SetPath("data",filename) |
66 |
dlg.Destroy() |
dlg.Destroy() |
67 |
|
|
68 |
|
def select_file_format(context): |
69 |
|
''' Display all available supported formats. |
70 |
|
''' |
71 |
|
|
72 |
|
canvas = context.mainwindow.canvas |
73 |
|
file = None |
74 |
|
map = canvas.Map() |
75 |
|
|
76 |
|
session = context.application.Session() |
77 |
|
|
78 |
|
dlg = ogrdialog.ChooseFileFormat(canvas, session) |
79 |
|
|
80 |
|
if dlg.ShowModal() == wxID_OK: |
81 |
|
context.mainwindow.RunMessageBox("dialog auf", "dialog auf") |
82 |
|
dlg.Destroy() |
83 |
|
|
84 |
|
def open_db(context): |
85 |
|
''' Open a table in a database as a layer. |
86 |
|
''' |
87 |
|
|
88 |
|
canvas = context.mainwindow.canvas |
89 |
|
map = canvas.Map() |
90 |
|
|
91 |
|
session = context.application.Session() |
92 |
|
dlg = ChooseOGRDBTableDialog(canvas, session) |
93 |
|
|
94 |
|
if dlg.ShowModal() == wxID_OK: |
95 |
|
dbconn, dbtable = dlg.GetTable() |
96 |
|
try: |
97 |
|
# Choose the correct Interface for the database type |
98 |
|
filename = ('PG: host=%s user=%s dbname=%s port=%s' |
99 |
|
%(dbconn.host, dbconn.user, dbconn.dbname, dbconn.port)) |
100 |
|
|
101 |
|
store = ogrshapes.OGRShapeStore(session, filename, dbtable) |
102 |
|
session.AddShapeStore(store) |
103 |
|
|
104 |
|
layer = Layer(dbtable, store) |
105 |
|
|
106 |
|
has_layers = map.HasLayers() |
107 |
|
map.AddLayer(layer) |
108 |
|
if not has_layers: |
109 |
|
canvas.FitMapToWindow() |
110 |
|
except: |
111 |
|
# Some error occured while initializing the layer |
112 |
|
context.mainwindow.RunMessageBox(_("Add Layer from database"), |
113 |
|
_("Can't open the database table '%s'") |
114 |
|
% dbtable) |
115 |
|
dlg.Destroy() |
116 |
|
|
117 |
|
|
118 |
|
|
119 |
# Thuban has named commands which can be registered in the central |
# Thuban has named commands which can be registered in the central |
130 |
|
|
131 |
# find the ogr menu (create it a new if not found) |
# find the ogr menu (create it a new if not found) |
132 |
ogr_menu = main_menu.FindOrInsertMenu('ogr', _('OGR')) |
ogr_menu = main_menu.FindOrInsertMenu('ogr', _('OGR')) |
133 |
|
registry.Add(Command('select_file_format', 'Select a file format', |
134 |
|
select_file_format, |
135 |
|
helptext = "Select a file format supported from ogr")) |
136 |
|
|
137 |
|
registry.Add(Command('open_db', 'Open a layer from a database', |
138 |
|
open_db, |
139 |
|
helptext = "Open a layer from a database, e.g. PostGIS")) |
140 |
|
|
141 |
# finally bind the new command with an entry in the extensions menu |
# finally bind the new command with an entry in the extensions menu |
142 |
ogr_menu.InsertItem('open_ogr_files') |
ogr_menu.InsertItem('open_ogr_files') |
143 |
|
ogr_menu.InsertItem('select_file_format') |
144 |
|
ogr_menu.InsertItem('open_db') |
145 |
|
|