Merge branch 'master' of github.com:systemed/potlatch2
[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.FileBank;
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             FileBank.getInstance().addFromFile("vectors.xml", onConfigLoad);
23                 }
24
25                 private function onConfigLoad(fileBank:FileBank, filename:String):void {
26                         var xml:XML = new XML(fileBank.getAsString(filename));
27
28                         // reconstitute results as Array, as we can't run .forEach over an XMLList
29                         var sets:Array = [];
30                         for each (var set:XML in xml.set) { sets.push(set); }
31                         
32                         // use .forEach to avoid closure problem (http://stackoverflow.com/questions/422784/how-to-fix-closure-problem-in-actionscript-3-as3#3971784)
33                         sets.forEach(function(set:XML, index:int, array:Array):void {
34
35                                 // Skip if just an example
36                                 if (!(set.@disabled=="true")) {
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 (validBbox) {
60
61                                                 var name:String = (set.name == undefined) ? null : String(set.name);
62                                                 var loader:String = set.loader;
63                                                 switch (loader) {
64                                                         case "TrackLoader":
65                                                                 break;
66                                                         case "KMLImporter":
67                                                                 break;
68                                                         case "GPXImporter":
69                                                                 if (set.url) {
70                                                                         if (set.@loaded == "true") {
71                                                                                 name ||= 'GPX file';
72                                                                                 var gpx_url:String = String(set.url);
73
74                                                                                 var connection:Connection = new Connection(name, gpx_url, null, null);
75                                                                                 var gpx:GpxImporter=new GpxImporter(connection, _map, 
76                                                                                 function(connection:Connection,options:Object,success:Boolean,message:String=null):void {
77                                                                                         if (!success) return;
78                                                                                         var paint:MapPaint = _map.addLayer(connection, "stylesheets/gpx.css");
79                                                                                         paint.updateEntityUIs(false, false);
80                                                                                         dispatchEvent(new Event("layers_changed"));
81                                                                                 }, false);
82                                                                                 gpx.importFromRemoteFiles([gpx_url]);
83                                                                         } else {
84                                                                         trace("VectorBackgrounds: configured but not loaded isn't supported yet");
85                                                                         }
86                                                                 } else {
87                                                                         trace("VectorBackgrounds: no url for GPXImporter");
88                                                                 }
89                                                                 break;
90
91                                                         case "BugLoader":
92                                                                 if (set.url && set.apiKey) {
93                                                                         name ||= 'Bugs';
94                                                                         var bugLoader:BugLoader = new BugLoader(_map, String(set.url), String(set.apikey), name, String(set.details));
95                                                                         if (set.@loaded == "true") {
96                                                                                 bugLoader.load();
97                                                                         }
98                                                                 } else {
99                                                                         trace("VectorBackgrounds: error with BugLoader");
100                                                                 }
101                                                                 break;
102
103                                                         case "BikeShopLoader":
104                                                                 if (set.url) {
105                                                                         name ||= 'Missing Bike Shops'
106                                                                         var bikeShopLoader:BikeShopLoader = new BikeShopLoader(_map, String(set.url), name);
107                                                                         if (set.@loaded == "true") {
108                                                                                 bikeShopLoader.load();
109                                                                         }
110                                                                 } else {
111                                                                         trace("VectorBackgrounds: no url for BikeShopLoader");
112                                                                 }
113                                                                 break;
114
115                                                         case "SnapshotLoader":
116                                                                 if (set.url) {
117                                                                         name ||= 'Snapshot Server'
118                                                                         var snapshotLoader:SnapshotLoader = new SnapshotLoader(_map, String(set.url), name, String(set.style));
119                                                                         if (set.@loaded == "true") {
120                                                                                 snapshotLoader.load();
121                                                                         }
122                                                                 } else {
123                                                                         trace("VectorBackgrounds: no url for SnapshotLoader");
124                                                                 }
125                                                                 break;
126
127                                                         default:
128                                                                 trace("VectorBackgrounds: unknown loader: " + loader);
129                                                 }
130                                         }
131                                 }
132                         });
133                 }
134         }
135 }