1
{# Author: Justin Bronn, Travis Pinney & Dane Springmeyer #}
2
{% block vars %}var {{ module }} = {};
3
{{ module }}.map = null; {{ module }}.controls = null; {{ module }}.panel = null; {{ module }}.re = new RegExp("^SRID=\d+;(.+)", "i"); {{ module }}.layers = {};
4
{{ module }}.wkt_f = new OpenLayers.Format.WKT();
5
{{ module }}.is_collection = {% if is_collection %}true{% else %}false{% endif %};
6
{{ module }}.collection_type = '{{ collection_type }}';
7
{{ module }}.is_linestring = {% if is_linestring %}true{% else %}false{% endif %};
8
{{ module }}.is_polygon = {% if is_polygon %}true{% else %}false{% endif %};
9
{{ module }}.is_point = {% if is_point %}true{% else %}false{% endif %};
11
{{ module }}.get_ewkt = function(feat){return 'SRID={{ srid }};' + {{ module }}.wkt_f.write(feat);}
12
{{ module }}.read_wkt = function(wkt){
13
// OpenLayers cannot handle EWKT -- we make sure to strip it out.
14
// EWKT is only exposed to OL if there's a validation error in the admin.
15
var match = {{ module }}.re.exec(wkt);
16
if (match){wkt = match[1];}
17
return {{ module }}.wkt_f.read(wkt);
19
{{ module }}.write_wkt = function(feat){
20
if ({{ module }}.is_collection){ {{ module }}.num_geom = feat.geometry.components.length;}
21
else { {{ module }}.num_geom = 1;}
22
document.getElementById('{{ id }}').value = {{ module }}.get_ewkt(feat);
24
{{ module }}.add_wkt = function(event){
25
// This function will sync the contents of the `vector` layer with the
26
// WKT in the text field.
27
if ({{ module }}.is_collection){
28
var feat = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.{{ geom_type }}());
29
for (var i = 0; i < {{ module }}.layers.vector.features.length; i++){
30
feat.geometry.addComponents([{{ module }}.layers.vector.features[i].geometry]);
32
{{ module }}.write_wkt(feat);
34
// Make sure to remove any previously added features.
35
if ({{ module }}.layers.vector.features.length > 1){
36
old_feats = [{{ module }}.layers.vector.features[0]];
37
{{ module }}.layers.vector.removeFeatures(old_feats);
38
{{ module }}.layers.vector.destroyFeatures(old_feats);
40
{{ module }}.write_wkt(event.feature);
43
{{ module }}.modify_wkt = function(event){
44
if ({{ module }}.is_collection){
45
if ({{ module }}.is_point){
46
{{ module }}.add_wkt(event);
49
// When modifying the selected components are added to the
50
// vector layer so we only increment to the `num_geom` value.
51
var feat = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.{{ geom_type }}());
52
for (var i = 0; i < {{ module }}.num_geom; i++){
53
feat.geometry.addComponents([{{ module }}.layers.vector.features[i].geometry]);
55
{{ module }}.write_wkt(feat);
58
{{ module }}.write_wkt(event.feature);
61
// Function to clear vector features and purge wkt from div
62
{{ module }}.deleteFeatures = function(){
63
{{ module }}.layers.vector.removeFeatures({{ module }}.layers.vector.features);
64
{{ module }}.layers.vector.destroyFeatures();
66
{{ module }}.clearFeatures = function (){
67
{{ module }}.deleteFeatures();
68
document.getElementById('{{ id }}').value = '';
69
{{ module }}.map.setCenter(new OpenLayers.LonLat({{ default_lon }}, {{ default_lat }}), {{ default_zoom }});
72
{{ module }}.addSelectControl = function(){
73
var select = new OpenLayers.Control.SelectFeature({{ module }}.layers.vector, {'toggle' : true, 'clickout' : true});
74
{{ module }}.map.addControl(select);
77
{{ module }}.enableDrawing = function(){ {{ module }}.map.getControlsByClass('OpenLayers.Control.DrawFeature')[0].activate();}
78
{{ module }}.enableEditing = function(){ {{ module }}.map.getControlsByClass('OpenLayers.Control.ModifyFeature')[0].activate();}
79
// Create an array of controls based on geometry type
80
{{ module }}.getControls = function(lyr){
81
{{ module }}.panel = new OpenLayers.Control.Panel({'displayClass': 'olControlEditingToolbar'});
82
var nav = new OpenLayers.Control.Navigation();
84
if ({{ module }}.is_linestring){
85
draw_ctl = new OpenLayers.Control.DrawFeature(lyr, OpenLayers.Handler.Path, {'displayClass': 'olControlDrawFeaturePath'});
86
} else if ({{ module }}.is_polygon){
87
draw_ctl = new OpenLayers.Control.DrawFeature(lyr, OpenLayers.Handler.Polygon, {'displayClass': 'olControlDrawFeaturePolygon'});
88
} else if ({{ module }}.is_point){
89
draw_ctl = new OpenLayers.Control.DrawFeature(lyr, OpenLayers.Handler.Point, {'displayClass': 'olControlDrawFeaturePoint'});
92
var mod = new OpenLayers.Control.ModifyFeature(lyr, {'displayClass': 'olControlModifyFeature'});
93
{{ module }}.controls = [nav, draw_ctl, mod];
95
{{ module }}.controls = [nav, darw_ctl];
98
{{ module }}.init = function(){
99
{% block map_options %}// The options hash, w/ zoom, resolution, and projection settings.
101
{% autoescape off %}{% for item in map_options.items %} '{{ item.0 }}' : {{ item.1 }}{% if not forloop.last %},{% endif %}
102
{% endfor %}{% endautoescape %} };{% endblock %}
103
// The admin map for this geometry field.
104
{{ module }}.map = new OpenLayers.Map('{{ id }}_map', options);
106
{{ module }}.layers.base = {% block base_layer %}new OpenLayers.Layer.WMS( "{{ wms_name }}", "{{ wms_url }}", {layers: '{{ wms_layer }}'} );{% endblock %}
107
{{ module }}.map.addLayer({{ module }}.layers.base);
108
{% block extra_layers %}{% endblock %}
109
{% if is_linestring %}OpenLayers.Feature.Vector.style["default"]["strokeWidth"] = 3; // Default too thin for linestrings. {% endif %}
110
{{ module }}.layers.vector = new OpenLayers.Layer.Vector(" {{ field_name }}");
111
{{ module }}.map.addLayer({{ module }}.layers.vector);
112
// Read WKT from the text field.
113
var wkt = document.getElementById('{{ id }}').value;
115
// After reading into geometry, immediately write back to
116
// WKT <textarea> as EWKT (so that SRID is included).
117
var admin_geom = {{ module }}.read_wkt(wkt);
118
{{ module }}.write_wkt(admin_geom);
119
if ({{ module }}.is_collection){
120
// If geometry collection, add each component individually so they may be
121
// edited individually.
122
for (var i = 0; i < {{ module }}.num_geom; i++){
123
{{ module }}.layers.vector.addFeatures([new OpenLayers.Feature.Vector(admin_geom.geometry.components[i].clone())]);
126
{{ module }}.layers.vector.addFeatures([admin_geom]);
128
// Zooming to the bounds.
129
{{ module }}.map.zoomToExtent(admin_geom.geometry.getBounds());
131
{{ module }}.map.setCenter(new OpenLayers.LonLat({{ default_lon }}, {{ default_lat }}), {{ default_zoom }});
133
// This allows editing of the geographic fields -- the modified WKT is
134
// written back to the content field (as EWKT, so that the ORM will know
135
// to transform back to original SRID).
136
{{ module }}.layers.vector.events.on({"featuremodified" : {{ module }}.modify_wkt});
137
{{ module }}.layers.vector.events.on({"featureadded" : {{ module }}.add_wkt});
140
// Add geometry specific panel of toolbar controls
141
{{ module }}.getControls({{ module }}.layers.vector);
142
{{ module }}.panel.addControls({{ module }}.controls);
143
{{ module }}.map.addControl({{ module }}.panel);
144
{{ module }}.addSelectControl();
145
// Then add optional visual controls
146
{% if mouse_position %}{{ module }}.map.addControl(new OpenLayers.Control.MousePosition());{% endif %}
147
{% if scale_text %}{{ module }}.map.addControl(new OpenLayers.Control.Scale());{% endif %}
148
{% if layerswitcher %}{{ module }}.map.addControl(new OpenLayers.Control.LayerSwitcher());{% endif %}
149
// Then add optional behavior controls
150
{% if scrollable %}{% else %}{{ module }}.map.getControlsByClass('OpenLayers.Control.Navigation')[0].disableZoomWheel();{% endif %}
153
{{ module }}.enableEditing();
155
{{ module }}.enableDrawing();