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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/services/3rdParty/GeoTypes-0.6.0/GeoTypes/_Point.py@2194
Revision 2194, 4.2 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
8"""
9Implementation of Point.
10
11ChangeLog:
12    17 jan 06: Change constructor to accept several argument types (fredericback@gmail.com)
13"""
14
15class Point:
16    """
17    Point provides an interface for a single point in 2d space.
18
19    The constructor accepts the following argument combinations:
20        - Point()                           initalised to (0.0,0.0)
21        - Point( '((x,y),(x,y))' )          parse from string, x and y are floats
22        - Box( 'BOX(x y,x y)' )             parse from string, x and y are floats
23        - Box( Point(x,y), Point(x,y) )     set lower left and upper right points
24
25    It has accessor functions for X and Y coords and support equallity operations
26    with other Point objects.
27    """
28   
29    def __init__(self,*args):
30        """
31        The constructor accepts the following argument combinations:
32            - Point()                           initalised to (0.0,0.0)
33            - Point( '((x,y),(x,y))' )          parse from string, x and y are floats
34            - Box( 'BOX(x y,x y)' )             parse from string, x and y are floats
35            - Box( Point(x,y), Point(x,y) )     set lower left and upper right points
36
37        Constructor. Optional (arg s) is a string as returned by postgres.
38        It is of the form '(x,y)' where x and y are floating point numbers.
39        """
40
41        if len(args) == 0:
42            self.setX(0.0)
43            self.setY(0.0)
44        elif len(args) == 1:
45
46             # avoid api breakage, args[0] may still be None
47            if args[0] == None:
48                self.setX(0.0)
49                self.setY(0.0)
50            else:
51                self.fromString(args[0])
52
53        elif len(args) == 2:
54            self.setX(args[0])
55            self.setY(args[1])
56        else:
57            raise ValueError, "wrong number of arguments"
58
59
60    def fromString(self,s):
61        """
62        Initialise the Point from a string.
63
64        (arg s) should be of the form '(x,y)' where x and y are floating
65        point numbers.
66        """
67        seq = eval(s,{},{})
68        self.setX(seq[0])
69        self.setY(seq[1])
70       
71    def setX(self,x):
72        """
73        Set the X coord of the Point.
74
75        (arg x) is a float.
76        """
77        self._x = x
78
79    def getX(self):
80        """
81        Return the X coord of the Point.
82        """
83        return self._x
84
85    def setY(self,y):
86        """
87        Set the Y coord of the Point.
88
89        (arg x) is a float.
90        """
91
92        self._y = y
93
94    def getY(self):
95        """
96        Return the X coord of the Point.
97        """
98
99        return self._y
100
101    def __str__(self):
102        """
103        Generate a string representation of the Point that is
104        suitable to use in a Postgres query.
105        """
106        return "'(%f,%f)'" % (self.getX(), self.getY())
107
108    def __repr__(self):
109        """
110        Generate a represention of the Point as a string
111        suitable for 'evaling' as a tuple.
112        """
113        if self._x is None or self._y is None:
114            return "(%s,%s)" % (self._x,self._y)
115        return "(%f,%f)" % (self.getX(), self.getY())
116
117    def __eq__(self,other):
118        """
119        Support equality operations.
120
121        A Point is equal to another point is X == other.X and Y = other.Y.
122        """
123        if (type(self) != type(other)):
124            return False
125
126        if self.getX() == other.getX() and \
127           self.getY() == other.getY():
128            return True
129        return False
130
131    def __ne__(self,other):
132        return not self.__eq__(other)
133
134# Some helpful factory methods
135
136def pointFromValues(x,y):
137    """
138    Return a Point object.
139
140    (arg x) is the X coord as a float
141    (arg y) is the Y coord as a float
142    """
143   
144    p = Point()
145    p.setX(x)
146    p.setY(y)
147    return p
148
149def pointFromSequence(seq):
150    """
151    Return a Point object.
152
153    (arg seq) is a sequence of the form (x,y) where x is the X coord as a float
154    and y is the Y coord as a float.
155    """
156
157    p = Point()
158    p.setX(seq[0])
159    p.setY(seq[1])
160    return p
Note: See TracBrowser for help on using the repository browser.