OpenLayers OpenLayers

root/trunk/openlayers/tests/Format/GML.html

Revision 7955, 27.8 kB (checked in by tschaub, 4 months ago)

Adding support to the gml parser for serializing OpenLayers.Bounds as gml:Box. This gives the filter format (and the sld format) the ability to write spatial filters (that have bounds as a value). r=me (closes #1543)

  • Property svn:eol-style set to native
Line 
1 <html> 
2 <head> 
3     <script src="../../lib/OpenLayers.js"></script>
4     <script type="text/javascript">
5
6     function test_Format_GML_constructor(t) {
7         t.plan(4);
8          
9         var options = {'foo': 'bar'};
10         var format = new OpenLayers.Format.GML(options);
11         t.ok(format instanceof OpenLayers.Format.GML,
12              "new OpenLayers.Format.GML returns object" );
13         t.eq(format.foo, "bar", "constructor sets options correctly");
14         t.eq(typeof format.read, "function", "format has a read function");
15         t.eq(typeof format.write, "function", "format has a write function");
16     }
17     function test_Format_GML_getFid(t) {
18         t.plan(2);
19         var parser = new OpenLayers.Format.GML();
20         data = parser.read(test_content[1]);
21         t.eq(data[0].fid, '221', 'fid on polygons set correctly (with whitespace)');
22         t.eq(data[1].fid, '8', 'fid on linestrings set correctly with whitespace');
23     }
24     function test_Format_GML_no_clobber(t) {
25         t.plan(1);
26         var parser = new OpenLayers.Format.GML();
27         data = parser.read(test_content[1]);
28         t.eq(window.i, undefined,
29              "i is undefined in window scope after reading.");
30     }
31     function test_Format_GML_read_3d(t) {
32         t.plan(2);
33         var parser = new OpenLayers.Format.GML();
34         data = parser.read(test_content[0]);
35         t.eq(data[0].geometry.getBounds().toBBOX(), "-1254041.389712,250906.951598,-634517.119991,762236.29408", "Reading 3d content returns geometry with correct bounds (no 0,0)");
36         t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.Polygon", "Reading 3d content returns polygon geometry");
37     }   
38     function test_Format_GML_write_geoms(t) {
39         t.plan(5);
40         var parser = new OpenLayers.Format.GML();
41        
42         var point = shell_start + serialize_geoms['point'] + shell_end;
43         data = parser.read(point);
44         var output = parser.write(data);
45         var output = output.replace(/<\?[^>]*\?>/, ''); // Remove XML Prolog
46         t.eq(output, point, "Point geometry round trips correctly.");
47        
48         var linestring = shell_start + serialize_geoms['linestring'] + shell_end;
49         data = parser.read(linestring);
50         var output = parser.write(data);
51         var output = output.replace(/<\?[^>]*\?>/, ''); // Remove XML Prolog
52         t.eq(output, linestring, "Line geometry round trips correctly.");
53        
54         var polygon = shell_start + serialize_geoms['polygon'] + shell_end;
55         data = parser.read(polygon);
56         var output = parser.write(data);
57         output = output.replace(/<\?[^>]*\?>/, ''); // Remove XML Prolog
58         t.eq(output, polygon, "Poly geometry round trips correctly.");
59        
60         var multipoint = shell_start + serialize_geoms['multipoint'] + shell_end;
61         data = parser.read(multipoint);
62         var output = parser.write(data);
63         var output = output.replace(/<\?[^>]*\?>/, ''); // Remove XML Prolog
64         t.eq(output, multipoint, "MultiPoint geometry round trips correctly.");
65        
66         var multilinestring = shell_start + serialize_geoms['multilinestring'] + shell_end;
67         data = parser.read(multilinestring);
68         var output = parser.write(data);
69         var output = output.replace(/<\?[^>]*\?>/, ''); // Remove XML Prolog
70         t.eq(output, multilinestring, "MultiLine geometry round trips correctly.");
71
72     }   
73     function test_Format_GML_read_point_geom(t) {
74         t.plan(3);
75        
76         var point = shell_start + geoms['point'] + shell_end;
77         var parser = new OpenLayers.Format.GML();
78         data = parser.read(point);
79         t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.Point", "Point GML returns correct classname");
80         t.eq(data[0].geometry.x, 1, "x coord correct");
81         t.eq(data[0].geometry.y, 2, "y coord correct");
82     }
83     function test_Format_GML_read_linestring_geom(t) {
84         t.plan(5);
85        
86         var line = shell_start + geoms['linestring'] + shell_end;
87         var parser = new OpenLayers.Format.GML();
88         data = parser.read(line);
89         t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.LineString", "LineString GML returns correct classname");
90         t.eq(data[0].geometry.components[0].x, 1, "first x coord correct");
91         t.eq(data[0].geometry.components[0].y, 2, "first y coord correct");
92         t.eq(data[0].geometry.components[1].x, 4, "second x coord correct");
93         t.eq(data[0].geometry.components[1].y, 5, "second y coord correct");
94     }   
95     function test_Format_GML_read_polygon_geom(t) {
96         t.plan(7);
97        
98         var polygon = shell_start + geoms['polygon'] + shell_end;
99         var parser = new OpenLayers.Format.GML();
100         data = parser.read(polygon);
101         t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.Polygon", "Polygon GML returns correct classname");
102         t.eq(data[0].geometry.components[0].components[0].x, 1, "first x coord correct");
103         t.eq(data[0].geometry.components[0].components[0].y, 2, "first y coord correct");
104         t.eq(data[0].geometry.components[0].components[1].x, 4, "second x coord correct");
105         t.eq(data[0].geometry.components[0].components[1].y, 5, "second y coord correct");
106         t.eq(data[0].geometry.components[0].components.length, 4, "coords length correct");
107         t.eq(data[0].geometry.components.length, 1, "rings length correct");
108     }   
109     function test_Format_GML_read_multipoint_geom(t) {
110         t.plan(6);
111        
112         var multipoint = shell_start + geoms['multipoint'] + shell_end;
113         var parser = new OpenLayers.Format.GML();
114         data = parser.read(multipoint);
115         t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.MultiPoint", "MultiPoint GML returns correct classname");
116         t.eq(data[0].geometry.components[0].x, 1, "x coord correct");
117         t.eq(data[0].geometry.components[0].y, 2, "y coord correct");
118         t.eq(data[0].geometry.components.length, 2, "length correct");
119         t.eq(data[0].geometry.components[1].x, 4, "x coord correct");
120         t.eq(data[0].geometry.components[1].y, 5, "y coord correct");
121     }   
122     function test_Format_GML_read_multilinestring_geom(t) {
123         t.plan(6);
124        
125         var multilinestring = shell_start + geoms['multilinestring'] + shell_end;
126         var parser = new OpenLayers.Format.GML();
127         data = parser.read(multilinestring);
128         t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.MultiLineString", "MultiLineString GML returns correct classname");
129         t.eq(data[0].geometry.components[0].components[0].x, 1, "x coord correct");
130         t.eq(data[0].geometry.components[0].components[0].y, 2, "y coord correct");
131         t.eq(data[0].geometry.components[0].components.length, 2, "length correct");
132         t.eq(data[0].geometry.components[0].components[1].x, 4, "x coord correct");
133         t.eq(data[0].geometry.components[0].components[1].y, 5, "y coord correct");
134        
135     }   
136     function test_Format_GML_read_polygon_with_holes_geom(t) {
137         t.plan(12);
138        
139         var polygon_with_holes = shell_start + geoms['polygon_with_holes'] + shell_end;
140         var parser = new OpenLayers.Format.GML();
141         data = parser.read(polygon_with_holes);
142         t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.Polygon", "Polygon GML returns correct classname");
143         t.eq(data[0].geometry.components[0].components[0].x, 1, "first x coord correct");
144         t.eq(data[0].geometry.components[0].components[0].y, 2, "first y coord correct");
145         t.eq(data[0].geometry.components[0].components[1].x, 4, "second x coord correct");
146         t.eq(data[0].geometry.components[0].components[1].y, 5, "second y coord correct");
147         t.eq(data[0].geometry.components[0].components.length, 4, "coords length correct");
148         t.eq(data[0].geometry.components[1].components[0].x, 11, "first x coord correct");
149         t.eq(data[0].geometry.components[1].components[0].y, 12, "first y coord correct");
150         t.eq(data[0].geometry.components[1].components[1].x, 14, "second x coord correct");
151         t.eq(data[0].geometry.components[1].components[1].y, 15, "second y coord correct");
152         t.eq(data[0].geometry.components[1].components.length, 4, "coords length correct");
153         t.eq(data[0].geometry.components.length, 2, "rings length correct");
154     }
155     function test_Format_GML_read_attributes(t) {
156         t.plan(2);
157         var parser = new OpenLayers.Format.GML();
158         data = parser.read(test_content[0]);
159         t.eq(data[0].attributes['NAME'], "WY", "Simple Attribute data is read correctly.");
160         t.eq(data[0].attributes['LONGNAME'], "Wyoming", "Attribute data is read from CDATA node correctly.");
161     }
162     function test_Format_GML_read_envelope_geom(t) {
163         t.plan(13);
164        
165         var envelope = shell_start + geoms['envelope'] + shell_end;
166         var parser = new OpenLayers.Format.GML();
167         data = parser.read(envelope);
168         t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.Polygon", "Envelope GML returns correct classname");
169         t.eq(data[0].geometry.components[0].components[0].x, 0, "first x coord correct");
170         t.eq(data[0].geometry.components[0].components[0].y, 1, "first y coord correct");
171         t.eq(data[0].geometry.components[0].components[1].x, 20, "second x coord correct");
172         t.eq(data[0].geometry.components[0].components[1].y, 1, "second y coord correct");
173         t.eq(data[0].geometry.components[0].components[2].x, 20, "third x coord correct");
174         t.eq(data[0].geometry.components[0].components[2].y, 21, "third y coord correct");
175         t.eq(data[0].geometry.components[0].components[3].x, 0, "fouth x coord correct");
176         t.eq(data[0].geometry.components[0].components[3].y, 21, "fourth y coord correct");
177         t.eq(data[0].geometry.components[0].components[4].x, 0, "fifth x coord correct");
178         t.eq(data[0].geometry.components[0].components[4].y, 1, "fifth y coord correct");
179         t.eq(data[0].geometry.components[0].components.length, 5, "coords length correct");
180         t.eq(data[0].geometry.components.length, 1, "rings length correct");
181     }   
182     ///////////////////////////////////////////////////////////////
183     // tests the y x order of gml point
184     /////////////////////////////////////////////////////////////
185     function test_Format_GML_read_point_geom_yx(t) {
186         t.plan(3);
187        
188         var point = shell_start + geoms_yx['point'] + shell_end;
189         var parser = new OpenLayers.Format.GML({'xy':false});
190         data = parser.read(point);
191         t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.Point", "Point GML returns correct classname");
192         t.eq(data[0].geometry.x, 1, "x coord correct");
193         t.eq(data[0].geometry.y, 2, "y coord correct");
194     }
195     function test_Format_GML_read_linestring_geom_yx(t) {
196         t.plan(5);
197        
198         var line = shell_start + geoms_yx['linestring'] + shell_end;
199         var parser = new OpenLayers.Format.GML({'xy':false});
200         data = parser.read(line);
201         t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.LineString", "LineString GML returns correct classname");
202         t.eq(data[0].geometry.components[0].x, 1, "first x coord correct");
203         t.eq(data[0].geometry.components[0].y, 2, "first y coord correct");
204         t.eq(data[0].geometry.components[1].x, 4, "second x coord correct");
205         t.eq(data[0].geometry.components[1].y, 5, "second y coord correct");
206     }   
207     function test_Format_GML_read_polygon_geom_yx(t) {
208         t.plan(7);
209        
210         var polygon = shell_start + geoms_yx['polygon'] + shell_end;
211         var parser = new OpenLayers.Format.GML({'xy':false});
212         data = parser.read(polygon);
213         t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.Polygon", "Polygon GML returns correct classname");
214         t.eq(data[0].geometry.components[0].components[0].x, 1, "first x coord correct");
215         t.eq(data[0].geometry.components[0].components[0].y, 2, "first y coord correct");
216         t.eq(data[0].geometry.components[0].components[1].x, 4, "second x coord correct");
217         t.eq(data[0].geometry.components[0].components[1].y, 5, "second y coord correct");
218         t.eq(data[0].geometry.components[0].components.length, 4, "coords length correct");
219         t.eq(data[0].geometry.components.length, 1, "rings length correct");
220     }   
221     function test_Format_GML_read_multipoint_geom_yx(t) {
222         t.plan(6);
223        
224         var multipoint = shell_start + geoms_yx['multipoint'] + shell_end;
225         var parser = new OpenLayers.Format.GML({'xy':false});
226         data = parser.read(multipoint);
227         t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.MultiPoint", "MultiPoint GML returns correct classname");
228         t.eq(data[0].geometry.components[0].x, 1, "x coord correct");
229         t.eq(data[0].geometry.components[0].y, 2, "y coord correct");
230         t.eq(data[0].geometry.components.length, 2, "length correct");
231         t.eq(data[0].geometry.components[1].x, 4, "x coord correct");
232         t.eq(data[0].geometry.components[1].y, 5, "y coord correct");
233     }   
234     function test_Format_GML_read_multilinestring_geom_yx(t) {
235         t.plan(6);
236        
237         var multilinestring = shell_start + geoms_yx['multilinestring'] + shell_end;
238         var parser = new OpenLayers.Format.GML({'xy':false});
239         data = parser.read(multilinestring);
240         t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.MultiLineString", "MultiLineString GML returns correct classname");
241         t.eq(data[0].geometry.components[0].components[0].x, 1, "x coord correct");
242         t.eq(data[0].geometry.components[0].components[0].y, 2, "y coord correct");
243         t.eq(data[0].geometry.components[0].components.length, 2, "length correct");
244         t.eq(data[0].geometry.components[0].components[1].x, 4, "x coord correct");
245         t.eq(data[0].geometry.components[0].components[1].y, 5, "y coord correct");
246        
247     }   
248     function test_Format_GML_read_polygon_with_holes_geom_yx(t) {
249         t.plan(12);
250        
251         var polygon_with_holes = shell_start + geoms_yx['polygon_with_holes'] + shell_end;
252         var parser = new OpenLayers.Format.GML({'xy':false});
253         data = parser.read(polygon_with_holes);
254         t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.Polygon", "Polygon GML returns correct classname");
255         t.eq(data[0].geometry.components[0].components[0].x, 1, "first x coord correct");
256         t.eq(data[0].geometry.components[0].components[0].y, 2, "first y coord correct");
257         t.eq(data[0].geometry.components[0].components[1].x, 4, "second x coord correct");
258         t.eq(data[0].geometry.components[0].components[1].y, 5, "second y coord correct");
259         t.eq(data[0].geometry.components[0].components.length, 4, "coords length correct");
260         t.eq(data[0].geometry.components[1].components[0].x, 11, "first x coord correct");
261         t.eq(data[0].geometry.components[1].components[0].y, 12, "first y coord correct");
262         t.eq(data[0].geometry.components[1].components[1].x, 14, "second x coord correct");
263         t.eq(data[0].geometry.components[1].components[1].y, 15, "second y coord correct");
264         t.eq(data[0].geometry.components[1].components.length, 4, "coords length correct");
265         t.eq(data[0].geometry.components.length, 2, "rings length correct");
266     }
267
268     function test_Format_GML_read_envelope_geom_yx(t) {
269         t.plan(13);
270        
271         var envelope = shell_start + geoms_yx['envelope'] + shell_end;
272         var parser = new OpenLayers.Format.GML({'xy':false});
273         data = parser.read(envelope);
274         t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.Polygon", "Envelope GML returns correct classname");
275         t.eq(data[0].geometry.components[0].components[0].x, 0, "first x coord correct");
276         t.eq(data[0].geometry.components[0].components[0].y, 1, "first y coord correct");
277         t.eq(data[0].geometry.components[0].components[1].x, 20, "second x coord correct");
278         t.eq(data[0].geometry.components[0].components[1].y, 1, "second y coord correct");
279         t.eq(data[0].geometry.components[0].components[2].x, 20, "third x coord correct");
280         t.eq(data[0].geometry.components[0].components[2].y, 21, "third y coord correct");
281         t.eq(data[0].geometry.components[0].components[3].x, 0, "fouth x coord correct");
282         t.eq(data[0].geometry.components[0].components[3].y, 21, "fourth y coord correct");
283         t.eq(data[0].geometry.components[0].components[4].x, 0, "fifth x coord correct");
284         t.eq(data[0].geometry.components[0].components[4].y, 1, "fifth y coord correct");
285         t.eq(data[0].geometry.components[0].components.length, 5, "coords length correct");
286         t.eq(data[0].geometry.components.length, 1, "rings length correct");
287     }
288    
289     function test_Format_GML_write_geoms_yx(t) {
290         t.plan(5);
291         var parser = new OpenLayers.Format.GML({'xy':false});
292        
293         var point_xy = shell_start + serialize_geoms['point'] + shell_end;
294         var point = shell_start + serialize_geoms_yx['point'] + shell_end;
295         data = parser.read(point);
296         var output = parser.write(data);
297         var output = output.replace(/<\?[^>]*\?>/, ''