Changeset 7418
- Timestamp:
- 06/21/08 10:45:52 (5 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
sandbox/edgemaster/openlayers/lib/OpenLayers/Format/GPX.js
r7406 r7418 17 17 */ 18 18 OpenLayers.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, 19 36 20 37 /** 21 38 * APIProperty: extractAttributes 22 * {Boolean} Extract attributes from GPX.39 * {Boolean} Extract feature attributes from GPX. (default: true) 23 40 * NOTE: Attributes as part of extensions to the GPX standard may not 24 41 * be extracted. … … 41 58 * APIMethod: read 42 59 * Return a list of features from a GPX doc 43 60 * 44 61 * Parameters: 45 * d ata- {Element}62 * doc - {Element} 46 63 * 47 64 * Returns: … … 52 69 doc = OpenLayers.Format.XML.prototype.read.apply(this, [doc]); 53 70 } 54 var tracks = doc.getElementsByTagName("trk");55 71 var features = []; 56 72 57 for (var i = 0; i < tracks.length; i++) { 58 // Attributes are only in trk nodes, not trkseg nodes 59 var attrs = {} 60 if(this.extractAttributes) { 61 attrs = this.parseAttributes(tracks[i]); 62 } 63 64 var segs = this.getElementsByTagNameNS(tracks[i], tracks[i].namespaceURI, "trkseg"); 65 for (var j = 0; j < segs.length; j++) { 66 // We don't yet support extraction of trkpt attributes 67 // All trksegs of a trk get that trk's attributes 68 var track = this.extractSegment(segs[j], "trkpt"); 69 features.push(new OpenLayers.Feature.Vector(track, attrs)); 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)); 70 82 } 71 83 } 72 84 73 var routes = doc.getElementsByTagName("rte"); 74 for (var k = 0; k < routes.length; k++) { 75 var attrs = {} 76 if(this.extractAttributes) { 77 attrs = this.parseAttributes(routes[k]); 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 } 78 101 } 79 var route = this.extractSegment(routes[k], "rtept");80 features.push(new OpenLayers.Feature.Vector(route, attrs));81 102 } 82 103 83 var waypoints = doc.getElementsByTagName("wpt"); 84 for (var l = 0; l < waypoints.length; l++) { 85 var attrs = {}; 86 if(this.extractAttributes) { 87 attrs = this.parseAttributes(waypoints[l]); 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)); 88 113 } 89 var wpt = new OpenLayers.Geometry.Point(waypoints[l].getAttribute("lon"), waypoints[l].getAttribute("lat"));90 features.push(new OpenLayers.Feature.Vector(wpt, attrs));91 114 } 92 115 … … 101 124 }, 102 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 */ 103 136 extractSegment: function(segment, segmentType) { 104 137 var points = this.getElementsByTagNameNS(segment, segment.namespaceURI, segmentType); … … 120 153 */ 121 154 parseAttributes: function(node) { 155 // node is either a wpt, trk or rte 156 // attributes are children of the form <attr>value</attr> 122 157 var attributes = {}; 123 // assume attributes are children of the first type 1 child124 158 var attrNode = node.firstChild; 125 159 while(attrNode) { … … 138 172 }, 139 173 140 CLASS_NAME: "OpenLayers.Format.GPX" 141 }); 174 CLASS_NAME: "OpenLayers.Format.GPX" 175 }); sandbox/edgemaster/openlayers/tests/Format/GPX.html
r7058 r7418 4 4 <script type="text/javascript"> 5 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>';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 7 8 8 function test_Format_GPX_constructor(t) { … … 22 22 var features = f.read(gpx_data); 23 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"); 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."); 27 34 } 28 35 </script>
