categories of additional tags for roads
[potlatch2.git] / net / systemeD / potlatch2 / mapfeatures / MapFeatures.as
1 package net.systemeD.potlatch2.mapfeatures {
2
3     import flash.events.EventDispatcher;
4     import flash.events.Event;
5     import flash.net.URLLoader;
6     import flash.net.URLRequest;
7
8         import flash.system.Security;
9         import flash.net.*;
10
11     import net.systemeD.halcyon.connection.*;
12
13
14         public class MapFeatures extends EventDispatcher {
15         private static var instance:MapFeatures;
16
17         public static function getInstance():MapFeatures {
18             if ( instance == null ) {
19                 instance = new MapFeatures();
20                 instance.loadFeatures();
21             }
22             return instance;
23         }
24
25
26
27         private var xml:XML = null;
28         private var _features:Array = null;
29         private var _categories:Array = null;
30
31         protected function loadFeatures():void {
32             var request:URLRequest = new URLRequest("map_features.xml");
33             var loader:URLLoader = new URLLoader();
34             loader.addEventListener(Event.COMPLETE, onFeatureLoad);
35             loader.load(request);
36         }
37
38         internal function get definition():XML {
39             return xml;
40         }
41         
42         private function onFeatureLoad(event:Event):void {
43             xml = new XML(URLLoader(event.target).data);
44             
45             _features = new Array();
46             for each(var feature:XML in xml.feature) {
47                 _features.push(new Feature(this, feature));
48             }            
49             _categories = new Array();
50             for each(var catXML:XML in xml.category) {
51                 if ( catXML.child("category").length() == 0 )
52                   _categories.push(new Category(this, catXML.@name, catXML.@id, _categories.length));
53             }
54             dispatchEvent(new Event("featuresLoaded"));
55         }
56
57         public function hasLoaded():Boolean {
58             return xml != null;
59         }
60
61         public function findMatchingFeature(entity:Entity):Feature {
62             if ( xml == null )
63                 return null;
64
65             for each(var feature:Feature in features) {
66                 // check for matching tags
67                 var match:Boolean = true;
68                 for each(var tag:Object in feature.tags) {
69                     var entityTag:String = entity.getTag(tag.k);
70                     match = entityTag == tag.v || (entityTag != null && tag.v == "*");
71                     if ( !match ) break;
72                 }
73                 if ( match )
74                     return feature;
75             }
76             return null;
77         }
78         
79         [Bindable(event="featuresLoaded")]
80         public function get categories():Array {
81             if ( xml == null )
82                 return null;            
83             return _categories;
84         }
85
86         [Bindable(event="featuresLoaded")]
87         public function get features():Array {
88             if ( xml == null )
89                 return null;            
90             return _features;
91         }
92
93     }
94
95 }
96
97