Added GeoJSON layer type
authorThierry Florac <tflorac@ulthar.net>
Wed, 27 Jan 2021 15:37:20 +0100
changeset 73 d9ee6f8ddb76
parent 72 032947e7ef0c
child 74 31687784fa16
Added GeoJSON layer type
src/pyams_gis/interfaces/layer.py
src/pyams_gis/layer.py
--- a/src/pyams_gis/interfaces/layer.py	Wed Jan 27 15:36:14 2021 +0100
+++ b/src/pyams_gis/interfaces/layer.py	Wed Jan 27 15:37:20 2021 +0100
@@ -10,22 +10,18 @@
 # FOR A PARTICULAR PURPOSE.
 #
 
-__docformat__ = 'restructuredtext'
+from zope.annotation.interfaces import IAttributeAnnotatable
+from zope.interface import Attribute
+from zope.location.interfaces import IContained
+from zope.schema import Bool, Choice, Int, Text, TextLine
+from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
+
+from pyams_gis.interfaces import LAYER_CRS_VOCABULARY
+from pyams_gis.schema import GeoAreaField
+from pyams_i18n.schema import I18nTextLineField
 
 
-# import standard library
-
-# import interfaces
-from pyams_gis.interfaces import LAYER_CRS_VOCABULARY
-from zope.annotation.interfaces import IAttributeAnnotatable
-from zope.location.interfaces import IContained
-
-# import packages
-from pyams_gis.schema import GeoAreaField
-from pyams_i18n.schema import I18nTextLineField
-from zope.interface import Attribute
-from zope.schema import Bool, Choice, Int, TextLine
-from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
+__docformat__ = 'restructuredtext'
 
 from pyams_gis import _
 
@@ -64,6 +60,18 @@
         """Get layer configuration mapping"""
 
 
+class IGeoJSONLayer(IMapLayer):
+    """GeoJSON map layer interface"""
+
+    url = TextLine(title=_("Layer URL"),
+                   description=_("URL used to get access to JSON data"),
+                   required=True)
+
+    style = Text(title=_("Layer style"),
+                 description=_("Layer style, provided in Leaflet JSON format"),
+                 required=False)
+
+
 class IBaseTileMapLayer(IMapLayer):
     """Base tile map layer interface"""
 
--- a/src/pyams_gis/layer.py	Wed Jan 27 15:36:14 2021 +0100
+++ b/src/pyams_gis/layer.py	Wed Jan 27 15:37:20 2021 +0100
@@ -16,7 +16,10 @@
 # import standard library
 
 # import interfaces
-from pyams_gis.interfaces.layer import IMapLayer, IBaseTileMapLayer, ITileMapLayer, IWMSMapLayer, IGeoportalMapLayer, \
+import json
+
+from pyams_gis.interfaces.layer import IGeoJSONLayer, IMapLayer, IBaseTileMapLayer, ITileMapLayer, \
+    IWMSMapLayer, IGeoportalMapLayer, \
     IEsriFeatureMapLayer, IGoogleMapLayer
 from pyams_i18n.interfaces import II18n
 
@@ -46,8 +49,10 @@
 
     def get_configuration(self):
         """Get configuration mapping"""
-        result = {'name': self.name,
-                  'title': II18n(self).query_attribute('title')}
+        result = {
+            'name': self.name,
+            'title': II18n(self).query_attribute('title')
+        }
         update_dict(result, 'factory', self.factory)
         update_dict(result, 'minZoom', self.min_zoom)
         update_dict(result, 'maxZoom', self.max_zoom)
@@ -59,6 +64,23 @@
         return result
 
 
+class GeoJSONLayer(MapLayer):
+    """GeoJSON layer"""
+
+    factory = 'PyAMS_GIS.factory.GeoJSON'
+    layer_type = _("GeoJSON")
+
+    url = FieldProperty(IGeoJSONLayer['url'])
+    style = FieldProperty(IGeoJSONLayer['style'])
+
+    def get_configuration(self):
+        result = super(GeoJSONLayer, self).get_configuration()
+        update_dict(result, 'url', self.url)
+        if self.style:
+            update_dict(result, 'style', json.loads(self.style))
+        return result
+
+
 class BaseTileMapLayer(MapLayer):
     """Base tile map layer"""
 
@@ -70,10 +92,13 @@
         update_dict(result, 'attribution', self.attribution)
         if self.bounds:
             point1, point2 = self.bounds.wgs_coordinates
-            result['bounds'] = [{'lat': float(point1[1]),
-                                 'lon': float(point1[0])},
-                                {'lat': float(point2[1]),
-                                 'lon': float(point2[0])}]
+            result['bounds'] = [{
+                'lat': float(point1[1]),
+                'lon': float(point1[0])
+            }, {
+                'lat': float(point2[1]),
+                'lon': float(point2[0])
+            }]
         return result