~ubuntu-branches/ubuntu/lucid/thuban/lucid

« back to all changes in this revision

Viewing changes to Thuban/UI/proj4dialog.py

  • Committer: Bazaar Package Importer
  • Author(s): Francesco Paolo Lovergine
  • Date: 2007-04-07 21:03:28 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070407210328-9a20n4dmbim3c3yc
Tags: 1.2.0-1
* New upstream release.
* Patchset updated.
* Policy bumped to 3.7.2 (no changes).
* Updated for current Python Policy by using python-support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2001, 2003 by Intevation GmbH
2
 
# Authors:
3
 
# Frank Koormann    <frank.koormann@intevation.de>
4
 
#
5
 
# This program is free software under the GPL (>=v2)
6
 
# Read the file COPYING coming with Thuban for details.
7
 
 
8
 
"""
9
 
Dialogs to the geographic projection library PROJ
10
 
"""
11
 
 
12
 
__version__ = "$Revision: 1.9 $"
13
 
 
14
 
from wxPython.wx import *
15
 
 
16
 
from Thuban import _
17
 
 
18
 
ID_PROJECTION_EDIT   = 4010
19
 
ID_PROJECTION_SELECT = 4011
20
 
ID_PROJECTION_OK     = 4001
21
 
ID_PROJECTION_CANCEL = 4002
22
 
 
23
 
ID_UTM_DIALOG_OK           = 4101
24
 
ID_UTM_DIALOG_CANCEL       = 4102
25
 
ID_UTM_DIALOG_PROPOSE_ZONE = 4103
26
 
 
27
 
ID_UTM_PROPOSE_ZONE_DIALOG_TAKE   = 4201
28
 
ID_UTM_PROPOSE_ZONE_DIALOG_CANCEL = 4202
29
 
 
30
 
projectionDict = {'None' : 'None', 'UTM' : 'utm'}
31
 
projectionMapping = {'None' : 'None', 'utm' : 'UTM'}
32
 
 
33
 
class Proj4Dialog(wxDialog):
34
 
 
35
 
    """Let the user select a projection and specify related parameters"""
36
 
    
37
 
    def __init__(self, parent, projectionParamsList, map_bounding_box):
