summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Format/OSM.html
blob: 6ceb316e42ecf7ed95332ba8becb046f6a73ebb5 (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
<html> 
<head> 
    <script src="../OLLoader.js"></script> 
    <script src="../data/osm.js"></script> 
    <script type="text/javascript">
    
    function test_Format_OSM_constructor(t) { 
        t.plan(5); 
         
        var options = {'foo': 'bar'}; 
        var format = new OpenLayers.Format.OSM(options); 
        t.ok(format instanceof OpenLayers.Format.OSM, 
             "new OpenLayers.Format.OSM 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_OSM_node(t) {
        t.plan(4);
        var f = new OpenLayers.Format.OSM();
        var features = f.read(osm_test_data['node']);
        var feat = features[0];
        t.eq(feat.attributes, {}, "attributes is empty");
        t.eq(feat.osm_id, 200545, "internal osm_id property set correctly");
        t.eq(feat.geometry.x, -1.8166417, "lon is correct"); 
        t.eq(feat.geometry.y, 52.5503033, "lat is correct"); 
    }
    function test_Format_OSM_node_with_tags(t) {
        t.plan(5);
        var f = new OpenLayers.Format.OSM();
        var features = f.read(osm_test_data['node_with_tags']);
        var feat = features[0];
        t.eq(feat.attributes, {'a':'b'}, "attributes match");
        t.eq(feat.osm_id, 200545, "internal osm_id property set correctly");
        t.eq(feat.fid, "node.200545", "OSM-based FID set correctly.");
        t.eq(feat.geometry.x, -1.8166417, "lon is correct"); 
        t.eq(feat.geometry.y, 52.5503033, "lat is correct"); 
    }
    function test_Format_OSM_way(t) {
        t.plan(8);
        var f = new OpenLayers.Format.OSM();
        var features = f.read(osm_test_data['way']);
        t.eq(features.length, 1, "One feature");
        var feat = features[0];
        t.eq(feat.osm_id, 4685537, "OSM ID set correctly.");
        t.eq(feat.fid, "way.4685537", "OSM-based FID set correctly.");
        t.eq(feat.geometry.CLASS_NAME, "OpenLayers.Geometry.Polygon", "returned as polygon");
        t.eq(feat.geometry.components[0].components.length, 11, "Correct number of components"); 
        t.eq(feat.geometry.components[0].components[0].osm_id, 29783472, "OSM ID set on components");
        t.eq(feat.geometry.toString(), "POLYGON((-1.8164007 52.5501836,-1.8170311 52.5506035,-1.8164092 52.5509559,-1.8169385 52.5513103,-1.8159626 52.5517893,-1.8145067 52.5518461,-1.8143197 52.5511883,-1.8141177 52.5506446,-1.8151451 52.5501275,-1.8157703 52.5505521,-1.8164007 52.5501836))", "WKT of feature is correct");
        t.eq(feat.attributes.landuse, "school", "landuse attribute correct");
    }
    
    function test_Format_OSM_node_way(t) {
        t.plan(5)
        var f = new OpenLayers.Format.OSM();
        var features = f.read(osm_test_data['node_way']);
        t.eq(features.length, 1, "One feature");
        var feat = features[0];
        t.eq(feat.osm_id, 21329267, "OSM ID set correctly");
        t.eq(feat.attributes.highway, "unclassified", "highway attribute is correct."); 
        t.eq(feat.geometry.CLASS_NAME, "OpenLayers.Geometry.LineString", "returned as linestring");
        t.eq(feat.geometry.components.length, 12, "correct number of segments");
    }
    
    function test_Format_OSM_node_way_checkTags(t) {
        t.plan(9)
        var f = new OpenLayers.Format.OSM({'checkTags': true});
        var features = f.read(osm_test_data['node_way']);
        t.eq(features.length, 3, "multiple features");
        
        var feat = features[1];
        t.eq(feat.geometry.CLASS_NAME, "OpenLayers.Geometry.Point", "point class");
        t.ok(feat.attributes != {}, "feature has attributes");
        
        var feat = features[2];
        t.eq(feat.geometry.CLASS_NAME, "OpenLayers.Geometry.Point", "point class");
        t.ok(feat.attributes != {}, "feature has attributes");
        
        feat = features[0];
        t.eq(feat.osm_id, 21329267, "OSM ID set correctly");
        t.eq(feat.attributes.highway, "unclassified", "highway attribute is correct."); 
        t.eq(feat.geometry.CLASS_NAME, "OpenLayers.Geometry.LineString", "returned as linestring");
        t.eq(feat.geometry.components.length, 12, "correct number of segments");
    }

    function test_Format_OSM_serialize(t) {
        t.plan(4);
        var f = new OpenLayers.Format.OSM({'checkTags': true});
        for (var key in osm_serialized_data) {
            var input = f.read(osm_test_data[key]);
            var output = f.write(input);
            output = output.replace(/<\?[^>]*\?>/, '');
            t.eq(output, osm_serialized_data[key], key + " serialized correctly");
        }
    }   
    function test_Format_OSM_write_reproject(t) {
        t.plan(1);
        var f = new OpenLayers.Format.OSM({'internalProjection': new OpenLayers.Projection("EPSG:900913")});
        var feat = new OpenLayers.Feature.Vector(
            new OpenLayers.Geometry.Point(100000, 100000)
            );
        var data = f.write([feat]);
        var f = new OpenLayers.Format.OSM();
        var features = f.read(data);

        t.eq(OpenLayers.Util.toFloat(features[0].geometry.x, 3), .898, "exported to lonlat and re-read as lonlat correctly")
    }    
    </script> 
</head> 
<body> 
</body> 
</html>