src/pyams_default_theme/component/paragraph/map.py
changeset 554 aaa7e73956b0
parent 543 75d913d682d4
equal deleted inserted replaced
553:00784fe4c215 554:aaa7e73956b0
    10 # FOR A PARTICULAR PURPOSE.
    10 # FOR A PARTICULAR PURPOSE.
    11 #
    11 #
    12 
    12 
    13 __docformat__ = 'restructuredtext'
    13 __docformat__ = 'restructuredtext'
    14 
    14 
    15 import json
       
    16 
       
    17 from onf_website.component.location import ILocationInfo, ILocationTarget
       
    18 from pyams_content.component.paragraph.interfaces.map import have_gis
    15 from pyams_content.component.paragraph.interfaces.map import have_gis
    19 from pyams_gis.layer import GeoJSONLayer
       
    20 from pyams_utils.interfaces import ICacheKeyValue
       
    21 from pyams_utils.traversing import get_parent
       
    22 from pyams_utils.url import absolute_url
       
    23 
       
    24 
    16 
    25 if have_gis:
    17 if have_gis:
       
    18 
       
    19     import json
    26 
    20 
    27     from zope.schema.fieldproperty import FieldProperty
    21     from zope.schema.fieldproperty import FieldProperty
    28 
    22 
    29     from pyams_content.component.paragraph.interfaces.map import IMapParagraph
    23     from pyams_content.component.paragraph.interfaces.map import IMapParagraph
    30     from pyams_content.features.renderer.interfaces import IContentRenderer
    24     from pyams_content.features.renderer.interfaces import IContentRenderer
   107         settings_interface = IMapParagraphDefaultRendererSettings
   101         settings_interface = IMapParagraphDefaultRendererSettings
   108 
   102 
   109         i18n_context_attrs = ('title', )
   103         i18n_context_attrs = ('title', )
   110 
   104 
   111         def get_map_configuration(self):
   105         def get_map_configuration(self):
   112             configuration = self.settings.configuration
   106             return json.dumps(self.settings.configuration)
   113             settings = self.settings
       
   114             if settings.display_context_forests:
       
   115                 parent = get_parent(self.context, ILocationTarget)
       
   116                 if parent is not None:
       
   117                     key = ICacheKeyValue(parent)
       
   118                     layer = GeoJSONLayer()
       
   119                     layer.name = '{}-forests'.format(key)
       
   120                     layer.url = absolute_url(parent, self.request, 'get-location-forests.json')
       
   121                     layer.style = json.dumps({
       
   122                         'color': '#00dd00',
       
   123                         'weight': '3',
       
   124                         'opacity': '0.5'
       
   125                     })
       
   126                     configuration.setdefault('layers', []).append(layer.get_configuration())
       
   127                     if settings.set_bounds_to_forests:
       
   128                         configuration['fitLayer'] = layer.name
       
   129             return json.dumps(configuration)
       
   130 
       
   131         def get_markers(self):
       
   132             if not self.context.display_marker:
       
   133                 return None
       
   134             if self.context.use_context_location:
       
   135                 parent = get_parent(self.context, ILocationTarget)
       
   136                 if parent is None:
       
   137                     return None
       
   138                 locations = ILocationInfo(parent).locations
       
   139                 if not locations:
       
   140                     return None
       
   141             else:
       
   142                 if not self.context.gps_location:
       
   143                     return None
       
   144                 locations = [self.context.gps_location]
       
   145             if not locations:
       
   146                 return None
       
   147             config = {
       
   148                 'icon': {
       
   149                     'url': '/--static--/onf_website/images/leaflet-marker.png',
       
   150                     'size': [47, 59],
       
   151                     'anchor': [23, 58]
       
   152                 },
       
   153                 'clusterClass': 'onfCluster',
       
   154                 'tooltipClass': 'onfTooltip',
       
   155                 'markers': []
       
   156             }
       
   157             append = config['markers'].append
       
   158             for idx, location in enumerate(locations):
       
   159                 append({
       
   160                     'id': '{}::{}'.format(ICacheKeyValue(self.context), idx),
       
   161                     'point': location.to_json()
       
   162                 })
       
   163             return json.dumps(config)