|
1 # |
|
2 # Copyright (c) 2008-2017 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 try: |
|
18 from osgeo.osr import SpatialReference, CoordinateTransformation |
|
19 have_gdal = True |
|
20 except ImportError: |
|
21 have_gdal = False |
|
22 from persistent import Persistent |
|
23 |
|
24 # import interfaces |
|
25 from pyams_gis.interfaces import IGeoPoint, IGeoPointZ, WGS84 |
|
26 |
|
27 # import packages |
|
28 from zope.interface import implementer |
|
29 from zope.schema.fieldproperty import FieldProperty |
|
30 |
|
31 |
|
32 @implementer(IGeoPoint) |
|
33 class GeoPoint(Persistent): |
|
34 """GeoPoint attribute object""" |
|
35 |
|
36 longitude = FieldProperty(IGeoPoint['longitude']) |
|
37 latitude = FieldProperty(IGeoPoint['latitude']) |
|
38 projection = FieldProperty(IGeoPoint['projection']) |
|
39 |
|
40 def __bool__(self): |
|
41 return bool(self.longitude and self.latitude) |
|
42 |
|
43 def get_coordinates(self, projection=WGS84): |
|
44 if projection == self.projection: |
|
45 return self.longitude, self.latitude |
|
46 if (not have_gdal) or not self: |
|
47 return None, None |
|
48 source = SpatialReference() |
|
49 source.ImportFromEPSG(self.projection) |
|
50 destination = SpatialReference() |
|
51 destination.ImportFromEPSG(projection) |
|
52 transformation = CoordinateTransformation(source, destination) |
|
53 return transformation.TransformPoint(float(self.longitude), float(self.latitude))[0:2] |
|
54 |
|
55 @property |
|
56 def wgs_coordinates(self): |
|
57 return self.get_coordinates(WGS84) |
|
58 |
|
59 |
|
60 @implementer(IGeoPointZ) |
|
61 class GeoPointZ(GeoPoint): |
|
62 """GeoPointZ attribute object""" |
|
63 |
|
64 altitude = FieldProperty(IGeoPointZ['altitude']) |