5f0816c3cec636cae75cb5b07fc1e8d02947de8a
[potlatch2.git] / net / systemeD / potlatch2 / utils / GpxImporter.as
1 package net.systemeD.potlatch2.utils {
2
3         import net.systemeD.halcyon.Map;
4         import net.systemeD.halcyon.connection.Connection;
5         import net.systemeD.halcyon.connection.Node;
6         import net.systemeD.halcyon.connection.Way;
7         import net.systemeD.potlatch2.tools.Simplify;
8
9     /**
10     * Implements parsing and loading of GPX files.
11     * For loading GPX traces from the OSM API, see halcyon/connection/Trace.as
12     */
13         public class GpxImporter extends Importer {
14
15                 public function GpxImporter(connection:Connection, map:Map, filenames:Array, callback:Function=null, simplify:Boolean=false) {
16                         super(connection,map,filenames,callback,simplify);
17                 }
18
19                 override protected function doImport(push:Function): void {
20                         var file:XML = new XML(files[0]);
21                         for each (var ns:Namespace in file.namespaceDeclarations()) {
22                                 if (ns.uri.match(/^http:\/\/www\.topografix\.com\/GPX\/1\/[01]$/)) {
23                                         default xml namespace = ns;
24                                 }
25                         }
26
27                         for each (var trkseg:XML in file..trkseg) {
28                                 var way:Way;
29                 var nodestring:Array = [];
30                 for each (var trkpt:XML in trkseg.trkpt) {
31                                         nodestring.push(connection.createNode({}, trkpt.@lat, trkpt.@lon, push));
32                                 }
33                 if (nodestring.length > 0) {
34                                         way = connection.createWay({}, nodestring, push);
35                                         if (simplify) { Simplify.simplify(way, map, false); }
36                                 }
37                         }
38
39             for each (var wpt:XML in file.wpt) {
40                                 var tags:Object = {};
41                                 for each (var tag:XML in wpt.children()) {
42                                         tags[tag.name().localName]=tag.toString().substr(0,255);
43                                 }
44                                 var node:Node = connection.createNode(tags, wpt.@lat, wpt.@lon, push);
45                                 connection.registerPOI(node);
46                         }
47
48                         default xml namespace = new Namespace("");
49                 }
50         }
51 }