Merge branch 'master' into history
[potlatch2.git] / net / systemeD / potlatch2 / utils / OsmImporter.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         public class OsmImporter extends Importer {
8
9                 public function OsmImporter(connection:Connection, map:Map, filenames:Array, callback:Function=null, simplify:Boolean=false) {
10                         super(connection,map,filenames,callback,simplify);
11                 }
12
13                 override protected function doImport(push:Function):void {
14                         var map:XML = new XML(files[0]);
15                         if (map.name().localName=='osmChange') { map=map.create[0]; }
16
17                         var data:XML;
18             var oldid:Number;
19             var tags:Object;
20
21                         var nodemap:Object={};
22                         var waymap:Object={};
23                         var relationmap:Object={};
24
25             for each(data in map.node) {
26                 oldid = Number(data.@id);
27                                 nodemap[oldid] = connection.createNode(parseTags(data.tag), Number(data.@lat), Number(data.@lon), push);
28             }
29
30             for each(data in map.way) {
31                 oldid = Number(data.@id);
32                 var nodes:Array = [];
33                 for each(var nd:XML in data.nd) { nodes.push(nodemap[Number(nd.@ref)]); }
34                                 waymap[oldid] = connection.createWay(parseTags(data.tag), nodes, push);
35             }
36             
37             for each(data in map.relation) {
38                 oldid = Number(data.@id);
39                 var members:Array = [];
40                 for each(var memberXML:XML in data.member) {
41                     var type:String = memberXML.@type.toLowerCase();
42                     var role:String = memberXML.@role;
43                     var memberID:Number = Number(memberXML.@ref);
44                     var member:Entity = null;
45                                         switch (memberXML.@type.toLowerCase()) {
46                                                 case 'node':            member=nodemap[memberID]; break;
47                                                 case 'way':                     member=waymap[memberID]; break;
48                                                 case 'relation':        break;  // ** TODO - cope with evil nested relations
49                                         }
50                                         if (member!=null) { members.push(new RelationMember(member,role)); }
51                 }
52                                 relationmap[oldid] = connection.createRelation(parseTags(data.tag), members, push);
53             }
54         }
55
56         private function parseTags(tagElements:XMLList):Object {
57             var tags:Object = {};
58             for each (var tagEl:XML in tagElements)
59                 tags[tagEl.@k] = tagEl.@v.substr(0,255);
60             return tags;
61         }
62
63         }
64 }