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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/services/3rdParty/GeoTypes-0.6.0/GeoTypes/_OGPolygon.py@2194
Revision 2194, 3.8 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 string
9
10from _OGSurface import OGSurface
11from _OGLinearRing import OGlinearRingFromSequence
12
13class OGPolygon(OGSurface):
14    """
15    OGPloygon provides an implementation of a sequence of OGLinearRing 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 OGLinearRings.
19    """
20   
21    def __init__(self,srid=None):
22        OGSurface.__init__(self,srid)
23        self.emptyLineList()
24
25    def emptyLineList(self):
26        """
27        Reset the Ploygon to have no Lines.
28        """
29        self._lines = []
30
31    def getLines(self):
32        """
33        Return a list of all the points on the Polygon.
34        """
35        return self._lines
36
37    def append(self, p):
38        self.getLines().append(p)
39       
40    def count(self, p):
41        return self.getLines().count(p)
42       
43    def index(self, p):
44        return self.getLines().index(p)
45       
46    def extend(self, path):
47        self.getLines().extend(path._lines)
48       
49    def insert(self,pos,p):
50        self.getLines().insert(pos,p)
51       
52    def pop(self):
53        return self.getLines().pop()
54   
55    def remove(self,p):
56        self.getLines().remove(p)
57
58    def __len__(self):
59        return len(self.getLines())
60   
61    def __getitem__(self, key):
62        return self.getLines()[key]
63   
64    def __setitem__(self, key, value):
65        # check that value is a Line!       
66        self.getLines()[key] = value
67       
68    def __delitem__(self, key):
69        del self.getLines()[key]
70   
71    def __iter__(self):
72
73        class __iter__:
74            def __init__(self,lines):
75                self._lines = lines
76                self.pos = 0
77
78            def __iter__(self): return self
79
80            def next(self):
81                if self.pos == len(self._lines):
82                    raise StopIteration
83               
84                line = self._lines[self.pos]
85                self.pos = self.pos + 1
86                return line
87
88        return __iter__(self.getLines())
89       
90    def __contains__(self, p):
91        return l in self.getLines()
92     
93    def _og_str(self):
94        s = string.join([ line.__repr__() for line in self.getLines() ],',')
95           
96        return "POLYGON (%s)" % (s,)
97   
98    def __repr__(self):
99        s = string.join([ line.__repr__() for line in self.getLines() ],',')
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 OGpolygonFromOGLines(*linearrings):
131    """
132    Return a OGPolygon.
133
134    (arg *linearrings) any number of OGLinearRings that make up the OGPolygon.
135
136    """
137    polygon = OGPolygon()
138    for line in linearrings:
139        polygon.append(line)
140       
141    return polygon
142
143def OGpolygonFromSequence(seq):
144    """
145    Return a OGPolygon.
146
147    (arg *seq) a sequence of the form (((x,y),...,(x,y)),((x,y),...,(x,y))) that make up the OGPolygon,
148    where x and y are floats.
149
150    """
151
152    polygon = OGPolygon()
153    for line in seq:
154        polygon.append(OGlinearRingFromSequence(line))
155       
156    return polygon
157
Note: See TracBrowser for help on using the repository browser.