ad6a391518f2b7c749584a8f866908eebd997183
[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                         var minlon:Number=map.bounds.@minlon;
34                         var maxlon:Number=map.bounds.@maxlon;
35                         var minlat:Number=map.bounds.@minlat;
36                         var maxlat:Number=map.bounds.@maxlat;
37
38             for each(var relData:XML in map.relation) {
39                 id = Number(relData.@id);
40                 version = uint(relData.@version);
41                 uid = Number(relData.@uid);
42                 timestamp = relData.@timestamp;
43                 
44                 var rel:Relation = getRelation(id);
45                 if ( rel == null || !rel.loaded ) {
46                     tags = parseTags(relData.tag);
47                     var members:Array = [];
48                     for each(var memberXML:XML in relData.member) {
49                         var type:String = memberXML.@type.toLowerCase();
50                         var role:String = memberXML.@role;
51                         var memberID:Number = Number(memberXML.@ref);
52                         var member:Entity = null;
53                         if ( type == "node" ) {
54                             member = getNode(memberID);
55                             if ( member == null ) {
56                                 member = new Node(memberID,0,{},false,0,0);
57                                 setNode(Node(member),true);
58                             } else if (member.isDeleted()) {
59                                 member.setDeletedState(false);
60                             }
61                         } else if ( type == "way" ) {
62                             member = getWay(memberID);
63                             if (member == null) {
64                                 member = new Way(memberID,0,{},false,[]);
65                                 setWay(Way(member),true);
66                             }
67                         } else if ( type == "relation" ) {
68                             member = getRelation(memberID);
69                             if (member == null) {
70                                 member = new Relation(memberID,0,{},false,[]);
71                                 setRelation(Relation(member),true);
72                             }
73                         }
74                         
75                         if ( member != null )
76                             members.push(new RelationMember(member, role));
77                     }
78                     
79                     if ( rel == null )
80                         setRelation(new Relation(id, version, tags, true, members, uid, timestamp), false);
81                     else {
82                         rel.update(version, tags, true, false, members, uid, timestamp);
83                         sendEvent(new EntityEvent(NEW_RELATION, rel), false);
84                     }
85                 }
86             }
87
88             for each(var nodeData:XML in map.node) {
89                                 id = Number(nodeData.@id);
90                                 node = getNode(id);
91                                 newNode = new Node(id, 
92                                                    uint(nodeData.@version), 
93                                                    parseTags(nodeData.tag),
94                                                    true, 
95                                                    Number(nodeData.@lat),
96                                                    Number(nodeData.@lon),
97                                                    Number(nodeData.@uid),
98                                                    nodeData.@timestamp);
99                                 
100                                 if ( node == null || !node.loaded) {
101                                         newNode.parentsLoaded=newNode.within(minlon,maxlon,minlat,maxlat);
102                                         setOrUpdateNode(newNode, true);
103                                 } else {
104                                         // the node's already in memory, but store it in case one of the new ways needs it
105                                         if (newNode.within(minlon,maxlon,minlat,maxlat)) newNode.parentsLoaded=true;
106                                         unusedNodes[id]=newNode;
107                                 }
108                         }
109             
110             for each(var data:XML in map.way) {
111                 id = Number(data.@id);
112                 version = uint(data.@version);
113                 uid = Number(data.@uid);
114                 timestamp = data.@timestamp;
115
116                 var way:Way = getWay(id);
117                 if ( way == null || !way.loaded ) {
118                     var nodes:Array = [];
119                     for each(var nd:XML in data.nd) {
120                                                 var nodeid:Number=Number(nd.@ref)
121                                                 if (getNode(nodeid).isDeleted() && unusedNodes[nodeid]) { 
122                                                         setOrUpdateNode(unusedNodes[nodeid], true); 
123                                                 }
124                         nodes.push(getNode(nodeid));
125                                         }
126                     tags = parseTags(data.tag);
127                     if ( way == null ) {
128                         setWay(new Way(id, version, tags, true, nodes, uid, timestamp),false);
129                     } else {
130                                                 waycount++;
131                         way.update(version, tags, true, true, nodes, uid, timestamp);
132                         sendEvent(new EntityEvent(NEW_WAY, way), false);
133                     }
134                 }
135             }
136             
137             registerPOINodes();
138         }
139         
140         protected function registerPOINodes():void {
141             for each (var nodeID:Number in getAllNodeIDs()) {
142                 var node:Node = getNode(nodeID);
143                 if (!node.hasParentWays)
144                     registerPOI(node);
145             }
146         }
147
148         private function parseTags(tagElements:XMLList):Object {
149             var tags:Object = {};
150             for each (var tagEl:XML in tagElements)
151                 tags[tagEl.@k] = tagEl.@v;
152             return tags;
153         }
154
155         }
156 }