src/pyams_default_theme/component/paragraph/map.py
changeset 543 75d913d682d4
parent 380 f549a5cd4781
child 554 aaa7e73956b0
equal deleted inserted replaced
542:39aef8542b47 543:75d913d682d4
    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
    15 
    16 
       
    17 from onf_website.component.location import ILocationInfo, ILocationTarget
    16 from pyams_content.component.paragraph.interfaces.map import have_gis
    18 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 
    17 if have_gis:
    25 if have_gis:
    18 
    26 
    19     from zope.schema.fieldproperty import FieldProperty
    27     from zope.schema.fieldproperty import FieldProperty
    20 
    28 
    21     from pyams_content.component.paragraph.interfaces.map import IMapParagraph
    29     from pyams_content.component.paragraph.interfaces.map import IMapParagraph
    22     from pyams_content.features.renderer.interfaces import IContentRenderer
    30     from pyams_content.features.renderer.interfaces import IContentRenderer
    23     from pyams_default_theme.component.paragraph.interfaces.map import IMapParagraphDefaultRendererSettings
    31     from pyams_default_theme.component.paragraph.interfaces.map import \
       
    32         IMapParagraphDefaultRendererSettings
    24     from pyams_default_theme.features.renderer import BaseContentRenderer
    33     from pyams_default_theme.features.renderer import BaseContentRenderer
    25     from pyams_gis.configuration import MapConfiguration
    34     from pyams_gis.configuration import MapConfiguration
    26     from pyams_gis.interfaces.configuration import IMapConfiguration
    35     from pyams_gis.interfaces.configuration import IMapConfiguration
    27     from pyams_gis.interfaces.utility import IMapManager
    36     from pyams_gis.interfaces.utility import IMapManager
    28     from pyams_skin.layer import IPyAMSLayer
    37     from pyams_skin.layer import IPyAMSLayer
    46         """Map paragraph default renderer settings"""
    55         """Map paragraph default renderer settings"""
    47 
    56 
    48         _use_default_map_configuration = FieldProperty(IMapParagraphDefaultRendererSettings[
    57         _use_default_map_configuration = FieldProperty(IMapParagraphDefaultRendererSettings[
    49                                                            'use_default_map_configuration'])
    58                                                            'use_default_map_configuration'])
    50 
    59 
       
    60         map_height = FieldProperty(IMapParagraphDefaultRendererSettings['map_height'])
       
    61 
    51         @property
    62         @property
    52         def use_default_map_configuration(self):
    63         def use_default_map_configuration(self):
    53             return self._use_default_map_configuration
    64             return self._use_default_map_configuration
    54 
    65 
    55         @use_default_map_configuration.setter
    66         @use_default_map_configuration.setter
    66 
    77 
    67         @property
    78         @property
    68         def configuration(self):
    79         def configuration(self):
    69             if self.use_default_map_configuration:
    80             if self.use_default_map_configuration:
    70                 manager = get_utility(IMapManager)
    81                 manager = get_utility(IMapManager)
    71                 return IMapConfiguration(manager)
    82                 return IMapConfiguration(manager).get_configuration()
    72             else:
    83             return self.get_configuration()
    73                 return self
       
    74 
    84 
    75 
    85 
    76     @adapter_config(context=IMapParagraph, provides=IMapParagraphDefaultRendererSettings)
    86     @adapter_config(context=IMapParagraph,
       
    87                     provides=IMapParagraphDefaultRendererSettings)
    77     def map_paragraph_default_renderer_settings_factory(context):
    88     def map_paragraph_default_renderer_settings_factory(context):
    78         """Map paragraph default renderer settings factory"""
    89         """Map paragraph default renderer settings factory"""
    79         return get_annotation_adapter(context, MAP_DEFAULT_RENDERER_SETTINGS_KEY,
    90         return get_annotation_adapter(context, MAP_DEFAULT_RENDERER_SETTINGS_KEY,
    80                                       IMapParagraphDefaultRendererSettings)
    91                                       IMapParagraphDefaultRendererSettings)
    81 
    92 
    82 
    93 
    83     #
    94     #
    84     # Map paragraph default renderer
    95     # Map paragraph default renderer
    85     #
    96     #
    86 
    97 
    87     @adapter_config(name='default', context=(IMapParagraph, IPyAMSLayer), provides=IContentRenderer)
    98     @adapter_config(name='default',
       
    99                     context=(IMapParagraph, IPyAMSLayer),
       
   100                     provides=IContentRenderer)
    88     @template_config(template='templates/map-default.pt', layer=IPyAMSLayer)
   101     @template_config(template='templates/map-default.pt', layer=IPyAMSLayer)
    89     class MapParagraphDefaultRenderer(BaseContentRenderer):
   102     class MapParagraphDefaultRenderer(BaseContentRenderer):
    90         """Map paragraph default renderer"""
   103         """Map paragraph default renderer"""
    91 
   104 
    92         label = _("Default map renderer")
   105         label = _("Default map renderer")
    93 
   106 
    94         settings_interface = IMapParagraphDefaultRendererSettings
   107         settings_interface = IMapParagraphDefaultRendererSettings
    95 
   108 
    96         i18n_context_attrs = ('title', )
   109         i18n_context_attrs = ('title', )
       
   110 
       
   111         def get_map_configuration(self):
       
   112             configuration = 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)