38
 
        wxDialog.__init__(self, parent, -1, _("Projection"),
39
 
                          style = wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
40
 
 
41
 
        self.map_bounding_box = map_bounding_box
42
 
        self.dialogLayout()
43
 
        self.projectionParams={}
44
 
        combo = self.projection
45
 
        if projectionParamsList is not None:
46
 
            for element in projectionParamsList:
47
 
                key, val = element.split('=')
48
 
                self.projectionParams[key] = val
49
 
            if self.projectionParams.has_key('proj'):
50
 
                proj = projectionMapping[self.projectionParams['proj']]
51
 
                combo.SetSelection(combo.FindString(proj))
52
 
            else:
53
 
                combo.SetSelection(combo.FindString('None'))
54
 
            self.UpdateProjectionInfo()
55
 
        else:
56
 
            self.projection.SetValue('None')
57
 
            self.UpdateProjectionInfo()
58
 
 
59
 
 
60
 
    def dialogLayout(self):
61
 
        topBox = wxBoxSizer(wxVERTICAL)
62
 
 
63
 
        # Projection selection:
64
 
        projectionBox = wxBoxSizer(wxHORIZONTAL)
65
 
        projectionBox.Add(wxStaticText(self, -1, _("Projection")), 
66
 
            0, wxALIGN_CENTER|wxALL, 4)
67
 
        
68
 
        self.projection = wxComboBox(self, ID_PROJECTION_SELECT, "", 
69
 
                                     style = wxCB_READONLY)
70
 
        for projection in projectionDict.keys():
71
 
            self.projection.Append(projection)
72
 
        projectionBox.Add(self.projection, 0, wxALL, 4)
73
 
        projectionBox.Add(wxButton(self, ID_PROJECTION_EDIT, _("Edit")), 
74
 
                          0, wxALL, 4) 
75
 
        EVT_COMBOBOX(self, ID_PROJECTION_SELECT, self.OnProj4Select)
76
 
        EVT_BUTTON(self, ID_PROJECTION_EDIT, self.OnEdit)
77
 
        
78
 
        topBox.Add(projectionBox, 0, 0)
79
 
 
80
 
        # Info about current projection:
81
 
        self.projectionInfo = wxTextCtrl(self, -1, "", 
82
 
                                         style = wxTE_MULTILINE|wxTE_READONLY,
83
 
                                         size = (-1, 60))
84
 
        topBox.Add(self.projectionInfo,1,wxEXPAND|wxALL, 4)
85
 
 
86
 
        # Control buttons: 
87
 
        buttonBox = wxBoxSizer( wxHORIZONTAL ) 
88
 
        buttonBox.Add(wxButton(self, ID_PROJECTION_OK, _("OK")), 
89
 
                      0, wxALL, 4) 
90
 
        buttonBox.Add(wxButton(self, ID_PROJECTION_CANCEL, _("Cancel")), 
91
 
                      0, wxALL, 4) 
92
 
        topBox.Add(buttonBox, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 10)
93
 
 
94
 
        EVT_BUTTON(self, ID_PROJECTION_OK, self.OnProj4OK)
95
 
        EVT_BUTTON(self, ID_PROJECTION_CANCEL, self.OnProj4Cancel)
96
 
 
97
 
        self.SetAutoLayout(True)
98
 
        self.SetSizer(topBox)
99
 
        topBox.Fit(self)
100
 
        topBox.SetSizeHints(self)
101
 
 
102
 
    def OnProj4Select(self, event):
103
 
        projection = self.projection.GetStringSelection()
104
 
        if projection == 'None':
105
 
            self.projectionParams = {}
106
 
            self.UpdateProjectionInfo()
107
 
 
108
 
    def OnEdit(self, event):
109
 
        projection = self.projection.GetStringSelection()
110
 
        if projection is not 'None':
111
 
            dialogname = getattr(self, 'launch' + projection + 'Dialog', None)
112
 
            if dialogname is not None:
113
 
                if dialogname(self):
114
 
                    self.UpdateProjectionInfo()
115
 
                else:
116
 
                    pass
117
 
        else:
118
 
            self.projectionParams = {}
119
 
 
120
 
    def UpdateProjectionInfo(self):
121
 
        self.projectionInfo.Clear()
122
 
        for key in self.projectionParams.keys():
123
 
            self.projectionInfo.AppendText(key+": "
124
 
                +str(self.projectionParams[key])+"\n")
125
 
        
126
 
 
127
 
    def OnProj4OK(self, event):
128
 
        self.EndModal(wxID_OK)
129
 
 
130
 
    def OnProj4Cancel(self, event):
131
 
        self.EndModal(wxID_CANCEL)
132
 
 
133
 
    def launchUTMDialog(self, parent):
134
 
        dlg = UTMDialog(parent, parent.projectionInfo )
135
 
        if dlg.ShowModal():
136
 
            return True
137
 
        else:
138
 
            return False
139
 
 
140
 
    def GetParams(self):
141
 
        if len(self.projectionParams.keys()) > 0:    
142
 
            projection = []
143
 
            for key in self.projectionParams.keys():
144
 
                projection.append(key+"="+str(self.projectionParams[key]))
145
 
        else:
146
 
            projection=None
147
 
 
148
 
        return projection
149
 
 
150
 
 
151
 
 
152
 
class UTMDialog(wxDialog):
153
 
 
154
 
    """Let the user specify parameters for UTM projection (Zone, Spheroid)"""
155
 
    
156
 
    def __init__(self, parent, projection):
157
 
        wxDialog.__init__(self, parent, -1, _("Projection: UTM Parameters"),
158
 
                          wxDefaultPosition, wxSize(200, 100))
159
 
        self.parent = parent
160
 
        self.dialogLayout()
161
 
        if self.parent.projectionParams.has_key('zone'):
162
 
            text = str(self.parent.projectionParams['zone'])
163
 
            self.zone.SetSelection(self.zone.FindString(text))
164
 
        if self.parent.projectionParams.has_key('ellps'):
165
 
            text = str(self.parent.projectionParams['ellps'])
166
 
            self.ellps.SetSelection(self.ellps.FindString(text))
167
 
 
168
 
    def dialogLayout(self):
169
 
        topBox = wxBoxSizer(wxVERTICAL)
170
 
 
171
 
        zoneBox = wxBoxSizer(wxHORIZONTAL)
172
 
        zoneBox.Add(wxStaticText(self, -1, _("UTM Zone")), 
173
 
                    0, wxALIGN_CENTER|wxALL, 4)
174
 
        self.zone = wxComboBox(self, -1, "", style = wxCB_READONLY)
175
 
        for zone in range(1,61):
176
 
            self.zone.Append(str(zone))
177
 
        zoneBox.Add(self.zone, 0, wxALIGN_CENTER|wxALL, 4)
178
 
        zoneBox.Add(wxButton(self, ID_UTM_DIALOG_PROPOSE_ZONE, _("Propose")), 
179
 
                    0, wxALL, 4) 
180
 
        EVT_BUTTON(self, ID_UTM_DIALOG_PROPOSE_ZONE, self.OnProposeZone)
181
 
 
182
 
        topBox.Add(zoneBox, 1, wxEXPAND|wxALL, 4)
183
 
 
184
 
        ellipsoidBox = wxBoxSizer(wxHORIZONTAL)
185
 
        ellipsoidBox.Add(wxStaticText(self, -1, _("Ellipsoid")), 
186
 
                         0, wxALIGN_CENTER|wxALL, 4)
187
 
        self.ellps = wxComboBox(self, -1, "", style = wxCB_READONLY)
188
 
        for ellps in ["clrk66", "GRS80"]:
189
 
            self.ellps.Append(ellps)
190
 
        ellipsoidBox.Add(self.ellps, 0, wxALIGN_CENTER|wxALL, 4)
191
 
 
192
 
        topBox.Add(ellipsoidBox, 1, wxEXPAND|wxALL, 4)
193
 
 
194
 
        buttonBox = wxBoxSizer(wxHORIZONTAL)
195
 
        buttonBox.Add(wxButton(self, ID_UTM_DIALOG_OK, _("OK")), 
196
 
                      0, wxALL, 4) 
197
 
        buttonBox.Add(wxButton(self, ID_UTM_DIALOG_CANCEL, _("Cancel")), 
198
 
                      0, wxALL, 4) 
199
 
        topBox.Add(buttonBox, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 10)
200
 
        EVT_BUTTON(self, ID_UTM_DIALOG_OK, self.OnOK)
201
 
        EVT_BUTTON(self, ID_UTM_DIALOG_CANCEL, self.OnCancel)
202
 
 
203
 
        self.SetAutoLayout(True)
204
 
        self.SetSizer(topBox)
205
 
        topBox.Fit(self)
206
 
        topBox.SetSizeHints(self)
207
 
 
208
 
    def OnProposeZone(self, event):
209
 
        dlg = UTMProposeZoneDialog(self)
210
 
        if dlg.ShowModal():
211
 
            return True
212
 
        else:
213
 
            return False
214
 
 
215
 
    def OnOK(self, event):
216
 
        self.parent.projectionParams = {}
217
 
        self.parent.projectionParams['zone'] = self.zone.GetStringSelection()
218
 
        self.parent.projectionParams['ellps'] = self.ellps.GetStringSelection()
219
 
        self.parent.projectionParams['proj'] = "utm"
220
 
        self.Close(True)
221
 
 
222
 
    def OnCancel(self, event):
223
 
        self.Close(False)
224
 
 
225
 
class UTMProposeZoneDialog(wxDialog):
226
 
 
227
 
    """Propose a sensible Zone considering the current map extent."""
228
 
    
229
 
    def __init__(self, parent):
230
 
        wxDialog.__init__(self, parent, -1, _("Projection: Propose UTM Zone"),
231
 
                          wxDefaultPosition, wxSize(200, 100))
232
 
        self.parent = parent
233
 
        x, y, x2, y2 = self.parent.parent.map_bounding_box
234
 
        x = x + 180
235
 
        x2 = x2 + 180
236
 
        center = (x2 - x) / 2 + x
237
 
        self.proposedZone = str(int(center / 6 + 1))
238
 
        self.dialogLayout()
239
 
 
240
 
    def dialogLayout(self):
241
 
        topBox = wxBoxSizer(wxVERTICAL)
242
 
 
243
 
        textBox = wxBoxSizer(wxVERTICAL)
244
 
        textBox.Add(wxStaticText(self, -1, _("The current map extent center "
245
 
                                             "lies in UTM Zone")),
246
 
                    0, wxALIGN_CENTER|wxALL, 4)
247
 
        textBox.Add(wxStaticText(self, -1, self.proposedZone), 
248
 
                    0, wxALIGN_CENTER|wxALL, 4)
249
 
 
250
 
        topBox.Add(textBox, 1, wxEXPAND|wxALL, 4)
251
 
 
252
 
        buttonBox = wxBoxSizer(wxHORIZONTAL)
253
 
        buttonBox.Add(wxButton(self, ID_UTM_PROPOSE_ZONE_DIALOG_TAKE,
254
 
                      _("Take")), 0, wxALL, 4) 
255
 
        buttonBox.Add(wxButton(self, ID_UTM_PROPOSE_ZONE_DIALOG_CANCEL,
256
 
                               _("Cancel")), 0, wxALL, 4) 
257
 
        topBox.Add(buttonBox, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 10)
258
 
        EVT_BUTTON(self, ID_UTM_PROPOSE_ZONE_DIALOG_TAKE, self.OnTake)
259
 
        EVT_BUTTON(self, ID_UTM_PROPOSE_ZONE_DIALOG_CANCEL, self.OnCancel)
260
 
 
261
 
        self.SetAutoLayout(True)
262
 
        self.SetSizer(topBox)
263
 
        topBox.Fit(self)
264
 
        topBox.SetSizeHints(self)
265
 
 
266
 
    def OnTake(self, event):
267
 
        self.parent.zone.SetSelection(self.parent.zone.FindString(self.proposedZone))
268
 
        self.Close(True)
269
 
 
270
 
    def OnCancel(self, event):
271
 
        self.Close(False)