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