Files
openlayers/tests/Format/KML.html

281 lines
16 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_style_outline = '<kml xmlns="http://earth.google.com/kml/2.0"> <Placemark> <Style> <PolyStyle> <outline>0</outline> <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(5);
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");
t.eq(format.externalProjection.getCode(), "EPSG:4326",
"default external projection is EPSG:4326");
}
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 = {
foldersName: "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_noNameDesc(t) {
t.plan(1);
var format = new OpenLayers.Format.KML({
foldersName: null,
foldersDesc: null
});
var geom = new OpenLayers.Geometry.Point(0, 0)
var feature = new OpenLayers.Feature.Vector(geom);
feature.id = 42;
var kmlOut = format.write(feature);
var expected = '<kml xmlns="http://earth.google.com/kml/2.0"><Folder><Placemark><name>42</name><description>No description available</description><Point><coordinates>0,0</coordinates></Point></Placemark></Folder></kml>'
t.eq(kmlOut, expected, "null foldersName or foldersDesc don't create elements");
}
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_extractStyleOutline(t) {
t.plan(2);
var f = new OpenLayers.Format.KML({extractStyles: true});
var features = f.read(test_style);
t.eq(features[0].style.strokeWidth, "10", "default stroke is set");
var features = f.read(test_style_outline);
t.eq(features[0].style.strokeWidth, "0", "KML Feature has no outline");
}
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");
}
function test_Format_KML_extendedData(t) {
t.plan(2);
var f = new OpenLayers.Format.KML();
var features = f.read(OpenLayers.Util.getElement("kml_extendeddata").value);
t.eq(features[0].attributes.all_bridges.value, "3030", "read value from extendeddata correctly.");
t.eq(features[0].attributes.all_bridges.displayName, "all bridges", "read displayName from extendeddata correctly.");
}
function test_Format_KML_extendedData_SchemaData(t) {
t.plan(4);
var f = new OpenLayers.Format.KML();
var features = f.read(OpenLayers.Util.getElement("kml_extendeddata2").value);
t.eq(features[0].attributes.TrailHeadName.value, "Pi in the sky", "read value from extendeddata (schema data) correctly.");
t.eq(features[0].attributes.TrailHeadName.displayName, "TrailHeadName", "read displayName from extendeddata correctly");
t.eq(features[0].attributes.ElevationGain.value, "10", "read value from extendeddata (schema data) correctly.");
t.eq(features[0].attributes.ElevationGain.displayName, "ElevationGain", "read displayName from extendeddata correctly");
}
</script>
</head>
<body>
<textarea id="kml_extendeddata" style="display:none">
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<Placemark>
<styleUrl>#default</styleUrl>
<ExtendedData>
<Data name='all_bridges'>
<displayName><![CDATA[all bridges]]></displayName>
<value><![CDATA[3030]]></value>
</Data>
<Data name='latitude'>
<displayName><![CDATA[latitude]]></displayName>
<value><![CDATA[43]]></value>
</Data>
<Data name='longitude'>
<displayName><![CDATA[longitude]]></displayName>
<value><![CDATA[-107.55]]></value>
</Data>
<Data name='functionally_obsolete__percent'>
<displayName><![CDATA[functionally obsolete, percent]]></displayName>
<value><![CDATA[8]]></value>
</Data>
<Data name='structurally_deficient__percent'>
<displayName><![CDATA[structurally deficient, percent]]></displayName>
<value><![CDATA[13]]></value>
</Data>
<Data name='state'>
<displayName><![CDATA[state]]></displayName>
<value><![CDATA[Wyoming]]></value>
</Data>
</ExtendedData>
<Point>
<coordinates>-107.55,43.0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
</textarea>
<textarea id="kml_extendeddata2" style="display:none">
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<Placemark>
<name>Easy trail</name>
<ExtendedData>
<SchemaData schemaUrl="#TrailHeadTypeId">
<SimpleData name="TrailHeadName">Pi in the sky</SimpleData>
<SimpleData name="TrailLength">3.14159</SimpleData>
<SimpleData name="ElevationGain">10</SimpleData>
</SchemaData>
</ExtendedData>
<Point>
<coordinates>-122.000,37.002</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Difficult trail</name>
<ExtendedData>
<SchemaData schemaUrl="#TrailHeadTypeId">
<SimpleData name="TrailHeadName">Mount Everest</SimpleData>
<SimpleData name="TrailLength">347.45</SimpleData>
<SimpleData name="ElevationGain">10000</SimpleData>
</SchemaData>
</ExtendedData>
<Point>
<coordinates>-122.000,37.002</coordinates>
</Point>
</Placemark>
</Document>
</kml>
</textarea>
</body>
</html>