OpenLayers OpenLayers

Ticket #1272: t1272r7446.Format.GPX.patch

File t1272r7446.Format.GPX.patch, 10.7 kB (added by edgemaster, 2 months ago)

most recent version of the GPX format class

  • tests/Format/GPX.html

    old new  
     1<html>  
     2<head>  
     3    <script src="../../lib/OpenLayers.js"></script> 
     4    <script type="text/javascript"> 
     5     
     6    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>'; 
     7     
     8    function test_Format_GPX_constructor(t) {  
     9        t.plan(4);  
     10          
     11        var options = {'foo': 'bar'};  
     12        var format = new OpenLayers.Format.GPX(options);  
     13        t.ok(format instanceof OpenLayers.Format.GPX,  
     14             "new OpenLayers.Format.GPX returns object" );  
     15        t.eq(format.foo, "bar", "constructor sets options correctly");  
     16        t.eq(typeof format.read, "function", "format has a read function");  
     17        t.eq(typeof format.write, "function", "format has a write function");  
     18    } 
     19    function test_Format_GPX_read(t) { 
     20        t.plan(4); 
     21        var f = new OpenLayers.Format.GPX(); 
     22        var features = f.read(gpx_data); 
     23        t.eq(features.length, 3, "Number of features read is correct"); 
     24        t.eq(features[0].geometry.toString(), "POINT(-0.1853562259 51.3697845627)", "waypoint feature correctly created"); 
     25        t.eq(features[1].geometry.toString(), "LINESTRING(-0.1721292044 51.3768216433,-0.1649230916 51.370833767,-0.1736741378 51.3644368725,-0.166259525 51.3576354272)", "track feature correctly created"); 
     26        t.eq(features[2].geometry.toString(), "LINESTRING(-0.1829991904 51.3761803674,-0.1758887005 51.3697894659,-0.1833202965 51.3639790884,-0.1751119509 51.3567607069)", "route feature correctly created"); 
     27    } 
     28    function test_format_GPX_read_attributes(t) { 
     29        t.plan(2); 
     30        var f = new OpenLayers.Format.GPX(); 
     31        var features = f.read(gpx_data); 
     32        t.eq(features[0].attributes['name'], "Mark", "Text attribute node read correctly."); 
     33        t.eq(features[0].attributes['sym'], "Flag", "CDATA attribute node read correctly."); 
     34    } 
     35    </script>  
     36</head>  
     37<body>  
     38</body>  
     39</html>  
  • tests/list-tests.html

    old new  
    2626    <li>Format/GeoJSON.html</li> 
    2727    <li>Format/GeoRSS.html</li> 
    2828    <li>Format/GML.html</li> 
     29    <li>Format/GPX.html</li> 
    2930    <li>Format/JSON.html</li> 
    3031    <li>Format/OSM.html</li> 
    3132    <li>Format/KML.html</li> 
  • lib/OpenLayers/Format/GPX.js

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

    old new  
    197198            "OpenLayers/Format/WFS.js", 
    198199            "OpenLayers/Format/WKT.js", 
    199200            "OpenLayers/Format/OSM.js", 
     201            "OpenLayers/Format/GPX.js", 
    200202            "OpenLayers/Format/SLD.js", 
    201203            "OpenLayers/Format/SLD/v1.js", 
    202204            "OpenLayers/Format/SLD/v1_0_0.js",