Ticket #9806: 9806.2.diff

File 9806.2.diff, 5.5 KB (added by ingenieroariel, 16 years ago)

Now allows edition of already populated geometries.

  • django/contrib/gis/admin/options.py

    diff --git a/django/contrib/gis/admin/options.py b/django/contrib/gis/admin/options.py
    index 71fb87b..07cd88c 100644
    a b class GeoModelAdmin(ModelAdmin):  
    6666        in the `widget` attribute) using the settings from the attributes set
    6767        in this class.
    6868        """
    69         is_collection = db_field._geom in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION')
    70         if is_collection:
    71             if db_field._geom == 'GEOMETRYCOLLECTION': collection_type = 'Any'
    72             else: collection_type = OGRGeomType(db_field._geom.replace('MULTI', ''))
     69        is_unknown = db_field._geom in ('GEOMETRY',)
     70        if not is_unknown:
     71            #If it is not generic, get the parameters from the db_field
     72            is_collection = db_field._geom in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION')
     73            if is_collection:
     74                if db_field._geom == 'GEOMETRYCOLLECTION': collection_type = 'Any'
     75                else: collection_type = OGRGeomType(db_field._geom.replace('MULTI', ''))
     76            else:
     77                collection_type = 'None'
     78            is_linestring = db_field._geom in ('LINESTRING', 'MULTILINESTRING')
     79            is_polygon = db_field._geom in ('POLYGON', 'MULTIPOLYGON')
     80            is_point = db_field._geom in ('POINT', 'MULTIPOINT')
     81            geom_type = OGRGeomType(db_field._geom)
    7382        else:
     83            #If it is generic, set sensible defaults
     84            is_collection = False
    7485            collection_type = 'None'
     86            is_linestring = False
     87            is_polygon = False
     88            is_point = False
     89            geom_type = OGRGeomType('Unknown')
    7590
    7691        class OLMap(self.widget):
    7792            template = self.map_template
    class GeoModelAdmin(ModelAdmin):  
    8196                      'default_lat' : self.default_lat,
    8297                      'default_zoom' : self.default_zoom,
    8398                      'display_wkt' : self.debug or self.display_wkt,
    84                       'geom_type' : OGRGeomType(db_field._geom),
     99                      'geom_type' : geom_type,
    85100                      'field_name' : db_field.name,
     101                      'is_unknown': is_unknown,
    86102                      'is_collection' : is_collection,
    87103                      'scrollable' : self.scrollable,
    88104                      'layerswitcher' : self.layerswitcher,
    89105                      'collection_type' : collection_type,
    90                       'is_linestring' : db_field._geom in ('LINESTRING', 'MULTILINESTRING'),
    91                       'is_polygon' : db_field._geom in ('POLYGON', 'MULTIPOLYGON'),
    92                       'is_point' : db_field._geom in ('POINT', 'MULTIPOINT'),
     106                      'is_linestring' : is_linestring,
     107                      'is_polygon' : is_polygon,
     108                      'is_point' : is_point,
    93109                      'num_zoom' : self.num_zoom,
    94110                      'max_zoom' : self.max_zoom,
    95111                      'min_zoom' : self.min_zoom,
  • django/contrib/gis/admin/widgets.py

    diff --git a/django/contrib/gis/admin/widgets.py b/django/contrib/gis/admin/widgets.py
    index 27abc8f..9732efa 100644
    a b from django.contrib.gis.gdal import OGRException  
    22from django.contrib.gis.geos import GEOSGeometry, GEOSException
    33from django.forms.widgets import Textarea
    44from django.template.loader import render_to_string
    5 
     5from django.contrib.gis.gdal import OGRGeomType
    66class OpenLayersWidget(Textarea):
    77    """
    88    Renders an OpenLayers map using the WKT of the geometry.
    class OpenLayersWidget(Textarea):  
    2323                value = GEOSGeometry(value)
    2424            except (GEOSException, ValueError):
    2525                value = None
    26 
    27         if value and value.geom_type.upper() != self.geom_type:
     26        if value and value.geom_type.upper() != self.geom_type and self.geom_type != 'GEOMETRY':
    2827            value = None
    2928
    3029        # Constructing the dictionary of the map options.
    class OpenLayersWidget(Textarea):  
    5150            # geometry.
    5251            self.params['wkt'] = wkt
    5352
     53            # Check if the field is generic so the proper values are overriden
     54            if self.params['is_unknown']:
     55                self.params['geom_type'] = OGRGeomType(value.geom_type)
     56                if value.geom_type.upper() == 'LINESTRING':
     57                    self.params['is_linestring'] = True
     58                elif value.geom_type.upper() == 'POLYGON':
     59                    self.params['is_polygon'] = True
     60                elif value.geom_type.upper() == 'POINT':
     61                    self.params['is_polygon'] = True
     62                elif value.geom_type.upper() in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION'):
     63                    self.params['is_collection']=True
     64                    if value.geom_type.upper() == 'GEOMETRYCOLLECTION': self.params['collection_type'] = 'Any'
     65                    else: self.params['collection_type'] = OGRGeomType(db_field._geom.replace('MULTI', ''))
     66        else:
     67            if self.params['is_unknown']:
     68                # If the geometry is unknown and the value is not set, make it as flexible as possible.
     69                self.params['geom_type'] = OGRGeomType('GEOMETRYCOLLECTION')
     70                self.params['is_collection']=True
     71                self.params['collection_type'] = 'Any'           
    5472        return render_to_string(self.template, self.params)
    5573   
    5674    def map_options(self):
Back to Top