22 |
from Thuban.Model.layer import Layer |
from Thuban.Model.layer import Layer |
23 |
|
|
24 |
# Import ogr related classes |
# Import ogr related classes |
25 |
from Extensions.ogr import ogrshapes |
from Extensions.ogr import ogrshapes, ogrdialog |
26 |
|
|
27 |
def open_with_ogr(context): |
def open_with_ogr(context): |
28 |
'''Export data depending on the set properties. |
'''Export data depending on the set properties. |
29 |
|
|
30 |
This is the main export funcation. |
This is the main export funcation. |
31 |
''' |
''' |
32 |
canvas = context.mainwindow.canvas |
canvas = context.mainwindow.canvas |
33 |
file = None |
file = None |
34 |
map = canvas.Map() |
map = canvas.Map() |
35 |
|
|
36 |
if hasattr(canvas, "export_path"): |
# Get the file to be opened |
37 |
export_path = canvas.export_path |
dlg = wxFileDialog(canvas, _("Select a data file"), |
38 |
else: |
".", "", |
|
export_path="." |
|
|
# Get the file to be opened |
|
|
dlg = wxFileDialog(canvas, _("Select a data file"), |
|
|
export_path, "", |
|
39 |
_("Shapefiles (*.shp)") + "|*.shp;*.SHP|" + |
_("Shapefiles (*.shp)") + "|*.shp;*.SHP|" + |
40 |
_("All Files (*.*)") + "|*.*", |
_("All Files (*.*)") + "|*.*", |
41 |
wxOPEN | wxMULTIPLE) |
wxOPEN | wxMULTIPLE) |
42 |
|
|
43 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
44 |
filenames = dlg.GetPaths() |
filenames = dlg.GetPaths() |
45 |
for filename in filenames: |
for filename in filenames: |
46 |
title = os.path.splitext(os.path.basename(filename))[0] |
title = os.path.splitext(os.path.basename(filename))[0] |
47 |
has_layers = map.HasLayers() |
has_layers = map.HasLayers() |
48 |
layername = title |
layername = title |
49 |
try: |
try: |
50 |
session = context.application.Session() |
session = context.application.Session() |
51 |
store = ogrshapes.OGRShapeStore(session, filename, layername) |
store = ogrshapes.OGRShapeStore(session, filename, layername) |
52 |
session.AddShapeStore(store) |
session.AddShapeStore(store) |
53 |
except IOError: |
except IOError: |
54 |
# the layer couldn't be opened |
# the layer couldn't be opened |
55 |
self.RunMessageBox(_("Add Layer"), |
self.RunMessageBox(("Add Layer"), |
56 |
_("Can't open the file '%s'.")%filename) |
("Can't open the file '%s'.")%filename) |
57 |
else: |
else: |
58 |
layer = Layer(title, store) |
layer = Layer(title, store) |
59 |
map.AddLayer(layer) |
map.AddLayer(layer) |
60 |
if not has_layers: |
if not has_layers: |
61 |
# if we're adding a layer to an empty map, fit the |
# if we're adding a layer to an empty map, fit the |
62 |
# new map to the window |
# new map to the window |
63 |
canvas.FitMapToWindow() |
canvas.FitMapToWindow() |
64 |
context.application.SetPath("data",filename) |
context.application.SetPath("data",filename) |
65 |
dlg.Destroy() |
dlg.Destroy() |
66 |
|
|
67 |
|
|
74 |
# See Thuban/UI/menu.py for the API of the Menu class |
# See Thuban/UI/menu.py for the API of the Menu class |
75 |
from Thuban.UI.mainwindow import main_menu |
from Thuban.UI.mainwindow import main_menu |
76 |
|
|
77 |
# create a new command and register it |
# create new commands and register them |
78 |
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, |
79 |
helptext = _('Open a file supported from ogr'))) |
helptext = 'Open a file supported from ogr')) |
80 |
|
|
81 |
# find the ogr menu (create it a new if not found) |
# find the ogr menu (create it a new if not found) |
82 |
ogr_menu = main_menu.FindOrInsertMenu('ogr', _('OGR')) |
ogr_menu = main_menu.FindOrInsertMenu('ogr', _('OGR')) |
83 |
|
|
84 |
# finally bind the new command with an entry in the extensions menu |
# finally bind the new command with an entry in the extensions menu |
85 |
ogr_menu.InsertItem('open_ogr_files') |
ogr_menu.InsertItem('open_ogr_files') |
86 |
|
|