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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/services/3rdParty/GeoTypes-0.6.0/GeoTypes/_Circle.py@2194
Revision 2194, 3.6 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 Circle.
10"""
11
12from _Point import Point, pointFromSequence
13
14class Circle:
15    """
16    Circle provides an implementation for a circle in 2d space.
17
18    It has accessor functions for a centre Point and radius size,
19    provides a constructor that can handle the strings returned from
20    Postgres for the circle type and support equallity operations
21    with other Circle objects.
22    """
23   
24    def __init__(self,s=None):
25        """
26        Constructor. Optional (arg s) is a string as returned by postgres.
27        It is of the form '<(x.y),r>' where x,y and r are floating point numbers.
28
29        If (arg s) is None or omitted the Circle is initalised to <(0.0,0.0),0>.
30        """
31        if s:
32            self.fromString(s)
33        else:
34            self.setCentre(Point())
35            self.setRadius(0.0)
36
37    def fromString(self,s):
38        """
39        Initalise the Circle from a string.
40
41        (arg s) should be of the form '<(x,y),r>' although this
42        method will also work with the form '((x,y),r)'.
43        """
44        # circles come out of the db as '<(x,y),r>'
45        # so we must first convert them to '((x,y),r'
46        s = s.replace('<','(')
47        s = s.replace('>',')')       
48        seq = eval(s,{},{})
49        self.setCentre(pointFromSequence(seq[0]))
50        self.setRadius(seq[1])
51
52    def setCentre(self,p):
53        """
54        Set the centre Point of the circle.
55
56        (arg p) is a Point.
57        """
58        self._centre = p
59
60    def getCentre(self):
61        """
62        Return the centre Point of the circle.
63        """
64        return self._centre
65
66    def setRadius(self,r):
67        """
68        Set the radius of the circle.
69
70        (arg r) is a float.
71        """
72        self._radius = r
73
74    def getRadius(self):
75        """
76        Return the radius of the circle.
77        """
78        return self._radius
79
80    def __str__(self):
81        """
82        Generate a string representation of the Circle that is
83        suitable to use in a Postgres query.
84        """
85        return "'<%s,%f>'" % (self.getCentre().__repr__(),
86                              self.getRadius())
87   
88    def __repr__(self):
89        """
90        Generate a represention of the Circle as a string
91        suitable for 'evaling' as a tuple.
92        """
93        return "(%s,%f)" % (self.getCentre().__repr__(),
94                            self.getRadius())
95
96    def __eq__(self,other):
97        """
98        Support for equality operations.
99
100        A Circle is equal to another Circle if the centre Points are equal
101        and the radius is equal.
102        """
103        if (type(self) != type(other)):
104            return False
105       
106        if self.getCentre() == other.getCentre() and \
107               self.getRadius() == other.getRadius():
108            return True
109        return False
110
111    def __ne__(self,other):
112        return not self.__eq__(other)
113
114
115# factory methods
116
117def circleFromCentreAndRadius(centre,radius):
118    """
119    Return a Circle.
120
121    (arg centre) is a Point.
122    (arg radius) is a float.
123    """
124    c = Circle()
125    c.setCentre(centre)
126    c.setRadius(radius)
127    return c
128
129def circelFromSequence(seq):
130    """
131    Return a Circle.
132
133    (arg seq) is a sequence of the form ((x,y),r) where x,y and r
134    are floats.
135    """
136    c = Circle()
137    c.setCentre(pointFromSequence(seq[0]))
138    c.setRadius(seq[1])
139    return c
140               
Note: See TracBrowser for help on using the repository browser.