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