Code

Ticket #13926: osm_ol_admin.diff

File osm_ol_admin.diff, 6.7 KB (added by elpaso66, 2 years ago)

New patch for django/contrib/gis/templates/gis/admin/openlayers.js, should apply on trunk.

Line 
1--- openlayers.txt      2012-01-05 10:41:19.895544985 +0100
2+++ openlayers.js       2012-01-05 10:53:26.315562835 +0100
3@@ -1,7 +1,11 @@
4 {% load l10n %}{# Author: Justin Bronn, Travis Pinney & Dane Springmeyer #}
5 OpenLayers.Projection.addTransform("EPSG:4326", "EPSG:3857", OpenLayers.Layer.SphericalMercator.projectForward);
6 {% block vars %}var {{ module }} = {};
7-{{ module }}.map = null; {{ module }}.controls = null; {{ module }}.panel = null; {{ module }}.re = new RegExp("^SRID=\d+;(.+)", "i"); {{ module }}.layers = {};
8+{{ module }}.map = null;
9+{{ module }}.controls = null;
10+{{ module }}.panel = null;
11+{{ module }}.re = /^SRID=\d+;(.+)/i;
12+{{ module }}.layers = {};
13 {{ module }}.modifiable = {{ modifiable|yesno:"true,false" }};
14 {{ module }}.wkt_f = new OpenLayers.Format.WKT();
15 {{ module }}.is_collection = {{ is_collection|yesno:"true,false" }};
16@@ -10,7 +14,24 @@
17 {{ module }}.is_polygon = {{ is_polygon|yesno:"true,false" }};
18 {{ module }}.is_point = {{ is_point|yesno:"true,false" }};
19 {% endblock %}
20-{{ module }}.get_ewkt = function(feat){return 'SRID={{ srid }};' + {{ module }}.wkt_f.write(feat);}
21+// Patch for http://trac.osgeo.org/openlayers/ticket/2240
22+{{ module }}.get_ewkt = function(feat){
23+  if (feat.constructor == Array) {
24+    wkts = [];
25+    for(var i = 0; i < feat.length; i++){
26+      wkts.push(feat[i].geometry.toString());
27+    }
28+    return 'SRID={{ srid }};GEOMETRYCOLLECTION(' + wkts.join(', ') + ')';
29+  } else if (feat.geometry.CLASS_NAME == 'OpenLayers.Geometry.Collection') {
30+    wkts = [];
31+    for (var i=0; i < feat.geometry.components.length; i++){
32+      wkts.push(feat.geometry.components[i].toString());
33+    }
34+    return 'SRID={{ srid }};GEOMETRYCOLLECTION(' + wkts.join(', ') + ')';
35+  } else {
36+    return 'SRID={{ srid }};' + {{ module }}.wkt_f.write(feat);
37+  }
38+}
39 {{ module }}.read_wkt = function(wkt){
40   // OpenLayers cannot handle EWKT -- we make sure to strip it out.
41   // EWKT is only exposed to OL if there's a validation error in the admin.
42@@ -19,15 +40,26 @@
43   return {{ module }}.wkt_f.read(wkt);
44 }
45 {{ module }}.write_wkt = function(feat){
46-  if ({{ module }}.is_collection){ {{ module }}.num_geom = feat.geometry.components.length;}
47-  else { {{ module }}.num_geom = 1;}
48+  if ({{ module }}.is_collection){
49+    if (feat.constructor == Array) {
50+      {{ module }}.num_geom = feat.length;
51+    } else {
52+      {{ module }}.num_geom = feat.geometry.components.length;
53+    }
54+  } else {
55+    {{ module }}.num_geom = 1;
56+  }
57   document.getElementById('{{ id }}').value = {{ module }}.get_ewkt(feat);
58 }
59 {{ module }}.add_wkt = function(event){
60   // This function will sync the contents of the `vector` layer with the
61   // WKT in the text field.
62   if ({{ module }}.is_collection){
63+    if ({{ module }}.collection_type == 'Any') {
64+      var feat = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Collection());
65+    } else {
66     var feat = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.{{ geom_type }}());
67+    }
68     for (var i = 0; i < {{ module }}.layers.vector.features.length; i++){
69       feat.geometry.addComponents([{{ module }}.layers.vector.features[i].geometry]);
70     }
71@@ -50,7 +82,11 @@
72     } else {
73       // When modifying the selected components are added to the
74       // vector layer so we only increment to the `num_geom` value.
75+      if ({{ module }}.collection_type == 'Any') {
76+        var feat = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Collection());
77+      } else {
78       var feat = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.{{ geom_type }}());
79+      }
80       for (var i = 0; i < {{ module }}.num_geom; i++){
81        feat.geometry.addComponents([{{ module }}.layers.vector.features[i].geometry]);
82       }
83@@ -84,25 +120,31 @@
84 {{ module }}.getControls = function(lyr){
85   {{ module }}.panel = new OpenLayers.Control.Panel({'displayClass': 'olControlEditingToolbar'});
86   var nav = new OpenLayers.Control.Navigation();
87-  var draw_ctl;
88-  if ({{ module }}.is_linestring){
89-    draw_ctl = new OpenLayers.Control.DrawFeature(lyr, OpenLayers.Handler.Path, {'displayClass': 'olControlDrawFeaturePath'});
90-  } else if ({{ module }}.is_polygon){
91-    draw_ctl = new OpenLayers.Control.DrawFeature(lyr, OpenLayers.Handler.Polygon, {'displayClass': 'olControlDrawFeaturePolygon'});
92-  } else if ({{ module }}.is_point){
93-    draw_ctl = new OpenLayers.Control.DrawFeature(lyr, OpenLayers.Handler.Point, {'displayClass': 'olControlDrawFeaturePoint'});
94+  var draw_ctl = [];
95+  if ({{ module }}.is_linestring || {{ module }}.is_collection){
96+    draw_ctl.push(new OpenLayers.Control.DrawFeature(lyr, OpenLayers.Handler.Path, {'displayClass': 'olControlDrawFeaturePath'}));
97+  }
98+  if ({{ module }}.is_polygon || {{ module }}.is_collection){
99+    draw_ctl.push(new OpenLayers.Control.DrawFeature(lyr, OpenLayers.Handler.Polygon, {'displayClass': 'olControlDrawFeaturePolygon'}));
100   }
101+  if ({{ module }}.is_point|| {{ module }}.is_collection){
102+    draw_ctl.push(new OpenLayers.Control.DrawFeature(lyr, OpenLayers.Handler.Point, {'displayClass': 'olControlDrawFeaturePoint'}));
103+  }
104+  var ctls = draw_ctl;
105   if ({{ module }}.modifiable){
106     var mod = new OpenLayers.Control.ModifyFeature(lyr, {'displayClass': 'olControlModifyFeature'});
107-    {{ module }}.controls = [nav, draw_ctl, mod];
108+    ctls.splice(0, 0, nav);
109+    ctls.push(mod);
110   } else {
111     if(!lyr.features.length){
112-      {{ module }}.controls = [nav, draw_ctl];
113+      ictls.splice(0, 0, nav);
114     } else {
115-      {{ module }}.controls = [nav];
116+      ctls = [nav];
117     }
118   }
119+  {{ module }}.controls = ctls;
120 }
121+
122 {{ module }}.init = function(){
123     {% block map_options %}// The options hash, w/ zoom, resolution, and projection settings.
124     var options = {
125@@ -127,14 +169,24 @@
126       if ({{ module }}.is_collection){
127        // If geometry collection, add each component individually so they may be
128        // edited individually.
129+        if ({{ module }}.collection_type == 'Any') {
130+          {{ module }}.layers.vector.addFeatures(admin_geom);
131+          var admin_bounds = new OpenLayers.Bounds();
132+          for(var i = 0; i < {{ module }}.num_geom; i++){
133+            admin_bounds.extend(admin_geom[i].geometry.getBounds());
134+          }
135+        } else {
136        for (var i = 0; i < {{ module }}.num_geom; i++){
137          {{ module }}.layers.vector.addFeatures([new OpenLayers.Feature.Vector(admin_geom.geometry.components[i].clone())]);
138        }
139+          var admin_bounds = admin_geom.geometry.getBounds();
140+        }
141       } else {
142        {{ module }}.layers.vector.addFeatures([admin_geom]);
143+        var admin_bounds = admin_geom.geometry.getBounds();
144       }
145       // Zooming to the bounds.
146-      {{ module }}.map.zoomToExtent(admin_geom.geometry.getBounds());
147+      {{ module }}.map.zoomToExtent(admin_bounds);
148       if ({{ module }}.is_point){
149           {{ module }}.map.zoomTo({{ point_zoom }});
150       }