Added JSON output
authorThierry Florac <tflorac@ulthar.net>
Wed, 27 Jan 2021 15:35:38 +0100
changeset 71 8a73283378e1
parent 70 6be49d6f567e
child 72 032947e7ef0c
Added JSON output
src/pyams_gis/area.py
src/pyams_gis/interfaces/__init__.py
src/pyams_gis/point.py
--- a/src/pyams_gis/area.py	Tue Jun 30 13:55:58 2020 +0200
+++ b/src/pyams_gis/area.py	Wed Jan 27 15:35:38 2021 +0100
@@ -10,10 +10,6 @@
 # FOR A PARTICULAR PURPOSE.
 #
 
-__docformat__ = 'restructuredtext'
-
-
-# import standard library
 try:
     from osgeo.osr import SpatialReference, CoordinateTransformation
     have_gdal = True
@@ -21,12 +17,12 @@
     have_gdal = False
 from persistent import Persistent
 
-# import interfaces
+from zope.interface import implementer
+from zope.schema.fieldproperty import FieldProperty
+
 from pyams_gis.interfaces import WGS84, IGeoArea
 
-# import packages
-from zope.interface import implementer
-from zope.schema.fieldproperty import FieldProperty
+__docformat__ = 'restructuredtext'
 
 
 @implementer(IGeoArea)
@@ -39,6 +35,17 @@
     y2 = FieldProperty(IGeoArea['y2'])
     projection = FieldProperty(IGeoArea['projection'])
 
+    def __init__(self, data=None, **kwargs):
+        super().__init__()
+        if 'x1' in kwargs:
+            self.x1 = kwargs['x1']
+        if 'y1' in kwargs:
+            self.y1 = kwargs['y1']
+        if 'x2' in kwargs:
+            self.x2 = kwargs['x2']
+        if 'y2' in kwargs:
+            self.y2 = kwargs['y2']
+
     def __bool__(self):
         return bool(self.x1 and self.y1 and self.x2 and self.y2)
 
@@ -58,3 +65,14 @@
     @property
     def wgs_coordinates(self):
         return self.get_coordinates(WGS84)
+
+    def to_json(self):
+        if not self:
+            return None
+        return {
+            'x1': float(self.x1),
+            'y1': float(self.y1),
+            'x2': float(self.x2),
+            'y2': float(self.y2),
+            'projection': self.projection
+        }
--- a/src/pyams_gis/interfaces/__init__.py	Tue Jun 30 13:55:58 2020 +0200
+++ b/src/pyams_gis/interfaces/__init__.py	Wed Jan 27 15:35:38 2021 +0100
@@ -10,18 +10,14 @@
 # FOR A PARTICULAR PURPOSE.
 #
 
-__docformat__ = 'restructuredtext'
+from zope.interface import Attribute, Interface, Invalid, invariant
+from zope.schema import Choice
+from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
+
+from pyams_utils.schema import DottedDecimalField
 
 
-# import standard library
-
-# import interfaces
-
-# import packages
-from pyams_utils.schema import DottedDecimalField
-from zope.interface import invariant, Interface, Invalid, Attribute
-from zope.schema import Choice
-from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
+__docformat__ = 'restructuredtext'
 
 from pyams_gis import _
 
@@ -35,29 +31,41 @@
 UTM_38S = 4471
 UTM_40S = 2975
 
-COORDINATES_PROJECTIONS = {WGS84: _("WGS84 (GPS)"),
-                           WGS84WM: _("WGS84 Web Mercator"),
-                           RGF93: _("Lambert 93 (Metropolitan France)"),
-                           LAMBERT_IIE: _("Extended Lambert II (Metropolitan France)"),
-                           UTM_20N: _("UTM Zone 20N (Martinique, Guadeloupe)"),
-                           UTM_22N: _("UTM Zone 22N (Guyane)"),
-                           UTM_38S: _("UTM Zone 38S (Mayotte)"),
-                           UTM_40S: _("UTM Zone 40S (La RĂ©union)")}
+COORDINATES_PROJECTIONS = {
+    WGS84: _("WGS84 (GPS)"),
+    WGS84WM: _("WGS84 Web Mercator"),
+    RGF93: _("Lambert 93 (Metropolitan France)"),
+    LAMBERT_IIE: _("Extended Lambert II (Metropolitan France)"),
+    UTM_20N: _("UTM Zone 20N (Martinique, Guadeloupe)"),
+    UTM_22N: _("UTM Zone 22N (Guyane)"),
+    UTM_38S: _("UTM Zone 38S (Mayotte)"),
+    UTM_40S: _("UTM Zone 40S (La RĂ©union)")
+}
 
