src/pyams_gis/configuration.py
changeset 0 c73bb834ccbe
child 35 39f7e00f9e51
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_gis/configuration.py	Thu May 18 17:23:48 2017 +0200
@@ -0,0 +1,95 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_gis.interfaces.configuration import IMapConfiguration, IMapConfigurationTarget
+from zope.annotation.interfaces import IAnnotations
+
+# import packages
+from persistent import Persistent
+from pyams_utils.adapter import adapter_config
+from pyramid.threadlocal import get_current_registry
+from zope.container.contained import Contained
+from zope.interface import implementer
+from zope.lifecycleevent import ObjectCreatedEvent
+from zope.location import locate
+from zope.schema.fieldproperty import FieldProperty
+
+
+@implementer(IMapConfiguration)
+class MapConfiguration(Persistent, Contained):
+    """Map configuration persistent class"""
+
+    crs = FieldProperty(IMapConfiguration['crs'])
+    layers = FieldProperty(IMapConfiguration['layers'])
+    attribution_control = FieldProperty(IMapConfiguration['attribution_control'])
+    zoom_control = FieldProperty(IMapConfiguration['zoom_control'])
+    layer_control = FieldProperty(IMapConfiguration['layer_control'])
+    initial_center = FieldProperty(IMapConfiguration['initial_center'])
+    zoom_level = FieldProperty(IMapConfiguration['zoom_level'])
+    initial_bounds = FieldProperty(IMapConfiguration['initial_bounds'])
+    keyboard = FieldProperty(IMapConfiguration['keyboard'])
+    scroll_wheel_zoom = FieldProperty(IMapConfiguration['scroll_wheel_zoom'])
+
+    def get_configuration(self):
+        result = {
+            'crs': self.crs,
+            'attributionControl': self.attribution_control,
+            'zoomControl': self.zoom_control,
+            'layerControl': self.layer_control,
+            'keyboard': self.keyboard,
+            'scrollWheelZoom': self.scroll_wheel_zoom
+        }
+        if self.initial_center:
+            gps_location = self.initial_center.wgs_coordinates
+            result['center'] = {'lat': float(gps_location[1]),
+                                'lon': float(gps_location[0])}
+            result['zoom'] = self.zoom_level
+        elif self.initial_bounds:
+            point1, point2 = self.initial_bounds.wgs_coordinates
+            result['bounds'] = [{'lat': float(point1[1]),
+                                 'lon': float(point1[0])},
+                                {'lat': float(point2[1]),
+                                 'lon': float(point2[0])}]
+        else:
+            # Near center default location
+            result['center'] = {'lat': 45,
+                                'lon': 5.0}
+            result['zoom'] = 2
+        if self.layers:
+            layers = []
+            for name in self.layers:
+                layer = self.__parent__.get(name)
+                if layer is not None:
+                    layers.append(layer.get_configuration())
+            result['layers'] = layers
+        return result
+
+
+MAP_CONFIGURATION_KEY = 'pyams_gis.map.configuration'
+
+
+@adapter_config(context=IMapConfigurationTarget, provides=IMapConfiguration)
+def MapConfigurationFactory(context):
+    """Map configuration factory"""
+    annotations = IAnnotations(context)
+    configuration = annotations.get(MAP_CONFIGURATION_KEY)
+    if configuration is None:
+        configuration = annotations[MAP_CONFIGURATION_KEY] = MapConfiguration()
+        get_current_registry().notify(ObjectCreatedEvent(configuration))
+        locate(configuration, context)
+    return configuration