6aa469e0ee3142cef506b7135b4419f1d46672ec
[potlatch2.git] / net / systemeD / halcyon / Map.as
1 package net.systemeD.halcyon {
2
3         import flash.text.TextField;
4         import flash.display.DisplayObjectContainer;
5         import flash.display.Sprite;
6         import flash.display.Shape;
7         import flash.display.Stage;
8         import flash.events.*;
9         import net.systemeD.halcyon.styleparser.*;
10         import flash.net.*;
11         
12     public class Map extends Sprite {
13
14                 public const MASTERSCALE:Number=5825.4222222222;// master map scale - how many Flash pixels in 1 degree longitude
15                                                                                                                 // (for Landsat, 5120)
16                 public const MINSCALE:uint=13;                                  // don't zoom out past this
17                 public const MAXSCALE:uint=19;                                  // don't zoom in past this
18
19                 public var ruleset:RuleSet=new RuleSet();               // rules
20                 
21                 public var ways:Object=new Object();                    // geodata
22                 public var nodes:Object=new Object();                   //  |
23                 public var pois:Object=new Object();                    //  |
24                 public var relations:Object=new Object();               //  |
25
26                 public var scale:uint=14;                                               // map scale
27                 public var scalefactor:Number=MASTERSCALE;              // current scaling factor for lon/latp
28                 public var bigedge_l:Number= 999999;                    // area of largest whichways
29                 public var bigedge_r:Number=-999999;                    //  |
30                 public var bigedge_b:Number= 999999;                    //  |
31                 public var bigedge_t:Number=-999999;                    //  |
32
33                 public var waycount:uint=0;                                             // ways:                number currently loaded
34                 public var waysrequested:uint=0;                                //                              total number requested
35                 public var waysreceived:uint=0;                                 //                              total number received
36                 public var relcount:uint=0;                                             // relations:   number currently loaded
37                 public var relsrequested:uint=0;                                //                              total number requested
38                 public var relsreceived:uint=0;                                 //                              total number received
39                 public var poicount:uint=0;                                             // POIs:                number currently loaded
40                 public var whichrequested:uint=0;                               // whichways:   total number requested
41                 public var whichreceived:uint=0;                                //                              total number received
42
43                 public var edge_l:Number;                                               // current bounding box
44                 public var edge_r:Number;                                               //  |
45                 public var edge_t:Number;                                               //  |
46                 public var edge_b:Number;                                               //  |
47
48                 public var baselon:Number;                                              // urllon-xradius/masterscale;
49                 public var basey:Number;                                                // lat2lat2p(urllat)+yradius/masterscale;
50                 public var mapwidth:uint;                                               // width (Flash pixels)
51                 public var mapheight:uint;                                              // height (Flash pixels)
52
53                 private var dragging:Boolean=false;                             // dragging map?
54                 private var lastxmouse:Number;                                  //  |
55                 private var lastymouse:Number;                                  //  |
56                 
57                 public var backdrop:Object;                                             // reference to backdrop sprite
58                 
59                 public var connection:AMFConnection;                    // server connection
60
61                 // ------------------------------------------------------------------------------------------
62                 // Map constructor function
63
64         public function Map() {
65
66                         for (var l:int=0; l<11; l++) {                          // 11 layers (10 is +5, 0 is -5)
67                                 var s:Sprite=new Sprite();
68                                 s.addChild(new Sprite());                               // [layer][0]=fill, [1]=stroke, [2]=names
69                                 s.addChild(new Sprite());
70                                 s.addChild(new Sprite());
71                                 addChild(s);
72                         }
73
74                         connection=new AMFConnection(
75                                 "http://127.0.0.1:3000/api/0.6/amf/read",
76                                 "http://127.0.0.1:3000/api/0.6/amf/write",
77                                 "http://127.0.0.1:3000/api/crossdomain.xml");
78                         connection.getEnvironment(new Responder(gotEnvironment,connectionError));
79
80         }
81
82                 public function gotEnvironment(r:Object):void {
83                         init(52.022,-1.2745);
84                 }
85
86                 // ------------------------------------------------------------------------------------------
87                 // Initialise map at a given lat/lon
88
89         public function init(startlat:Number,startlon:Number):void {
90
91                         ruleset.load("test.yaml?d="+Math.random());
92 //                      rules.initExample();            // initialise dummy rules
93
94                         updateSize();
95                         baselon  =startlon                      -(mapwidth /2)/MASTERSCALE;
96                         basey    =lat2latp(startlat)+(mapheight/2)/MASTERSCALE;
97                         addDebug("Baselon "+baselon+", basey "+basey);
98                         updateCoords(0,0);
99                         download();
100                         
101         }
102
103                 // ------------------------------------------------------------------------------------------
104                 // Recalculate co-ordinates from new Flash origin
105
106                 public function updateCoords(tx:Number,ty:Number):void {
107                         x=tx; y=ty;
108
109                         // ** calculate tile_l etc.
110                         edge_t=coord2lat(-y          );
111                         edge_b=coord2lat(-y+mapheight);
112                         edge_l=coord2lon(-x          );
113                         edge_r=coord2lon(-x+mapwidth );
114                         addDebug("Lon "+edge_l+"-"+edge_r);
115                         addDebug("Lat "+edge_b+"-"+edge_t);
116                 }
117                 
118                 public function updateCoordsFromLatLon(lat:Number,lon:Number):void {
119                         var cy:Number=-(lat2coord(lat)-mapheight/2);
120                         var cx:Number=-(lon2coord(lon)-mapwidth/2);
121                         updateCoords(cx,cy);
122                 }
123
124
125                 // Co-ordinate conversion functions
126
127                 public function latp2coord(a:Number):Number     { return -(a-basey)*scalefactor; }
128                 public function coord2latp(a:Number):Number     { return a/-scalefactor+basey; }
129                 public function lon2coord(a:Number):Number      { return (a-baselon)*scalefactor; }
130                 public function coord2lon(a:Number):Number      { return a/scalefactor+baselon; }
131
132                 public function latp2lat(a:Number):Number       { return 180/Math.PI * (2 * Math.atan(Math.exp(a*Math.PI/180)) - Math.PI/2); }
133                 public function lat2latp(a:Number):Number       { return 180/Math.PI * Math.log(Math.tan(Math.PI/4+a*(Math.PI/180)/2)); }
134
135                 public function lat2coord(a:Number):Number      { return -(lat2latp(a)-basey)*scalefactor; }
136                 public function coord2lat(a:Number):Number      { return latp2lat(a/-scalefactor+basey); }
137
138 //              public function centrelat(o) { return coord2lat((yradius-_root.map._y-o)/Math.pow(2,_root.scale-13)); }
139 //              public function centrelon(o) { return coord2lon((xradius-_root.map._x-o)/Math.pow(2,_root.scale-13)); }
140
141
142                 // ------------------------------------------------------------------------------------------
143                 // Resize map size based on current stage and height
144
145                 public function updateSize():void {
146                         mapwidth =stage.stageWidth; mask.width=mapwidth; backdrop.width=mapwidth;
147                         mapheight=stage.stageHeight; mask.height=mapheight; backdrop.height=mapheight;
148                 }
149
150                 // ------------------------------------------------------------------------------------------
151                 // Download map data
152                 // (typically from whichways, but will want to add more connections)
153
154                 public function download():void {
155                         if (edge_l>=bigedge_l && edge_r<=bigedge_r &&
156                                 edge_b>=bigedge_b && edge_t<=bigedge_t) { return; }     // we have already loaded this area, so ignore
157                         bigedge_l=edge_l; bigedge_r=edge_r;
158                         bigedge_b=edge_b; bigedge_t=edge_t;
159                         addDebug("Calling with "+edge_l+"-"+edge_r+", "+edge_t+"-"+edge_b);
160                         connection.getBbox(edge_l,edge_r,edge_t,edge_b,new Responder(gotBbox,connectionError));
161                 }
162
163                 public function gotBbox(r:Object):void {
164                         addDebug("got whichways");
165                         var code:uint         =r.shift(); if (code) { connectionError(); return; }
166                         var message:String    =r.shift();
167                         var waylist:Array     =r[0];
168                         var pointlist:Array   =r[1];
169                         var relationlist:Array=r[2];
170                         var i:uint, v:uint;
171
172                         for each (var w:Array in waylist) {
173                                 i=w[0]; v=w[1];
174                                 if (ways[i] && ways[i].version==v) { continue; }
175                                 ways[i]=new Way(i,v,this);
176                                 ways[i].load(connection);
177                         }
178
179                         for each (var p:Array in pointlist) {
180                                 i=w[0]; v=w[4];
181                                 if (pois[i] && pois[i].version==v) { continue; }
182                                 pois[i]=new POI(i,v,w[1],w[2],w[3],this);
183                         }
184
185                         addDebug("waylist is "+waylist);
186                 }
187
188
189                 // ------------------------------------------------------------------------------------------
190                 // Redraw all items, zoom in and out
191                 
192                 public function redraw():void {
193                         addDebug("redrawing");
194                         var s:String='';
195                         for each (var w:Way in ways) { w.redraw(); s+=w.id+","; }
196                         addDebug(s);
197                         // ** do POIs, etc.
198                 }
199
200                 public function zoomIn():void {
201                         if (scale==MAXSCALE) { return; }
202                         changeScale(scale+1);
203                 }
204
205                 public function zoomOut():void {
206                         if (scale==MINSCALE) { return; }
207                         changeScale(scale-1);
208                 }
209
210                 private function changeScale(newscale:uint):void {
211                         addDebug("new scale "+newscale);
212                         scale=newscale;
213                         scalefactor=MASTERSCALE/Math.pow(2,14-scale);
214                         updateCoordsFromLatLon((edge_t+edge_b)/2,(edge_l+edge_r)/2);    // recentre
215                         download();
216                         redraw();
217                 }
218
219                 private function reportPosition():void {
220                         addDebug("lon "+coord2lon(mouseX)+", lat "+coord2lat(mouseY));
221                 }
222
223
224                 // ==========================================================================================
225                 // Events
226                 
227                 // ------------------------------------------------------------------------------------------
228                 // Mouse events
229                 
230                 public function mouseDownHandler(event:MouseEvent):void {
231                         dragging=true;
232                         lastxmouse=mouseX; lastymouse=mouseY;
233                 }
234         
235                 public function mouseUpHandler(event:MouseEvent):void {
236                         if (!dragging) { return; }
237                         dragging=false;
238                         updateCoords(x,y);
239                         download();
240                 }
241         
242                 public function mouseMoveHandler(event:MouseEvent):void {
243                         if (!dragging) { return; }
244                         x+=mouseX-lastxmouse;
245                         y+=mouseY-lastymouse;
246                         lastxmouse=mouseX; lastymouse=mouseY;
247                 }
248         
249                 // ------------------------------------------------------------------------------------------
250                 // Miscellaneous events
251                 
252                 public function keyUpHandler(event:KeyboardEvent):void {
253 addDebug("pressed "+event.keyCode);
254                         if (event.keyCode==82) { this.redraw(); }                       // R - redraw
255                         if (event.keyCode==73) { this.zoomIn(); }                       // I - zoom in
256                         if (event.keyCode==79) { this.zoomOut(); }                      // O - zoom out
257                         if (event.keyCode==76) { this.reportPosition(); }       // L - report lat/long
258                 }
259
260                 public function connectionError(err:Object=null): void {
261                         addDebug("got error"); 
262                 }
263
264                 // ------------------------------------------------------------------------------------------
265                 // Debugging
266                 
267                 public function addDebug(text:String):void {
268                         Globals.vars.debug.appendText(text+"\n");
269                         Globals.vars.debug.scrollV=Globals.vars.debug.maxScrollV;
270                 }
271                 
272         }
273 }