src/pyams_gis/layer.py
changeset 0 c73bb834ccbe
child 73 d9ee6f8ddb76
equal deleted inserted replaced
-1:000000000000 0:c73bb834ccbe
       
     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.layer import IMapLayer, IBaseTileMapLayer, ITileMapLayer, IWMSMapLayer, IGeoportalMapLayer, \
       
    20     IEsriFeatureMapLayer, IGoogleMapLayer
       
    21 from pyams_i18n.interfaces import II18n
       
    22 
       
    23 # import packages
       
    24 from persistent import Persistent
       
    25 from pyams_gis import leaflet_gp, leaflet_esri, leaflet_google_mutant
       
    26 from pyams_utils.dict import update_dict
       
    27 from pyams_utils.fanstatic import get_resource_path
       
    28 from zope.container.contained import Contained
       
    29 from zope.interface import implementer
       
    30 from zope.schema.fieldproperty import FieldProperty
       
    31 
       
    32 from pyams_gis import _
       
    33 
       
    34 
       
    35 class MapLayer(Persistent, Contained):
       
    36     """Base tile map layer persistent class"""
       
    37 
       
    38     factory = None
       
    39     depends = {}
       
    40     layer_type = None
       
    41 
       
    42     name = FieldProperty(IMapLayer['name'])
       
    43     title = FieldProperty(IMapLayer['title'])
       
    44     min_zoom = FieldProperty(IMapLayer['min_zoom'])
       
    45     max_zoom = FieldProperty(IMapLayer['max_zoom'])
       
    46 
       
    47     def get_configuration(self):
       
    48         """Get configuration mapping"""
       
    49         result = {'name': self.name,
       
    50                   'title': II18n(self).query_attribute('title')}
       
    51         update_dict(result, 'factory', self.factory)
       
    52         update_dict(result, 'minZoom', self.min_zoom)
       
    53         update_dict(result, 'maxZoom', self.max_zoom)
       
    54         if self.depends:
       
    55             depends = {}
       
    56             for name, resource in self.depends.items():
       
    57                 depends[name] = get_resource_path(resource)
       
    58             update_dict(result, 'dependsOn', depends)
       
    59         return result
       
    60 
       
    61 
       
    62 class BaseTileMapLayer(MapLayer):
       
    63     """Base tile map layer"""
       
    64 
       
    65     attribution = FieldProperty(IBaseTileMapLayer['attribution'])
       
    66     bounds = FieldProperty(IBaseTileMapLayer['bounds'])
       
    67 
       
    68     def get_configuration(self):
       
    69         result = super(BaseTileMapLayer, self).get_configuration()
       
    70         update_dict(result, 'attribution', self.attribution)
       
    71         if self.bounds:
       
    72             point1, point2 = self.bounds.wgs_coordinates
       
    73             result['bounds'] = [{'lat': float(point1[1]),
       
    74                                  'lon': float(point1[0])},
       
    75                                 {'lat': float(point2[1]),
       
    76                                  'lon': float(point2[0])}]
       
    77         return result
       
    78 
       
    79 
       
    80 @implementer(ITileMapLayer)
       
    81 class TileMapLayer(BaseTileMapLayer):
       
    82     """Base tile map layer persistent class"""
       
    83 
       
    84     factory = 'PyAMS_GIS.factory.TileLayer'
       
    85     layer_type = _("Tile")
       
    86 
       
    87     url = FieldProperty(ITileMapLayer['url'])
       
    88 
       
    89     def get_configuration(self):
       
    90         result = super(TileMapLayer, self).get_configuration()
       
    91         update_dict(result, 'url', self.url)
       
    92         return result
       
    93 
       
    94 
       
    95 @implementer(IWMSMapLayer)
       
    96 class WMSMapLayer(TileMapLayer):
       
    97     """WMS map mayer persistent class"""
       
    98 
       
    99     factory = 'PyAMS_GIS.factory.WMS'
       
   100     layer_type = _("WMS")
       
   101 
       
   102     crs = FieldProperty(IWMSMapLayer['crs'])
       
   103     layers = FieldProperty(IWMSMapLayer['layers'])
       
   104     styles = FieldProperty(IWMSMapLayer['styles'])
       
   105     format = FieldProperty(IWMSMapLayer['format'])
       
   106     transparent = FieldProperty(IWMSMapLayer['transparent'])
       
   107     version = FieldProperty(IWMSMapLayer['version'])
       
   108     uppercase = FieldProperty(IWMSMapLayer['uppercase'])
       
   109 
       
   110     def get_configuration(self):
       
   111         result = super(WMSMapLayer, self).get_configuration()
       
   112         update_dict(result, 'crs', self.crs)
       
   113         update_dict(result, 'layers', self.layers)
       
   114         update_dict(result, 'styles', self.styles)
       
   115         update_dict(result, 'format', self.format)
       
   116         update_dict(result, 'transparent', self.transparent)
       
   117         update_dict(result, 'version', self.version)
       
   118         update_dict(result, 'uppercase', self.uppercase)
       
   119         return result
       
   120 
       
   121 
       
   122 @implementer(IGeoportalMapLayer)
       
   123 class GeoportalMapLayer(BaseTileMapLayer):
       
   124     """Geoportal map layer persistent class"""
       
   125 
       
   126     factory = 'PyAMS_GIS.factory.Geoportal.WMS'
       
   127     depends = {'L.geoportalLayer.WMS': leaflet_gp}
       
   128     layer_type = _("Geoportal")
       
   129 
       
   130     api_key = FieldProperty(IGeoportalMapLayer['api_key'])
       
   131     layer_name = FieldProperty(IGeoportalMapLayer['layer_name'])
       
   132     crs = FieldProperty(IGeoportalMapLayer['crs'])
       
   133 
       
   134     def get_configuration(self):
       
   135         result = super(GeoportalMapLayer, self).get_configuration()
       
   136         update_dict(result, 'apiKey', self.api_key)
       
   137         update_dict(result, 'layer', self.layer_name)
       
   138         update_dict(result, 'crs', self.crs)
       
   139         return result
       
   140 
       
   141 
       
   142 @implementer(IEsriFeatureMapLayer)
       
   143 class EsriFeatureMapLayer(MapLayer):
       
   144     """ESRI feature map layer"""
       
   145 
       
   146     factory = 'PyAMS_GIS.factory.ESRI.Feature'
       
   147     depends = {'L.esri.featureLayer': leaflet_esri}
       
   148     layer_type = _("ESRI Features")
       
   149 
       
   150     url = FieldProperty(IEsriFeatureMapLayer['url'])
       
   151     token = FieldProperty(IEsriFeatureMapLayer['token'])
       
   152     where = FieldProperty(IEsriFeatureMapLayer['where'])
       
   153 
       
   154     def get_configuration(self):
       
   155         result = super(EsriFeatureMapLayer, self).get_configuration()
       
   156         update_dict(result, 'url', self.url)
       
   157         update_dict(result, 'token', self.token)
       
   158         update_dict(result, 'where', self.where)
       
   159         return result
       
   160 
       
   161 
       
   162 @implementer(IGoogleMapLayer)
       
   163 class GoogleMapLayer(MapLayer):
       
   164     """Google maps layer"""
       
   165 
       
   166     factory = 'PyAMS_GIS.factory.Google'
       
   167     depends = {'L.gridLayer.googleMutant': leaflet_google_mutant}
       
   168     layer_type = _("Google")
       
   169 
       
   170     api_key = FieldProperty(IGoogleMapLayer['api_key'])
       
   171     type = FieldProperty(IGoogleMapLayer['type'])
       
   172 
       
   173     def get_configuration(self):
       
   174         result = super(GoogleMapLayer, self).get_configuration()
       
   175         update_dict(result, 'apiKey', self.api_key)
       
   176         update_dict(result, 'type', self.type)
       
   177         return result