Merge branch 'master' of github.com:systemed/potlatch2
[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.*;
5         import net.systemeD.potlatch2.tools.Simplify;
6
7     /**
8     * Implements parsing and loading of GPX files.
9     * For loading GPX traces from the OSM API, see halcyon/connection/Trace.as
10     */
11         public class GpxImporter extends Importer {
12
13                 public function GpxImporter(connection:Connection, map:Map, callback:Function=null, simplify:Boolean=false, options:Object=null) {
14                         super(connection,map,callback,simplify,options);
15                 }
16
17                 override protected function doImport(): void {
18                         var action:CompositeUndoableAction = new CompositeUndoableAction("Import GPX "+connection.name);
19
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, action.push));
32                                 }
33                 if (nodestring.length > 0) {
34                                         way = connection.createWay({}, nodestring, action.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, action.push);
45                         }
46
47                         default xml namespace = new Namespace("");
48                         action.doAction();
49                         finish();
50                 }
51         }
52 }