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