Esempio n. 1
0
File: admin.py Progetto: phulc/cis-1
 def get_map_widget(self, db_field):
     widget = super(GeoPortalAdmin, self).get_map_widget(db_field)
     widget.params['map_info'] = self.map_info
     widget.params['layers'] = utils.get_layers(self.layers)
     widget.params['api_key'] = settings.GEOPORTAL_API_KEY
     widget.params['feature_color'] = utils.DEFAULT_COLOR
     widget.params['feature_opacity'] = utils.DEFAULT_OPACITY
     return widget
Esempio n. 2
0
 def __init__(self, *args, **kwargs):
     super(BaseWidget, self).__init__(*args, **kwargs)
     attrs = kwargs.pop('attrs', {})
     self.options = {
         'width': attrs.pop('width', utils.DEFAULT_WIDTH),
         'height': attrs.pop('height', utils.DEFAULT_HEIGHT),
         'color': attrs.pop('color', utils.DEFAULT_COLOR),
         'opacity': attrs.pop('opacity', utils.DEFAULT_OPACITY),
         'default_zoom': attrs.pop('default_zoom', utils.DEFAULT_ZOOM),
         'default_lon': attrs.pop('default_lon', utils.DEFAULT_LON),
         'default_lat': attrs.pop('default_lat', utils.DEFAULT_LAT),
         'layers': utils.get_layers(attrs.pop('layers', (('maps', 1),))),
         'srid': attrs.pop('srid', 4326),
     }
    def render(self, context):
        self.parse_options(self.args_)
        # Generate a probably unique name for javascript variables -- in case
        # there are several maps on a page
        map_var = ''.join(random.sample('abcdefghijklmopqrstuvwxyz', 5))
        map_var = 'map_' + map_var

        # Field type
        geo_field = self.geo_field.resolve(context)
        ftype = geo_field.geom_type.upper()
        is_collection = ftype in ('MULTIPOINT', 'MULTILINESTRING',
                                  'MULTIPOLYGON', 'GEOMETRYCOLLECTION')
        if is_collection:
            if ftype == 'GEOMETRYCOLLECTION':
                collection_type = 'Any'
            else:
                collection_type = OGRGeomType(ftype.replace('MULTI', ''))
        else:
            collection_type = 'None'

        for (key, value) in self.options.items():
            self.options[key] = value.resolve(context)

        # Default options
        if not 'width' in self.options:
            self.options['width'] = getattr(settings,
                                            'GEOPORTAL_DEFAULT_WIDTH',
                                            utils.DEFAULT_WIDTH)

        if not 'height' in self.options:
            self.options['height'] = getattr(settings,
                                             'GEOPORTAL_DEFAULT_HEIGHT',
                                             utils.DEFAULT_HEIGHT)

        if not 'color' in self.options:
            self.options['color'] = getattr(settings,
                                            'GEOPORTAL_DEFAULT_COLOR',
                                            utils.DEFAULT_COLOR)

        if not 'opacity' in self.options:
            self.options['opacity'] = getattr(settings,
                                              'GEOPORTAL_DEFAULT_OPACITY',
                                              utils.DEFAULT_OPACITY)
        self.options['opacity'] = str(self.options['opacity'])

        self.check_booleans()

        isolated_context = template.Context({
            'options': self.options,
            'api_key': settings.GEOPORTAL_API_KEY,
            'map_var': map_var,
            'is_point': ftype in ('POINT', 'MULTIPOINT'),
            'is_linestring': ftype in ('LINESTRING', 'MULTILINESTRING'),
            'is_polygon': ftype in ('POLYGON', 'MULTIPOLYGON'),
            'is_collection': is_collection,
            'collection_type': collection_type,
            'layers': utils.get_layers((('maps', 1),)),
            'default_lon': getattr(settings, 'GEOPORTAL_DEFAULT_LON',
                                   utils.DEFAULT_LON),
            'default_lat': getattr(settings, 'GEOPORTAL_DEFAULT_LAT',
                                   utils.DEFAULT_LAT),
            'default_zoom': getattr(settings, 'GEOPORTAL_DEFAULT_ZOOM',
                                    utils.DEFAULT_ZOOM),
            'point_zoom': getattr(settings, 'GEOPORTAL_POINT_ZOOM',
                                  utils.POINT_ZOOM),
            'srid': 4326,
            'field_name': ftype.capitalize(),
            'wkt': geo_field.wkt,
            'wms_url': utils.WMS_URL,
        })
        loaded = template.loader.get_template('geoportal/map.html')
        rendered = loaded.render(isolated_context)
        if self.var_name is not None:
            context[self.var_name] = map_var
        return rendered