1 |
# Copyright (C) 2003, 2004 by Intevation GmbH |
2 |
# Authors: |
3 |
# Jan-Oliver Wagner <[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 |
""" |
9 |
Provide layers via OGC WMS. |
10 |
|
11 |
This extension is in a very experimental stage! |
12 |
It just demonstrates how to add a special |
13 |
layer into Thuban via an extension. |
14 |
Some things are not wired, so be prepared for Exceptions |
15 |
everywhere. |
16 |
""" |
17 |
|
18 |
__version__ = "$Revision$" |
19 |
# $Source$ |
20 |
# $Id$ |
21 |
|
22 |
import os, sys |
23 |
import xml.dom.minidom |
24 |
import tempfile |
25 |
|
26 |
from wxPython.wx import * |
27 |
|
28 |
from Thuban.Model.proj import Projection |
29 |
from Thuban.Model.extension import Extension |
30 |
from Thuban.UI.command import registry, Command |
31 |
from Thuban.UI.mainwindow import main_menu |
32 |
from Thuban import _ |
33 |
import Thuban.UI.baserenderer |
34 |
from Thuban.UI.extensionregistry import ExtensionDesc, ext_registry |
35 |
|
36 |
from layer import WMSLayer |
37 |
|
38 |
ext_registry.add(ExtensionDesc( |
39 |
name = 'WMS', |
40 |
version = '0.1.0', |
41 |
authors= [ 'Jan-Oliver Wagner' ], |
42 |
copyright = '2003, 2004 Intevation GmbH', |
43 |
desc = _("Provide layers via OGC WMS."))) |
44 |
|
45 |
|
46 |
class WMSExtension(Extension): |
47 |
def TreeInfo(self): |
48 |
return (_("Extension: %s") % self.title, |
49 |
[ object.TreeInfo() for object in self.objects ]) |
50 |
|
51 |
|
52 |
def render_wms_layer(renderer, layer): |
53 |
offx, offy = renderer.offset |
54 |
width, height = renderer.dc.GetSizeTuple() |
55 |
|
56 |
scale = renderer.scale |
57 |
xmin = (0 - offx) / scale |
58 |
ymin = (offy - height) / scale |
59 |
xmax = (width - offx) / scale |
60 |
ymax = (offy - 0) / scale |
61 |
|
62 |
img, format = layer.GetMapImg(width, height, (xmin, ymin, xmax, ymax)) |
63 |
renderer.draw_raster_data(img, format) |
64 |
|
65 |
return () |
66 |
|
67 |
Thuban.UI.baserenderer.add_renderer_extension(WMSLayer, render_wms_layer) |
68 |
|
69 |
|
70 |
class SelectWMSServer(wxDialog): |
71 |
|
72 |
ID_COMBOVALUE = 4003 |
73 |
|
74 |
def __init__(self, parent): |
75 |
wxDialog.__init__(self, parent, -1, _("Select WMS Server"), |
76 |
style = wxDEFAULT_DIALOG_STYLE |
77 |
| wxSYSTEM_MENU |
78 |
| wxRESIZE_BORDER) |
79 |
|
80 |
self.combo_value = wxComboBox(self, self.ID_COMBOVALUE, size=(500,-1)) |
81 |
self.combo_value.Append("") |
82 |
self.combo_value.Append('http://frida.intevation.org/cgi-bin/frida_wms?') |
83 |
#self.combo_value.Append('http://wms.jpl.nasa.gov/wms.cgi?') |
84 |
#self.combo_value.Append('http://eukrante.hq:9089/cgi-bin/wms_shg?') |
85 |
#self.combo_value.Append('http://131.220.106.112:8080/deegree0.7/wms?') |
86 |
#self.combo_value.Append('http://demo.cubewerx.com/demo/cubeserv/cubeserv.cgi?CONFIG=gita&') |
87 |
self.combo_value.SetSelection(0) |
88 |
|
89 |
button_ok = wxButton(self, wxID_OK, _("OK")) |
90 |
button_ok.SetDefault() |
91 |
button_close = wxButton(self, wxID_CANCEL, _("Close")) |
92 |
|
93 |
vbox = wxBoxSizer(wxVERTICAL) |
94 |
vbox.Add(self.combo_value, 1, wxEXPAND|wxALL|wxCB_SORT, 10) |
95 |
hbox = wxBoxSizer(wxHORIZONTAL) |
96 |
hbox.Add(button_ok, 0, wxALL, 10) |
97 |
hbox.Add(button_close, 0, wxALL, 10) |
98 |
vbox.Add(hbox, 0, 10) |
99 |
|
100 |
self.SetAutoLayout(True) |
101 |
self.SetSizer(vbox) |
102 |
vbox.Fit(self) |
103 |
vbox.SetSizeHints(self) |
104 |
self.Layout() |
105 |
|
106 |
EVT_BUTTON(self, wxID_OK, self.OnOK) |
107 |
EVT_BUTTON(self, wxID_CANCEL, self.OnCancel) |
108 |
|
109 |
def OnOK(self, event): |
110 |
self.url = self.combo_value.GetValue() |
111 |
self.EndModal(wxID_OK) |
112 |
|
113 |
def OnCancel(self, event): |
114 |
self.EndModal(wxID_CANCEL) |
115 |
|
116 |
def wms_dialog(context): |
117 |
"""Request URL from user and add WMS Layer. |
118 |
|
119 |
context -- The Thuban context. |
120 |
""" |
121 |
dialog = SelectWMSServer(context.mainwindow) |
122 |
|
123 |
if dialog.ShowModal() == wxID_OK: |
124 |
url = dialog.url |
125 |
if len(url) == 0: |
126 |
url = None |
127 |
else: |
128 |
url = None |
129 |
dialog.Destroy() |
130 |
|
131 |
if url is None: |
132 |
return |
133 |
|
134 |
wms_layer = WMSLayer('A WMS Layer', url) |
135 |
if wms_layer.error_msg is not None: |
136 |
context.mainwindow.RunMessageBox(_('WMS'), wms_layer.error_msg) |
137 |
|
138 |
map = context.mainwindow.canvas.Map() |
139 |
if map.projection is None: |
140 |
map.SetProjection(wms_layer.projection) |
141 |
has_layers = map.HasLayers() |
142 |
map.AddLayer(wms_layer) |
143 |
if not has_layers: |
144 |
# if we're adding a layer to an empty map, fit the |
145 |
# new map to the window |
146 |
context.mainwindow.canvas.FitMapToWindow() |
147 |
|
148 |
wxInitAllImageHandlers() |
149 |
wms_extension = WMSExtension('WMS') |
150 |
|
151 |
# register the new command |
152 |
registry.Add(Command('wms', _('Add WMS layer ...'), wms_dialog, |
153 |
helptext = _('Add a WMS Layer'))) |
154 |
|
155 |
# find the experimental menu (create it anew if not found) |
156 |
experimental_menu = main_menu.FindOrInsertMenu('experimental', |
157 |
_('Experimenta&l')) |
158 |
|
159 |
# finally add the new entry to the experimental menu |
160 |
experimental_menu.InsertItem('wms') |