give Halcyon the ability to load .osm files
[potlatch2.git] / net / systemeD / halcyon / connection / XMLBaseConnection.as
1 package net.systemeD.halcyon.connection {
2
3     import flash.events.*;
4
5         import flash.system.Security;
6         import flash.net.*;
7     import org.iotashan.oauth.*;
8
9         import net.systemeD.halcyon.Globals;
10
11         public class XMLBaseConnection extends Connection {
12
13                 public function XMLBaseConnection() {
14                 }
15                 
16         protected function loadedMap(event:Event):void {
17             dispatchEvent(new Event(LOAD_COMPLETED));
18
19             var map:XML = new XML(URLLoader(event.target).data);
20             var id:Number;
21             var version:uint;
22             var tags:Object;
23
24             for each(var nodeData:XML in map.node) {
25                 id = Number(nodeData.@id);
26                 version = uint(nodeData.@version);
27
28                 var node:Node = getNode(id);
29                 if ( node == null || !node.loaded ) {
30                     var lat:Number = Number(nodeData.@lat);
31                     var lon:Number = Number(nodeData.@lon);
32                     tags = parseTags(nodeData.tag);
33                     if ( node == null )
34                         setNode(new Node(id, version, tags, true, lat, lon),false);
35                     else {
36                         node.update(version, tags, true, lat, lon);
37                         sendEvent(new EntityEvent(NEW_NODE, node), false);
38                     }
39                 }
40             }
41
42             for each(var data:XML in map.way) {
43                 id = Number(data.@id);
44                 version = uint(data.@version);
45
46                 var way:Way = getWay(id);
47                 if ( way == null || !way.loaded ) {
48                     var nodes:Array = [];
49                     for each(var nd:XML in data.nd)
50                         nodes.push(getNode(Number(nd.@ref)));
51                     tags = parseTags(data.tag);
52                     if ( way == null )
53                         setWay(new Way(id, version, tags, true, nodes),false);
54                     else {
55                         way.update(version, tags, true, nodes);
56                         sendEvent(new EntityEvent(NEW_WAY, way), false);
57                     }
58                 }
59             }
60             
61             for each(var relData:XML in map.relation) {
62                 id = Number(relData.@id);
63                 version = uint(relData.@version);
64                 
65                 var rel:Relation = getRelation(id);
66                 if ( rel == null || !rel.loaded ) {
67                     tags = parseTags(relData.tag);
68                     var members:Array = [];
69                     for each(var memberXML:XML in relData.member) {
70                         var type:String = memberXML.@type.toLowerCase();
71                         var role:String = memberXML.@role;
72                         var memberID:Number = Number(memberXML.@ref);
73                         var member:Entity = null;
74                         if ( type == "node" ) {
75                             member = getNode(memberID);
76                             if ( member == null ) {
77                                 member = new Node(memberID,0,{},false,0,0);
78                                 setNode(Node(member),true);
79                             }
80                         } else if ( type == "way" ) {
81                             member = getWay(memberID);
82                             if (member == null) {
83                                 member = new Way(memberID,0,{},false,[]);
84                                 setWay(Way(member),true);
85                             }
86                         } else if ( type == "relation" ) {
87                             member = getRelation(memberID);
88                             if (member == null) {
89                                 member = new Relation(memberID,0,{},false,[]);
90                                 setRelation(Relation(member),true);
91                             }
92                         }
93                         
94                         if ( member != null )
95                             members.push(new RelationMember(member, role));
96                     }
97                     
98                     if ( rel == null )
99                         setRelation(new Relation(id, version, tags, true, members), false);
100                     else {
101                         rel.update(version,tags,true,members);
102                         sendEvent(new EntityEvent(NEW_RELATION, rel), false);
103                     }
104                 }
105             }
106             
107             registerPOINodes();
108         }
109         
110         protected function registerPOINodes():void {
111             for each (var nodeID:Number in getAllNodeIDs()) {
112                 var node:Node = getNode(nodeID);
113                 if (!node.hasParentWays)
114                     registerPOI(node);
115             }
116         }
117
118         private function parseTags(tagElements:XMLList):Object {
119             var tags:Object = {};
120             for each (var tagEl:XML in tagElements)
121                 tags[tagEl.@k] = tagEl.@v;
122             return tags;
123         }
124
125         }
126 }