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