OpenLayers OpenLayers

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

Revision 8532, 10.0 kB (checked in by crschmidt, 3 weeks ago)

fix for KML element "fill" is unsupported, patch by rdewit, r=me, tests by me,
(Closes #1724)

  • 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 test_content = '<kml xmlns="http://earth.google.com/kml/2.0"><Folder><name>OpenLayers export</name><description>Vector geometries from OpenLayers</description><Placemark id="KML.Polygon"><name>OpenLayers.Feature.Vector_344</name><description>A KLM Polygon</description><Polygon><outerBoundaryIs><LinearRing><coordinates>5.001370157823406,49.26855713824488 8.214706453896161,49.630662409673505 8.397385910100951,48.45172350357396 5.001370157823406,49.26855713824488</coordinates></LinearRing></outerBoundaryIs></Polygon></Placemark><Placemark id="KML.LineString"><name>OpenLayers.Feature.Vector_402</name><description>A KML LineString</description><LineString><coordinates>5.838523393080493,49.74814616928052 5.787079558782349,48.410795432216574 8.91427702008381,49.28932499608202</coordinates></LineString></Placemark><Placemark id="KML.Point"><name>OpenLayers.Feature.Vector_451</name><description>A KML Point</description><Point><coordinates>6.985073041685488,49.8682250149058</coordinates></Point></Placemark><Placemark id="KML.MultiGeometry"><name>SF Marina Harbor Master</name><description>KML MultiGeometry</description><MultiGeometry><LineString><coordinates>-122.4425587930444,37.80666418607323 -122.4428379594768,37.80663578323093</coordinates></LineString><LineString><coordinates>-122.4425509770566,37.80662588061205 -122.4428340530617,37.8065999493009</coordinates></LineString></MultiGeometry></Placemark></Folder></kml>';
7     var test_style = '<kml xmlns="http://earth.google.com/kml/2.0"> <Placemark>    <Style> <LineStyle> <color>870000ff</color> <width>10</width> </LineStyle> </Style>  <LineString> <coordinates> -112,36 -113,37 </coordinates> </LineString> </Placemark></kml>';
8     var test_style_fill = '<kml xmlns="http://earth.google.com/kml/2.0"> <Placemark>    <Style> <PolyStyle> <fill>0</fill> <color>870000ff</color> <width>10</width> </PolyStyle> </Style>  <LineString> <coordinates> -112,36 -113,37 </coordinates> </LineString> </Placemark></kml>';
9     var test_nl = '<kml xmlns="http://earth.google.com/kml/2.2"> <Document> <NetworkLink> <Link> <href>http://maker.geocommons.com/maps/1717/overlays/0</href> </Link> </NetworkLink> </Document></kml>';
10
11     function test_Format_KML_constructor(t) {
12         t.plan(4);
13          
14         var options = {'foo': 'bar'};
15         var format = new OpenLayers.Format.KML(options);
16         t.ok(format instanceof OpenLayers.Format.KML,
17              "new OpenLayers.Format.KML returns object" );
18         t.eq(format.foo, "bar", "constructor sets options correctly");
19         t.eq(typeof format.read, "function", "format has a read function");
20         t.eq(typeof format.write, "function", "format has a write function");
21     }
22
23     function test_Format_KML_read(t) {
24         t.plan(5);
25         var features = (new OpenLayers.Format.KML()).read(this.test_content);
26         t.eq(features.length, 4, "Number of features read is correct");
27         t.ok(features[0].geometry.toString() == "POLYGON((5.001370157823406 49.26855713824488,8.214706453896161 49.630662409673505,8.397385910100951 48.45172350357396,5.001370157823406 49.26855713824488))", "polygon feature geometry correctly created");
28         t.ok(features[1].geometry.toString() == "LINESTRING(5.838523393080493 49.74814616928052,5.787079558782349 48.410795432216574,8.91427702008381 49.28932499608202)", "linestring feature geometry correctly created");
29         t.ok(features[2].geometry.toString() == "POINT(6.985073041685488 49.8682250149058)", "point feature geometry correctly created");
30         t.ok(features[3].geometry.CLASS_NAME == "OpenLayers.Geometry.Collection",
31              "read geometry collection");
32     }
33
34     function test_Format_KML_readCdataAttributes_20(t) {
35         t.plan(2);
36         var cdata = '<kml xmlns="http://earth.google.com/kml/2.0"><Document><Placemark><name><![CDATA[Pezinok]]></name><description><![CDATA[Full of text.]]></description><styleUrl>#rel1.0</styleUrl><Point> <coordinates>17.266666, 48.283333</coordinates></Point></Placemark></Document></kml>';
37         var features = (new OpenLayers.Format.KML()).read(cdata);
38         t.eq(features[0].attributes.description, "Full of text.", "Description attribute in cdata read correctly");
39         t.eq(features[0].attributes.name, "Pezinok", "title attribute in cdata read correctly");
40        
41     }
42    
43     function test_Format_KML_networklink(t) {
44         t.plan(1);
45         var f = new OpenLayers.Format.KML({'maxDepth':1});
46         f.fetchLink = function(url) {
47             t.eq(url, "http://maker.geocommons.com/maps/1717/overlays/0", "network link fetched a link correctly.");
48             return '';
49         }
50         f.read(test_nl);
51     }
52     function test_Format_KML_readCdataAttributes_21(t) {
53         t.plan(2);
54         var cdata = '<kml xmlns="http://earth.google.com/kml/2.1"><Document><Placemark><name><![CDATA[Pezinok]]></name><description><![CDATA[Full of text.]]></description><styleUrl>#rel1.0</styleUrl><Point> <coordinates>17.266666, 48.283333</coordinates></Point></Placemark></Document></kml>';
55         var features = (new OpenLayers.Format.KML()).read(cdata);
56         t.eq(features[0].attributes.description, "Full of text.", "Description attribute in cdata read correctly");
57         t.eq(features[0].attributes.name, "Pezinok", "title attribute in cdata read correctly");
58        
59     }
60    
61     function test_Format_KML_write(t) {
62         // make sure id, name, and description are preserved
63         t.plan(1);
64         var kmlExpected = this.test_content;
65         var options = {
66             folderName: "OpenLayers export",
67             foldersDesc: "Vector geometries from OpenLayers"
68         }
69
70         var format = new OpenLayers.Format.KML(options);
71         var features = format.read(kmlExpected);
72         var kmlOut = format.write(features);
73         var kmlOut = kmlOut.replace(/<\?[^>]*\?>/, ''); // Remove XML Prolog
74         t.eq(kmlOut, kmlExpected, "correctly writes an KML doc string");
75     }
76    
77     function test_Format_KML_write_multis(t) {
78         /**
79          * KML doesn't have a representation for multi geometries of a
80          * specific type.  KML MultiGeometry maps to OL Geometry.Collection.
81          * Because of this, multi-geometries in OL can't make a round trip
82          * through KML (an OL MultiPoint maps to a KML MultiGeometry
83          * containing points, which maps back to an OL Collection containing
84          * points).  So we need to acceptance tests for the writing of
85          * multi-geometries specifically instead of relying on the round-trip
86          * write test above.
87          */
88         t.plan(3);
89         var format = new OpenLayers.Format.KML({foldersDesc: "test output"});
90         var multi, feature, output, expected;
91        
92         // test multipoint
93         var multi = new OpenLayers.Geometry.MultiPoint([
94             new OpenLayers.Geometry.Point(0, 1)
95         ]);
96         feature = new OpenLayers.Feature.Vector(multi, {name: "test name"});
97         output = format.write(feature);
98         expected = '<kml xmlns="http://earth.google.com/kml/2.0"><Folder><name>OpenLayers export</name><description>test output</description><Placemark><name>test name</name><description>No description available</description><MultiGeometry><Point><coordinates>0,1</coordinates></Point></MultiGeometry></Placemark></Folder></kml>';
99         var output = output.replace(/<\?[^>]*\?>/, ''); // Remove XML Prolog
100         t.eq(output, expected, "multipoint correctly written");
101        
102         // test multilinestring
103         var multi = new OpenLayers.Geometry.MultiLineString([
104             new OpenLayers.Geometry.LineString([
105                 new OpenLayers.Geometry.Point(1, 0),
106                 new OpenLayers.Geometry.Point(0, 1)
107             ])
108         ]);
109         feature = new OpenLayers.Feature.Vector(multi, {name: "test name"});
110         output = format.write(feature);
111         expected = '<kml xmlns="http://earth.google.com/kml/2.0"><Folder><name>OpenLayers export</name><description>test output</description><Placemark><name>test name</name><description>No description available</description><MultiGeometry><LineString><coordinates>1,0 0,1</coordinates></LineString></MultiGeometry></Placemark></Folder></kml>';
112         var output = output.replace(/<\?[^>]*\?>/, ''); // Remove XML Prolog
113         t.eq(output, expected, "multilinestring correctly written");
114
115         // test multipolygon
116         var multi = new OpenLayers.Geometry.MultiPolygon([
117             new OpenLayers.Geometry.Polygon([
118                 new OpenLayers.Geometry.LinearRing([
119                     new OpenLayers.Geometry.Point(0, 0),
120                     new OpenLayers.Geometry.Point(1, 0),
121                     new OpenLayers.Geometry.Point(0, 1)
122                 ])
123             ])
124         ]);
125         feature = new OpenLayers.Feature.Vector(multi, {name: "test name"});
126         output = format.write(feature);
127         expected = '<kml xmlns="http://earth.google.com/kml/2.0"><Folder><name>OpenLayers export</name><description>test output</description><Placemark><name>test name</name><description>No description available</description><MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>0,0 1,0 0,1 0,0</coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry></Placemark></Folder></kml>';
128         var output = output.replace(/<\?[^>]*\?>/, ''); // Remove XML Prolog
129         t.eq(output, expected, "multilinestring correctly written");
130
131     }
132     function test_Format_KML_extractStyle(t) {
133         t.plan(1);
134         var f = new OpenLayers.Format.KML();
135         var features = f.read(test_style);   
136         t.ok(features[0].style == undefined, "KML Feature has no style with extractStyle false");       
137     }
138     function test_Format_KML_extractStyleFill(t) {
139         t.plan(2);
140         var f = new OpenLayers.Format.KML({extractStyles: true});
141         var features = f.read(test_style);   
142         t.eq(features[0].style.fillColor, "#ff0000", "default fill is set");       
143         var features = f.read(test_style_fill);   
144         t.eq(features[0].style.fillColor, "none", "KML Feature has none fill");       
145     }
146
147     </script>
148 </head> 
149 <body> 
150 </body> 
151 </html> 
Note: See TracBrowser for help on using the browser.