allow deleted nodes to be reloaded if they're referenced by a newly loaded way. First...
[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     /**
12     * XMLBaseConnection is the common code between connecting to an OSM server
13     * (i.e. XMLConnection) and connecting to a standalone .osm file (i.e. OSMConnection)
14     * and so mainly concerns itself with /map -call-ish related matters
15     */
16         public class XMLBaseConnection extends Connection {
17
18                 public function XMLBaseConnection() {
19                 }
20                 
21         protected function loadedMap(event:Event):void {
22             dispatchEvent(new Event(LOAD_COMPLETED));
23
24             var map:XML = new XML(URLLoader(event.target).data);
25             var id:Number;
26             var version:uint;
27             var uid:Number;
28             var timestamp:String;
29             var tags:Object;
30             var node:Node, newNode:Node;
31             var unusedNodes:Object={};
32
33             for each(var relData:XML in map.relation) {
34                 id = Number(relData.@id);
35                 version = uint(relData.@version);
36                 uid = Number(relData.@uid);
37                 timestamp = relData.@timestamp;
38                 
39                 var rel:Relation = getRelation(id);
40                 if ( rel == null || !rel.loaded ) {
41                     tags = parseTags(relData.tag);
42                     var members:Array = [];
43                     for each(var memberXML:XML in relData.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                         if ( type == "node" ) {
49                             member = getNode(memberID);
50                             if ( member == null ) {
51                                 member = new Node(memberID,0,{},false,0,0);
52                                 setNode(Node(member),true);
53                             } else if (member.isDeleted()) {
54                                 member.setDeletedState(false);
55                             }
56                         } else if ( type == "way" ) {
57                             member = getWay(memberID);
58                             if (member == null) {
59                                 member = new Way(memberID,0,{},false,[]);
60                                 setWay(Way(member),true);
61                             }
62                         } else if ( type == "relation" ) {
63                             member = getRelation(memberID);
64                             if (member == null) {
65                                 member = new Relation(memberID,0,{},false,[]);
66                                 setRelation(Relation(member),true);
67                             }
68                         }
69                         
70                         if ( member != null )
71                             members.push(new RelationMember(member, role));
72                     }
73                     
74                     if ( rel == null )
75                         setRelation(new Relation(id, version, tags, true, members, uid, timestamp), false);
76                     else {
77                         rel.update(version, tags, true, members, uid, timestamp);
78                         sendEvent(new EntityEvent(NEW_RELATION, rel), false);
79                     }
80                 }
81             }
82
83             for each(var nodeData:XML in map.node) {
84                                 id = Number(nodeData.@id);
85                                 node = getNode(id);
86                                 newNode = new Node(id, 
87                                                    uint(nodeData.@version), 
88                                                    parseTags(nodeData.tag),
89                                                    true, 
90                                                    Number(nodeData.@lat),
91                                                    Number(nodeData.@lon),
92                                                    Number(nodeData.@uid),
93                                                    nodeData.@timestamp);
94                                 
95                                 if ( node == null || !node.loaded) {
96                                         setOrUpdateNode(newNode, true);
97                                 } else {
98                                         // the node's already in memory, but store it in case one of the new ways needs it
99                                         unusedNodes[id]=newNode;
100                                 }
101                         }
102             
103             for each(var data:XML in map.way) {
104                 id = Number(data.@id);
105                 version = uint(data.@version);
106                 uid = Number(data.@uid);
107                 timestamp = data.@timestamp;
108
109                 var way:Way = getWay(id);
110                 if ( way == null || !way.loaded ) {
111                     var nodes:Array = [];
112                     for each(var nd:XML in data.nd) {
113                                                 var nodeid:Number=Number(nd.@ref)
114                                                 if (getNode(nodeid).isDeleted() && unusedNodes[nodeid]) { 
115                                                         setOrUpdateNode(unusedNodes[nodeid], true); 
116                                                 }
117                         nodes.push(getNode(nodeid));
118                                         }
119                     tags = parseTags(data.tag);
120                     if ( way == null ) {
121                         setWay(new Way(id, version, tags, true, nodes, uid, timestamp),false);
122                     } else {
123                                                 waycount++;
124                         way.update(version, tags, true, nodes, uid, timestamp);
125                         sendEvent(new EntityEvent(NEW_WAY, way), false);
126                     }
127                 }
128             }
129             
130             registerPOINodes();
131         }
132         
133         protected function registerPOINodes():void {
134             for each (var nodeID:Number in getAllNodeIDs()) {
135                 var node:Node = getNode(nodeID);
136                 if (!node.hasParentWays)
137                     registerPOI(node);
138             }
139         }
140
141         private function parseTags(tagElements:XMLList):Object {
142             var tags:Object = {};
143             for each (var tagEl:XML in tagElements)
144                 tags[tagEl.@k] = tagEl.@v;
145             return tags;
146         }
147
148         }
149 }