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 |
nhueffme |
2549 |
from Thuban.UI.dbdialog import DBDialog |
24 |
nhueffme |
2435 |
|
25 |
|
|
# Import ogr related classes |
26 |
nhueffme |
2493 |
from Extensions.ogr import ogrshapes, ogrdialog |
27 |
nhueffme |
2549 |
from Extensions.ogr.ogrdialog import ChooseOGRDBTableDialog |
28 |
nhueffme |
2435 |
|
29 |
|
|
def open_with_ogr(context): |
30 |
nhueffme |
2549 |
'''Open a file supported by ogr. |
31 |
nhueffme |
2493 |
''' |
32 |
|
|
canvas = context.mainwindow.canvas |
33 |
|
|
map = canvas.Map() |
34 |
nhueffme |
2435 |
|
35 |
nhueffme |
2493 |
# Get the file to be opened |
36 |
|
|
dlg = wxFileDialog(canvas, _("Select a data file"), |
37 |
|
|
".", "", |
38 |
nhueffme |
2549 |
_("Shape/GML files (*.shp/*.gml)") + "|*.shp;*.gml|" + |
39 |
nhueffme |
2435 |
_("All Files (*.*)") + "|*.*", |
40 |
|
|
wxOPEN | wxMULTIPLE) |
41 |
|
|
|
42 |
nhueffme |
2493 |
if dlg.ShowModal() == wxID_OK: |
43 |
|
|
filenames = dlg.GetPaths() |
44 |
|
|
for filename in filenames: |
45 |
|
|
title = os.path.splitext(os.path.basename(filename))[0] |
46 |
|
|
has_layers = map.HasLayers() |
47 |
nhueffme |
2549 |
layerDlg = ogrdialog.ChooseLayer(canvas, filename) |
48 |
|
|
if layerDlg.ShowModal() == wxID_OK: |
49 |
|
|
layername = layerDlg.GetLayer() |
50 |
nhueffme |
2493 |
try: |
51 |
|
|
session = context.application.Session() |
52 |
|
|
store = ogrshapes.OGRShapeStore(session, filename, layername) |
53 |
|
|
session.AddShapeStore(store) |
54 |
nhueffme |
2549 |
except: |
55 |
nhueffme |
2493 |
# the layer couldn't be opened |
56 |
nhueffme |
2549 |
context.mainwindow.RunMessageBox(("Add Layer"), |
57 |
nhueffme |
2493 |
("Can't open the file '%s'.")%filename) |
58 |
|
|
else: |
59 |
|
|
layer = Layer(title, store) |
60 |
|
|
map.AddLayer(layer) |
61 |
|
|
if not has_layers: |
62 |
|
|
# if we're adding a layer to an empty map, fit the |
63 |
|
|
# new map to the window |
64 |
|
|
canvas.FitMapToWindow() |
65 |
|
|
context.application.SetPath("data",filename) |
66 |
nhueffme |
2435 |
dlg.Destroy() |
67 |
|
|
|
68 |
nhueffme |
2549 |
def select_file_format(context): |
69 |
|
|
''' Display all available supported formats. |
70 |
|
|
''' |
71 |
nhueffme |
2435 |
|
72 |
nhueffme |
2549 |
canvas = context.mainwindow.canvas |
73 |
|
|
file = None |
74 |
|
|
map = canvas.Map() |
75 |
nhueffme |
2435 |
|
76 |
nhueffme |
2549 |
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 |
nhueffme |
2435 |
# Thuban has named commands which can be registered in the central |
120 |
|
|
# instance registry. |
121 |
|
|
from Thuban.UI.command import registry, Command |
122 |
|
|
|
123 |
|
|
# The instance of the main menu of the Thuban application |
124 |
|
|
# See Thuban/UI/menu.py for the API of the Menu class |
125 |
|
|
from Thuban.UI.mainwindow import main_menu |
126 |
|
|
|
127 |
nhueffme |
2493 |
# create new commands and register them |
128 |
|
|
registry.Add(Command('open_ogr_files', 'Open an ogr-file', open_with_ogr, |
129 |
|
|
helptext = 'Open a file supported from ogr')) |
130 |
nhueffme |
2435 |
|
131 |
|
|
# find the ogr menu (create it a new if not found) |
132 |
|
|
ogr_menu = main_menu.FindOrInsertMenu('ogr', _('OGR')) |
133 |
nhueffme |
2549 |
registry.Add(Command('select_file_format', 'Select a file format', |
134 |
|
|
select_file_format, |
135 |
|
|
helptext = "Select a file format supported from ogr")) |
136 |
nhueffme |
2435 |
|
137 |
nhueffme |
2549 |
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 |
nhueffme |
2435 |
# finally bind the new command with an entry in the extensions menu |
142 |
|
|
ogr_menu.InsertItem('open_ogr_files') |
143 |
nhueffme |
2549 |
ogr_menu.InsertItem('select_file_format') |
144 |
|
|
ogr_menu.InsertItem('open_db') |
145 |
nhueffme |
2493 |
|