More refactoring in progress
[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(name:String,api:String,policy:String,initparams:Object) {
17                         super(name,api,policy,initparams);
18                 }
19                 
20         protected function loadedMap(event:Event):void {
21             var map:XML = new XML(URLLoader(event.target).data);
22             var id:Number;
23             var version:uint;
24             var uid:Number;
25             var timestamp: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                 
46                 var rel:Relation = getRelation(id);
47                 if ( rel == null || !rel.loaded ) {
48                     tags = parseTags(relData.tag);
49                     var members:Array = [];
50                     for each(var memberXML:XML in relData.member) {
51                         var type:String = memberXML.@type.toLowerCase();
52                         var role:String = memberXML.@role;
53                         var memberID:Number = Number(memberXML.@ref);
54                         var member:Entity = null;
55                         if ( type == "node" ) {
56                             member = getNode(memberID);
57                             if ( member == null ) {
58                                 member = new Node(this,memberID,0,{},false,0,0);
59                                 setNode(Node(member),true);
60                             } else if (member.isDeleted()) {
61                                 member.setDeletedState(false);
62                             }
63                         } else if ( type == "way" ) {
64                             member = getWay(memberID);
65                             if (member == null) {
66                                 member = new Way(this,memberID,0,{},false,[]);
67                                 setWay(Way(member),true);
68                             }
69                         } else if ( type == "relation" ) {
70                             member = getRelation(memberID);
71                             if (member == null) {
72                                 member = new Relation(this,memberID,0,{},false,[]);
73                                 setRelation(Relation(member),true);
74                             }
75                         }
76                         
77                         if ( member != null )
78                             members.push(new RelationMember(member, role));
79                     }
80                     
81                     if ( rel == null )
82                         setRelation(new Relation(this, id, version, tags, true, members, uid, timestamp), false);
83                     else {
84                         rel.update(version, tags, true, false, members, uid, timestamp);
85                         sendEvent(new EntityEvent(NEW_RELATION, rel), false);
86                     }
87                 }
88             }
89
90             for each(var nodeData:XML in map.node) {
91                                 id = Number(nodeData.@id);
92                                 node = getNode(id);
93                                 newNode = new Node(this,
94                                                    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                                 
103                                 if ( singleEntityRequest ) {
104                                         // it's a revert request, so create/update the node
105                                         setOrUpdateNode(newNode, true);
106                                 } else if ( node == null || !node.loaded) {
107                                         // the node didn't exist before, so create/update it
108                                         newNode.parentsLoaded=newNode.within(minlon,maxlon,minlat,maxlat);
109                                         setOrUpdateNode(newNode, true);
110                                 } else {
111                                         // the node's already in memory, but store it in case one of the new ways needs it
112                                         if (newNode.within(minlon,maxlon,minlat,maxlat)) newNode.parentsLoaded=true;
113                                         unusedNodes[id]=newNode;
114                                 }
115                         }
116             
117             for each(var data:XML in map.way) {
118                 id = Number(data.@id);
119                 version = uint(data.@version);
120                 uid = Number(data.@uid);
121                 timestamp = data.@timestamp;
122
123                 var way:Way = getWay(id);
124                 if ( way == null || !way.loaded || singleEntityRequest) {
125                     var nodes:Array = [];
126                     for each(var nd:XML in data.nd) {
127                                                 var nodeid:Number=Number(nd.@ref)
128                                                 if (getNode(nodeid).isDeleted() && unusedNodes[nodeid]) { 
129                                                         setOrUpdateNode(unusedNodes[nodeid], true); 
130                                                 }
131                         nodes.push(getNode(nodeid));
132                                         }
133                     tags = parseTags(data.tag);
134                     if ( way == null ) {
135                         setWay(new Way(this, id, version, tags, true, nodes, uid, timestamp),false);
136                     } else {
137                                                 waycount++;
138                         way.update(version, tags, true, true, nodes, uid, timestamp);
139                         sendEvent(new EntityEvent(NEW_WAY, way), false);
140                     }
141                 }
142             }
143             
144             markBboxLoaded(minlon,maxlon,maxlat,minlat);
145             registerPOINodes();
146             dispatchEvent(new Event(LOAD_COMPLETED));
147         }
148         
149         protected function registerPOINodes():void {
150             for each (var nodeID:Number in getAllNodeIDs()) {
151                 var node:Node = getNode(nodeID);
152                 if (!node.hasParentWays)
153                     registerPOI(node);
154             }
155         }
156
157         private function parseTags(tagElements:XMLList):Object {
158             var tags:Object = {};
159             for each (var tagEl:XML in tagElements)
160                 tags[tagEl.@k] = tagEl.@v;
161             return tags;
162         }
163
164         }
165 }