Removed executable flag from files which don't need it.
[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
7         import flash.system.Security;
8         import flash.net.*;
9
10         import mx.core.UIComponent;
11         import mx.controls.DataGrid;
12
13     import net.systemeD.halcyon.connection.*;
14         import net.systemeD.halcyon.DebugURLRequest;
15
16         public class MapFeatures extends EventDispatcher {
17         private static var instance:MapFeatures;
18
19         public static function getInstance():MapFeatures {
20             if ( instance == null ) {
21                 instance = new MapFeatures();
22                 instance.loadFeatures();
23             }
24             return instance;
25         }
26
27         private var xml:XML = null;
28         private var _features:Array = null;
29         private var _categories:Array = null;
30 //              private var _keys:Array = null;
31                 private var _tags:Object = null;
32
33         protected function loadFeatures():void {
34             var request:DebugURLRequest = new DebugURLRequest("map_features.xml");
35             var loader:URLLoader = new URLLoader();
36             loader.addEventListener(Event.COMPLETE, onFeatureLoad);
37             loader.load(request.request);
38         }
39
40         internal function get definition():XML {
41             return xml;
42         }
43         
44         private function onFeatureLoad(event:Event):void {
45                         var f:Feature;
46
47             xml = new XML(URLLoader(event.target).data);
48             _features = [];
49                         _tags = { relation:{}, way:{}, node:{} };
50
51             for each(var feature:XML in xml.feature) {
52                 f=new Feature(this,feature);
53                                 _features.push(f);
54                                 for each (var tag:Object in f.tags) {
55                                         if (f.isType('line') || f.isType('area')) { addToTagList('way',tag); }
56                                         if (f.isType('relation'))                                 { addToTagList('relation',tag); }
57                                         if (f.isType('point'))                                    { addToTagList('node',tag); }
58                                 }
59             }            
60
61             _categories = new Array();
62             for each(var catXML:XML in xml.category) {
63                 if ( catXML.child("category").length() == 0 )
64                   _categories.push(new Category(this, catXML.@name, catXML.@id, _categories.length));
65             }
66             dispatchEvent(new Event("featuresLoaded"));
67         }
68
69                 private function addToTagList(type:String,tag:Object):void {
70                         if (tag.v=='*') { return; }
71                         if (!_tags[type][tag.k]) { _tags[type][tag.k]=new Array(); }
72                         if (_tags[type][tag.k].indexOf(tag.v)==-1) { _tags[type][tag.k].push(tag.v); }
73                 }
74
75         public function hasLoaded():Boolean {
76             return xml != null;
77         }
78
79         public function findMatchingFeature(entity:Entity):Feature {
80             if ( xml == null )
81                 return null;
82
83             for each(var feature:Feature in features) {
84                 var match:Boolean = true;
85
86                 // check for matching tags
87                 for each(var tag:Object in feature.tags) {
88                     var entityTag:String = entity.getTag(tag.k);
89                     match = entityTag == tag.v || (entityTag != null && tag.v == "*");
90                     if ( !match ) break;
91                 }
92
93                                 // check for matching withins
94                                 if (match) {
95                                         for each (var within:Object in feature.withins) {
96                                                 match = entity.countParentObjects(within) >= (within.minimum ? within.minimum : 1);
97                                                 if (!match) { break; }
98                                         }
99                                 }
100
101                 if (match) {
102                     return feature;
103                                 }
104             }
105             return null;
106         }
107         
108         [Bindable(event="featuresLoaded")]
109         public function get categories():Array {
110             if ( xml == null )
111                 return null;        
112             return _categories;
113         }
114
115         [Bindable(event="featuresLoaded")]
116         public function getCategoriesForType(type:String):Array {
117             if ( xml == null )
118                 return null;
119             if ( type == null || type == "" )  
120                 return []; //_categories;
121                 
122             var filteredCategories:Array = new Array();
123             for each( var cat:Category in _categories ) {
124                 if ( cat.getFeaturesForType(type).length > 0 )
125                     filteredCategories.push(cat);
126             }
127             return filteredCategories;
128         }
129
130         [Bindable(event="featuresLoaded")]
131         public function get features():Array {
132             if ( xml == null )
133                 return null;            
134             return _features;
135         }
136
137         [Bindable(event="featuresLoaded")]
138         public function get pois():Array {
139             if (xml == null )
140                 return null;
141             var pois:Array = [];
142             var counter:int = 0;
143             for each ( var feature:Feature in _features ) {
144               if (feature.isType("point")) {
145                 pois.push(feature);
146               }
147             }
148             return pois;
149         }
150
151                 [Bindable(event="featuresLoaded")]
152                 public function getAutoCompleteKeys(type:String):Array {
153                         var list:Array=[];
154                         var a:Array=[];
155
156                         for (var k:String in _tags[type]) { list.push(k); }
157                         list.sort();
158
159                         for each (k in list) { a.push( { name: k } ); }
160                         return a;
161                 }
162                 
163                 [Bindable(event="featuresLoaded")]
164                 public function getAutoCompleteValues(type:String,key:String):Array {
165                         var a:Array=[];
166                         if (_tags[type][key]) {
167                                 _tags[type][key].sort();
168                                 for each (var v:String in _tags[type][key]) { a.push( { name: v } ); }
169                         }
170                         return a;
171                 }
172                 
173     }
174
175 }
176
177