|
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 WGS84, IGeoArea |
|
26 |
|
27 # import packages |
|
28 from zope.interface import implementer |
|
29 from zope.schema.fieldproperty import FieldProperty |
|
30 |
|
31 |
|
32 @implementer(IGeoArea) |
|
33 class GeoArea(Persistent): |
|
34 """GeoArea attribute object""" |
|
35 |
|
36 x1 = FieldProperty(IGeoArea['x1']) |
|
37 y1 = FieldProperty(IGeoArea['y1']) |
|
38 x2 = FieldProperty(IGeoArea['x2']) |
|
39 y2 = FieldProperty(IGeoArea['y2']) |
|
40 projection = FieldProperty(IGeoArea['projection']) |
|
41 |
|
42 def __bool__(self): |
|
43 return bool(self.x1 and self.y1 and self.x2 and self.y2) |
|
44 |
|
45 def get_coordinates(self, projection=WGS84): |
|
46 if projection == self.projection: |
|
47 return (self.x1, self.y1), (self.x2, self.y2) |
|
48 if (not have_gdal) or not self: |
|
49 return None, None |
|
50 source = SpatialReference() |
|
51 source.ImportFromEPSG(self.projection) |
|
52 destination = SpatialReference() |
|
53 destination.ImportFromEPSG(projection) |
|
54 transformation = CoordinateTransformation(source, destination) |
|
55 return transformation.TransformPoint(float(self.x1), float(self.y1))[0:2], \ |
|
56 transformation.TransformPoint(float(self.x2), float(self.y2))[0:2] |
|
57 |
|
58 @property |
|
59 def wgs_coordinates(self): |
|
60 return self.get_coordinates(WGS84) |