Code

Ticket #9806: 9806.4.diff

File 9806.4.diff, 10.2 KB (added by slinkp, 4 years ago)
Line 
1diff -u -r gis-orig/admin/options.py gis/admin/options.py
2--- gis-orig/admin/options.py   2010-09-24 14:24:21.000000000 -0400
3+++ gis/admin/options.py        2010-09-24 15:16:00.431318898 -0400
4@@ -67,12 +67,28 @@
5         in the `widget` attribute) using the settings from the attributes set
6         in this class.
7         """
8-        is_collection = db_field.geom_type in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION')
9-        if is_collection:
10-            if db_field.geom_type == 'GEOMETRYCOLLECTION': collection_type = 'Any'
11-            else: collection_type = OGRGeomType(db_field.geom_type.replace('MULTI', ''))
12+
13+        is_unknown = db_field.geom_type in ('GEOMETRY',)
14+        if not is_unknown:
15+            #If it is not generic, get the parameters from the db_field
16+            is_collection = db_field.geom_type in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION')
17+            if is_collection:
18+                if db_field.geom_type == 'GEOMETRYCOLLECTION': collection_type = 'Any'
19+                else: collection_type = OGRGeomType(db_field.geom_type.upper().replace('MULTI', ''))
20+            else:
21+                collection_type = 'None'
22+            is_linestring = db_field.geom_type in ('LINESTRING', 'MULTILINESTRING')
23+            is_polygon = db_field.geom_type in ('POLYGON', 'MULTIPOLYGON')
24+            is_point = db_field.geom_type in ('POINT', 'MULTIPOINT')
25+            geom_type = OGRGeomType(db_field.geom_type)
26         else:
27+            #If it is generic, set sensible defaults
28+            is_collection = False
29             collection_type = 'None'
30+            is_linestring = False
31+            is_polygon = False
32+            is_point = False
33+            geom_type = OGRGeomType('Unknown')
34 
35         class OLMap(self.widget):
36             template = self.map_template
37@@ -81,15 +97,16 @@
38                       'default_lat' : self.default_lat,
39                       'default_zoom' : self.default_zoom,
40                       'display_wkt' : self.debug or self.display_wkt,
41-                      'geom_type' : OGRGeomType(db_field.geom_type),
42+                      'geom_type' : geom_type,
43                       'field_name' : db_field.name,
44+                      'is_unknown': is_unknown,
45                       'is_collection' : is_collection,
46                       'scrollable' : self.scrollable,
47                       'layerswitcher' : self.layerswitcher,
48                       'collection_type' : collection_type,
49-                      'is_linestring' : db_field.geom_type in ('LINESTRING', 'MULTILINESTRING'),
50-                      'is_polygon' : db_field.geom_type in ('POLYGON', 'MULTIPOLYGON'),
51-                      'is_point' : db_field.geom_type in ('POINT', 'MULTIPOINT'),
52+                      'is_linestring' : is_linestring,
53+                      'is_polygon' : is_polygon,
54+                      'is_point' : is_point,
55                       'num_zoom' : self.num_zoom,
56                       'max_zoom' : self.max_zoom,
57                       'min_zoom' : self.min_zoom,
58diff -u -r gis-orig/admin/widgets.py gis/admin/widgets.py
59--- gis-orig/admin/widgets.py   2010-09-24 14:24:21.000000000 -0400
60+++ gis/admin/widgets.py        2010-09-24 15:21:53.933461558 -0400
61@@ -4,6 +4,7 @@
62 from django.forms.widgets import Textarea
63 from django.template import loader, Context
64 from django.utils import translation
65+from django.contrib.gis.gdal import OGRGeomType
66 
67 # Creating a template context that contains Django settings
68 # values needed by admin map templates.
69@@ -31,8 +32,7 @@
70                 value = GEOSGeometry(value)
71             except (GEOSException, ValueError):
72                 value = None
73-
74-        if value and value.geom_type.upper() != self.geom_type:
75+        if value and value.geom_type.upper() != self.geom_type and self.geom_type != 'GEOMETRY':
76             value = None
77 
78         # Constructing the dictionary of the map options.
79@@ -65,6 +65,29 @@
80             # geometry.
81             self.params['wkt'] = wkt
82 
83+            # Check if the field is generic so the proper values are overriden
84+            if self.params['is_unknown']:
85+                self.params['geom_type'] = OGRGeomType(value.geom_type)
86+                if value.geom_type.upper() in ('LINESTRING', 'MULTILINESTRING'):
87+                    self.params['is_linestring'] = True
88+                elif value.geom_type.upper() in ('POLYGON', 'MULTIPOLYGON'):
89+                    self.params['is_polygon'] = True
90+                elif value.geom_type.upper() in ('POINT', 'MULTIPOINT'):
91+                    self.params['is_point'] = True
92+                if value.geom_type.upper() in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION'):
93+                    self.params['is_collection']=True
94+                    if value.geom_type.upper() == 'GEOMETRYCOLLECTION':
95+                        self.params['collection_type'] = 'Any'
96+                    else:
97+                        self.params['collection_type'] = OGRGeomType(value.geom_type.upper().replace('MULTI', ''))
98+
99+        else:
100+            if self.params['is_unknown']:
101+                # If the geometry is unknown and the value is not set, make it as flexible as possible.
102+                self.params['geom_type'] = OGRGeomType('GEOMETRYCOLLECTION')
103+                self.params['is_collection']=True
104+                self.params['collection_type'] = 'Any'
105+
106         return loader.render_to_string(self.template, self.params,
107                                        context_instance=geo_context)
108 
109diff -u -r gis-orig/templates/gis/admin/openlayers.js gis/templates/gis/admin/openlayers.js
110--- gis-orig/templates/gis/admin/openlayers.js  2010-09-24 14:24:21.000000000 -0400
111+++ gis/templates/gis/admin/openlayers.js       2010-09-24 15:40:43.691209474 -0400
112@@ -1,3 +1,5 @@
113+// Overrides for Openblock: support multipolygon and multipoint.
114+// TODO: submit upstream patch at http://code.djangoproject.com/ticket/9806
115 {# Author: Justin Bronn, Travis Pinney & Dane Springmeyer #}
116 {% block vars %}var {{ module }} = {};
117 {{ module }}.map = null; {{ module }}.controls = null; {{ module }}.panel = null; {{ module }}.re = new RegExp("^SRID=\d+;(.+)", "i"); {{ module }}.layers = {};
118@@ -9,19 +11,19 @@
119 {{ module }}.is_polygon = {{ is_polygon|yesno:"true,false" }};
120 {{ module }}.is_point = {{ is_point|yesno:"true,false" }};
121 {% endblock %}
122-{{ module }}.get_ewkt = function(feat){return 'SRID={{ srid }};' + {{ module }}.wkt_f.write(feat);}
123+{{ module }}.get_ewkt = function(feat){return 'SRID={{ srid }};' + {{ module }}.wkt_f.write(feat);};
124 {{ module }}.read_wkt = function(wkt){
125   // OpenLayers cannot handle EWKT -- we make sure to strip it out.
126   // EWKT is only exposed to OL if there's a validation error in the admin.
127   var match = {{ module }}.re.exec(wkt);
128   if (match){wkt = match[1];}
129   return {{ module }}.wkt_f.read(wkt);
130-}
131+};
132 {{ module }}.write_wkt = function(feat){
133   if ({{ module }}.is_collection){ {{ module }}.num_geom = feat.geometry.components.length;}
134   else { {{ module }}.num_geom = 1;}
135   document.getElementById('{{ id }}').value = {{ module }}.get_ewkt(feat);
136-}
137+};
138 {{ module }}.add_wkt = function(event){
139   // This function will sync the contents of the `vector` layer with the
140   // WKT in the text field.
141@@ -40,7 +42,7 @@
142     }
143     {{ module }}.write_wkt(event.feature);
144   }
145-}
146+};
147 {{ module }}.modify_wkt = function(event){
148   if ({{ module }}.is_collection){
149     if ({{ module }}.is_point){
150@@ -58,7 +60,7 @@
151   } else {
152     {{ module }}.write_wkt(event.feature);
153   }
154-}
155+};
156 // Function to clear vector features and purge wkt from div
157 {{ module }}.deleteFeatures = function(){
158   {{ module }}.layers.vector.removeFeatures({{ module }}.layers.vector.features);
159@@ -68,15 +70,15 @@
160   {{ module }}.deleteFeatures();
161   document.getElementById('{{ id }}').value = '';
162   {{ module }}.map.setCenter(new OpenLayers.LonLat({{ default_lon }}, {{ default_lat }}), {{ default_zoom }});
163-}
164+};
165 // Add Select control
166 {{ module }}.addSelectControl = function(){
167   var select = new OpenLayers.Control.SelectFeature({{ module }}.layers.vector, {'toggle' : true, 'clickout' : true});
168   {{ module }}.map.addControl(select);
169   select.activate();
170-}
171-{{ module }}.enableDrawing = function(){ {{ module }}.map.getControlsByClass('OpenLayers.Control.DrawFeature')[0].activate();}
172-{{ module }}.enableEditing = function(){ {{ module }}.map.getControlsByClass('OpenLayers.Control.ModifyFeature')[0].activate();}
173+};
174+{{ module }}.enableDrawing = function(){ {{ module }}.map.getControlsByClass('OpenLayers.Control.DrawFeature')[0].activate();};
175+{{ module }}.enableEditing = function(){ {{ module }}.map.getControlsByClass('OpenLayers.Control.ModifyFeature')[0].activate();};
176 // Create an array of controls based on geometry type
177 {{ module }}.getControls = function(lyr){
178   {{ module }}.panel = new OpenLayers.Control.Panel({'displayClass': 'olControlEditingToolbar'});
179@@ -88,18 +90,24 @@
180     draw_ctl = new OpenLayers.Control.DrawFeature(lyr, OpenLayers.Handler.Polygon, {'displayClass': 'olControlDrawFeaturePolygon'});
181   } else if ({{ module }}.is_point){
182     draw_ctl = new OpenLayers.Control.DrawFeature(lyr, OpenLayers.Handler.Point, {'displayClass': 'olControlDrawFeaturePoint'});
183-  }
184-  if ({{ module }}.modifiable){
185-    var mod = new OpenLayers.Control.ModifyFeature(lyr, {'displayClass': 'olControlModifyFeature'});
186-    {{ module }}.controls = [nav, draw_ctl, mod];
187-  } else {
188-    if(!lyr.features.length){
189-      {{ module }}.controls = [nav, draw_ctl];
190+  };
191+  // TODO: draw_ctl is undefined if is_collection==true and collection_type=='Any'. Don't know what handler to use then.
192+  if (draw_ctl != undefined) {
193+    if ({{module}}.is_collection )  {
194+      draw_ctl.multi = true;
195+    };
196+    if ({{ module }}.modifiable){
197+      var mod = new OpenLayers.Control.ModifyFeature(lyr, {'displayClass': 'olControlModifyFeature'});
198+      {{ module }}.controls = [nav, draw_ctl, mod];
199     } else {
200-      {{ module }}.controls = [nav];
201+      if(!lyr.features.length){
202+        {{ module }}.controls = [nav, draw_ctl];
203+      } else {
204+        {{ module }}.controls = [nav];
205+      }
206     }
207   }
208-}
209+};
210 {{ module }}.init = function(){
211     {% block map_options %}// The options hash, w/ zoom, resolution, and projection settings.
212     var options = {
213@@ -164,4 +172,4 @@
214     } else {
215       {{ module }}.enableDrawing();
216     }
217-}
218+};