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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/services/3rdParty/GeoTypes-0.6.0/GeoTypes/_OGLineString.py@2194
Revision 2194, 3.9 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
8import types, string
9
10from _OGCurve import OGCurve
11from _OGPoint import OGPoint, OGpointFromSequence
12
13class OGLineString(OGCurve):
14    """
15    OGLineString provides and implementation of a sequence of connected OGPoints in 2 or 3d space.
16
17    It implements most of the methods of a container class and should be usable as if
18    it were a list of OGPoints.
19    """
20   
21    def __init__(self,srid=None):
22        OGCurve.__init__(self,srid)
23        self.emptyPointList()
24
25    def emptyPointList(self):
26        """
27        Reset the LineString to have no Points.
28        """
29        self._points = []
30
31    def getPoints(self):
32        """
33        Return a list of all the points on the LineString.
34        """
35        return self._points
36
37    def append(self, p):
38        self.getPoints().append(p)
39       
40    def count(self, p):
41        return self.getPoints().count(p)
42       
43    def index(self, p):
44        return self.getPoints().index(p)
45       
46    def extend(self, path):
47        self.getPoints().extend(path._points)
48       
49    def insert(self,pos,p):
50        self.getPoints().insert(pos,p)
51       
52    def pop(self):
53        return self.getPoints().pop()
54   
55    def remove(self,p):
56        self.getPoints().remove(p)
57
58    def __len__(self):
59        return len(self.getPoints())
60   
61    def __getitem__(self, key):
62        return self.getPoints()[key]
63   
64    def __setitem__(self, key, value):
65        # check that value is a Point!       
66        self.getPoints()[key] = value
67       
68    def __delitem__(self, key):
69        del self.getPoints()[key]
70   
71    def __iter__(self):
72
73        class __iter__:
74            def __init__(self,points):
75                self._points = points
76                self.pos = 0
77
78            def __iter__(self): return self
79
80            def next(self):
81                if self.pos == len(self._points):
82                    raise StopIteration
83               
84                point = self._points[self.pos]
85                self.pos = self.pos + 1
86                return point
87
88        return __iter__(self.getPoints())
89       
90    def __contains__(self, p):
91        return p in self.getPoints()
92     
93    def _og_str(self):
94        s = string.join([ point.__repr__() for point in self.getPoints() ],',')
95           
96        return "LINESTRING (%s)" % (s,)
97   
98    def __repr__(self):
99        s = string.join([ point.__repr__() for point in self.getPoints() ],',')
100           
101        return "(%s)" % (s,)
102
103    def __eq__(self,other):
104        """
105        Equality for paths means that:
106             both paths are either open or closed.
107             both paths have the same number of points
108             all points are equal
109       
110        """
111        if (type(self) != type(other)):
112            return False
113
114        if not len(self) == len(other):
115            return False
116
117        count = 0
118        while count < len(self):
119            if self[count] != other[count]: return False
120            count = count + 1
121
122        return True
123           
124    def __ne__(self,other):
125        return not self.__eq__(other)
126
127
128# factory methods
129
130def OGlineStringFromOGPoints(*points):
131    """
132    Return a OGLineString.
133
134    (arg *points) any number of OGPoints that make up the OGLineString.
135
136    """
137    linestring = OGLineString()
138    for point in points:
139        linestring.append(point)
140       
141    return linestring
142
143def OGlineStringFromSequence(seq):
144    """
145    Return a OGLineString.
146
147    (arg *seq) a sequence of the form ((x,y),...,(x,y)) that make up the OGLineString,
148    where x and y are floats.
149
150    """
151
152    linestring = OGLineString()
153    for point in seq:
154        linestring.append(OGpointFromSequence(point))
155       
156    return linestring
Note: See TracBrowser for help on using the repository browser.