|
1 # |
|
2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net> |
|
3 # All Rights Reserved. |
|
4 # |
|
5 # This software is subject to the provisions of the Zope Public License, |
|
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
10 # FOR A PARTICULAR PURPOSE. |
|
11 # |
|
12 |
|
13 __docformat__ = 'restructuredtext' |
|
14 |
|
15 |
|
16 # import standard library |
|
17 |
|
18 # import interfaces |
|
19 from pyams_gis.interfaces.configuration import IMapConfiguration, IMapConfigurationTarget |
|
20 from zope.annotation.interfaces import IAnnotations |
|
21 |
|
22 # import packages |
|
23 from persistent import Persistent |
|
24 from pyams_utils.adapter import adapter_config |
|
25 from pyramid.threadlocal import get_current_registry |
|
26 from zope.container.contained import Contained |
|
27 from zope.interface import implementer |
|
28 from zope.lifecycleevent import ObjectCreatedEvent |
|
29 from zope.location import locate |
|
30 from zope.schema.fieldproperty import FieldProperty |
|
31 |
|
32 |
|
33 @implementer(IMapConfiguration) |
|
34 class MapConfiguration(Persistent, Contained): |
|
35 """Map configuration persistent class""" |
|
36 |
|
37 crs = FieldProperty(IMapConfiguration['crs']) |
|
38 layers = FieldProperty(IMapConfiguration['layers']) |
|
39 attribution_control = FieldProperty(IMapConfiguration['attribution_control']) |
|
40 zoom_control = FieldProperty(IMapConfiguration['zoom_control']) |
|
41 layer_control = FieldProperty(IMapConfiguration['layer_control']) |
|
42 initial_center = FieldProperty(IMapConfiguration['initial_center']) |
|
43 zoom_level = FieldProperty(IMapConfiguration['zoom_level']) |
|
44 initial_bounds = FieldProperty(IMapConfiguration['initial_bounds']) |
|
45 keyboard = FieldProperty(IMapConfiguration['keyboard']) |
|
46 scroll_wheel_zoom = FieldProperty(IMapConfiguration['scroll_wheel_zoom']) |
|
47 |
|
48 def get_configuration(self): |
|
49 result = { |
|
50 'crs': self.crs, |
|
51 'attributionControl': self.attribution_control, |
|
52 'zoomControl': self.zoom_control, |
|
53 'layerControl': self.layer_control, |
|
54 'keyboard': self.keyboard, |
|
55 'scrollWheelZoom': self.scroll_wheel_zoom |
|
56 } |
|
57 if self.initial_center: |
|
58 gps_location = self.initial_center.wgs_coordinates |
|
59 result['center'] = {'lat': float(gps_location[1]), |
|
60 'lon': float(gps_location[0])} |
|
61 result['zoom'] = self.zoom_level |
|
62 elif self.initial_bounds: |
|
63 point1, point2 = self.initial_bounds.wgs_coordinates |
|
64 result['bounds'] = [{'lat': float(point1[1]), |
|
65 'lon': float(point1[0])}, |
|
66 {'lat': float(point2[1]), |
|
67 'lon': float(point2[0])}] |
|
68 else: |
|
69 # Near center default location |
|
70 result['center'] = {'lat': 45, |
|
71 'lon': 5.0} |
|
72 result['zoom'] = 2 |
|
73 if self.layers: |
|
74 layers = [] |
|
75 for name in self.layers: |
|
76 layer = self.__parent__.get(name) |
|
77 if layer is not None: |
|
78 layers.append(layer.get_configuration()) |
|
79 result['layers'] = layers |
|
80 return result |
|
81 |
|
82 |
|
83 MAP_CONFIGURATION_KEY = 'pyams_gis.map.configuration' |
|
84 |
|
85 |
|
86 @adapter_config(context=IMapConfigurationTarget, provides=IMapConfiguration) |
|
87 def MapConfigurationFactory(context): |
|
88 """Map configuration factory""" |
|
89 annotations = IAnnotations(context) |
|
90 configuration = annotations.get(MAP_CONFIGURATION_KEY) |
|
91 if configuration is None: |
|
92 configuration = annotations[MAP_CONFIGURATION_KEY] = MapConfiguration() |
|
93 get_current_registry().notify(ObjectCreatedEvent(configuration)) |
|
94 locate(configuration, context) |
|
95 return configuration |