-COORDINATES_PROJECTION_VOCABULARY = SimpleVocabulary(sorted([SimpleTerm(v, title=t)
-                                                             for v, t in COORDINATES_PROJECTIONS.items()],
-                                                            key=lambda x: x.title))
+COORDINATES_PROJECTION_VOCABULARY = SimpleVocabulary(sorted([
+    SimpleTerm(v, title=t) for v, t in COORDINATES_PROJECTIONS.items()
+], key=lambda x: x.title))
 
-LAYER_CRS = {WGS84: 'L.CRS.EPSG4326',
-             WGS84WM: 'L.CRS.EPSG3857',
-             RGF93: 'L.geoportalCRS.EPSG2154',
-             LAMBERT_IIE: 'L.geoportalCRS.EPSG27572'}
+LAYER_CRS = {
+    WGS84: 'L.CRS.EPSG4326',
+    WGS84WM: 'L.CRS.EPSG3857',
+    RGF93: 'L.geoportalCRS.EPSG2154',
+    LAMBERT_IIE: 'L.geoportalCRS.EPSG27572'
+}
 
-LAYER_CRS_VOCABULARY = SimpleVocabulary([SimpleTerm(t, title=COORDINATES_PROJECTIONS[v])
-                                         for v, t in LAYER_CRS.items()])
+LAYER_CRS_VOCABULARY = SimpleVocabulary([
+    SimpleTerm(t, title=COORDINATES_PROJECTIONS[v]) for v, t in LAYER_CRS.items()
+])
 
 
-class IGeoPoint(Interface):
+class IGeoInfo(Interface):
+    """Base geographic information interface"""
+
+    def to_json(self):
+        """Return JSON representation of current object"""
+
+
+class IGeoPoint(IGeoInfo):
     """GeoPoint attribute interface"""
 
     longitude = DottedDecimalField(title=_("Longitude"),
@@ -92,7 +100,7 @@
                                   required=False)
 
 
-class IGeoArea(Interface):
+class IGeoArea(IGeoInfo):
     """Geographic area defined by a rectangle"""
 
     x1 = DottedDecimalField(title=_("West limit"),
--- a/src/pyams_gis/point.py	Tue Jun 30 13:55:58 2020 +0200
+++ b/src/pyams_gis/point.py	Wed Jan 27 15:35:38 2021 +0100
@@ -10,10 +10,8 @@
 # FOR A PARTICULAR PURPOSE.
 #
 
-__docformat__ = 'restructuredtext'
+from decimal import Decimal
 
-
-# import standard library
 try:
     from osgeo.osr import SpatialReference, CoordinateTransformation
     have_gdal = True
@@ -21,12 +19,12 @@
     have_gdal = False
 from persistent import Persistent
 
-# import interfaces
+from zope.interface import implementer
+from zope.schema.fieldproperty import FieldProperty
+
 from pyams_gis.interfaces import IGeoPoint, IGeoPointZ, WGS84
 
-# import packages
-from zope.interface import implementer
-from zope.schema.fieldproperty import FieldProperty
+__docformat__ = 'restructuredtext'
 
 
 @implementer(IGeoPoint)
@@ -37,6 +35,15 @@
     latitude = FieldProperty(IGeoPoint['latitude'])
     projection = FieldProperty(IGeoPoint['projection'])
 
+    def __init__(self, data=None, **kwargs):
+        super().__init__()
+        if 'longitude' in kwargs:
+            self.longitude = Decimal(kwargs.get('longitude'))
+        if 'latitude' in kwargs:
+            self.latitude = Decimal(kwargs.get('latitude'))
+        if 'projection' in kwargs:
+            self.projection = Decimal(kwargs.get('projection'))
+
     def __bool__(self):
         return bool(self.longitude and self.latitude)
 
@@ -56,9 +63,31 @@
     def wgs_coordinates(self):
         return self.get_coordinates(WGS84)
 
+    def to_json(self):
+        if not self:
+            return None
+        return {
+            'x': float(self.longitude),
+            'y': float(self.latitude),
+            'crs': float(self.projection)
+        }
+
 
 @implementer(IGeoPointZ)
 class GeoPointZ(GeoPoint):
     """GeoPointZ attribute object"""
 
     altitude = FieldProperty(IGeoPointZ['altitude'])
+
+    def __init__(self, data=None, **kwargs):
+        super().__init__(data, **kwargs)
+        if 'altitude' in kwargs:
+            self.altitude = kwargs.get('altitude')
+
+    def to_json(self):
+        result = super().to_json()
+        if result:
+            result.update({
+                'z': float(self.altitude)
+            })
+        return result