1 package net.systemeD.potlatch2.collections {
4 import flash.display.*;
6 import flash.text.TextField;
7 import net.systemeD.halcyon.DebugURLRequest;
8 import net.systemeD.halcyon.Map;
9 import net.systemeD.halcyon.MapEvent;
10 import net.systemeD.potlatch2.FunctionKeyManager;
11 import net.systemeD.potlatch2.Yahoo;
12 import mx.collections.ArrayCollection;
15 There's lots of further tidying we can do:
16 - remove all the horrid Yahoo stuff
17 - remove the backreferences to _map and send events instead
18 but this will do for now and help remove the clutter from potlatch2.mxml.
21 public class Imagery extends EventDispatcher {
23 private static const GLOBAL_INSTANCE:Imagery = new Imagery();
24 public static function instance():Imagery { return GLOBAL_INSTANCE; }
26 public var collection:Array=[];
27 public var selected:Object={};
30 private var _overlay:Sprite;
31 private var _yahoo:Yahoo;
33 /* Load catalogue file */
35 public function init(map:Map, overlay:Sprite, yahoo:Yahoo):void {
41 var request:DebugURLRequest = new DebugURLRequest("imagery.xml");
42 var loader:URLLoader = new URLLoader();
43 loader.addEventListener(Event.COMPLETE, onImageryLoad);
44 loader.load(request.request);
46 // create map listeners
47 map.addEventListener(MapEvent.MOVE, moveHandler);
48 map.addEventListener(MapEvent.RESIZE, resizeHandler);
51 private function onImageryLoad(event:Event):void {
52 var xml:XML = new XML(URLLoader(event.target).data);
53 var saved:Object = {};
55 if (SharedObject.getLocal("user_state").data['background_url']) {
56 saved={ name: SharedObject.getLocal("user_state").data['background_name'],
57 url: SharedObject.getLocal("user_state").data['background_url' ] };
60 var isSet:Boolean=false;
61 var backgroundSet:Boolean = false;
63 // Read all values from XML file
65 { name: "None", url: "" },
66 { name: "Yahoo", url: "yahoo", sourcetag: "Yahoo" } );
67 for each(var set:XML in xml.set) {
70 for each (a in set.@*) { obj[a.name().localName]=a.toString(); }
71 for each (a in set.* ) { obj[a.name() ]=a.toString(); }
73 if ((saved.url && obj.url ==saved.url) ||
74 (saved.name && obj.name==saved.name && obj.name!='Custom')) { isSet=true; }
77 // Add user's previous preference (from SharedObject) if we didn't find it in the XML file
78 if (!isSet && saved.name && saved.url && saved.url!='' && saved.url!='yahoo') {
79 collection.push(saved);
83 // Automatically select the user's previous preference
84 var defaultBackground:Object=null;
85 for each (bg in collection) {
86 if (bg.name==saved.name || bg.url==saved.url) {
89 } else if (bg.default) {
94 // Otherwise, set whatever's specified as default
95 if (!backgroundSet && defaultBackground) {
96 setBackground(defaultBackground);
99 // Get any attribution and logo details
100 for each (bg in collection) {
103 var loader:Loader = new Loader();
104 var thisbg1:Object = bg; // scope it for the closure
105 loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void { onLogoLoad(e,thisbg1); });
106 loader.load(new URLRequest(bg.logo));
108 if (bg.attribution_url) {
109 // load the attribution
110 trace("requesting "+bg.attribution_url);
111 var urlloader:URLLoader = new URLLoader();
112 var thisbg2:Object = bg; // scope it for the closure
113 urlloader.addEventListener(Event.COMPLETE, function(e:Event):void { onAttributionLoad(e,thisbg2); });
114 urlloader.load(new URLRequest(bg.attribution_url));
118 // Tell the function key manager that we'd like to receive function key calls
119 FunctionKeyManager.instance().registerListener('Background imagery',
120 function(o:String):void { setBackground(findBackgroundWithName(o)); });
121 dispatchEvent(new Event("collection_changed"));
124 public function onLogoLoad(e:Event, bg:Object):void {
125 bg.logoData = Bitmap(LoaderInfo(e.target).content).bitmapData;
126 bg.logoWidth = e.target.loader.width;
127 bg.logoHeight= e.target.loader.height;
131 public function onAttributionLoad(e:Event,bg: Object):void {
132 trace ("onAttributionLoad");
133 // if we ever need to cope with non-Microsoft attribution, then this should look at bg.scheme
134 // someone who actually likes XML can replace the following with the 'proper' way of doing it
135 var xmlnsPattern:RegExp = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
136 var xsiPattern:RegExp = new RegExp("xsi[^\"]*\"[^\"]*\"", "gi");
137 var s:String=e.target.data;
138 var xml:XML = new XML(s.replace(xmlnsPattern, "").replace(xsiPattern, ""));
139 var attribution:Object = {};
140 for each (var ResourceSets:XML in xml.child("ResourceSets")) {
141 for each (var ResourceSet:XML in ResourceSets.child("ResourceSet")) {
142 for each (var Resources:XML in ResourceSet.child("Resources")) {
143 for each (var ImageryMetadata:XML in Resources.child("ImageryMetadata")) {
144 for each (var ImageryProvider:XML in ImageryMetadata.child("ImageryProvider")) {
146 for each (var CoverageArea:XML in ImageryProvider.child("CoverageArea")) {
147 areas.push([CoverageArea.ZoomMin,
148 CoverageArea.ZoomMax,
149 CoverageArea.BoundingBox.SouthLatitude,
150 CoverageArea.BoundingBox.WestLongitude,
151 CoverageArea.BoundingBox.NorthLatitude,
152 CoverageArea.BoundingBox.EastLongitude]);
154 attribution[ImageryProvider.Attribution]=areas;
160 bg.attribution=attribution;
164 public function setBackground(bg:Object):void {
167 if (bg.url=='yahoo') { _map.setBackground({url:''}); _yahoo.show(); }
168 else { _map.setBackground(bg ); _yahoo.hide(); }
169 // update attribution and logo
170 _overlay.visible=bg.attribution || bg.logo || bg.terms_url;
171 setLogo(); setAttribution(); setTerms();
172 // save as SharedObject for next time
173 var obj:SharedObject = SharedObject.getLocal("user_state");
174 obj.setProperty('background_url' ,String(bg.url));
175 obj.setProperty('background_name',String(bg.name));
179 private function findBackgroundWithName(name:String):Object {
180 for each (var bg:Object in collection) {
181 if (bg.name==name) { return bg; }
186 private function moveHandler(event:MapEvent):void {
189 private function setAttribution():void {
190 var tf:TextField=TextField(_overlay.getChildAt(0));
192 if (!selected.attribution) return;
194 for (var provider:String in selected.attribution) {
195 for each (var bounds:Array in selected.attribution[provider]) {
196 if (_map.scale>=bounds[0] && _map.scale<=bounds[1] &&
197 ((_map.edge_l>bounds[3] && _map.edge_l<bounds[5]) ||
198 (_map.edge_r>bounds[3] && _map.edge_r<bounds[5]) ||
199 (_map.edge_l<bounds[3] && _map.edge_r>bounds[5])) &&
200 ((_map.edge_b>bounds[2] && _map.edge_b<bounds[4]) ||
201 (_map.edge_t>bounds[2] && _map.edge_t<bounds[4]) ||
202 (_map.edge_b<bounds[2] && _map.edge_t>bounds[4]))) {
207 if (attr.length==0) return;
208 tf.text="Background "+attr.join(", ");
209 positionAttribution();
210 dispatchEvent(new MapEvent(MapEvent.BUMP, { y: tf.textHeight })); // don't let the toolbox obscure it
212 private function positionAttribution():void {
213 var tf:TextField=TextField(_overlay.getChildAt(0));
214 tf.x=_map.mapwidth - 5 - tf.textWidth;
215 tf.y=_map.mapheight - 5 - tf.textHeight;
218 private function setLogo():void {
219 while (_overlay.numChildren>2) { _overlay.removeChildAt(2); }
220 if (!selected.logoData) return;
221 var logo:Sprite=new Sprite();
222 logo.addChild(new Bitmap(selected.logoData));
223 if (selected.logo_url) { logo.buttonMode=true; logo.addEventListener(MouseEvent.CLICK, launchLogoLink, false, 0, true); }
224 _overlay.addChild(logo);
227 private function positionLogo():void {
228 _overlay.getChildAt(2).x=5;
229 _overlay.getChildAt(2).y=_map.mapheight - 5 - selected.logoHeight - (selected.terms_url ? 10 : 0);
231 private function launchLogoLink(e:Event):void {
232 if (!selected.logo_url) return;
233 navigateToURL(new URLRequest(selected.logo_url), '_blank');
235 private function setTerms():void {
236 var terms:TextField=TextField(_overlay.getChildAt(1));
237 if (!selected.terms_url) { terms.text=''; return; }
238 terms.text="Background terms of use";
240 terms.addEventListener(MouseEvent.CLICK, launchTermsLink, false, 0, true);
242 private function positionTerms():void {
243 _overlay.getChildAt(1).x=5;
244 _overlay.getChildAt(1).y=_map.mapheight - 15;
246 private function launchTermsLink(e:Event):void {
247 if (!selected.terms_url) return;
248 navigateToURL(new URLRequest(selected.terms_url), '_blank');
251 private function resizeHandler(event:MapEvent):void {
252 if (selected.logoData) positionLogo();
253 if (selected.terms_url) positionTerms();
254 if (selected.attribution) positionAttribution();
257 [Bindable(event="collection_changed")]
258 public function getAvailableImagery():ArrayCollection {
259 var available:Array=[];
260 for each (var bg:Object in collection) {
262 // if there's a bbox, check the current viewport intersects it
263 if (((_map.edge_l>bg.minlon && _map.edge_l<bg.maxlon) ||
264 (_map.edge_r>bg.minlon && _map.edge_r<bg.maxlon) ||
265 (_map.edge_l<bg.minlon && _map.edge_r>bg.maxlon)) &&
266 ((_map.edge_b>bg.minlat && _map.edge_b<bg.maxlat) ||
267 (_map.edge_t>bg.minlat && _map.edge_t<bg.maxlat) ||
268 (_map.edge_b<bg.minlat && _map.edge_t>bg.maxlat))) {
272 // if there's no bbox (i.e. global set), include it anyway
276 return new ArrayCollection(available);