summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Format/GPX.html
blob: 6286cfe3956b1c871e9e21dd4b22e7aa3bf6eab9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<html> 
<head> 
    <script src="../OLLoader.js"></script>
    <script type="text/javascript">
    
    var gpx_data = '<?xml version="1.0" encoding="ISO-8859-1"?><gpx version="1.1" creator="Memory-Map 5.1.3.715 http://www.memory-map.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"><wpt lat="51.3697845627" lon="-0.1853562259"><name>Mark</name><sym><![CDATA[Flag]]></sym><type><![CDATA[Marks]]></type></wpt><rte><name><![CDATA[Route8]]></name><type><![CDATA[Route]]></type><rtept lat="51.3761803674" lon="-0.1829991904"><name><![CDATA[WP0801]]></name><sym><![CDATA[Dot]]></sym><type><![CDATA[Waypoints]]></type></rtept><rtept lat="51.3697894659" lon="-0.1758887005"><name><![CDATA[WP0802]]></name><sym><![CDATA[Dot]]></sym><type><![CDATA[Waypoints]]></type></rtept><rtept lat="51.3639790884" lon="-0.1833202965"><name><![CDATA[WP0803]]></name><sym><![CDATA[Dot]]></sym><type><![CDATA[Waypoints]]></type></rtept><rtept lat="51.3567607069" lon="-0.1751119509"><name><![CDATA[WP0804]]></name><sym><![CDATA[Dot]]></sym><type><![CDATA[Waypoints]]></type></rtept></rte><trk><name><![CDATA[Track]]></name><type><![CDATA[Track]]></type><trkseg><trkpt lat="51.3768216433" lon="-0.1721292044"></trkpt><trkpt lat="51.3708337670" lon="-0.1649230916"></trkpt><trkpt lat="51.3644368725" lon="-0.1736741378"></trkpt><trkpt lat="51.3576354272" lon="-0.1662595250"></trkpt></trkseg></trk></gpx>';
    
    function test_Format_GPX_constructor(t) { 
        t.plan(5); 
         
        var options = {'foo': 'bar'}; 
        var format = new OpenLayers.Format.GPX(options); 
        t.ok(format instanceof OpenLayers.Format.GPX, 
             "new OpenLayers.Format.GPX 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_GPX_read(t) {
        t.plan(7);
        
        var origDefaultPrecision = OpenLayers.Util.DEFAULT_PRECISION;
        OpenLayers.Util.DEFAULT_PRECISION = 9;
        
        var expected,
            P = OpenLayers.Geometry.Point,
            LS = OpenLayers.Geometry.LineString;
        var f = new OpenLayers.Format.GPX();
        var features = f.read(gpx_data);
        t.eq(features.length, 3, "Number of features read is correct");
        expected = new P(-0.1853562259, 51.3697845627);
        t.geom_eq(features[2].geometry, expected, "waypoint feature correctly created");
        expected = new LS([
                new P(-0.1721292044, 51.3768216433),
                new P(-0.1649230916, 51.370833767),
                new P(-0.1736741378, 51.3644368725),
                new P(-0.166259525, 51.3576354272)
        ]);
        t.geom_eq(features[0].geometry, expected, "track feature correctly created");
        expected = new LS([
                new P(-0.1829991904, 51.3761803674),
                new P(-0.1758887005, 51.3697894659),
                new P(-0.1833202965, 51.3639790884),
                new P(-0.1751119509, 51.3567607069)
        ]);
        t.geom_eq(features[1].geometry, expected, "route feature correctly created");

        f.internalProjection = new OpenLayers.Projection("EPSG:3857");
        features = f.read(gpx_data);
        expected = new P(-20633.760679678744, 6686966.841929403);
        t.geom_eq(features[2].geometry, expected, "transformed waypoint feature correctly created");
        expected = new LS([
                new P(-19161.33538179203, 6688221.743275255),
                new P(-18359.1545744088, 6687153.931130851),
                new P(-19333.316581165607, 6686013.33343931),
                new P(-18507.925659955214, 6684800.777090962)
        ]);
        t.geom_eq(features[0].geometry, expected, "transformed track feature correctly created");
        expected = new LS([
                new P(-20371.3766880736, 6688107.378491073),
                new P(-19579.84057322507, 6686967.716235109),
                new P(-20407.12205561124, 6685931.714395953),
                new P(-19493.373203291227, 6684644.845706556)
        ]);
        t.geom_eq(features[1].geometry, expected, "transformed route feature correctly created");

        OpenLayers.Util.DEFAULT_PRECISION = origDefaultPrecision;
    }
    function test_format_GPX_read_attributes(t) {
        t.plan(2);
        var f = new OpenLayers.Format.GPX();
        var features = f.read(gpx_data);
        t.eq(features[2].attributes['name'], "Mark", "Text attribute node read correctly.");
        t.eq(features[2].attributes['sym'], "Flag", "CDATA attribute node read correctly.");
    }
    function test_Format_GPX_serialize_points(t) { 
        t.plan(2);

        var parser = new OpenLayers.Format.GPX();

        var point = new OpenLayers.Geometry.Point(-111.04, 45.68);  
        var point2 = new OpenLayers.Geometry.Point(-112.04, 45.68); 
        var features = [
            new OpenLayers.Feature.Vector(point, {name: 'foo', description: 'bar'}),
            new OpenLayers.Feature.Vector(point2, {name: 'foo', description: 'bar'})
        ];
        var data = parser.write(features);
        t.xml_eq(data, '<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="OpenLayers" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><wpt lon="-111.04" lat="45.68"><name>foo</name><desc>bar</desc></wpt><wpt lon="-112.04" lat="45.68"><name>foo</name><desc>bar</desc></wpt></gpx>', 'GPX serializes points correctly');

        parser.internalProjection = new OpenLayers.Projection("EPSG:3857");
        point = new OpenLayers.Geometry.Point(-12367595.42541111, 5621521.485409545);  
        point2 = new OpenLayers.Geometry.Point(-12472235.746742222, 5621521.485409545); 
        features = [
            new OpenLayers.Feature.Vector(point, {name: 'foo', description: 'bar'}),
            new OpenLayers.Feature.Vector(point2, {name: 'foo', description: 'bar'})
        ];
        data = parser.write(features);
        t.xml_eq(data, '<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="OpenLayers" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><wpt lon="-111.1" lat="45"><name>foo</name><desc>bar</desc></wpt><wpt lon="-112.04" lat="45"><name>foo</name><desc>bar</desc></wpt></gpx>', 'GPX serializes transformed points correctly');
    }
    function test_Format_GPX_serialize_line(t) { 
        t.plan(2);

        var parser = new OpenLayers.Format.GPX();

        var point = new OpenLayers.Geometry.Point(-111.04, 45.68);  
        var point2 = new OpenLayers.Geometry.Point(-112.04, 45.68); 
        var line = new OpenLayers.Geometry.LineString([point, point2]);
        var f = new OpenLayers.Feature.Vector(line, {name: 'foo', description: 'bar'});
        var data = parser.write(f);
        t.xml_eq(data, '<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="OpenLayers" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><trk><name>foo</name><desc>bar</desc><trkseg><trkpt lon="-111.04" lat="45.68"/><trkpt lon="-112.04" lat="45.68"/></trkseg></trk></gpx>', 'GPX serializes line correctly');

        parser.internalProjection = new OpenLayers.Projection("EPSG:3857");
        point = new OpenLayers.Geometry.Point(-12367595.42541111, 5621521.485409545);  
        point2 = new OpenLayers.Geometry.Point(-12472235.746742222, 5621521.485409545); 
        line = new OpenLayers.Geometry.LineString([point, point2]);
        f = new OpenLayers.Feature.Vector(line, {name: 'foo', description: 'bar'});
        data = parser.write(f);
        t.xml_eq(data, '<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="OpenLayers" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><trk><name>foo</name><desc>bar</desc><trkseg><trkpt lon="-111.1" lat="45"/><trkpt lon="-112.04" lat="45"/></trkseg></trk></gpx>', 'GPX serializes transformed line correctly');
    }
    function test_Format_GPX_serialize_lines(t) { 
        t.plan(1);

        var parser = new OpenLayers.Format.GPX();

        var point = new OpenLayers.Geometry.Point(-111.04, 45.68);  
        var point2 = new OpenLayers.Geometry.Point(-112.04, 45.68); 
        var line = new OpenLayers.Geometry.LineString([point, point2]);
        var point3 = new OpenLayers.Geometry.Point(1, 2);  
        var point4 = new OpenLayers.Geometry.Point(3, 4); 
        var line2 = new OpenLayers.Geometry.LineString([point3, point4]);
        var f = new OpenLayers.Feature.Vector(line, {name: 'foo', description: 'bar'});
        var f2 = new OpenLayers.Feature.Vector(line2, {name: 'dude', description: 'truite'});
        var data = parser.write([f, f2]);
        t.xml_eq(data, '<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="OpenLayers" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><trk><name>foo</name><desc>bar</desc><trkseg><trkpt lon="-111.04" lat="45.68"/><trkpt lon="-112.04" lat="45.68"/></trkseg></trk><trk><name>dude</name><desc>truite</desc><trkseg><trkpt lon="1" lat="2"/><trkpt lon="3" lat="4"/></trkseg></trk></gpx>', 'GPX serializes lines correctly');
    }
    function test_Format_GPX_serialize_multiline(t) { 
        t.plan(1);

        var parser = new OpenLayers.Format.GPX();

        var point = new OpenLayers.Geometry.Point(-111.04, 45.68);  
        var point2 = new OpenLayers.Geometry.Point(-112.04, 45.68); 
        var line = new OpenLayers.Geometry.LineString([point, point2]);
        var point3 = new OpenLayers.Geometry.Point(1, 2);  
        var point4 = new OpenLayers.Geometry.Point(3, 4); 
        var line2 = new OpenLayers.Geometry.LineString([point3, point4]);
        var multiline = new OpenLayers.Geometry.MultiLineString([line, line2]);
        var f = new OpenLayers.Feature.Vector(multiline, {name: 'foo', description: 'bar'});
        var data = parser.write([f]);
        t.xml_eq(data, '<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="OpenLayers" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><trk><name>foo</name><desc>bar</desc><trkseg><trkpt lon="-111.04" lat="45.68"/><trkpt lon="-112.04" lat="45.68"/></trkseg><trkseg><trkpt lon="1" lat="2"/><trkpt lon="3" lat="4"/></trkseg></trk></gpx>', 'GPX serializes multiline correctly');
    }
    function test_Format_GPX_serialize_polygon(t) { 
        t.plan(1);

        var parser = new OpenLayers.Format.GPX();

        var point = new OpenLayers.Geometry.Point(-111.04, 45.68);  
        var point2 = new OpenLayers.Geometry.Point(-112.04, 45.68); 
        var linearRing = new OpenLayers.Geometry.LinearRing([point, point2, point.clone()]);
        var polygon = new OpenLayers.Geometry.Polygon([linearRing]);
        var f = new OpenLayers.Feature.Vector(polygon, {name: 'foo', description: 'bar'});
        var data = parser.write([f]);
        t.xml_eq(data, '<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="OpenLayers" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><trk><name>foo</name><desc>bar</desc><trkseg><trkpt lon="-111.04" lat="45.68"/><trkpt lon="-112.04" lat="45.68"/><trkpt lon="-111.04" lat="45.68"/></trkseg></trk></gpx>', 'GPX serializes polygon correctly');
    }
    function test_Format_GPX_serialize_metadata(t) { 
        t.plan(1);

        var parser = new OpenLayers.Format.GPX();

        var data = parser.write([], {name: 'foo', desc: 'bar'});
        t.xml_eq(data, '<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="OpenLayers" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><metadata><name>foo</name><desc>bar</desc></metadata></gpx>', 'GPX serializes metadata correctly');
    }
    </script> 
</head> 
<body> 
</body> 
</html>