basic POI rendering support
[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<13; 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                         s=new Sprite(); addChild(s);                            // 11 - POIs
74                         s=new Sprite(); addChild(s);                            // 12 - shields
75
76                         connection=new AMFConnection(
77                                 "http://127.0.0.1:3000/api/0.6/amf/read",
78                                 "http://127.0.0.1:3000/api/0.6/amf/write",
79                                 "http://127.0.0.1:3000/api/crossdomain.xml");
80                         connection.getEnvironment(new Responder(gotEnvironment,connectionError));
81
82         }
83
84                 public function gotEnvironment(r:Object):void {
85                         init(52.022,-1.2745);
86                 }
87
88                 // ------------------------------------------------------------------------------------------
89                 // Initialise map at a given lat/lon
90
91         public function init(startlat:Number,startlon:Number):void {
92
93                         ruleset.load("test.yaml?d="+Math.random());
94 //                      rules.initExample();            // initialise dummy rules
95
96                         updateSize();
97                         baselon  =startlon                      -(mapwidth /2)/MASTERSCALE;
98                         basey    =lat2latp(startlat)+(mapheight/2)/MASTERSCALE;
99                         addDebug("Baselon "+baselon+", basey "+basey);
100                         updateCoords(0,0);
101                         download();
102                         
103         }
104
105                 // ------------------------------------------------------------------------------------------
106                 // Recalculate co-ordinates from new Flash origin
107
108                 public function updateCoords(tx:Number,ty:Number):void {
109                         x=tx; y=ty;
110
111                         // ** calculate tile_l etc.
112                         edge_t=coord2lat(-y          );
113                         edge_b=coord2lat(-y+mapheight);
114                         edge_l=coord2lon(-x          );
115                         edge_r=coord2lon(-x+mapwidth );
116                         addDebug("Lon "+edge_l+"-"+edge_r);
117                         addDebug("Lat "+edge_b+"-"+edge_t);
118                 }
119                 
120                 public function updateCoordsFromLatLon(lat:Number,lon:Number):void {
121                         var cy:Number=-(lat2coord(lat)-mapheight/2);
122                         var cx:Number=-(lon2coord(lon)-mapwidth/2);
123                         updateCoords(cx,cy);
124                 }
125
126
127                 // Co-ordinate conversion functions
128
129                 public function latp2coord(a:Number):Number     { return -(a-basey)*scalefactor; }
130                 public function coord2latp(a:Number):Number     { return a/-scalefactor+basey; }
131                 public function lon2coord(a:Number):Number      { return (a-baselon)*scalefactor; }
132                 public function coord2lon(a:Number):Number      { return a/scalefactor+baselon; }
133
134                 public function latp2lat(a:Number):Number       { return 180/Math.PI * (2 * Math.atan(Math.exp(a*Math.PI/180)) - Math.PI/2); }
135                 public function lat2latp(a:Number):Number       { return 180/Math.PI * Math.log(Math.tan(Math.PI/4+a*(Math.PI/180)/2)); }
136
137                 public function lat2coord(a:Number):Number      { return -(lat2latp(a)-basey)*scalefactor; }
138                 public function coord2lat(a:Number):Number      { return latp2lat(a/-scalefactor+basey); }
139
140 //              public function centrelat(o) { return coord2lat((yradius-_root.map._y-o)/Math.pow(2,_root.scale-13)); }
141 //              public function centrelon(o) { return coord2lon((xradius-_root.map._x-o)/Math.pow(2,_root.scale-13)); }
142
143
144                 // ------------------------------------------------------------------------------------------
145                 // Resize map size based on current stage and height
146
147                 public function updateSize():void {
148                         mapwidth =stage.stageWidth; mask.width=mapwidth; backdrop.width=mapwidth;
149                         mapheight=stage.stageHeight; mask.height=mapheight; backdrop.height=mapheight;
150                 }
151
152                 // ------------------------------------------------------------------------------------------
153                 // Download map data
154                 // (typically from whichways, but will want to add more connections)
155
156                 public function download():void {
157                         if (edge_l>=bigedge_l && edge_r<=bigedge_r &&
158                                 edge_b>=bigedge_b && edge_t<=bigedge_t) { return; }     // we have already loaded this area, so ignore
159                         bigedge_l=edge_l; bigedge_r=edge_r;
160                         bigedge_b=edge_b; bigedge_t=edge_t;
161                         addDebug("Calling with "+edge_l+"-"+edge_r+", "+edge_t+"-"+edge_b);
162                         connection.getBbox(edge_l,edge_r,edge_t,edge_b,new Responder(gotBbox,connectionError));
163                 }
164
165                 public function gotBbox(r:Object):void {
166                         addDebug("got whichways");
167                         var code:uint         =r.shift(); if (code) { connectionError(); return; }
168                         var message:String    =r.shift();
169                         var waylist:Array     =r[0];
170                         var pointlist:Array   =r[1];
171                         var relationlist:Array=r[2];
172                         var i:uint, v:uint;
173
174                         for each (var w:Array in waylist) {
175                                 i=w[0]; v=w[1];
176                                 if (ways[i] && ways[i].version==v) { continue; }
177                                 ways[i]=new Way(i,v,this);
178                                 ways[i].load(connection);
179                         }
180
181                         for each (var p:Array in pointlist) {
182                                 i=p[0]; v=p[4];
183                                 if (pois[i] && pois[i].version==v) { continue; }
184                                 pois[i]=new POI(i,v,p[1],p[2],p[3],this);
185                         }
186
187                         addDebug("waylist is "+waylist);
188                 }
189
190
191                 // ------------------------------------------------------------------------------------------
192                 // Redraw all items, zoom in and out
193                 
194                 public function redraw():void {
195                         addDebug("redrawing");
196                         var s:String='';
197                         for each (var w:Way in ways) { w.redraw(); s+=w.id+","; }
198                         addDebug(s);
199                         // ** do POIs, etc.
200                 }
201
202                 public function zoomIn():void {
203                         if (scale==MAXSCALE) { return; }
204                         changeScale(scale+1);
205                 }
206
207                 public function zoomOut():void {
208                         if (scale==MINSCALE) { return; }
209                         changeScale(scale-1);
210                 }
211
212                 private function changeScale(newscale:uint):void {
213                         addDebug("new scale "+newscale);
214                         scale=newscale;
215                         scalefactor=MASTERSCALE/Math.pow(2,14-scale);
216                         updateCoordsFromLatLon((edge_t+edge_b)/2,(edge_l+edge_r)/2);    // recentre
217                         download();
218                         redraw();
219                 }
220
221                 private function reportPosition():void {
222                         addDebug("lon "+coord2lon(mouseX)+", lat "+coord2lat(mouseY));
223                 }
224
225
226                 // ==========================================================================================
227                 // Events
228                 
229                 // ------------------------------------------------------------------------------------------
230                 // Mouse events
231                 
232                 public function mouseDownHandler(event:MouseEvent):void {
233                         dragging=true;
234                         lastxmouse=mouseX; lastymouse=mouseY;
235                 }
236         
237                 public function mouseUpHandler(event:MouseEvent):void {
238                         if (!dragging) { return; }
239                         dragging=false;
240                         updateCoords(x,y);
241                         download();
242                 }
243         
244                 public function mouseMoveHandler(event:MouseEvent):void {
245                         if (!dragging) { return; }
246                         x+=mouseX-lastxmouse;
247                         y+=mouseY-lastymouse;
248                         lastxmouse=mouseX; lastymouse=mouseY;
249                 }
250         
251                 // ------------------------------------------------------------------------------------------
252                 // Miscellaneous events
253                 
254                 public function keyUpHandler(event:KeyboardEvent):void {
255 addDebug("pressed "+event.keyCode);
256                         if (event.keyCode==82) { this.redraw(); }                       // R - redraw
257                         if (event.keyCode==73) { this.zoomIn(); }                       // I - zoom in
258                         if (event.keyCode==79) { this.zoomOut(); }                      // O - zoom out
259                         if (event.keyCode==76) { this.reportPosition(); }       // L - report lat/long
260                 }
261
262                 public function connectionError(err:Object=null): void {
263                         addDebug("got error"); 
264                 }
265
266                 // ------------------------------------------------------------------------------------------
267                 // Debugging
268                 
269                 public function addDebug(text:String):void {
270                         Globals.vars.debug.appendText(text+"\n");
271                         Globals.vars.debug.scrollV=Globals.vars.debug.maxScrollV;
272                 }
273                 
274         }
275 }