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 |
nhueffme |
2559 |
#from wxPython.wx import * |
14 |
nhueffme |
2435 |
from wxPython.wx import wxFileDialog, wxOPEN, wxMULTIPLE, wxID_OK, \ |
15 |
|
|
wxOK, wxICON_HAND |
16 |
|
|
|
17 |
|
|
# We need os.path |
18 |
|
|
import os |
19 |
|
|
|
20 |
|
|
# use _() already now for all strings that may later be translated |
21 |
|
|
from Thuban import _ |
22 |
|
|
|
23 |
|
|
from Thuban.Model.layer import Layer |
24 |
nhueffme |
2549 |
from Thuban.UI.dbdialog import DBDialog |
25 |
nhueffme |
2435 |
|
26 |
|
|
# Import ogr related classes |
27 |
nhueffme |
2493 |
from Extensions.ogr import ogrshapes, ogrdialog |
28 |
nhueffme |
2549 |
from Extensions.ogr.ogrdialog import ChooseOGRDBTableDialog |
29 |
nhueffme |
2435 |
|
30 |
nhueffme |
2559 |
from Thuban.UI.menu import Menu |
31 |
|
|
|
32 |
nhueffme |
2435 |
def open_with_ogr(context): |
33 |
nhueffme |
2549 |
'''Open a file supported by ogr. |
34 |
nhueffme |
2493 |
''' |
35 |
|
|
canvas = context.mainwindow.canvas |
36 |
|
|
map = canvas.Map() |
37 |
nhueffme |
2435 |
|
38 |
nhueffme |
2493 |
# Get the file to be opened |
39 |
|
|
dlg = wxFileDialog(canvas, _("Select a data file"), |
40 |
|
|
".", "", |
41 |
nhueffme |
2549 |
_("Shape/GML files (*.shp/*.gml)") + "|*.shp;*.gml|" + |
42 |
nhueffme |
2435 |
_("All Files (*.*)") + "|*.*", |
43 |
|
|
wxOPEN | wxMULTIPLE) |
44 |
|
|
|
45 |
nhueffme |
2493 |
if dlg.ShowModal() == wxID_OK: |
46 |
|
|
filenames = dlg.GetPaths() |
47 |
|
|
for filename in filenames: |
48 |
|
|
title = os.path.splitext(os.path.basename(filename))[0] |
49 |
|
|
has_layers = map.HasLayers() |
50 |
nhueffme |
2549 |
layerDlg = ogrdialog.ChooseLayer(canvas, filename) |
51 |
|
|
if layerDlg.ShowModal() == wxID_OK: |
52 |
|
|
layername = layerDlg.GetLayer() |
53 |
nhueffme |
2493 |
try: |
54 |
|
|
session = context.application.Session() |
55 |
nhueffme |
2559 |
store = ogrshapes.OGRShapeStore(filename, layername) |
56 |
nhueffme |
2493 |
session.AddShapeStore(store) |
57 |
nhueffme |
2549 |
except: |
58 |
nhueffme |
2493 |
# the layer couldn't be opened |
59 |
nhueffme |
2549 |
context.mainwindow.RunMessageBox(("Add Layer"), |
60 |
nhueffme |
2493 |
("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 |
nhueffme |
2435 |
dlg.Destroy() |
70 |
|
|
|
71 |
nhueffme |
2549 |
def select_file_format(context): |
72 |
|
|
''' Display all available supported formats. |
73 |
|
|
''' |
74 |
nhueffme |
2435 |
|
75 |
nhueffme |
2549 |
canvas = context.mainwindow.canvas |
76 |
|
|
file = None |
77 |
|
|
map = canvas.Map() |
78 |
nhueffme |
2435 |
|
79 |
nhueffme |
2549 |
session = context.application.Session() |
80 |
|
|
|
81 |
|
|
dlg = ogrdialog.ChooseFileFormat(canvas, session) |
82 |
|
|
|
83 |
|
|
if dlg.ShowModal() == wxID_OK: |
84 |
|
|
context.mainwindow.RunMessageBox("dialog auf", "dialog auf") |
85 |
|
|
dlg.Destroy() |
86 |
|
|
|
87 |
|
|
def open_db(context): |
88 |
|
|
''' Open a table in a database as a layer. |
89 |
|
|
''' |
90 |
|
|
|
91 |
|
|
canvas = context.mainwindow.canvas |
92 |
|
|
map = canvas.Map() |
93 |
nhueffme |
2559 |
|
94 |
nhueffme |
2549 |
session = context.application.Session() |
95 |
|
|
dlg = ChooseOGRDBTableDialog(canvas, session) |
96 |
|
|
|
97 |
|
|
if dlg.ShowModal() == wxID_OK: |
98 |
|
|
dbconn, dbtable = dlg.GetTable() |
99 |
|
|
try: |
100 |
|
|
# Choose the correct Interface for the database type |
101 |
|
|
filename = ('PG: host=%s user=%s dbname=%s port=%s' |
102 |
|
|
%(dbconn.host, dbconn.user, dbconn.dbname, dbconn.port)) |
103 |
|
|
|
104 |
nhueffme |
2559 |
store = ogrshapes.OGRShapeStore(filename, dbtable) |
105 |
nhueffme |
2549 |
session.AddShapeStore(store) |
106 |
|
|
|
107 |
|
|
layer = Layer(dbtable, store) |
108 |
|
|
|
109 |
|
|
has_layers = map.HasLayers() |
110 |
|
|
map.AddLayer(layer) |
111 |
|
|
if not has_layers: |
112 |
|
|
canvas.FitMapToWindow() |
113 |
|
|
except: |
114 |
|
|
# Some error occured while initializing the layer |
115 |
|
|
context.mainwindow.RunMessageBox(_("Add Layer from database"), |
116 |
|
|
_("Can't open the database table '%s'") |
117 |
|
|
% dbtable) |
118 |
|
|
dlg.Destroy() |
119 |
|
|
|
120 |
|
|
|
121 |
|
|
|
122 |
nhueffme |
2435 |
# Thuban has named commands which can be registered in the central |
123 |
|
|
# instance registry. |
124 |
|
|
from Thuban.UI.command import registry, Command |
125 |
|
|
|
126 |
|
|
# The instance of the main menu of the Thuban application |
127 |
|
|
# See Thuban/UI/menu.py for the API of the Menu class |
128 |
|
|
from Thuban.UI.mainwindow import main_menu |
129 |
|
|
|
130 |
nhueffme |
2559 |
|
131 |
|
|
# find the map menu (create it a new if not found) |
132 |
|
|
map_menu = main_menu.FindOrInsertMenu('map', _('Map')) |
133 |
|
|
ogr_menu = Menu("ogr", _("Open layer via OGR"),[]) |
134 |
|
|
|
135 |
|
|
|
136 |
nhueffme |
2493 |
# create new commands and register them |
137 |
|
|
registry.Add(Command('open_ogr_files', 'Open an ogr-file', open_with_ogr, |
138 |
|
|
helptext = 'Open a file supported from ogr')) |
139 |
nhueffme |
2435 |
|
140 |
nhueffme |
2559 |
registry.Add(Command('select_file_format', 'Select a file format', |
141 |
nhueffme |
2549 |
select_file_format, |
142 |
|
|
helptext = "Select a file format supported from ogr")) |
143 |
nhueffme |
2435 |
|
144 |
nhueffme |
2549 |
registry.Add(Command('open_db', 'Open a layer from a database', |
145 |
|
|
open_db, |
146 |
|
|
helptext = "Open a layer from a database, e.g. PostGIS")) |
147 |
|
|
|
148 |
nhueffme |
2435 |
# finally bind the new command with an entry in the extensions menu |
149 |
nhueffme |
2559 |
ogr_menu.InsertItem("open_ogr_files") |
150 |
nhueffme |
2549 |
ogr_menu.InsertItem('select_file_format') |
151 |
|
|
ogr_menu.InsertItem('open_db') |
152 |
nhueffme |
2493 |
|
153 |
nhueffme |
2559 |
# Add ogr menu to map menu |
154 |
|
|
map_menu.InsertItem(ogr_menu, after = "rasterlayer_add") |
155 |
|
|
|