support GPX 1.1 too
[potlatch2.git] / net / systemeD / potlatch2 / utils / GpxImporter.as
1 package net.systemeD.potlatch2.utils {
2
3         import net.systemeD.halcyon.MapPaint;
4         import net.systemeD.halcyon.Globals;
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(container:*, paint:MapPaint, filenames:Array, callback:Function=null, simplify:Boolean=false) {
16                         super(container,paint,filenames,callback,simplify);
17                 }
18
19                 override protected function doImport(): 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(container.createNode({}, trkpt.@lat, trkpt.@lon));
32                                 }
33                 if (nodestring.length > 0) {
34                                         way = container.createWay({}, nodestring);
35                                         if (simplify) { Simplify.simplify(way, paint.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();
43                                 }
44                                 var node:Node = container.createNode(tags, wpt.@lat, wpt.@lon);
45                                 container.registerPOI(node);
46                         }
47                 }
48         }
49 }