Separate Halcyon (rendering) into standalone target without Flex dependency; JavaScri...
[potlatch2.git] / net / systemeD / halcyon / connection / AMFConnection.as
1 package net.systemeD.halcyon.connection {
2
3     import flash.events.Event;
4     import flash.net.URLLoader;
5     import flash.net.URLRequest;
6
7         import flash.system.Security;
8         import flash.net.*;
9
10         public class AMFConnection extends Connection {
11
12                 public var readConnection:NetConnection;
13                 public var writeConnection:NetConnection;
14                 private var mapLoader:URLLoader;
15
16                 // ------------------------------------------------------------
17                 // Constructor for new AMFConnection
18
19                 public function AMFConnection() {
20
21                         if (Connection.policyURL!='')
22                 Security.loadPolicyFile(Connection.policyURL);
23
24                         readConnection=new NetConnection();
25                         readConnection.objectEncoding = flash.net.ObjectEncoding.AMF0;
26                         readConnection.connect(Connection.apiBaseURL+"amf/read");
27                         
28                         writeConnection=new NetConnection();
29                         writeConnection.objectEncoding = flash.net.ObjectEncoding.AMF0;
30                         writeConnection.connect(Connection.apiBaseURL+"amf/write");
31                         
32                 }
33
34                 override public function getEnvironment(responder:Responder):void {
35                         readConnection.call("getpresets",responder,null,"en");
36                 }
37                 
38                 override public function loadBbox(left:Number,right:Number,
39                                                                 top:Number,bottom:Number):void {
40                         readConnection.call("whichways",new Responder(gotBbox, error),left,bottom,right,top);
41                 }
42
43         private function gotBbox(r:Object):void {
44                         var code:uint         =r.shift();
45             if (code) {
46                 error(new Array(r.shift()));
47                 return;
48             }
49
50                         var message:String    =r.shift();
51                         var waylist:Array     =r[0];
52                         var pointlist:Array   =r[1];
53                         var relationlist:Array=r[2];
54                         var id:Number, version:uint;
55
56                         // Load ways
57
58                         for each (var w:Array in waylist) {
59                                 id=Number(w[0]);
60                 version=uint(w[1]);
61
62                 var way:Way = getWay(id);
63                 if ( way == null ) {
64                     loadWay(id);
65                 }
66                         }
67
68                         // Create POIs
69
70                         for each (var p:Array in pointlist) {
71                                 id = Number(p[0]);
72                 version = uint(p[4]);
73
74                 var node:Node = getNode(id);
75                 if ( node == null ) {
76                     var lat:Number = Number(p[2]);
77                     var lon:Number = Number(p[1]);
78                     var tags:Object = p[3];
79                     node = new Node(id, version, tags, lat, lon);
80                     setNode(node);
81                 }
82                 registerPOI(node);
83                         }
84         }
85
86         private function error(r:Object):void {}
87
88                 private function loadWay(id:uint):void {
89                         readConnection.call("getway",new Responder(gotWay, error),id);
90                 }
91
92                 private function gotWay(r:Object):void {
93                         var code:uint = r.shift();
94             if (code) {
95                 error(new Array(r.shift()));
96                 return;
97             }
98
99                         var message:String=r.shift();
100             var id:Number = Number(r[0]);
101                         var version:uint = uint(r[3]);
102
103             var way:Way = getWay(id);
104             if ( way != null )
105                 return;
106
107             var nodesAMF:Array = r[1];
108                         var tags:Object = r[2];
109                         
110             var nodes:Array = [];
111                         for each (var p:Array in nodesAMF) {
112                 var nodeID:Number = Number(p[2]);
113                 var nodeVersion:uint = uint(p[4]);
114
115                 var node:Node = getNode(nodeID);
116                 if ( node == null ) {
117                     var lon:Number = Number(p[0]);
118                     var lat:Number = Number(p[1]);
119                     var nodeTags:Object = p[3];
120                     node = new Node(nodeID, nodeVersion, nodeTags, lat, lon);
121                     setNode(node);
122                 }
123                 nodes.push(node);
124                         }
125
126             way = new Way(id, version, tags, nodes);
127             setWay(way);
128                 }
129
130         }
131 }