Merge branch 'master' into history
[potlatch2.git] / net / systemeD / potlatch2 / collections / VectorBackgrounds.as
1 package net.systemeD.potlatch2.collections {
2
3         import flash.events.*
4         import flash.net.*
5         import flash.system.Security;
6         import net.systemeD.halcyon.Map;
7         import net.systemeD.halcyon.MapPaint;
8         import net.systemeD.halcyon.connection.Connection;
9         import net.systemeD.halcyon.DebugURLRequest;
10         import net.systemeD.potlatch2.utils.*;
11                 
12         public class VectorBackgrounds extends EventDispatcher {
13
14                 private static const GLOBAL_INSTANCE:VectorBackgrounds = new VectorBackgrounds();
15                 public static function instance():VectorBackgrounds { return GLOBAL_INSTANCE; }
16
17                 private var _map:Map;
18
19
20                 public function init(map:Map):void {
21                         _map = map;
22                         var request:DebugURLRequest = new DebugURLRequest("vectors.xml");
23                         var loader:URLLoader = new URLLoader();
24                         loader.addEventListener(Event.COMPLETE, onConfigLoad);
25                         loader.load(request.request);
26                 }
27
28                 public function onConfigLoad(e:Event):void {
29                         var xml:XML = XML(e.target.data);
30
31                         // reconstitute results as Array, as we can't run .forEach over an XMLList
32                         var sets:Array = [];
33                         for each (var set:XML in xml.set) { sets.push(set); }
34                         
35                         // use .forEach to avoid closure problem (http://stackoverflow.com/questions/422784/how-to-fix-closure-problem-in-actionscript-3-as3#3971784)
36                         sets.forEach(function(set:XML, index:int, array:Array):void {
37
38                                 // Skip if just an example
39                                 if (!(set.@disabled=="true")) {
40
41                                         if (!(set.policyfile == undefined)) {
42                                                 Security.loadPolicyFile(String(set.policyfile));
43                                         }
44
45                         // Check for any bounds for the vector layer. Obviously won't kick in during subsequent panning
46                         var validBbox:Boolean = false;
47                         if (set.@minlon && String(set.@minlon) != '') {
48                             if (((_map.edge_l>set.@minlon && _map.edge_l<set.@maxlon) ||
49                                  (_map.edge_r>set.@minlon && _map.edge_r<set.@maxlon) ||
50                                  (_map.edge_l<set.@minlon && _map.edge_r>set.@maxlon)) &&
51                                 ((_map.edge_b>set.@minlat && _map.edge_b<set.@maxlat) ||
52                                  (_map.edge_t>set.@minlat && _map.edge_t<set.@maxlat) ||
53                                  (_map.edge_b<set.@minlat && _map.edge_t>set.@maxlat))) {
54                                 validBbox = true;
55                             } else {
56                                 validBbox = false; // out of bounds
57                             }
58                         } else {
59                             validBbox = true; // global set
60                         }
61
62                                         if (validBbox) {
63
64                                                 var name:String = (set.name == undefined) ? null : String(set.name);
65                                                 var loader:String = set.loader;
66                                                 switch (loader) {
67                                                         case "TrackLoader":
68                                                                 break;
69                                                         case "KMLImporter":
70                                                                 break;
71                                                         case "GPXImporter":
72                                                                 if (set.url) {
73                                                                         if (set.@loaded == "true") {
74                                                                                 name ||= 'GPX file';
75                                                                                 var gpx_url:String = String(set.url);
76
77                                                                                 var connection:Connection = new Connection(name, gpx_url, null, null);
78                                                                                 var gpx:GpxImporter=new GpxImporter(connection, _map, [gpx_url],
79                                                                                 function(success:Boolean,message:String=null):void {
80                                                                                         if (!success) return;
81                                                                                         var paint:MapPaint = _map.addLayer(connection, "stylesheets/gpx.css");
82                                                                                         paint.updateEntityUIs(false, false);
83                                                                                         dispatchEvent(new Event("layers_changed"));
84                                                                                 }, false);
85                                                                         } else {
86                                                                         trace("VectorBackgrounds: configured but not loaded isn't supported yet");
87                                                                         }
88                                                                 } else {
89                                                                         trace("VectorBackgrounds: no url for GPXImporter");
90                                                                 }
91                                                                 break;
92
93                                                         case "BugLoader":
94                                                                 if (set.url && set.apiKey) {
95                                                                         name ||= 'Bugs';
96                                                                         var bugLoader:BugLoader = new BugLoader(_map, String(set.url), String(set.apikey), name, String(set.details));
97                                                                         if (set.@loaded == "true") {
98                                                                                 bugLoader.load();
99                                                                         }
100                                                                 } else {
101                                                                         trace("VectorBackgrounds: error with BugLoader");
102                                                                 }
103                                                                 break;
104
105                                                         case "BikeShopLoader":
106                                                                 if (set.url) {
107                                                                         name ||= 'Missing Bike Shops'
108                                                                         var bikeShopLoader:BikeShopLoader = new BikeShopLoader(_map, String(set.url), name);
109                                                                         if (set.@loaded == "true") {
110                                                                                 bikeShopLoader.load();
111                                                                         }
112                                                                 } else {
113                                                                         trace("VectorBackgrounds: no url for BikeShopLoader");
114                                                                 }
115                                                                 break;
116
117                                                         case "SnapshotLoader":
118                                                                 if (set.url) {
119                                                                         name ||= 'Snapshot Server'
120                                                                         var snapshotLoader:SnapshotLoader = new SnapshotLoader(_map, String(set.url), name, String(set.style));
121                                                                         if (set.@loaded == "true") {
122                                                                                 snapshotLoader.load();
123                                                                         }
124                                                                 } else {
125                                                                         trace("VectorBackgrounds: no url for SnapshotLoader");
126                                                                 }
127                                                                 break;
128
129                                                         default:
130                                                                 trace("VectorBackgrounds: unknown loader: " + loader);
131                                                 }
132                                         }
133                                 }
134                         });
135                 }
136         }
137 }