more turn restriction stuff, and checkboxes, and a bunch of little fixes
[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 relData:XML in map.relation) {
25                 id = Number(relData.@id);
26                 version = uint(relData.@version);
27                 
28                 var rel:Relation = getRelation(id);
29                 if ( rel == null || !rel.loaded ) {
30                     tags = parseTags(relData.tag);
31                     var members:Array = [];
32                     for each(var memberXML:XML in relData.member) {
33                         var type:String = memberXML.@type.toLowerCase();
34                         var role:String = memberXML.@role;
35                         var memberID:Number = Number(memberXML.@ref);
36                         var member:Entity = null;
37                         if ( type == "node" ) {
38                             member = getNode(memberID);
39                             if ( member == null ) {
40                                 member = new Node(memberID,0,{},false,0,0);
41                                 setNode(Node(member),true);
42                             }
43                         } else if ( type == "way" ) {
44                             member = getWay(memberID);
45                             if (member == null) {
46                                 member = new Way(memberID,0,{},false,[]);
47                                 setWay(Way(member),true);
48                             }
49                         } else if ( type == "relation" ) {
50                             member = getRelation(memberID);
51                             if (member == null) {
52                                 member = new Relation(memberID,0,{},false,[]);
53                                 setRelation(Relation(member),true);
54                             }
55                         }
56                         
57                         if ( member != null )
58                             members.push(new RelationMember(member, role));
59                     }
60                     
61                     if ( rel == null )
62                         setRelation(new Relation(id, version, tags, true, members), false);
63                     else {
64                         rel.update(version,tags,true,members);
65                         sendEvent(new EntityEvent(NEW_RELATION, rel), false);
66                     }
67                 }
68             }
69             
70             for each(var nodeData:XML in map.node) {
71                 id = Number(nodeData.@id);
72                 version = uint(nodeData.@version);
73
74                 var node:Node = getNode(id);
75                 if ( node == null || !node.loaded ) {
76                     var lat:Number = Number(nodeData.@lat);
77                     var lon:Number = Number(nodeData.@lon);
78                     tags = parseTags(nodeData.tag);
79                     if ( node == null )
80                         setNode(new Node(id, version, tags, true, lat, lon),false);
81                     else {
82                         node.update(version, tags, true, lat, lon);
83                         sendEvent(new EntityEvent(NEW_NODE, node), false);
84                     }
85                 }
86             }
87
88             for each(var data:XML in map.way) {
89                 id = Number(data.@id);
90                 version = uint(data.@version);
91
92                 var way:Way = getWay(id);
93                 if ( way == null || !way.loaded ) {
94                     var nodes:Array = [];
95                     for each(var nd:XML in data.nd)
96                         nodes.push(getNode(Number(nd.@ref)));
97                     tags = parseTags(data.tag);
98                     if ( way == null )
99                         setWay(new Way(id, version, tags, true, nodes),false);
100                     else {
101                         way.update(version, tags, true, nodes);
102                         sendEvent(new EntityEvent(NEW_WAY, way), 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 }