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 |
|
|
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 |
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 |
|
|
if hasattr(canvas, "export_path"): |
37 |
|
|
export_path = canvas.export_path |
38 |
|
|
else: |
39 |
|
|
export_path="." |
40 |
|
|
# Get the file to be opened |
41 |
|
|
dlg = wxFileDialog(canvas, _("Select a data file"), |
42 |
|
|
export_path, "", |
43 |
|
|
_("Shapefiles (*.shp)") + "|*.shp;*.SHP|" + |
44 |
|
|
_("All Files (*.*)") + "|*.*", |
45 |
|
|
wxOPEN | wxMULTIPLE) |
46 |
|
|
|
47 |
|
|
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 |
|
|
layername = title |
53 |
|
|
try: |
54 |
|
|
session = context.application.Session() |
55 |
|
|
store = ogrshapes.OGRShapeStore(session, filename, layername) |
56 |
|
|
session.AddShapeStore(store) |
57 |
|
|
except IOError: |
58 |
|
|
# the layer couldn't be opened |
59 |
|
|
self.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 |
|
|
|
72 |
|
|
|
73 |
|
|
# Thuban has named commands which can be registered in the central |
74 |
|
|
# instance registry. |
75 |
|
|
from Thuban.UI.command import registry, Command |
76 |
|
|
|
77 |
|
|
# The instance of the main menu of the Thuban application |
78 |
|
|
# See Thuban/UI/menu.py for the API of the Menu class |
79 |
|
|
from Thuban.UI.mainwindow import main_menu |
80 |
|
|
|
81 |
|
|
# create a new command and register it |
82 |
|
|
registry.Add(Command('open_ogr_files', _('Open an ogr-file'), open_with_ogr, |
83 |
|
|
helptext = _('Open a file supported from ogr'))) |
84 |
|
|
|
85 |
|
|
# find the ogr menu (create it a new if not found) |
86 |
|
|
ogr_menu = main_menu.FindOrInsertMenu('ogr', _('OGR')) |
87 |
|
|
|
88 |
|
|
# finally bind the new command with an entry in the extensions menu |
89 |
|
|
ogr_menu.InsertItem('open_ogr_files') |