source: TI02-CSML/trunk/services/3rdParty/GeoTypes-0.6.0/GeoTypes/_OGPoint.py @ 2194

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/services/3rdParty/GeoTypes-0.6.0/GeoTypes/_OGPoint.py@2194
Revision 2194, 3.5 KB checked in by lawrence, 13 years ago (diff)

Adding various specs and 3rd party code of interest for the CSML
services development.

Line 
1
2################################################################################
3# Copyright (c) QinetiQ Plc 2003
4#
5# Licensed under the LGPL. For full license details see the LICENSE file.
6################################################################################
7
8from _OGGeometry import OGGeometry
9
10class OGPoint(OGGeometry):
11
12    def __init__(self, srid=None):
13        OGGeometry.__init__(self,srid)
14        self.setX(0.0)
15        self.setY(0.0)
16        self.setZ(None)
17
18   
19    def fromString(self,s):
20        """
21        Initialise the Point from a string.
22
23        (arg s) should be of the form '(x y z)' where x and y are floating
24        point numbers.
25        """
26        seq = s.split(' ')
27        self.setX(float(seq[0]))
28        self.setY(float(seq[1]))
29        if len(seq) == 3:
30            self.setZ(float(seq[2]))
31        else:
32            self.setZ(None)
33       
34
35    def setX(self,x):
36        """
37        Set the X coord of the Point.
38
39        (arg x) is a float.
40        """
41        self._x = x
42
43    def getX(self):
44        """
45        Return the X coord of the Point.
46        """
47        return self._x
48
49    def setY(self,y):
50        """
51        Set the Y coord of the Point.
52
53        (arg x) is a float.
54        """
55
56        self._y = y
57
58    def getY(self):
59        """
60        Return the X coord of the Point.
61        """
62
63        return self._y
64
65    def setZ(self,z):
66        """
67        Set the Z coord of the Point.
68
69        (arg x) is a float.or None. If (arg x) is None the Point becomes 2d.
70        """
71
72        self._z = z
73
74    def getZ(self):
75        """
76        Return the Z coord of the Point.
77        """
78
79        return self._z
80
81    def _og_str(self):
82        """
83        Generate a string representation of the Point that is
84        suitable to use in a Postgres query.
85        """
86        if self.getZ():           
87            ret = "POINT(%f %f %f)" % (self.getX(),
88                                       self.getY(),
89                                       self.getZ())
90        else:
91            ret = "POINT(%f %f)" % (self.getX(),
92                                    self.getY())
93                                   
94        return ret
95
96    def __repr__(self):
97        """
98        Generate a represention of the Point as a string
99        suitable for 'evaling' as a tuple.
100        """
101        if self.getZ():           
102            ret = "%f %f %f" % (self.getX(), self.getY(), self.getZ())
103        else:
104            ret = "%f %f" % (self.getX(), self.getY())
105
106        return ret
107
108    def __eq__(self,other):
109        """
110        Support equality operations.
111
112        A Point is equal to another point is X == other.X and Y = other.Y.
113        """
114        if (type(self) != type(other)):
115            return False
116
117        if self.getX() == other.getX() and \
118           self.getY() == other.getY() and \
119           self.getZ() == other.getZ():
120            return True
121        return False
122
123    def __ne__(self,other):
124        return not self.__eq__(other)
125
126# Some helpful factory methods
127
128def OGpointFromValues(x,y,z=None):
129    """
130    Return a Point object.
131
132    (arg x) is the X coord as a float
133    (arg y) is the Y coord as a float
134    """
135   
136    p = OGPoint()
137    p.setX(x)
138    p.setY(y)
139    p.setZ(z)
140    return p
141
142def OGpointFromSequence(seq):
143    """
144    Return a Point object.
145
146    (arg seq) is a sequence of the form (x,y) where x is the X coord as a float
147    and y is the Y coord as a float.
148    """
149
150   
151    p = OGPoint()
152    p.setX(seq[0])
153    p.setY(seq[1])
154    if len(seq) == 3:
155        p.setZ(seq[2])
156       
157    return p
Note: See TracBrowser for help on using the repository browser.