Ticket #9806: 9806.4.diff

File 9806.4.diff, 10.2 KB (added by slinkp, 5 years ago)
  • admin/options.py

    diff -u -r gis-orig/admin/options.py gis/admin/options.py
    old new  
    6767        in the `widget` attribute) using the settings from the attributes set
    6868        in this class.
    6969        """
    70         is_collection = db_field.geom_type in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION')
    71         if is_collection:
    72             if db_field.geom_type == 'GEOMETRYCOLLECTION': collection_type = 'Any'
    73             else: collection_type = OGRGeomType(db_field.geom_type.replace('MULTI', ''))
     70
     71        is_unknown = db_field.geom_type in ('GEOMETRY',)
     72        if not is_unknown:
     73            #If it is not generic, get the parameters from the db_field
     74            is_collection = db_field.geom_type in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION')
     75            if is_collection:
     76                if db_field.geom_type == 'GEOMETRYCOLLECTION': collection_type = 'Any'
     77                else: collection_type = OGRGeomType(db_field.geom_type.upper().replace('MULTI', ''))
     78            else:
     79                collection_type = 'None'
     80            is_linestring = db_field.geom_type in ('LINESTRING', 'MULTILINESTRING')
     81            is_polygon = db_field.geom_type in ('POLYGON', 'MULTIPOLYGON')
     82            is_point = db_field.geom_type in ('POINT', 'MULTIPOINT')
     83            geom_type = OGRGeomType(db_field.geom_type)
    7484        else:
     85            #If it is generic, set sensible defaults
     86            is_collection = False
    7587            collection_type = 'None'
     88            is_linestring = False
     89            is_polygon = False
     90            is_point = False
     91            geom_type = OGRGeomType('Unknown')
    7692
    7793        class OLMap(self.widget):
    7894            template = self.map_template
     
    8197                      'default_lat' : self.default_lat,
    8298                      'default_zoom' : self.default_zoom,
    8399                      'display_wkt' : self.debug or self.display_wkt,
    84                       'geom_type' : OGRGeomType(db_field.geom_type),
     100                      'geom_type' : geom_type,
    85101                      'field_name' : db_field.name,
     102                      'is_unknown': is_unknown,
    86103                      'is_collection' : is_collection,
    87104                      'scrollable' : self.scrollable,
    88105                      'layerswitcher' : self.layerswitcher,
    89106                      'collection_type' : collection_type,
    90                       'is_linestring' : db_field.geom_type in ('LINESTRING', 'MULTILINESTRING'),
    91                       'is_polygon' : db_field.geom_type in ('POLYGON', 'MULTIPOLYGON'),
    92                       'is_point' : db_field.geom_type in ('POINT', 'MULTIPOINT'),
     107                      'is_linestring' : is_linestring,
     108                      'is_polygon' : is_polygon,
     109                      'is_point' : is_point,
    93110                      'num_zoom' : self.num_zoom,
    94111                      'max_zoom' : self.max_zoom,
    95112                      'min_zoom' : self.min_zoom,
  • admin/widgets.py

    diff -u -r gis-orig/admin/widgets.py gis/admin/widgets.py
    old new  
    44from django.forms.widgets import Textarea
    55from django.template import loader, Context
    66from django.utils import translation
     7from django.contrib.gis.gdal import OGRGeomType
    78
    89# Creating a template context that contains Django settings
    910# values needed by admin map templates.
     
    3132                value = GEOSGeometry(value)
    3233            except (GEOSException, ValueError):
    3334                value = None
    34 
    35         if value and value.geom_type.upper() != self.geom_type:
     35        if value and value.geom_type.upper() != self.geom_type and self.geom_type != 'GEOMETRY':
    3636            value = None
    3737
    3838        # Constructing the dictionary of the map options.
     
    6565            # geometry.
    6666            self.params['wkt'] = wkt
    6767
     68            # Check if the field is generic so the proper values are overriden
     69            if self.params['is_unknown']:
     70                self.params['geom_type'] = OGRGeomType(value.geom_type)
     71                if value.geom_type.upper() in ('LINESTRING', 'MULTILINESTRING'):
     72                    self.params['is_linestring'] = True
     73                elif value.geom_type.upper() in ('POLYGON', 'MULTIPOLYGON'):
     74                    self.params['is_polygon'] = True
     75                elif value.geom_type.upper() in ('POINT', 'MULTIPOINT'):
     76                    self.params['is_point'] = True
     77                if value.geom_type.upper() in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION'):
     78                    self.params['is_collection']=True
     79                    if value.geom_type.upper() == 'GEOMETRYCOLLECTION':
     80                        self.params['collection_type'] = 'Any'
     81                    else:
     82                        self.params['collection_type'] = OGRGeomType(value.geom_type.upper().replace('MULTI', ''))
     83
     84        else:
     85            if self.params['is_unknown']:
     86                # If the geometry is unknown and the value is not set, make it as flexible as possible.
     87                self.params['geom_type'] = OGRGeomType('GEOMETRYCOLLECTION')
     88                self.params['is_collection']=True
     89                self.params['collection_type'] = 'Any'
     90
    6891        return loader.render_to_string(self.template, self.params,
    6992                                       context_instance=geo_context)
    7093
  • templates/gis/admin/openlayers.js

    diff -u -r gis-orig/templates/gis/admin/openlayers.js gis/templates/gis/admin/openlayers.js
    old new  
     1// Overrides for Openblock: support multipolygon and multipoint.
     2// TODO: submit upstream patch at http://code.djangoproject.com/ticket/9806
    13{# Author: Justin Bronn, Travis Pinney & Dane Springmeyer #}
    24{% block vars %}var {{ module }} = {};
    35{{ module }}.map = null; {{ module }}.controls = null; {{ module }}.panel = null; {{ module }}.re = new RegExp("^SRID=\d+;(.+)", "i"); {{ module }}.layers = {};
     
    911{{ module }}.is_polygon = {{ is_polygon|yesno:"true,false" }};
    1012{{ module }}.is_point = {{ is_point|yesno:"true,false" }};
    1113{% endblock %}
    12 {{ module }}.get_ewkt = function(feat){return 'SRID={{ srid }};' + {{ module }}.wkt_f.write(feat);}
     14{{ module }}.get_ewkt = function(feat){return 'SRID={{ srid }};' + {{ module }}.wkt_f.write(feat);};
    1315{{ module }}.read_wkt = function(wkt){
    1416  // OpenLayers cannot handle EWKT -- we make sure to strip it out.
    1517  // EWKT is only exposed to OL if there's a validation error in the admin.
    1618  var match = {{ module }}.re.exec(wkt);
    1719  if (match){wkt = match[1];}
    1820  return {{ module }}.wkt_f.read(wkt);
    19 }
     21};
    2022{{ module }}.write_wkt = function(feat){
    2123  if ({{ module }}.is_collection){ {{ module }}.num_geom = feat.geometry.components.length;}
    2224  else { {{ module }}.num_geom = 1;}
    2325  document.getElementById('{{ id }}').value = {{ module }}.get_ewkt(feat);
    24 }
     26};
    2527{{ module }}.add_wkt = function(event){
    2628  // This function will sync the contents of the `vector` layer with the
    2729  // WKT in the text field.
     
    4042    }
    4143    {{ module }}.write_wkt(event.feature);
    4244  }
    43 }
     45};
    4446{{ module }}.modify_wkt = function(event){
    4547  if ({{ module }}.is_collection){
    4648    if ({{ module }}.is_point){
     
    5860  } else {
    5961    {{ module }}.write_wkt(event.feature);
    6062  }
    61 }
     63};
    6264// Function to clear vector features and purge wkt from div
    6365{{ module }}.deleteFeatures = function(){
    6466  {{ module }}.layers.vector.removeFeatures({{ module }}.layers.vector.features);
     
    6870  {{ module }}.deleteFeatures();
    6971  document.getElementById('{{ id }}').value = '';
    7072  {{ module }}.map.setCenter(new OpenLayers.LonLat({{ default_lon }}, {{ default_lat }}), {{ default_zoom }});
    71 }
     73};
    7274// Add Select control
    7375{{ module }}.addSelectControl = function(){
    7476  var select = new OpenLayers.Control.SelectFeature({{ module }}.layers.vector, {'toggle' : true, 'clickout' : true});
    7577  {{ module }}.map.addControl(select);
    7678  select.activate();
    77 }
    78 {{ module }}.enableDrawing = function(){ {{ module }}.map.getControlsByClass('OpenLayers.Control.DrawFeature')[0].activate();}
    79 {{ module }}.enableEditing = function(){ {{ module }}.map.getControlsByClass('OpenLayers.Control.ModifyFeature')[0].activate();}
     79};
     80{{ module }}.enableDrawing = function(){ {{ module }}.map.getControlsByClass('OpenLayers.Control.DrawFeature')[0].activate();};
     81{{ module }}.enableEditing = function(){ {{ module }}.map.getControlsByClass('OpenLayers.Control.ModifyFeature')[0].activate();};
    8082// Create an array of controls based on geometry type
    8183{{ module }}.getControls = function(lyr){
    8284  {{ module }}.panel = new OpenLayers.Control.Panel({'displayClass': 'olControlEditingToolbar'});
     
    8890    draw_ctl = new OpenLayers.Control.DrawFeature(lyr, OpenLayers.Handler.Polygon, {'displayClass': 'olControlDrawFeaturePolygon'});
    8991  } else if ({{ module }}.is_point){
    9092    draw_ctl = new OpenLayers.Control.DrawFeature(lyr, OpenLayers.Handler.Point, {'displayClass': 'olControlDrawFeaturePoint'});
    91   }
    92   if ({{ module }}.modifiable){
    93     var mod = new OpenLayers.Control.ModifyFeature(lyr, {'displayClass': 'olControlModifyFeature'});
    94     {{ module }}.controls = [nav, draw_ctl, mod];
    95   } else {
    96     if(!lyr.features.length){
    97       {{ module }}.controls = [nav, draw_ctl];
     93  };
     94  // TODO: draw_ctl is undefined if is_collection==true and collection_type=='Any'. Don't know what handler to use then.
     95  if (draw_ctl != undefined) {
     96    if ({{module}}.is_collection )  {
     97      draw_ctl.multi = true;
     98    };
     99    if ({{ module }}.modifiable){
     100      var mod = new OpenLayers.Control.ModifyFeature(lyr, {'displayClass': 'olControlModifyFeature'});
     101      {{ module }}.controls = [nav, draw_ctl, mod];
    98102    } else {
    99       {{ module }}.controls = [nav];
     103      if(!lyr.features.length){
     104        {{ module }}.controls = [nav, draw_ctl];
     105      } else {
     106        {{ module }}.controls = [nav];
     107      }
    100108    }
    101109  }
    102 }
     110};
    103111{{ module }}.init = function(){
    104112    {% block map_options %}// The options hash, w/ zoom, resolution, and projection settings.
    105113    var options = {
     
    164172    } else {
    165173      {{ module }}.enableDrawing();
    166174    }
    167 }
     175};
Back to Top