give Halcyon the ability to load .osm files
[potlatch2.git] / net / systemeD / halcyon / connection / OSMConnection.as
1 package net.systemeD.halcyon.connection {
2
3     import flash.events.*;
4         import flash.net.*;
5         import flash.utils.Dictionary;
6         import flash.system.Security;
7
8         import net.systemeD.halcyon.ExtendedURLLoader;
9         import net.systemeD.halcyon.Globals;
10
11         // Read-only connection from local files (for Halcyon)
12
13         // For a limited set of arbitrary files, invoke it like this:
14         //              fo.addVariable("api","http://127.0.0.1/~richard/potlatch2");            // base URL
15         //              fo.addVariable("connection","OSM");
16         //              fo.addVariable("files","-1.5_-1.4_52.1_52.2.osm");
17
18         // For evenly arranged tiles, invoke it like this:
19         //              fo.addVariable("api","http://127.0.0.1/~richard/potlatch2");            // base URL
20         //              fo.addVariable("connection","OSM");
21         //              fo.addVariable("tile_resolution","0.2");
22         // and it'll then look for '-1.4_-1.2_52_52.2.osm', '-1.2_-1_52_52.2.osm', and so on
23         // (this needs some more testing)
24
25         public class OSMConnection extends XMLBaseConnection {
26
27                 private var filemode:uint;
28                 private static const NAMED:uint=0;
29                 private static const TILED:uint=1;
30                 // are we running from a limited set of files, or can we request tiles for any bbox?
31
32                 private var bboxes:Dictionary=new Dictionary();
33                 private static const AVAILABLE:uint=0;
34                 private static const LOADED:uint=1;
35                 private static const LOADING:uint=2;
36                 private static const UNAVAILABLE:uint=3;
37                 // a hash of known files [left,right,top,bottom], and their current status
38
39                 private var tileResolution:Number;
40                 // degree resolution for tiles (e.g. 0.2)
41
42                 private static const FILENAME:RegExp=/([\-\d\.]+)_([\-\d\.]+)_([\-\d\.]+)_([\-\d\.]+)\./i;
43
44                 public function OSMConnection() {
45
46                         if (Connection.policyURL!='')
47                 Security.loadPolicyFile(Connection.policyURL);
48
49             tileResolution = Number(Connection.getParam("tile_resolution", "0.2"));
50
51                         var o:Object = new Object();
52                         var files:String = Connection.getParam("files","");
53                         if (files=="") {
54                                 filemode=TILED;
55                         } else {
56                                 filemode=NAMED;
57                                 for each (var file:String in files.split(/,/)) {
58                                         if ((o=FILENAME.exec(file))) {
59                                                 bboxes[[o[1],o[2],o[3],o[4]]]=AVAILABLE;
60                                         }
61                                 }
62                         }
63                 }
64                 
65                 override public function loadBbox(left:Number,right:Number,
66                                                                 top:Number,bottom:Number):void {
67                         var l:Number, r:Number, t:Number, b:Number, x:Number, y:Number, k:Array;
68
69                         // look through bboxes, assemble any within the requested bbox that are AVAILABLE
70                         for (var box:* in bboxes) {
71                                 k=box as Array;
72                                 l=k[0]; r=k[1]; t=k[2]; b=k[3];
73                                 if ( ( (left>=l && left<=r) || (right>=l && right<=r) || (left<l && right>r) ) &&
74                                          ( (top>=b && top<=t) || (bottom>=b && bottom<=t) || (bottom<b && top>t) ) ) {
75                                         // yay, it intersects
76                                         if (bboxes[box]==AVAILABLE) { loadFile(box); }
77                                 }
78                         }
79                         if (filemode==NAMED) { return; }
80                         
81                         // look through tiles for any areas that are not covered
82                         for (x=roundDown(left, tileResolution); x<=roundUp(right, tileResolution); x+=tileResolution) {
83                                 for (y=roundDown(bottom, tileResolution); y<=roundUp(top, tileResolution); y+=tileResolution) {
84                                         k=[x,x+tileResolution,y,y+tileResolution];
85                                         if (bboxes[k]) { 
86                                                 if (bboxes[k]==AVAILABLE) { loadFile(k); }
87                                         } else {
88                                                 loadFile(k);
89                                         }
90                                 }
91                         }
92                 }
93                 
94                 private function loadFile(box:Array):void {
95                         Globals.vars.root.addDebug("called loadFile for "+box);
96                         bboxes[box]=LOADING;
97
98             var mapRequest:URLRequest = new URLRequest(Connection.apiBaseURL+"/"+box[0]+"_"+box[1]+"_"+box[2]+"_"+box[3]+".osm");
99                         var mapLoader:ExtendedURLLoader = new ExtendedURLLoader();
100                         mapLoader.info['bbox']=box;
101                         mapLoader.addEventListener(Event.COMPLETE, markMapLoaded);
102                         mapLoader.addEventListener(IOErrorEvent.IO_ERROR, markMapUnloadable);
103                         mapLoader.load(mapRequest);
104                         dispatchEvent(new Event(LOAD_STARTED));
105                 }
106                 
107                 private function markMapLoaded(e:Event):void {
108                         bboxes[e.target.info['bbox']]=LOADED;
109                         loadedMap(e);
110                 }
111                 
112                 private function markMapUnloadable(e:Event):void {
113                         bboxes[e.target.info['bbox']]=UNAVAILABLE;
114                 }
115
116                 override public function purgeOutside(left:Number, right:Number, top:Number, bottom:Number):void {
117                         // we don't purge in an OSMConnection
118                 }
119
120                 private function roundUp(a:Number,i:Number):Number {
121                         if (a/i==Math.floor(a/i)) { return a/i; }
122                         return Math.floor(a/i+1)*i;
123                 }
124                 private function roundDown(a:Number,i:Number):Number {
125                         return Math.floor(a/i)*i;
126                 }
127
128         }
129 }