15 from persistent import Persistent |
15 from persistent import Persistent |
16 from zope.container.contained import Contained |
16 from zope.container.contained import Contained |
17 from zope.schema.fieldproperty import FieldProperty |
17 from zope.schema.fieldproperty import FieldProperty |
18 |
18 |
19 from pyams_gis.interfaces.configuration import IMapConfiguration, IMapConfigurationTarget |
19 from pyams_gis.interfaces.configuration import IMapConfiguration, IMapConfigurationTarget |
|
20 from pyams_gis.interfaces.utility import IMapManager |
20 from pyams_utils.adapter import adapter_config, get_annotation_adapter |
21 from pyams_utils.adapter import adapter_config, get_annotation_adapter |
21 from pyams_utils.factory import factory_config |
22 from pyams_utils.factory import factory_config |
|
23 from pyams_utils.registry import get_utility |
22 |
24 |
23 |
25 |
24 @factory_config(IMapConfiguration) |
26 @factory_config(IMapConfiguration) |
25 class MapConfiguration(Persistent, Contained): |
27 class MapConfiguration(Persistent, Contained): |
26 """Map configuration persistent class""" |
28 """Map configuration persistent class""" |
27 |
29 |
28 crs = FieldProperty(IMapConfiguration['crs']) |
30 crs = FieldProperty(IMapConfiguration['crs']) |
29 layers = FieldProperty(IMapConfiguration['layers']) |
31 layers = FieldProperty(IMapConfiguration['layers']) |
|
32 auto_adjust = FieldProperty(IMapConfiguration['auto_adjust']) |
|
33 initial_center = FieldProperty(IMapConfiguration['initial_center']) |
|
34 zoom_level = FieldProperty(IMapConfiguration['zoom_level']) |
|
35 initial_bounds = FieldProperty(IMapConfiguration['initial_bounds']) |
30 attribution_control = FieldProperty(IMapConfiguration['attribution_control']) |
36 attribution_control = FieldProperty(IMapConfiguration['attribution_control']) |
31 zoom_control = FieldProperty(IMapConfiguration['zoom_control']) |
37 zoom_control = FieldProperty(IMapConfiguration['zoom_control']) |
32 layer_control = FieldProperty(IMapConfiguration['layer_control']) |
38 layer_control = FieldProperty(IMapConfiguration['layer_control']) |
33 initial_center = FieldProperty(IMapConfiguration['initial_center']) |
|
34 zoom_level = FieldProperty(IMapConfiguration['zoom_level']) |
|
35 initial_bounds = FieldProperty(IMapConfiguration['initial_bounds']) |
|
36 keyboard = FieldProperty(IMapConfiguration['keyboard']) |
39 keyboard = FieldProperty(IMapConfiguration['keyboard']) |
37 scroll_wheel_zoom = FieldProperty(IMapConfiguration['scroll_wheel_zoom']) |
40 scroll_wheel_zoom = FieldProperty(IMapConfiguration['scroll_wheel_zoom']) |
38 |
41 |
39 def get_configuration(self): |
42 def get_configuration(self): |
40 result = { |
43 result = { |
41 'crs': self.crs, |
44 'crs': self.crs, |
|
45 'layerControl': self.layer_control, |
42 'attributionControl': self.attribution_control, |
46 'attributionControl': self.attribution_control, |
43 'zoomControl': self.zoom_control, |
47 'zoomControl': self.zoom_control, |
44 'layerControl': self.layer_control, |
|
45 'keyboard': self.keyboard, |
48 'keyboard': self.keyboard, |
46 'scrollWheelZoom': self.scroll_wheel_zoom |
49 'scrollWheelZoom': self.scroll_wheel_zoom, |
|
50 'zoom': self.zoom_level |
47 } |
51 } |
|
52 if self.auto_adjust: |
|
53 result['adjust'] = 'auto' |
48 if self.initial_center: |
54 if self.initial_center: |
49 gps_location = self.initial_center.wgs_coordinates |
55 gps_location = self.initial_center.wgs_coordinates |
50 result['center'] = {'lat': float(gps_location[1]), |
56 result['center'] = { |
51 'lon': float(gps_location[0])} |
57 'lat': float(gps_location[1]), |
52 result['zoom'] = self.zoom_level |
58 'lon': float(gps_location[0]) |
|
59 } |
53 elif self.initial_bounds: |
60 elif self.initial_bounds: |
54 point1, point2 = self.initial_bounds.wgs_coordinates |
61 point1, point2 = self.initial_bounds.wgs_coordinates |
55 result['bounds'] = [{'lat': float(point1[1]), |
62 result['bounds'] = [{ |
56 'lon': float(point1[0])}, |
63 'lat': float(point1[1]), |
57 {'lat': float(point2[1]), |
64 'lon': float(point1[0]) |
58 'lon': float(point2[0])}] |
65 }, { |
|
66 'lat': float(point2[1]), |
|
67 'lon': float(point2[0]) |
|
68 }] |
59 else: |
69 else: |
60 # Near center default location |
70 # Near center default location |
61 result['center'] = {'lat': 45, |
71 result['center'] = { |
62 'lon': 5.0} |
72 'lat': 45, |
63 result['zoom'] = 2 |
73 'lon': 5.0 |
|
74 } |
64 if self.layers: |
75 if self.layers: |
65 layers = [] |
76 layers = [] |
66 for name in self.layers: |
77 for name in self.layers: |
67 layer = self.__parent__.get(name) |
78 manager = get_utility(IMapManager) |
|
79 layer = manager.get(name) |
68 if layer is not None: |
80 if layer is not None: |
69 layers.append(layer.get_configuration()) |
81 layers.append(layer.get_configuration()) |
70 result['layers'] = layers |
82 result['layers'] = layers |
71 return result |
83 return result |
72 |
84 |