Draw new markers when they are created, and save the data loaders from worrying about...
[potlatch2.git] / net / systemeD / potlatch2 / utils / OsmImporter.as
1 package net.systemeD.potlatch2.utils {
2
3         import net.systemeD.halcyon.MapPaint;
4         import net.systemeD.halcyon.connection.*;
5         import net.systemeD.potlatch2.tools.Simplify;
6
7         public class OsmImporter extends Importer {
8
9                 public function OsmImporter(container:*, paint:MapPaint, filenames:Array, callback:Function=null, simplify:Boolean=false) {
10                         super(container,paint,filenames,callback,simplify);
11                 }
12
13                 override protected function doImport():void {
14                         var map:XML = new XML(files[0]);
15                         var data:XML;
16                         
17             var oldid:Number;
18             var tags:Object;
19
20                         var nodemap:Object={};
21                         var waymap:Object={};
22                         var relationmap:Object={};
23
24             for each(data in map.node) {
25                 oldid = Number(data.@id);
26                                 nodemap[oldid] = container.createNode(parseTags(data.tag), Number(data.@lat), Number(data.@lon));
27             }
28
29             for each(data in map.way) {
30                 oldid = Number(data.@id);
31                 var nodes:Array = [];
32                 for each(var nd:XML in data.nd) { nodes.push(nodemap[Number(nd.@ref)]); }
33                                 waymap[oldid] = container.createWay(parseTags(data.tag), nodes);
34             }
35             
36             for each(data in map.relation) {
37                 oldid = Number(data.@id);
38                 var members:Array = [];
39                 for each(var memberXML:XML in data.member) {
40                     var type:String = memberXML.@type.toLowerCase();
41                     var role:String = memberXML.@role;
42                     var memberID:Number = Number(memberXML.@ref);
43                     var member:Entity = null;
44                                         switch (memberXML.@type.toLowerCase()) {
45                                                 case 'node':            member=nodemap[memberID]; break;
46                                                 case 'way':                     member=waymap[memberID]; break;
47                                                 case 'relation':        break;  // ** TODO - cope with evil nested relations
48                                         }
49                                         if (member!=null) { members.push(new RelationMember(member,role)); }
50                 }
51                                 relationmap[oldid] = container.createRelation(parseTags(data.tag), members);
52             }
53         }
54
55         private function parseTags(tagElements:XMLList):Object {
56             var tags:Object = {};
57             for each (var tagEl:XML in tagElements)
58                 tags[tagEl.@k] = tagEl.@v;
59             return tags;
60         }
61
62         }
63 }