OpenLayers OpenLayers

Ticket #1272: GPXformat.patch

File GPXformat.patch, 7.5 kB (added by edgemaster, 4 months ago)

GPX format based on crschmidt's original workings, tests included

  • 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><![CDATA[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.ok(features[0].geometry.toString() == "LINESTRING(-0.1721292044 51.3768216433,-0.1649230916 51.370833767,-0.1736741378 51.3644368725,-0.166259525 51.3576354272)", "track feature correctly created"); 
     25        t.ok(features[1].geometry.toString() == "LINESTRING(-0.1829991904 51.3761803674,-0.1758887005 51.3697894659,-0.1833202965 51.3639790884,-0.1751119509 51.3567607069)", "route feature correctly created"); 
     26        t.ok(features[2].geometry.toString() == "POINT(-0.1853562259 51.3697845627)", "waypoint feature correctly created"); 
     27    } 
     28    </script>  
     29</head>  
     30<body>  
     31</body>  
     32</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    /** 
     21     * Constructor: OpenLayers.Format.GPX 
     22     * Create a new parser for GPX. 
     23     * 
     24     * Parameters: 
     25     * options - {Object} An optional object whose properties will be set on 
     26     *     this instance. 
     27     */ 
     28    initialize: function(options) { 
     29        OpenLayers.Format.XML.prototype.initialize.apply(this, [options]); 
     30    }, 
     31     
     32    /** 
     33     * APIMethod: read 
     34     * Return a list of features from a GPX doc 
     35      
     36     * Parameters: 
     37     * data - {Element}  
     38     * 
     39     * Returns: 
     40     * An Array of <OpenLayers.Feature.Vector>s 
     41     */ 
     42    read: function(doc) { 
     43        if (typeof doc == "string") {  
     44            doc = OpenLayers.Format.XML.prototype.read.apply(this, [doc]); 
     45        } 
     46        var tracks = doc.getElementsByTagName("trk"); 
     47        var features = []; 
     48        for (var i = 0; i < tracks.length; i++) { 
     49            var segs = this.getElementsByTagNameNS(tracks[i], tracks[i].namespaceURI, "trkseg"); 
     50            for (var j = 0; j < segs.length; j++) { 
     51                var track = this.extractSegment(segs[i], "trkpt"); 
     52                features.push(track); 
     53            } 
     54        } 
     55        var routes = doc.getElementsByTagName("rte"); 
     56        for (var k = 0; k < routes.length; k++) { 
     57            var route = this.extractSegment(routes[k], "rtept"); 
     58            features.push(route); 
     59        } 
     60        var waypoints = doc.getElementsByTagName("wpt"); 
     61        for (var l = 0; l < waypoints.length; l++) { 
     62            features.push(new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(waypoints[l].getAttribute("lon"), waypoints[l].getAttribute("lat")))); 
     63        } 
     64         
     65        if (this.internalProjection && this.externalProjection) { 
     66            for (var g = 0; g < features.length; g++) { 
     67                features[g].geometry.transform(this.externalProjection, 
     68                                    this.internalProjection); 
     69            } 
     70        } 
     71         
     72        return features; 
     73    }, 
     74     
     75    extractSegment: function(segment, segmentType) { 
     76        var points = this.getElementsByTagNameNS(segment, segment.namespaceURI, segmentType); 
     77        var point_features = []; 
     78        for (var i = 0; i < points.length; i++) { 
     79            point_features.push(new OpenLayers.Geometry.Point(points[i].getAttribute("lon"), points[i].getAttribute("lat"))); 
     80        } 
     81        return new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString(point_features)); 
     82         
     83    }, 
     84     
     85    CLASS_NAME: "OpenLayers.Format.GPX"  
     86});     
  • lib/OpenLayers.js

    old new  
    195196            "OpenLayers/Format/WFS.js", 
    196197            "OpenLayers/Format/WKT.js", 
    197198            "OpenLayers/Format/OSM.js", 
     199            "OpenLayers/Format/GPX.js", 
    198200            "OpenLayers/Format/SLD.js", 
    199201            "OpenLayers/Format/SLD/v1.js", 
    200202            "OpenLayers/Format/SLD/v1_0_0.js",