Files
openlayers/tests/Format/KML.html
crschmidt 32ca128b18 Simple fix to KML style parsing (from Edgemaster). This change makes getStyle
return clones. Since the results of getStyle are extended later in the code
by the inlineStyle, this fixes a case where kml.styleUrl symbolizers can 
be changed by <Style> blocks attached to features which share a style.
r=me. (Closes #1578)


git-svn-id: http://svn.openlayers.org/trunk/openlayers@8776 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
2009-01-28 02:07:09 +00:00

161 lines
10 KiB
HTML

<html>
<head>
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript">
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>';
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>';
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>';
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>';
function test_Format_KML_constructor(t) {
t.plan(4);
var options = {'foo': 'bar'};
var format = new OpenLayers.Format.KML(options);
t.ok(format instanceof OpenLayers.Format.KML,
"new OpenLayers.Format.KML returns object" );
t.eq(format.foo, "bar", "constructor sets options correctly");
t.eq(typeof format.read, "function", "format has a read function");
t.eq(typeof format.write, "function", "format has a write function");
}
function test_Format_KML_read(t) {
t.plan(5);
var features = (new OpenLayers.Format.KML()).read(this.test_content);
t.eq(features.length, 4, "Number of features read is correct");
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");
t.ok(features[1].geometry.toString() == "LINESTRING(5.838523393080493 49.74814616928052,5.787079558782349 48.410795432216574,8.91427702008381 49.28932499608202)", "linestring feature geometry correctly created");
t.ok(features[2].geometry.toString() == "POINT(6.985073041685488 49.8682250149058)", "point feature geometry correctly created");
t.ok(features[3].geometry.CLASS_NAME == "OpenLayers.Geometry.Collection",
"read geometry collection");
}
function test_Format_KML_readCdataAttributes_20(t) {
t.plan(2);
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>';
var features = (new OpenLayers.Format.KML()).read(cdata);
t.eq(features[0].attributes.description, "Full of text.", "Description attribute in cdata read correctly");
t.eq(features[0].attributes.name, "Pezinok", "title attribute in cdata read correctly");
}
function test_Format_KML_networklink(t) {
t.plan(1);
var f = new OpenLayers.Format.KML({'maxDepth':1});
f.fetchLink = function(url) {
t.eq(url, "http://maker.geocommons.com/maps/1717/overlays/0", "network link fetched a link correctly.");
return '';
}
f.read(test_nl);
}
function test_Format_KML_readCdataAttributes_21(t) {
t.plan(2);
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>';
var features = (new OpenLayers.Format.KML()).read(cdata);
t.eq(features[0].attributes.description, "Full of text.", "Description attribute in cdata read correctly");
t.eq(features[0].attributes.name, "Pezinok", "title attribute in cdata read correctly");
}
function test_Format_KML_write(t) {
// make sure id, name, and description are preserved
t.plan(1);
var kmlExpected = this.test_content;
var options = {
folderName: "OpenLayers export",
foldersDesc: "Vector geometries from OpenLayers"
}
var format = new OpenLayers.Format.KML(options);
var features = format.read(kmlExpected);
var kmlOut = format.write(features);
var kmlOut = kmlOut.replace(/<\?[^>]*\?>/, ''); // Remove XML Prolog
t.eq(kmlOut, kmlExpected, "correctly writes an KML doc string");
}
function test_Format_KML_write_multis(t) {
/**
* KML doesn't have a representation for multi geometries of a
* specific type. KML MultiGeometry maps to OL Geometry.Collection.
* Because of this, multi-geometries in OL can't make a round trip
* through KML (an OL MultiPoint maps to a KML MultiGeometry
* containing points, which maps back to an OL Collection containing
* points). So we need to acceptance tests for the writing of
* multi-geometries specifically instead of relying on the round-trip
* write test above.
*/
t.plan(3);
var format = new OpenLayers.Format.KML({foldersDesc: "test output"});
var multi, feature, output, expected;
// test multipoint
var multi = new OpenLayers.Geometry.MultiPoint([
new OpenLayers.Geometry.Point(0, 1)
]);
feature = new OpenLayers.Feature.Vector(multi, {name: "test name"});
output = format.write(feature);
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>';
var output = output.replace(/<\?[^>]*\?>/, ''); // Remove XML Prolog
t.eq(output, expected, "multipoint correctly written");
// test multilinestring
var multi = new OpenLayers.Geometry.MultiLineString([
new OpenLayers.Geometry.LineString([
new OpenLayers.Geometry.Point(1, 0),
new OpenLayers.Geometry.Point(0, 1)
])
]);
feature = new OpenLayers.Feature.Vector(multi, {name: "test name"});
output = format.write(feature);
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>';
var output = output.replace(/<\?[^>]*\?>/, ''); // Remove XML Prolog
t.eq(output, expected, "multilinestring correctly written");
// test multipolygon
var multi = new OpenLayers.Geometry.MultiPolygon([
new OpenLayers.Geometry.Polygon([
new OpenLayers.Geometry.LinearRing([
new OpenLayers.Geometry.Point(0, 0),
new OpenLayers.Geometry.Point(1, 0),
new OpenLayers.Geometry.Point(0, 1)
])
])
]);
feature = new OpenLayers.Feature.Vector(multi, {name: "test name"});
output = format.write(feature);
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>';
var output = output.replace(/<\?[^>]*\?>/, ''); // Remove XML Prolog
t.eq(output, expected, "multilinestring correctly written");
}
function test_Format_KML_extractStyle(t) {
t.plan(1);
var f = new OpenLayers.Format.KML();
var features = f.read(test_style);
t.ok(features[0].style == undefined, "KML Feature has no style with extractStyle false");
}
function test_Format_KML_extractStyleFill(t) {
t.plan(2);
var f = new OpenLayers.Format.KML({extractStyles: true});
var features = f.read(test_style);
t.eq(features[0].style.fillColor, "#ff0000", "default fill is set");
var features = f.read(test_style_fill);
t.eq(features[0].style.fillColor, "none", "KML Feature has none fill");
}
function test_Format_KML_getStyle(t) {
t.plan(1);
var style = {t: true};
var f = new OpenLayers.Format.KML();
f.styles = {test: style};
var gotStyle = f.getStyle('test');
gotStyle.t = false;
t.ok(style.t, "getStyle returns copy of style rather than reference");
}
</script>
</head>
<body>
</body>
</html>