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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/services/3rdParty/GeoTypes-0.6.0/tests/EWKBParser_Test.py@2194
Revision 2194, 7.1 KB checked in by lawrence, 13 years ago (diff)

Adding various specs and 3rd party code of interest for the CSML
services development.

RevLine 
[2194]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 unittest
9import sys, traceback
10import psycopg
11import os
12
13import TestConfig
14
15if not os.environ.get('USECOVERAGE') == '1':
16    import GeoTypes
17
18           
19from _PsycopgInit import initialisePsycopgTypes
20
21dbname='mq_test'
22
23initialisePsycopgTypes(psycopg_module=psycopg,connect_string='dbname=%s user=postgres' % (dbname,),
24                       register_opengis_types=None)
25
26conn = psycopg.connect('dbname=%s user=postgres' % (dbname,))
27curs = conn.cursor()
28
29class EWKBParserTestBase(unittest.TestCase):
30
31     
32    def _testPoint2d(self):
33        geo = self._getGeom('SRID=4;POINT(0 0)')
34       
35        assert geo.__class__.__name__ == "OGPoint"
36        assert geo.SRID() == 4
37
38    def _testPoint3d(self):
39        geo = self._getGeom('SRID=4;POINT(1.0 2.0 3.0)')
40       
41        assert geo.__class__.__name__ == "OGPoint"
42        assert geo.getZ() == 3.0
43        assert geo.SRID() == 4
44
45    def _testLineString(self):
46        geo = self._getGeom('SRID=4;LINESTRING(0 0,1 1,1 2)')
47       
48        assert geo.__class__.__name__ == "OGLineString"
49        assert geo.SRID() == 4
50
51    def _testLineString3d(self):
52        geo = self._getGeom('SRID=4;LINESTRING(0 0 3.0,1 1 1,1 2 3)')
53       
54        assert geo.__class__.__name__ == "OGLineString"
55        assert geo[0].getZ() == 3.0
56        assert geo.SRID() == 4
57
58    def _testPolygon(self):
59        geo = self._getGeom('SRID=4;POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1))')
60       
61        assert geo.__class__.__name__ == "OGPolygon"
62        assert geo.SRID() == 4
63
64    def _testPolygon3d(self):
65        geo = self._getGeom('SRID=4;POLYGON((0 0 3.0,4 0 4,4 4 4,0 4 4,0 0 3.0),(1 1 4, 2 1 4, 2 2 4, 1 2 4,1 1 4))')
66       
67        assert geo.__class__.__name__ == "OGPolygon"
68        assert geo.SRID() == 4
69
70    def _testMultiPoint(self):
71        geo = self._getGeom('SRID=4;MULTIPOINT(0 0,1 2)')
72       
73        assert geo.__class__.__name__ == "OGMultiPoint"
74        assert geo.SRID() == 4
75
76    def _testMultiPoint3d(self):
77        geo = self._getGeom('SRID=4;MULTIPOINT(0 0 3.0,1 2 3.0)')
78       
79        assert geo.__class__.__name__ == "OGMultiPoint"
80        assert geo.SRID() == 4
81
82    def _testMultiLineString(self):
83        geo = self._getGeom('SRID=4;MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))')
84       
85        assert geo.__class__.__name__ == "OGMultiLineString"
86        assert geo.SRID() == 4
87
88    def _testMultiLineString3d(self):
89        geo = self._getGeom('SRID=4;MULTILINESTRING((0 0 0,1 1 1,1 2 2),(2 3 3,3 2 4,5 4 5))')
90       
91        assert geo.__class__.__name__ == "OGMultiLineString"
92        assert geo.SRID() == 4
93
94    def _testMultiPolygon(self):
95        geo = self._getGeom('SRID=4;MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))')
96       
97        assert geo.__class__.__name__ == "OGMultiPolygon"
98        assert geo.SRID() == 4
99
100    def _testMultiPolygon3d(self):
101        geo = self._getGeom('SRID=4;MULTIPOLYGON(((0 0 0,4 0 4,4 4 4,0 4 4,0 0 0),(1 1 1,2 1 1,2 2 1,1 2 3,1 1 1)), ((-1 -1 -1,-1 -2 -1,-2 -2 -2,-2 -1 -2,-1 -1 -1)))')
102       
103        assert geo.__class__.__name__ == "OGMultiPolygon"
104        assert geo.SRID() == 4
105
106    def _testGeometryCollection(self):
107        geo = self._getGeom('SRID=4;GEOMETRYCOLLECTION(POINT(2 3))')
108       
109        assert geo.__class__.__name__ == "OGGeometryCollection"
110        assert geo.SRID() == 4
111
112
113class EWKBParserTest(EWKBParserTestBase):
114
115    def setUp(self):
116        self.factory = GeoTypes.OGGeoTypeFactory()
117        self.parser = GeoTypes.EWKBParser(self.factory)
118   
119    def _tearDown(self):
120        pass
121   
122
123    def _getGeom(self,query):
124        try:
125            curs.execute("""DECLARE zot BINARY CURSOR FOR SELECT '%s'::geometry FOR READ ONLY""" \
126                         % (query,))
127            curs.execute("""FETCH ALL FROM zot""")
128            f = curs.fetchall()
129            self.parser.parseGeometry(f[0][0])
130        finally:
131            conn.commit()
132
133        return self.factory.getGeometry()
134
135    testPoint2d = EWKBParserTestBase._testPoint2d
136
137    testPoint3d = EWKBParserTestBase._testPoint3d
138
139    testLineString = EWKBParserTestBase._testLineString
140
141    testLineString3d = EWKBParserTestBase._testLineString3d
142
143    testPolygon = EWKBParserTestBase._testPolygon
144
145    testPolygon3d = EWKBParserTestBase._testPolygon3d
146
147    testMultiPoint = EWKBParserTestBase._testMultiPoint
148
149    testMultiPoint3d = EWKBParserTestBase._testMultiPoint3d
150
151    testMultiLineString = EWKBParserTestBase._testMultiLineString
152
153    testMultiLineString3d = EWKBParserTestBase._testMultiLineString3d
154
155    testMultiPolygon = EWKBParserTestBase._testMultiPolygon
156
157    testMultiPolygon3d = EWKBParserTestBase._testMultiPolygon3d
158
159    testGeometryCollection = EWKBParserTestBase._testGeometryCollection
160
161class HEXEWKBParserTest(EWKBParserTestBase):
162
163    def setUp(self):
164        self.factory = GeoTypes.OGGeoTypeFactory()
165        self.parser = GeoTypes.HEXEWKBParser(self.factory)
166   
167    def _tearDown(self):
168        pass
169   
170
171    def _getGeom(self,query):
172        try:
173            curs.execute("""SELECT '%s'::geometry""" \
174                         % (query,))
175            f = curs.fetchall()
176            self.parser.parseGeometry(f[0][0])
177        finally:
178            conn.commit()
179
180        return self.factory.getGeometry()
181
182    testPoint2d = EWKBParserTestBase._testPoint2d
183
184    testPoint3d = EWKBParserTestBase._testPoint3d
185
186    testLineString = EWKBParserTestBase._testLineString
187
188    testLineString3d = EWKBParserTestBase._testLineString3d
189
190    testPolygon = EWKBParserTestBase._testPolygon
191
192    testPolygon3d = EWKBParserTestBase._testPolygon3d
193
194    testMultiPoint = EWKBParserTestBase._testMultiPoint
195
196    testMultiPoint3d = EWKBParserTestBase._testMultiPoint3d
197
198    testMultiLineString = EWKBParserTestBase._testMultiLineString
199
200    testMultiLineString3d = EWKBParserTestBase._testMultiLineString3d
201
202    testMultiPolygon = EWKBParserTestBase._testMultiPolygon
203
204    testMultiPolygon3d = EWKBParserTestBase._testMultiPolygon3d
205
206    testGeometryCollection = EWKBParserTestBase._testGeometryCollection
207
208def testSuite():
209    return unittest.TestSuite((
210        unittest.makeSuite(EWKBParserTest,'test'),
211        unittest.makeSuite(HEXEWKBParserTest,'test'),
212        ))
213
214if __name__ == "__main__":
215    import os
216    if os.environ.get('USECOVERAGE') == '1':
217        try:
218            import coverage
219            coverage.erase()
220            coverage.start()
221            COVERAGE = 1
222            import GeoTypes         
223
224        except:
225            print "Error setting up coverage checking"
226            COVERAGE = 0
227    else:
228        COVERAGE = 0
229
230    if os.environ.get('USEPYCHECK') == '1':
231        try:
232            import pychecker.checker
233        except:
234            print "Pychecker not installed on this machine"
235
236
237    unittest.TextTestRunner().run(testSuite())
238
239    if COVERAGE:
240        coverage.stop()
241        x = coverage.analysis(GeoTypes)
242        print "\n"
243        coverage.report(GeoTypes)
244
245
Note: See TracBrowser for help on using the repository browser.