OpenLayers OpenLayers

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

Revision 7536, 10.5 kB (checked in by pgiraud, 6 months ago)

removed extra comma (IE complains), no functional change

  • 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     var points = [];
7     for(var i=0; i<12; ++i) {
8         points.push(new OpenLayers.Feature.Vector(
9             new OpenLayers.Geometry.Point(Math.random() * 100,
10                                           Math.random() * 100))
11         );
12     }
13     var multipoint = new OpenLayers.Feature.Vector(
14         new OpenLayers.Geometry.MultiPoint([
15             points[0].geometry,
16             points[1].geometry,
17             points[2].geometry
18         ])
19     );
20      
21     var linestrings = [
22         new OpenLayers.Feature.Vector(
23             new OpenLayers.Geometry.LineString([
24                 points[0].geometry,
25                 points[1].geometry,
26                 points[2].geometry
27             ])
28         ),
29         new OpenLayers.Feature.Vector(
30             new OpenLayers.Geometry.LineString([
31                 points[3].geometry,
32                 points[4].geometry,
33                 points[5].geometry
34             ])
35         )
36     ];
37      
38     var multilinestring = new OpenLayers.Feature.Vector(
39         new OpenLayers.Geometry.MultiLineString([
40             linestrings[0].geometry,
41             linestrings[1].geometry
42         ])
43     );
44  
45     var rings = [
46         new OpenLayers.Geometry.LinearRing([
47             points[0].geometry,
48             points[1].geometry,
49             points[2].geometry
50         ]),
51         new OpenLayers.Geometry.LinearRing([
52             points[3].geometry,
53             points[4].geometry,
54             points[5].geometry
55         ]),
56         new OpenLayers.Geometry.LinearRing([
57             points[6].geometry,
58             points[7].geometry,
59             points[8].geometry
60         ]),
61         new OpenLayers.Geometry.LinearRing([
62             points[9].geometry,
63             points[10].geometry,
64             points[11].geometry
65         ])
66     ];
67  
68     var polygons = [
69         new OpenLayers.Feature.Vector(
70             new OpenLayers.Geometry.Polygon([rings[0], rings[1]])
71         ),
72         new OpenLayers.Feature.Vector(
73             new OpenLayers.Geometry.Polygon([rings[2], rings[3]])
74         )
75     ];
76      
77     var multipolygon = new OpenLayers.Feature.Vector(
78         new OpenLayers.Geometry.MultiPolygon([
79             polygons[0].geometry,
80             polygons[1].geometry
81         ])
82     );
83      
84     var collection = [points[0], linestrings[0]];
85      
86     function test_Format_WKT_constructor(t) {
87         t.plan(4);
88          
89         var options = {'foo': 'bar'};
90         var format = new OpenLayers.Format.WKT(options);
91         t.ok(format instanceof OpenLayers.Format.WKT,
92              "new OpenLayers.Format.WKT returns object" );
93         t.eq(format.foo, "bar", "constructor sets options correctly");
94         t.eq(typeof format.read, "function", "format has a read function");
95         t.eq(typeof format.write, "function", "format has a write function");
96     }
97
98     function test_Format_WKT_write(t) {
99         t.plan(7);
100
101         var format = new OpenLayers.Format.WKT();
102
103         // test a point
104        
105         t.eq(format.write(points[0]),
106              "POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")",
107              "format correctly writes Point WKT");
108  
109         // test a multipoint
110         t.eq(format.write(multipoint),
111              "MULTIPOINT(" + points[0].geometry.x + " " + points[0].geometry.y + "," +
112                              points[1].geometry.x + " " + points[1].geometry.y + "," +
113                              points[2].geometry.x + " " + points[2].geometry.y + ")",
114              "format correctly writes MultiPoint WKT");
115  
116         // test a linestring
117         t.eq(format.write(linestrings[0]),
118              "LINESTRING(" + points[0].geometry.x + " " + points[0].geometry.y + "," +
119                              points[1].geometry.x + " " + points[1].geometry.y + "," +
120                              points[2].geometry.x + " " + points[2].geometry.y + ")",
121              "format correctly writes LineString WKT");
122  
123         // test a multilinestring
124         t.eq(format.write(multilinestring),
125              "MULTILINESTRING((" + points[0].geometry.x + " " + points[0].geometry.y + "," +
126                                    points[1].geometry.x + " " + points[1].geometry.y + "," +
127                                    points[2].geometry.x + " " + points[2].geometry.y + ")," +
128                              "(" + points[3].geometry.x + " " + points[3].geometry.y + "," +
129                                    points[4].geometry.x + " " + points[4].geometry.y + "," +
130                                    points[5].geometry.x + " " + points[5].geometry.y + "))",
131              "format correctly writes MultiLineString WKT");
132  
133         // test a polygon
134         t.eq(format.write(polygons[0]),
135              "POLYGON((" + points[0].geometry.x + " " + points[0].geometry.y + "," +
136                            points[1].geometry.x + " " + points[1].geometry.y + "," +
137                            points[2].geometry.x + " " + points[2].geometry.y + "," +
138                            points[0].geometry.x + " " + points[0].geometry.y + ")," +
139                      "(" + points[3].geometry.x + " " + points[3].geometry.y + "," +
140                            points[4].geometry.x + " " + points[4].geometry.y + "," +
141                            points[5].geometry.x + " " + points[5].geometry.y + "," +
142                            points[3].geometry.x + " " + points[3].geometry.y + "))",
143              "format correctly writes Polygon WKT");
144  
145         // test a multipolygon
146         t.eq(format.write(multipolygon),
147              "MULTIPOLYGON(((" + points[0].geometry.x + " " + points[0].geometry.y + "," +
148                                  points[1].geometry.x + " " + points[1].geometry.y + "," +
149                                  points[2].geometry.x + " " + points[2].geometry.y + "," +
150                                  points[0].geometry.x + " " + points[0].geometry.y + ")," +
151                            "(" + points[3].geometry.x + " " + points[3].geometry.y + "," +
152                                  points[4].geometry.x + " " + points[4].geometry.y + "," +
153                                  points[5].geometry.x + " " + points[5].geometry.y + "," +
154                                  points[3].geometry.x + " " + points[3].geometry.y + "))," +
155                           "((" + points[6].geometry.x + " " + points[6].geometry.y + "," +
156                                  points[7].geometry.x + " " + points[7].geometry.y + "," +
157                                  points[8].geometry.x + " " + points[8].geometry.y + "," +
158                                  points[6].geometry.x + " " + points[6].geometry.y + ")," +
159                            "(" + points[9].geometry.x + " " + points[9].geometry.y + "," +
160                                  points[10].geometry.x + " " + points[10].geometry.y + "," +
161                                  points[11].geometry.x + " " + points[11].geometry.y + "," +
162                                  points[9].geometry.x + " " + points[9].geometry.y + ")))",
163              "format correctly writes MultiPolygon WKT");
164          
165         // test a geometrycollection
166         t.eq(format.write(collection),
167              "GEOMETRYCOLLECTION(POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")," +
168                                 "LINESTRING(" + points[0].geometry.x + " " + points[0].geometry.y + "," +
169                                                 points[1].geometry.x + " " + points[1].geometry.y + "," +
170                                                 points[2].geometry.x + " " + points[2].geometry.y + "))",
171              "format correctly writes GeometryCollection WKT");
172  
173     }
174
175     function test_Format_WKT_read(t) {
176         t.plan(7);
177
178         var format = new OpenLayers.Format.WKT();
179          
180         /**
181          * Since we're explicitly testing calls to write, the read tests
182          * just make sure that geometry can make a round trip from read to write.
183          */
184  
185         // test a point
186         t.ok(points[0].geometry.equals(format.read(format.write(points[0])).geometry),
187              "format correctly reads Point WKT");
188
189         // test a multipoint
190         t.ok(multipoint.geometry.equals(format.read(format.write(multipoint)).geometry),
191              "format correctly reads MultiPoint WKT");
192  
193         // test a linestring
194         t.ok(linestrings[0].geometry.equals(format.read(format.write(linestrings[0])).geometry),
195              "format correctly reads LineString WKT");
196  
197         // test a multilinestring
198         t.ok(multilinestring.geometry.equals(format.read(format.write(multilinestring)).geometry),
199              "format correctly reads MultiLineString WKT");
200  
201         // test a polygon
202         t.ok(polygons[0].geometry.equals(format.read(format.write(polygons[0])).geometry),
203              "format correctly reads Polygon WKT");
204  
205         // test a multipolygon
206         t.ok(multipolygon.geometry.equals(format.read(format.write(multipolygon)).geometry),
207              "format correctly reads MultiPolygon WKT");
208          
209         // test a geometrycollection
210         t.eq(format.write(collection),
211              "GEOMETRYCOLLECTION(POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")," +
212                                 "LINESTRING(" + points[0].geometry.x + " " + points[0].geometry.y + "," +
213                                                 points[1].geometry.x + " " + points[1].geometry.y + "," +
214                                                 points[2].geometry.x + " " + points[2].geometry.y + "))",
215              "format correctly writes GeometryCollection WKT");
216  
217     }
218
219     function test_Format_WKT_read_projection(t) {
220         t.plan(1);
221
222         var projections = {
223             src:  new OpenLayers.Projection("EPSG:4326"),
224             dest: new OpenLayers.Projection("EPSG:900913")
225         };
226
227         var points = {
228             src:  new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(-87.9, 41.9)),
229             dest: new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(-9784983.239366667, 5146011.678566458))
230         };
231
232         var format = new OpenLayers.Format.WKT({
233             externalProjection: projections["src"],
234             internalProjection: projections["dest"]
235         });
236         var feature = format.read("GEOMETRYCOLLECTION(POINT(" + points["src"].geometry.x + " " + points["src"].geometry.y + "))")[0];
237         t.eq(feature.geometry.toString(), points["dest"].geometry.toString(),
238             "Geometry collections aren't transformed twice when reprojection.");
239     }
240     </script>
241 </head> 
242 <body> 
243 </body> 
244 </html> 
Note: See TracBrowser for help on using the browser.