Require entity constructors to provide uid and timestamps. Finish relation-related...
[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
31             for each(var relData:XML in map.relation) {
32                 id = Number(relData.@id);
33                 version = uint(relData.@version);
34                 uid = Number(relData.@uid);
35                 timestamp = relData.@timestamp;
36                 
37                 var rel:Relation = getRelation(id);
38                 if ( rel == null || !rel.loaded ) {
39                     tags = parseTags(relData.tag);
40                     var members:Array = [];
41                     for each(var memberXML:XML in relData.member) {
42                         var type:String = memberXML.@type.toLowerCase();
43                         var role:String = memberXML.@role;
44                         var memberID:Number = Number(memberXML.@ref);
45                         var member:Entity = null;
46                         if ( type == "node" ) {
47                             member = getNode(memberID);
48                             if ( member == null ) {
49                                 member = new Node(memberID,0,{},false,0,0);
50                                 setNode(Node(member),true);
51                             }
52                         } else if ( type == "way" ) {
53                             member = getWay(memberID);
54                             if (member == null) {
55                                 member = new Way(memberID,0,{},false,[]);
56                                 setWay(Way(member),true);
57                             }
58                         } else if ( type == "relation" ) {
59                             member = getRelation(memberID);
60                             if (member == null) {
61                                 member = new Relation(memberID,0,{},false,[]);
62                                 setRelation(Relation(member),true);
63                             }
64                         }
65                         
66                         if ( member != null )
67                             members.push(new RelationMember(member, role));
68                     }
69                     
70                     if ( rel == null )
71                         setRelation(new Relation(id, version, tags, true, members, uid, timestamp), false);
72                     else {
73                         rel.update(version, tags, true, members, uid, timestamp);
74                         sendEvent(new EntityEvent(NEW_RELATION, rel), false);
75                     }
76                 }
77             }
78             
79             for each(var nodeData:XML in map.node) {
80                 id = Number(nodeData.@id);
81                 version = uint(nodeData.@version);
82                 uid = Number(nodeData.@uid);
83                 timestamp = nodeData.@timestamp;
84
85                 var node:Node = getNode(id);
86                 if ( node == null || !node.loaded ) {
87                     var lat:Number = Number(nodeData.@lat);
88                     var lon:Number = Number(nodeData.@lon);
89                     tags = parseTags(nodeData.tag);
90                     if ( node == null )
91                         setNode(new Node(id, version, tags, true, lat, lon, uid, timestamp),false);
92                     else {
93                         node.update(version, tags, true, lat, lon, uid, timestamp);
94                         sendEvent(new EntityEvent(NEW_NODE, node), false);
95                     }
96                 }
97             }
98
99             for each(var data:XML in map.way) {
100                 id = Number(data.@id);
101                 version = uint(data.@version);
102                 uid = Number(data.@uid);
103                 timestamp = data.@timestamp;
104
105                 var way:Way = getWay(id);
106                 if ( way == null || !way.loaded ) {
107                     var nodes:Array = [];
108                     for each(var nd:XML in data.nd)
109                         nodes.push(getNode(Number(nd.@ref)));
110                     tags = parseTags(data.tag);
111                     if ( way == null )
112                         setWay(new Way(id, version, tags, true, nodes, uid, timestamp),false);
113                     else {
114                         way.update(version, tags, true, nodes, uid, timestamp);
115                         sendEvent(new EntityEvent(NEW_WAY, way), false);
116                     }
117                 }
118             }
119             
120             registerPOINodes();
121         }
122         
123         protected function registerPOINodes():void {
124             for each (var nodeID:Number in getAllNodeIDs()) {
125                 var node:Node = getNode(nodeID);
126                 if (!node.hasParentWays)
127                     registerPOI(node);
128             }
129         }
130
131         private function parseTags(tagElements:XMLList):Object {
132             var tags:Object = {};
133             for each (var tagEl:XML in tagElements)
134                 tags[tagEl.@k] = tagEl.@v;
135             return tags;
136         }
137
138         }
139 }