src/pyams_gis/point.py
changeset 71 8a73283378e1
parent 0 c73bb834ccbe
equal deleted inserted replaced
70:6be49d6f567e 71:8a73283378e1
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
    10 # FOR A PARTICULAR PURPOSE.
    10 # FOR A PARTICULAR PURPOSE.
    11 #
    11 #
    12 
    12 
    13 __docformat__ = 'restructuredtext'
    13 from decimal import Decimal
    14 
    14 
    15 
       
    16 # import standard library
       
    17 try:
    15 try:
    18     from osgeo.osr import SpatialReference, CoordinateTransformation
    16     from osgeo.osr import SpatialReference, CoordinateTransformation
    19     have_gdal = True
    17     have_gdal = True
    20 except ImportError:
    18 except ImportError:
    21     have_gdal = False
    19     have_gdal = False
    22 from persistent import Persistent
    20 from persistent import Persistent
    23 
    21 
    24 # import interfaces
    22 from zope.interface import implementer
       
    23 from zope.schema.fieldproperty import FieldProperty
       
    24 
    25 from pyams_gis.interfaces import IGeoPoint, IGeoPointZ, WGS84
    25 from pyams_gis.interfaces import IGeoPoint, IGeoPointZ, WGS84
    26 
    26 
    27 # import packages
    27 __docformat__ = 'restructuredtext'
    28 from zope.interface import implementer
       
    29 from zope.schema.fieldproperty import FieldProperty
       
    30 
    28 
    31 
    29 
    32 @implementer(IGeoPoint)
    30 @implementer(IGeoPoint)
    33 class GeoPoint(Persistent):
    31 class GeoPoint(Persistent):
    34     """GeoPoint attribute object"""
    32     """GeoPoint attribute object"""
    35 
    33 
    36     longitude = FieldProperty(IGeoPoint['longitude'])
    34     longitude = FieldProperty(IGeoPoint['longitude'])
    37     latitude = FieldProperty(IGeoPoint['latitude'])
    35     latitude = FieldProperty(IGeoPoint['latitude'])
    38     projection = FieldProperty(IGeoPoint['projection'])
    36     projection = FieldProperty(IGeoPoint['projection'])
       
    37 
       
    38     def __init__(self, data=None, **kwargs):
       
    39         super().__init__()
       
    40         if 'longitude' in kwargs:
       
    41             self.longitude = Decimal(kwargs.get('longitude'))
       
    42         if 'latitude' in kwargs:
       
    43             self.latitude = Decimal(kwargs.get('latitude'))
       
    44         if 'projection' in kwargs:
       
    45             self.projection = Decimal(kwargs.get('projection'))
    39 
    46 
    40     def __bool__(self):
    47     def __bool__(self):
    41         return bool(self.longitude and self.latitude)
    48         return bool(self.longitude and self.latitude)
    42 
    49 
    43     def get_coordinates(self, projection=WGS84):
    50     def get_coordinates(self, projection=WGS84):
    54 
    61 
    55     @property
    62     @property
    56     def wgs_coordinates(self):
    63     def wgs_coordinates(self):
    57         return self.get_coordinates(WGS84)
    64         return self.get_coordinates(WGS84)
    58 
    65 
       
    66     def to_json(self):
       
    67         if not self:
       
    68             return None
       
    69         return {
       
    70             'x': float(self.longitude),
       
    71             'y': float(self.latitude),
       
    72             'crs': float(self.projection)
       
    73         }
       
    74 
    59 
    75 
    60 @implementer(IGeoPointZ)
    76 @implementer(IGeoPointZ)
    61 class GeoPointZ(GeoPoint):
    77 class GeoPointZ(GeoPoint):
    62     """GeoPointZ attribute object"""
    78     """GeoPointZ attribute object"""
    63 
    79 
    64     altitude = FieldProperty(IGeoPointZ['altitude'])
    80     altitude = FieldProperty(IGeoPointZ['altitude'])
       
    81 
       
    82     def __init__(self, data=None, **kwargs):
       
    83         super().__init__(data, **kwargs)
       
    84         if 'altitude' in kwargs:
       
    85             self.altitude = kwargs.get('altitude')
       
    86 
       
    87     def to_json(self):
       
    88         result = super().to_json()
       
    89         if result:
       
    90             result.update({
       
    91                 'z': float(self.altitude)
       
    92             })
       
    93         return result