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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Extra widgets and fields
"""
from django import forms
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from chimere import settings
from django.contrib.gis.db import models
URL_OSM_JS = ["http://www.openlayers.org/api/OpenLayers.js",
"http://openstreetmap.org/openlayers/OpenStreetMap.js"]
def getMapJS():
'''Variable initialization for drawing the map
'''
# projection, center and bounds definitions
js = u"epsg_display_projection = new OpenLayers.Projection('EPSG:%d');\n" %\
settings.EPSG_DISPLAY_PROJECTION
js += u"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 maxExtent = new OpenLayers.Bounds(%f, %f, %f, %f);\n" % \
settings.MAP_BOUNDS
js += u"var media_path = '%s';\n" % settings.MEDIA_URL
js = u"""<script type="text/javascript"><!--
%s// !--></script>
""" % js
return js
class PointChooserWidget(forms.TextInput):
"""
Manage the edition of point on a map
"""
class Media:
css = {
"all": ("%sforms.css" % settings.MEDIA_URL,)
}
js = ["%sedit_map.js" % settings.MEDIA_URL] + URL_OSM_JS
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());
''' % (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)
|