~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/openlayers/lib/OpenLayers/Format/GPX.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2006-2007 MetaCarta, Inc., published under a modified BSD license.
 
2
 * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt 
 
3
 * for the full text of the license. */
 
4
 
 
5
/**
 
6
 * @requires OpenLayers/Format/XML.js
 
7
 * @requires OpenLayers/Feature/Vector.js
 
8
 * @requires OpenLayers/Geometry/Point.js
 
9
 * @requires OpenLayers/Geometry/LineString.js
 
10
 */
 
11
 
 
12
/**
 
13
 * Class: OpenLayers.Format.GPX
 
14
 * Read/write GPX parser. Create a new instance with the 
 
15
 *     <OpenLayers.Format.GPX> constructor.
 
16
 *
 
17
 * Inherits from:
 
18
 *  - <OpenLayers.Format.XML>
 
19
 */
 
20
OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, {
 
21
   /**
 
22
    * APIProperty: extractWaypoints
 
23
    * {Boolean} Extract waypoints from GPX. (default: true)
 
24
    */
 
25
    extractWaypoints: true,
 
26
    
 
27
   /**
 
28
    * APIProperty: extractTracks
 
29
    * {Boolean} Extract tracks from GPX. (default: true)
 
30
    */
 
31
    extractTracks: true,
 
32
    
 
33
   /**
 
34
    * APIProperty: extractRoutes
 
35
    * {Boolean} Extract routes from GPX. (default: true)
 
36
    */
 
37
    extractRoutes: true,
 
38
    
 
39
    /**
 
40
     * APIProperty: extractAttributes
 
41
     * {Boolean} Extract feature attributes from GPX. (default: true)
 
42
     *     NOTE: Attributes as part of extensions to the GPX standard may not
 
43
     *     be extracted.
 
44
     */
 
45
    extractAttributes: true,
 
46
    
 
47
    /**
 
48
     * Constructor: OpenLayers.Format.GPX
 
49
     * Create a new parser for GPX.
 
50
     *
 
51
     * Parameters:
 
52
     * options - {Object} An optional object whose properties will be set on
 
53
     *     this instance.
 
54
     */
 
55
    initialize: function(options) {
 
56
        OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
 
57
    },
 
58
    
 
59
    /**
 
60
     * APIMethod: read
 
61
     * Return a list of features from a GPX doc
 
62
     *
 
63
     * Parameters:
 
64
     * doc - {Element} 
 
65
     *
 
66
     * Returns:
 
67
     * An Array of <OpenLayers.Feature.Vector>s
 
68
     */
 
69
    read: function(doc) {
 
70
        if (typeof doc == "string") { 
 
71
            doc = OpenLayers.Format.XML.prototype.read.apply(this, [doc]);
 
72
        }
 
73
        var features = [];
 
74
        
 
75
        if(this.extractWaypoints) {
 
76
            var waypoints = doc.getElementsByTagName("wpt");
 
77
            for (var l = 0, len = waypoints.length; l < len; l++) {
 
78
                var attrs = {};
 
79
                if(this.extractAttributes) {
 
80
                    attrs = this.parseAttributes(waypoints[l]);
 
81
                }
 
82
                var wpt = new OpenLayers.Geometry.Point(waypoints[l].getAttribute("lon"), waypoints[l].getAttribute("lat"));
 
83
                features.push(new OpenLayers.Feature.Vector(wpt, attrs));
 
84
            }
 
85
        }
 
86
        
 
87
        if(this.extractTracks) {
 
88
            var tracks = doc.getElementsByTagName("trk");
 
89
            for (var i=0, len=tracks.length; i<len; i++) {
 
90
                // Attributes are only in trk nodes, not trkseg nodes
 
91
                var attrs = {};
 
92
                if(this.extractAttributes) {
 
93
                    attrs = this.parseAttributes(tracks[i]);
 
94
                }
 
95
                
 
96
                var segs = this.getElementsByTagNameNS(tracks[i], tracks[i].namespaceURI, "trkseg");
 
97
                for (var j = 0, seglen = segs.length; j < seglen; j++) {
 
98
                    // We don't yet support extraction of trkpt attributes
 
99
                    // All trksegs of a trk get that trk's attributes
 
100
                    var track = this.extractSegment(segs[j], "trkpt");
 
101
                    features.push(new OpenLayers.Feature.Vector(track, attrs));
 
102
                }
 
103
            }
 
104
        }
 
105
        
 
106
        if(this.extractRoutes) {
 
107
            var routes = doc.getElementsByTagName("rte");
 
108
            for (var k=0, klen=routes.length; k<klen; k++) {
 
109
                var attrs = {};
 
110
                if(this.extractAttributes) {
 
111
                    attrs = this.parseAttributes(routes[k]);
 
112
                }
 
113
                var route = this.extractSegment(routes[k], "rtept");
 
114
                features.push(new OpenLayers.Feature.Vector(route, attrs));
 
115
            }
 
116
        }
 
117
        
 
118
        if (this.internalProjection && this.externalProjection) {
 
119
            for (var g = 0, featLength = features.length; g < featLength; g++) {
 
120
                features[g].geometry.transform(this.externalProjection,
 
121
                                    this.internalProjection);
 
122
            }
 
123
        }
 
124
        
 
125
        return features;
 
126
    },
 
127
    
 
128
   /**
 
129
    * Method: extractSegment
 
130
    *
 
131
    * Parameters:
 
132
    * segment - {<DOMElement>} a trkseg or rte node to parse
 
133
    * segmentType - {String} nodeName of waypoints that form the line
 
134
    *
 
135
    * Returns:
 
136
    * {<OpenLayers.Geometry.LineString>} A linestring geometry
 
137
    */
 
138
    extractSegment: function(segment, segmentType) {
 
139
        var points = this.getElementsByTagNameNS(segment, segment.namespaceURI, segmentType);
 
140
        var point_features = [];
 
141
        for (var i = 0, len = points.length; i < len; i++) {
 
142
            point_features.push(new OpenLayers.Geometry.Point(points[i].getAttribute("lon"), points[i].getAttribute("lat")));
 
143
        }
 
144
        return new OpenLayers.Geometry.LineString(point_features);
 
145
    },
 
146
    
 
147
    /**
 
148
     * Method: parseAttributes
 
149
     *
 
150
     * Parameters:
 
151
     * node - {<DOMElement>}
 
152
     *
 
153
     * Returns:
 
154
     * {Object} An attributes object.
 
155
     */
 
156
    parseAttributes: function(node) {
 
157
        // node is either a wpt, trk or rte
 
158
        // attributes are children of the form <attr>value</attr>
 
159
        var attributes = {};
 
160
        var attrNode = node.firstChild;
 
161
        while(attrNode) {
 
162
            if(attrNode.nodeType == 1) {
 
163
                var value = attrNode.firstChild;
 
164
                if(value.nodeType == 3 || value.nodeType == 4) {
 
165
                    name = (attrNode.prefix) ?
 
166
                        attrNode.nodeName.split(":")[1] :
 
167
                        attrNode.nodeName;
 
168
                    attributes[name] = value.nodeValue;
 
169
                }
 
170
            }
 
171
            attrNode = attrNode.nextSibling;
 
172
        }
 
173
        return attributes;
 
174
    },
 
175
    
 
176
    CLASS_NAME: "OpenLayers.Format.GPX"
 
177
});