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