PageRenderTime 25ms CodeModel.GetById 9ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/src/com/google/maps/extras/arcgislink/Layer.as

http://gmaps-utility-library-flash.googlecode.com/
ActionScript | 177 lines | 111 code | 15 blank | 51 comment | 20 complexity | a55b73c3b8baf9ae6ddfe1d1717f91d5 MD5 | raw file
  1/*
  2 * ArcGIS for Google Maps Flash API
  3 *
  4 * License http://www.apache.org/licenses/LICENSE-2.0
  5 */
  6 /**
  7 * @author nianwei at gmail dot com
  8 */ 
  9
 10package com.google.maps.extras.arcgislink {
 11  import flash.events.*;
 12 
 13  /**
 14   * This class represents a Map layer inside an ArcGISMapService. It carries
 15   *  information about a layer's name, id and other information such as scales etc.
 16   *  Due to the way REST API is implemented, each layers extra meta data must retrieved
 17   *  individually. However, most operations do not require those extra info and can be
 18   *  used directly.
 19   */
 20  // dynamic for now
 21  public dynamic class Layer implements IEventDispatcher {
 22    public var url:String;
 23    private var loaded_:Boolean;
 24    private var correct_:Boolean;
 25    public var definition:String;
 26    public var id:int;
 27    public var name:String;
 28    public var visible:Boolean;
 29
 30    /**
 31     * Create a  map Layer using it's url ( 	http://[mapservice-url]/[layerId])
 32     * @name ArcGISLayer
 33     * @param {String} url
 34     * @property {Number} [id] layer ID
 35     * @property {String} [name] layer Name
 36     * @property {Number} [parentLayerId] parent LayerId
 37     * @property {Layer} [parentLayer] parent Layer {@link ArcGISLayer}
 38     * @property {Boolean} [defaultVisibility] defaultVisibility
 39     * @property {Number[]} [subLayerIds] sub LayerIds. null if no sub layers
 40     * @property {Layer[]} [subLayers] sub Layers. {@link ArcGISLayer}[].
 41     * @property {Boolean} [visibility] Visibility of this layer
 42     * @property {String} [definition] Layer definition.
 43     * @property {String} [type] layer type(Feature Layer|), only available after load.
 44     * @property {String} [geometryType] geometryType type(esriGeometryPoint|..), only available after load.
 45     * @property {String} [copyrightText] copyrightText, only available after load.
 46     * @property {Number} [minScale] minScale, only available after load.
 47     * @property {Number} [maxScale] maxScale, only available after load.
 48     * @property {Envelope} [extent] extent, only available after load.
 49     * @property {String} [displayField] displayField, only available after load.
 50     * @property {Field[]} [fields] fields, only available after load. See {@link ArcGISField}
 51     */
 52    public function Layer(url:String, opts:LayerOptions=null) {
 53      this.url=url;
 54      this.loaded_=false;
 55      this.correct_=false;
 56      this.definition=null;
 57      dispatcher_=new EventDispatcher(this);
 58      if (opts!=null && opts.initLoad===false){
 59        return ;
 60      }else{
 61        loadInfo();
 62      }
 63    }
 64
 65    public function loadInfo(opt_callback:Function=null):void {
 66      trace('ArcGISLayer.loadInfo:'+this.url);
 67      var me:Layer=this;
 68      if (this.loaded_ && this.correct_) {
 69        return;
 70      }
 71      ArcGISUtil.restRequest(this.url, {f: 'json'}, this, function(json:Object):void {
 72          if (json.error) {
 73            me.correct_=false;
 74          } else {
 75            me.correct_=true;
 76            ArcGISUtil.augmentObject(json, me);
 77          }
 78          me.loaded_=true;
 79          me.dispatchEvent(new ServiceEvent(ServiceEvent.LOAD));
 80          if (opt_callback !== null) {
 81            opt_callback.call(null, me);
 82          }
 83        });
 84    }
 85
 86    public function hasLoaded():Boolean {
 87      return this.loaded_;
 88    }
 89
 90    public function getFieldNames():Array {
 91      var ret:Array=[];
 92      if (this.hasLoaded()) {
 93        for (var i:int=0; i < this.fields.length; i++) {
 94          ret.push(this.fields[i].name);
 95        }
 96      }
 97      return ret;
 98    }
 99    ;
100
101    /**
102     * Whether the layer is viewable at given scale
103     * @param {Number} scale
104     * @return {Boolean}
105     */
106    public function isInScale(scale:Number):Boolean {
107      // note if the layer's extra info is not loaded, it will return true
108      if (this.maxScale && this.maxScale > scale) {
109        return false;
110      }
111      if (this.minScale && this.minScale < scale) {
112        return false;
113      }
114      return true;
115    }
116    ;
117
118    /**
119     * The query operation is performed on a layer resource. The result of this operation is a resultset resource that will be
120     * passed in the callback function. param is an instance of {@link ArcGISQueryParameters}
121     * <br/>For more info see <a href  = 'http://resources.esri.com/help/9.3/arcgisserver/apis/rest/query.html'>Query Operation</a>.
122     * @param {QueryParameters} params
123     * @param {Function} callback
124     */
125    public function query(qparams:QueryParameters, callbackFn:Function=null, failedFn:Function=null, ovOpts:OverlayOptions=null):void {
126      if (!qparams) {
127        return;
128      }
129      var params:Object={
130        f:'json',
131        returnGeometry:qparams.returnGeometry === false ? false : true,
132        where:qparams.where,
133        outSR:SpatialReferences.WGS84.wkid
134      };
135      if (qparams.outFields && qparams.outFields.length>0){
136        params.outFields=qparams.outFields.join(',');
137      }
138      if (qparams.geometry && qparams.geometry.length>0){
139        params.geometry=ArcGISUtil.fromGeometryToJSON(ArcGISUtil.fromOverlaysToGeometry(qparams.geometry, SpatialReferences.WGS84));
140        params.spatialRel=qparams.spatialRelationship;
141        params.inSR=SpatialReferences.WGS84.wkid;
142        
143      }
144      var me:Layer=this;
145      ArcGISUtil.restRequest(this.url + '/query', params, this, function(json:*):void {
146          var res:ResultSet=new ResultSet(json);
147          if (callbackFn != null) {
148            callbackFn.call(me, res);
149          }
150          me.dispatchEvent(new ServiceEvent(ServiceEvent.QUERY_COMPLETE, res));
151        }, failedFn);
152    }
153    ;
154
155    private var dispatcher_:EventDispatcher;
156
157    public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void {
158      dispatcher_.addEventListener(type, listener, useCapture, priority);
159    }
160
161    public function dispatchEvent(evt:Event):Boolean {
162      return dispatcher_.dispatchEvent(evt);
163    }
164
165    public function hasEventListener(type:String):Boolean {
166      return dispatcher_.hasEventListener(type);
167    }
168
169    public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void {
170      dispatcher_.removeEventListener(type, listener, useCapture);
171    }
172
173    public function willTrigger(type:String):Boolean {
174      return dispatcher_.willTrigger(type);
175    }
176  }
177}