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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/services/3rdParty/GeoTypes-0.6.0/GeoTypes/_LineSeg.py@2194
Revision 2194, 2.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 LineSeg.
10"""
11
12from _Point import Point, pointFromSequence
13
14class LineSeg:
15    """
16    LineSeg provides an interface for a line segment in 2d space.
17
18    It has accessor functions for start and end Points, provides a constructor
19    that can handle the strings returned from Postgres for the lseg
20    type and support equallity operations with other LinSeg objects.
21    """
22
23    def __init__(self,s=None):
24        """
25        Constructor. Optional (arg s) is a string as returned by postgres.
26        It is of the form '((start_x.start_y),(end_x,end_y))' where x and y
27        are floating point numbers.
28
29        If (arg s) is None or omitted the LineSeg is initalised to ((0.0,0.0),(0.0,0.0)).
30        """
31
32        if s:
33            self.fromString(s)
34        else:
35            self.setStart(Point())
36            self.setEnd(Point())
37
38    def fromString(self,s):
39        seq = eval(s,{},{})
40        self.setStart(pointFromSequence(seq[0]))
41        self.setEnd(pointFromSequence(seq[1]))
42
43    def setStart(self,p):
44        self._start = p
45
46    def getStart(self):
47        return self._start
48
49    def setEnd(self,p):
50        self._end = p
51
52    def getEnd(self):
53        return self._end
54
55    def __str__(self):
56        return "'(%s,%s)'" % (self.getStart().__repr__(),
57                              self.getEnd().__repr__())
58   
59    def __repr__(self):
60        return "(%s,%s)" % (self.getStart().__repr__(),
61                            self.getEnd().__repr__())
62
63    def __eq__(self,other):
64        """
65        Two LineSegs are considered equal if thier start and end Points
66        are the same.
67        """
68        if (type(self) != type(other)):
69            return False
70       
71        if self.getStart() == other.getStart() and \
72           self.getEnd() == other.getEnd():
73            return True
74        return False
75
76    def __ne__(self,other):
77        return not self.__eq__(other)
78
79
80# factory methods
81
82def lineSegFromPoints(start,end):
83    """
84    Return a LineSeg.
85
86    (arg start) is a Point object.
87    (arg end) is a Point object.
88    """
89    l = LineSeg()
90    l.setStart(start)
91    l.setEnd(end)
92    return l
93
94def lineSegFromSequence(seq):
95    """
96    Return a LineSeg.
97
98    (arg seq) is a sequence of the form '((start_x.start_y),(end_x,end_y))'.
99    """
100    l = LineSeg()
101    l.setStart(pointFromSequence(seq[0]))
102    l.setEnd(pointFromSequence(seq[1]))
103    return l
104               
Note: See TracBrowser for help on using the repository browser.