1 package net.systemeD.halcyon.connection {
5 import flash.utils.Dictionary;
6 import flash.system.Security;
8 import net.systemeD.halcyon.ExtendedURLLoader;
9 import net.systemeD.halcyon.Globals;
11 // Read-only connection from local files (for Halcyon)
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");
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)
25 public class OSMConnection extends XMLBaseConnection {
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?
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
39 private var tileResolution:Number;
40 // degree resolution for tiles (e.g. 0.2)
42 private static const FILENAME:RegExp=/([\-\d\.]+)_([\-\d\.]+)_([\-\d\.]+)_([\-\d\.]+)\./i;
44 public function OSMConnection() {
46 if (Connection.policyURL!='')
47 Security.loadPolicyFile(Connection.policyURL);
49 tileResolution = Number(Connection.getParam("tile_resolution", "0.2"));
51 var o:Object = new Object();
52 var files:String = Connection.getParam("files","");
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;
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;
69 // look through bboxes, assemble any within the requested bbox that are AVAILABLE
70 for (var box:* in bboxes) {
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) ) ) {
76 if (bboxes[box]==AVAILABLE) { loadFile(box); }
79 if (filemode==NAMED) { return; }
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];
86 if (bboxes[k]==AVAILABLE) { loadFile(k); }
94 private function loadFile(box:Array):void {
95 Globals.vars.root.addDebug("called loadFile for "+box);
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));
107 private function markMapLoaded(e:Event):void {
108 bboxes[e.target.info['bbox']]=LOADED;
112 private function markMapUnloadable(e:Event):void {
113 bboxes[e.target.info['bbox']]=UNAVAILABLE;
116 override public function purgeOutside(left:Number, right:Number, top:Number, bottom:Number):void {
117 // we don't purge in an OSMConnection
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;
124 private function roundDown(a:Number,i:Number):Number {
125 return Math.floor(a/i)*i;