8ec7e5fad20aedb92459e4eee421f1920a3c8893
[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                                 if (!(set.policyfile == undefined)) {
39                                         Security.loadPolicyFile(String(set.policyfile));
40                                 }
41
42                 // Check for any bounds for the vector layer. Obviously won't kick in during subsequent panning
43                 var validBbox:Boolean = false;
44                 if (set.@minlon && String(set.@minlon) != '') {
45                     if (((_map.edge_l>set.@minlon && _map.edge_l<set.@maxlon) ||
46                          (_map.edge_r>set.@minlon && _map.edge_r<set.@maxlon) ||
47                          (_map.edge_l<set.@minlon && _map.edge_r>set.@maxlon)) &&
48                         ((_map.edge_b>set.@minlat && _map.edge_b<set.@maxlat) ||
49                          (_map.edge_t>set.@minlat && _map.edge_t<set.@maxlat) ||
50                          (_map.edge_b<set.@minlat && _map.edge_t>set.@maxlat))) {
51                         validBbox = true;
52                     } else {
53                         validBbox = false; // out of bounds
54                     }
55                 } else {
56                     validBbox = true; // global set
57                 }
58
59                                 if (set.@disabled == "true") {
60                     // Don't do anything with it. The "disabled" attribute allows examples to appear in the config file
61                                 } else if (validBbox) {
62
63                                         var name:String = (set.name == undefined) ? null : String(set.name);
64                                         var loader:String = set.loader;
65                                         switch (loader) {
66                                                 case "TrackLoader":
67                                                         break;
68                                                 case "KMLImporter":
69                                                         break;
70                                                 case "GPXImporter":
71                                                         if (set.url) {
72                                                                 if (set.@loaded == "true") {
73                                                                         name ||= 'GPX file';
74                                                                         var gpx_url:String = String(set.url);
75
76                                                                         var connection:Connection = new Connection(name, gpx_url, null, null);
77                                                                         var gpx:GpxImporter=new GpxImporter(connection, _map, [gpx_url],
78                                                                         function(success:Boolean,message:String=null):void {
79                                                                                 if (!success) return;
80                                                                                 var paint:MapPaint = _map.addLayer(connection, "stylesheets/gpx.css");
81                                                                                 paint.updateEntityUIs(false, false);
82                                                                                 dispatchEvent(new Event("layers_changed"));
83                                                                         }, false);
84                                                                 } else {
85                                                                 trace("VectorBackgrounds: configured but not loaded isn't supported yet");
86                                                                 }
87                                                         } else {
88                                                                 trace("VectorBackgrounds: no url for GPXImporter");
89                                                         }
90                                                         break;
91
92                                                 case "BugLoader":
93                                                         if (set.url && set.apiKey) {
94                                                                 name ||= 'Bugs';
95                                                                 var bugLoader:BugLoader = new BugLoader(_map, String(set.url), String(set.apikey), name, String(set.details));
96                                                                 if (set.@loaded == "true") {
97                                                                         bugLoader.load();
98                                                                 }
99                                                         } else {
100                                                                 trace("VectorBackgrounds: error with BugLoader");
101                                                         }
102                                                         break;
103
104                                                 case "BikeShopLoader":
105                                                         if (set.url) {
106                                                                 name ||= 'Missing Bike Shops'
107                                                                 var bikeShopLoader:BikeShopLoader = new BikeShopLoader(_map, String(set.url), name);
108                                                                 if (set.@loaded == "true") {
109                                                                         bikeShopLoader.load();
110                                                                 }
111                                                         } else {
112                                                                 trace("VectorBackgrounds: no url for BikeShopLoader");
113                                                         }
114                                                         break;
115
116                                                 case "SnapshotLoader":
117                                                         if (set.url) {
118                                                                 name ||= 'Snapshot Server'
119                                                                 var snapshotLoader:SnapshotLoader = new SnapshotLoader(_map, String(set.url), name, String(set.style));
120                                                                 if (set.@loaded == "true") {
121                                                                         snapshotLoader.load();
122                                                                 }
123                                                         } else {
124                                                                 trace("VectorBackgrounds: no url for SnapshotLoader");
125                                                         }
126                                                         break;
127
128                                                 default:
129                                                         trace("VectorBackgrounds: unknown loader: " + loader);
130                                         }
131                                 }
132                         });
133                 }
134         }
135 }