1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2008 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# See the file COPYING for details.
"""
Extra widgets and fields
"""
from django import forms
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
from django.contrib.gis.geos import fromstr
from chimere import settings
from django.contrib.gis.db import models
URL_OSM_CSS = ["http://www.openlayers.org/api/theme/default/style.css"]
URL_OSM_JS = ["http://www.openlayers.org/api/OpenLayers.js",
"http://www.openstreetmap.org/openlayers/OpenStreetMap.js"]
def getMapJS():
'''Variable initialization for drawing the map
'''
# projection, center and bounds definitions
js = u"var epsg_display_projection = new OpenLayers.Projection('EPSG:%d')\
;\n" % settings.EPSG_DISPLAY_PROJECTION
js += u"var epsg_projection = new OpenLayers.Projection('EPSG:%d');\n" % \
settings.EPSG_PROJECTION
js += u"var centerLonLat = new OpenLayers.LonLat(%f,\
%f).transform(epsg_display_projection, epsg_projection);\n" % \
settings.DEFAULT_CENTER
js += u"var media_path = '%s';\n" % settings.MEDIA_URL
js += u"var map_layer = %s;\n" % settings.MAP_LAYER
js = u"""<script type="text/javascript"><!--
%s// !--></script>
""" % js
return js
class TextareaWidget(forms.Textarea):
"""
Manage the edition of a text using TinyMCE
"""
class Media:
js = ["%stiny_mce.js" % settings.TINYMCE_URL,
"%stextareas.js" % settings.MEDIA_URL,]
class PointChooserWidget(forms.TextInput):
"""
Manage the edition of point on a map
"""
class Media:
css = {
"all": URL_OSM_CSS + ["%sforms.css" % settings.MEDIA_URL,]
}
js = URL_OSM_JS + ["%sedit_map.js" % settings.MEDIA_URL,
"%sbase.js" % settings.MEDIA_URL,]
def render(self, name, value, attrs=None):
'''
Render a map and latitude, longitude information field
'''
val = '0'
value_x, value_y = 0, 0
if value:
val = str(value)
if hasattr(value, 'x') and hasattr(value, 'y'):
value_x, value_y = value.x, value.y
elif isinstance(value, unicode) and value.startswith('POINT('):
try:
value_x, value_y = value.split('(')[1][:-1].split(' ')
value_x, value_y = float(value_x), float(value_y)
except:
value = None
else:
value = None
tpl = getMapJS()
tpl += u'<script src="%sedit_map.js"></script>\n' % settings.MEDIA_URL
tpl += u"""<div id='map_edit'></div>
<div id='live_lonlat'>
<p><label for='live_latitude'>%s</label>\
<input type='texte' name='live_latitude' id='live_latitude' size='8' \
disabled='true' value='%f'/></p>
<p><label for='live_longitude'>%s</label><input type='texte' \
name='live_longitude' id='live_longitude' size='8' disabled='true' \
value='%f'/></p>
</div>
<input type='hidden' name='%s' id='id_%s' value="%s"/>
""" % (_("Latitude"), value_y, _("Longitude"), value_x, name, name, val)
tpl += """<script type='text/javascript'><!--
init();"""
if value:
tpl += '''var mylonlat = new OpenLayers.LonLat(%f,%f);
putMarker(mylonlat.transform(epsg_display_projection,
map.getProjectionObject()).clone(), true);
''' % (value_x, value_y)
tpl += """// --></script>
<hr class='spacer'/>
"""
return mark_safe(tpl)
class PointField(models.PointField):
'''
Set the widget for the form field
'''
def formfield(self, **keys):
defaults = {'widget': PointChooserWidget}
keys.update(defaults)
return super(PointField, self).formfield(**keys)
def clean(self, value):
if len(value) != 2 and self.required:
raise ValidationError(_("Invalid point"))
return value
class RouteChooserWidget(forms.TextInput):
"""
Manage the edition of route on a map
"""
class Media:
css = {
"all": URL_OSM_CSS + ["%sforms.css" % settings.MEDIA_URL,]
}
js = ["%sedit_route_map.js" % settings.MEDIA_URL,
"%sbase.js" % settings.MEDIA_URL,] + URL_OSM_JS
def render(self, name, value, attrs=None):
'''
Render a map and latitude, longitude information field
'''
tpl = getMapJS()
help_create = ''
if not value:
help_create = """<h3>%s</h3>
<p>%s</p>
<p>%s</p>
<p>%s</p>
<p>%s</p>
<p>%s</p>""" % (_("Creation mode"),
_("To start drawing the route click on the toggle button : \"Start drawing\"."),
_("Then click on the map to begin the drawing."),
_("You can add points by clicking again."),
_("To finish the drawing double click. When the drawing is finished you can \
edit it."),
_("While creating to undo a drawing click again on the toggle button \"Stop \
drawing\"."))
help_modify = """<h3>%s</h3>
<p>%s</p>
<p>%s</p>
<p>%s</p>""" % (_("Modification mode"),
_("To move a point click on it and drag it to the desired position."),
_("To delete a point move the mouse cursor over it and press the \"d\" key."),
_("To add a point click in the middle of a segment and drag the new point to \
the desired position"))
tpl += u'<script src="%sedit_route_map.js"></script>\n' % \
settings.MEDIA_URL
if not value:
tpl += u"""<div id='draw-toggle-off' class='toggle-button' \
onclick='toggleDrawOn();'>%s</div>
<div id='draw-toggle-on' class='toggle-button' \
onclick='toggleDrawOff();'>%s</div>
<hr class='spacer'/>""" % (_("Start drawing"), _("Stop drawing"))
tpl += """
<div id='map_edit'></div>"""
if not value:
tpl += '''
<div class='help-route' id='help-route-create'>%s</div>''' % help_create
style = ''
if value:
style = " style='display:block'"
tpl += """
<div class='help-route' id='help-route-modify'%s>%s</div>
<hr class='spacer'/>
<input type='hidden' name='%s' id='id_%s' value="%s"/>
""" % (style, help_modify, name, name, value)
tpl += """<script type='text/javascript'><!--
init();"""
if value:
val = value
if type(value) == unicode:
try:
val = fromstr(value)
except:
pass
if hasattr(val, 'json'):
tpl += """
var geometry='%s';
initFeature(geometry);""" % val.json
tpl += """
// --></script>
"""
return mark_safe(tpl)
class RouteField(models.LineStringField):
'''
Set the widget for the form field
'''
def formfield(self, **keys):
defaults = {'widget': RouteChooserWidget}
keys.update(defaults)
return super(RouteField, self).formfield(**keys)
class AreaWidget(forms.TextInput):
"""
Manage the edition of an area on the map
"""
class Media:
css = {
"all": URL_OSM_CSS + ["%sforms.css" % settings.MEDIA_URL,]
}
js = URL_OSM_JS + ["%sedit_area.js" % settings.MEDIA_URL,
"%sbase.js" % settings.MEDIA_URL,]
def render(self, name, value, attrs=None):
"""
Render a map
"""
upper_left_lat, upper_left_lon = 0, 0
lower_right_lat, lower_right_lon = 0, 0
if value:
if len(value) == 2:
upper_left = value[0]
lower_right = value[1]
if hasattr(upper_left, 'x') and hasattr(upper_left, 'y'):
upper_left_lon, upper_left_lat = upper_left.x, upper_left.y
if hasattr(lower_right, 'x') and hasattr(lower_right, 'y'):
lower_right_lon, lower_right_lat = lower_right.x, \
lower_right.y
tpl = getMapJS()
tpl += u"""<div id='map_edit'></div>
<input type='hidden' name='upper_left_lat' id='upper_left_lat' value='%f'/>
<input type='hidden' name='upper_left_lon' id='upper_left_lon' value='%f'/>
<input type='hidden' name='lower_right_lat' id='lower_right_lat' value='%f'/>
<input type='hidden' name='lower_right_lon' id='lower_right_lon' value='%f'/>
""" % (upper_left_lat, upper_left_lon, lower_right_lat, lower_right_lon)
tpl += """<script type='text/javascript'><!--
init();"""
if value:
tpl += """var extent = new OpenLayers.Bounds(%f, %f, %f, %f);
map.zoomToExtent(extent, true);""" % (upper_left_lat, upper_left_lon, lower_right_lat,
lower_right_lon)
tpl += """// --></script>
<hr class='spacer'/>
"""
return mark_safe(tpl)
def value_from_datadict(self, data, files, name):
"""
Return the appropriate values
"""
values = []
for keys in (('upper_left_lat', 'upper_left_lon',),
('lower_right_lat', 'lower_right_lon')):
value = []
for key in keys:
val = data.get(key, None)
if not val:
return []
value.append(val)
values.append(value)
return values
class AreaField(forms.MultiValueField):
'''
Set the widget for the form field
'''
widget = AreaWidget
def compress(self, data_list):
if not data_list:
return None
return data_list
|