Getting the result of query without using FeatureLayer

429
4
Jump to solution
05-31-2012 10:11 AM
AlaeddineSaadaoui
Occasional Contributor
Hi all,
I have implemented a function to get the result of a query and I have put the following code after defining queryTask and myQuery:
queryTask.execute(myQuery);
var attrs:Object=queryTask.executeLastResult.attributes;
But when I execute this function, attrs is always a Null object. So I am wondering how I can get the result of my query without using the feature layer option because I could not control the order of its execution.
Any help would be appreciated.
Thank you in advance,
Alaeddine Saadaoui.
Rahall Transportation Institute.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
IvanBespalov
Regular Contributor
working with FeatureLayer
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"        xmlns:s="library://ns.adobe.com/flex/spark"        xmlns:mx="library://ns.adobe.com/flex/mx">    <fx:Script>   <![CDATA[    import com.esri.ags.FeatureSet;    import com.esri.ags.Graphic;    import com.esri.ags.events.LayerEvent;    import com.esri.ags.layers.FeatureLayer;    import com.esri.ags.layers.supportClasses.Field;    import com.esri.ags.tasks.supportClasses.Query;        import mx.collections.ArrayCollection;    import mx.controls.Alert;    import mx.rpc.AsyncResponder;    import mx.rpc.Fault;    import mx.utils.StringUtil;        import spark.components.gridClasses.GridColumn;        [Bindable]    private var gridSource:ArrayCollection = new ArrayCollection();        [Bindable]    private var gridColumns:ArrayCollection = new ArrayCollection();        private var queryLayer:FeatureLayer = null;        private function initializeLayer():void    {     queryLayer = new FeatureLayer(txtServiceUrl.text);     queryLayer.mode = FeatureLayer.MODE_ON_DEMAND;     queryLayer.outFields = new Array("*");     queryLayer.addEventListener(LayerEvent.LOAD, onLayerLoaded, false, 0, true);     dispatchEvent(new LayerEvent(LayerEvent.LOAD, queryLayer));    }        protected function onLayerLoaded(event:LayerEvent):void    {     queryLayer.removeEventListener(LayerEvent.LOAD, onLayerLoaded);     txtServiceUrl.editable = false;     executeQuery();    }        protected function onExecuteButtonClick(event:MouseEvent):void    {     if (queryLayer != null && queryLayer.loaded)     {      executeQuery();     }     else      {      initializeLayer();     }    }        private function executeQuery():void    {     var query:Query = new Query();     query.where = txtWhere.text;          var outs:Array = StringUtil.trim(txtOuts.text).split(",");     query.outFields = queryLayer.outFields = outs;          queryLayer.queryFeatures(query, new AsyncResponder(onQueryResult, onQueryFault));    }        protected function onQueryResult(featureSet:FeatureSet, token:Object = null):void    {     gridSource = new ArrayCollection();     lblResultsCount.text = StringUtil.substitute("Found: {0} features.", featureSet.features.length);     for each(var gr:Graphic in featureSet.features)     {      gridSource.addItem(gr.attributes);     }     gridSource.refresh();          gridColumns = new ArrayCollection();     for each (var field:Field in featureSet.fields)     {      var gridColumn:GridColumn = new GridColumn(field.alias);      gridColumns.addItem(gridColumn);     }     gridColumns.refresh();           }        protected function onQueryFault(fault:Fault, token:Object = null):void    {     trace(fault.getStackTrace());     Alert.show("Query faults.", "Error");    }        protected function onGetLastResults(event:MouseEvent):void    {     if (gridSource) // != null     {      var messgae:String = StringUtil.substitute("Last results length is {0}", gridSource.length);      Alert.show(messgae, "Message");     }     else     {      Alert.show("No last results found!", "Warning");     }    }   ]]>  </fx:Script>      <s:Panel width="100%"      title="Query parameters">      <s:VGroup gap="5"        paddingLeft="5"        paddingTop="5"        paddingRight="5"        paddingBottom="5"        width="100%">        <s:HGroup width="100%">          <s:Label text="Url:"         width="150" />          <s:TextInput id="txtServiceUrl"         width="100%"         text="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5" />         </s:HGroup>        <s:HGroup width="100%">          <s:Label text="Where clause:"         width="150" />          <s:TextInput id="txtWhere"          width="100%"         text="1=1" />         </s:HGroup>        <s:HGroup width="100%">          <s:Label text="Out fields (CSV):"         width="150" />          <s:TextInput id="txtOuts"          width="100%"         text="STATE_NAME,STATE_FIPS,SUB_REGION,STATE_ABBR,POP2000,POP2007" />         </s:HGroup>        <s:HGroup width="100%">          <s:Button label="Execute" click="onExecuteButtonClick(event)" />          <s:Button label="Last result length" click="onGetLastResults(event)" />         </s:HGroup>       </s:VGroup>     </s:Panel>    <s:Panel title="Results"      width="100%"     height="100%">      <s:VGroup gap="5"        paddingLeft="5"        paddingTop="5"        paddingRight="5"        paddingBottom="5"        width="100%"       height="100%">        <s:Label id="lblResultsCount" />        <s:DataGrid width="100%"       height="100%"       dataProvider="{gridSource}"       columns="{gridColumns}"/>       </s:VGroup>     </s:Panel>   </s:Application>


or working without feature layer (using QueryTask)
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"        xmlns:s="library://ns.adobe.com/flex/spark"        xmlns:mx="library://ns.adobe.com/flex/mx">    <fx:Script>   <![CDATA[    import com.esri.ags.FeatureSet;    import com.esri.ags.Graphic;    import com.esri.ags.layers.supportClasses.Field;    import com.esri.ags.tasks.QueryTask;    import com.esri.ags.tasks.supportClasses.Query;        import mx.collections.ArrayCollection;    import mx.controls.Alert;    import mx.rpc.AsyncResponder;    import mx.rpc.Fault;    import mx.utils.StringUtil;        import spark.components.gridClasses.GridColumn;        [Bindable]    private var gridSource:ArrayCollection = new ArrayCollection();        [Bindable]    private var gridColumns:ArrayCollection = new ArrayCollection();        private var queryTask:QueryTask;        protected function onExecuteButtonClick(event:MouseEvent):void    {     executeQuery();    }        private function executeQuery():void    {     var query:Query = new Query();     query.where = txtWhere.text;     query.returnGeometry = false;          var outs:Array = StringUtil.trim(txtOuts.text).split(",");     query.outFields = outs;          queryTask = new QueryTask(txtServiceUrl.text);     queryTask.execute(query, new AsyncResponder(onQueryResult, onQueryFault));    }        protected function onQueryResult(featureSet:FeatureSet, token:Object = null):void    {     gridSource = new ArrayCollection();     lblResultsCount.text = StringUtil.substitute("Found: {0} features.", featureSet.features.length);     for each(var gr:Graphic in featureSet.features)     {      gridSource.addItem(gr.attributes);     }     gridSource.refresh();          gridColumns = new ArrayCollection();     for each (var field:Field in featureSet.fields)     {      var gridColumn:GridColumn = new GridColumn(field.alias);      gridColumns.addItem(gridColumn);     }     gridColumns.refresh();           }        protected function onQueryFault(fault:Fault, token:Object = null):void    {     trace(fault.getStackTrace());     Alert.show("Query faults.", "Error");    }        protected function onGetLastResults(event:MouseEvent):void    {     if (queryTask && queryTask.executeLastResult) // != null     {      var results:Array = queryTask.executeLastResult.attributes;      var messgae:String = StringUtil.substitute("Last results length is {0}", results.length);      Alert.show(messgae, "Message");     }     else     {      Alert.show("Query task is null or has no results!", "Warning");     }    }   ]]>  </fx:Script>      <s:Panel width="100%"      title="Query parameters">      <s:VGroup gap="5"        paddingLeft="5"        paddingTop="5"        paddingRight="5"        paddingBottom="5"        width="100%">        <s:HGroup width="100%">          <s:Label text="Url:"         width="150" />          <s:TextInput id="txtServiceUrl"         width="100%"         text="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5" />         </s:HGroup>        <s:HGroup width="100%">          <s:Label text="Where clause:"         width="150" />          <s:TextInput id="txtWhere"          width="100%"         text="1=1" />         </s:HGroup>        <s:HGroup width="100%">          <s:Label text="Out fields (CSV):"         width="150" />          <s:TextInput id="txtOuts"          width="100%"         text="STATE_NAME,STATE_FIPS,SUB_REGION,STATE_ABBR,POP2000,POP2007" />         </s:HGroup>        <s:HGroup width="100%">          <s:Button label="Execute" click="onExecuteButtonClick(event)" />          <s:Button label="Last result length" click="onGetLastResults(event)" />         </s:HGroup>       </s:VGroup>     </s:Panel>    <s:Panel title="Results"      width="100%"     height="100%">      <s:VGroup gap="5"        paddingLeft="5"        paddingTop="5"        paddingRight="5"        paddingBottom="5"        width="100%"       height="100%">        <s:Label id="lblResultsCount" />        <s:DataGrid width="100%"       height="100%"       dataProvider="{gridSource}"       columns="{gridColumns}"/>       </s:VGroup>     </s:Panel>   </s:Application>


Query execution returns:

  • query faults (wrong query params, url, ...)

  • query complete, but no results returned (nothing found ...)

  • query complete, and results returned

so you must listen/handle query complete/fault events to know "what happens?", to know "where happens?", to know "why queryTask.executeLastResult.attributes is null?"...

good luck

View solution in original post

0 Kudos
4 Replies
ThomasMcCracken
New Contributor III
I believe the results of your query are multiple objects so an array would be more appropriate for your variable.

Thomas McCracken
GIS Analyst
Georgia Power Company
0 Kudos
AlaeddineSaadaoui
Occasional Contributor
Hi Thomas,
My problem is with queryTask.executeLastResult.attributes, it always returns a null object.
Thank you.
Alaeddine Saadaoui.
0 Kudos
IvanBespalov
Regular Contributor
working with FeatureLayer
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"        xmlns:s="library://ns.adobe.com/flex/spark"        xmlns:mx="library://ns.adobe.com/flex/mx">    <fx:Script>   <![CDATA[    import com.esri.ags.FeatureSet;    import com.esri.ags.Graphic;    import com.esri.ags.events.LayerEvent;    import com.esri.ags.layers.FeatureLayer;    import com.esri.ags.layers.supportClasses.Field;    import com.esri.ags.tasks.supportClasses.Query;        import mx.collections.ArrayCollection;    import mx.controls.Alert;    import mx.rpc.AsyncResponder;    import mx.rpc.Fault;    import mx.utils.StringUtil;        import spark.components.gridClasses.GridColumn;        [Bindable]    private var gridSource:ArrayCollection = new ArrayCollection();        [Bindable]    private var gridColumns:ArrayCollection = new ArrayCollection();        private var queryLayer:FeatureLayer = null;        private function initializeLayer():void    {     queryLayer = new FeatureLayer(txtServiceUrl.text);     queryLayer.mode = FeatureLayer.MODE_ON_DEMAND;     queryLayer.outFields = new Array("*");     queryLayer.addEventListener(LayerEvent.LOAD, onLayerLoaded, false, 0, true);     dispatchEvent(new LayerEvent(LayerEvent.LOAD, queryLayer));    }        protected function onLayerLoaded(event:LayerEvent):void    {     queryLayer.removeEventListener(LayerEvent.LOAD, onLayerLoaded);     txtServiceUrl.editable = false;     executeQuery();    }        protected function onExecuteButtonClick(event:MouseEvent):void    {     if (queryLayer != null && queryLayer.loaded)     {      executeQuery();     }     else      {      initializeLayer();     }    }        private function executeQuery():void    {     var query:Query = new Query();     query.where = txtWhere.text;          var outs:Array = StringUtil.trim(txtOuts.text).split(",");     query.outFields = queryLayer.outFields = outs;          queryLayer.queryFeatures(query, new AsyncResponder(onQueryResult, onQueryFault));    }        protected function onQueryResult(featureSet:FeatureSet, token:Object = null):void    {     gridSource = new ArrayCollection();     lblResultsCount.text = StringUtil.substitute("Found: {0} features.", featureSet.features.length);     for each(var gr:Graphic in featureSet.features)     {      gridSource.addItem(gr.attributes);     }     gridSource.refresh();          gridColumns = new ArrayCollection();     for each (var field:Field in featureSet.fields)     {      var gridColumn:GridColumn = new GridColumn(field.alias);      gridColumns.addItem(gridColumn);     }     gridColumns.refresh();           }        protected function onQueryFault(fault:Fault, token:Object = null):void    {     trace(fault.getStackTrace());     Alert.show("Query faults.", "Error");    }        protected function onGetLastResults(event:MouseEvent):void    {     if (gridSource) // != null     {      var messgae:String = StringUtil.substitute("Last results length is {0}", gridSource.length);      Alert.show(messgae, "Message");     }     else     {      Alert.show("No last results found!", "Warning");     }    }   ]]>  </fx:Script>      <s:Panel width="100%"      title="Query parameters">      <s:VGroup gap="5"        paddingLeft="5"        paddingTop="5"        paddingRight="5"        paddingBottom="5"        width="100%">        <s:HGroup width="100%">          <s:Label text="Url:"         width="150" />          <s:TextInput id="txtServiceUrl"         width="100%"         text="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5" />         </s:HGroup>        <s:HGroup width="100%">          <s:Label text="Where clause:"         width="150" />          <s:TextInput id="txtWhere"          width="100%"         text="1=1" />         </s:HGroup>        <s:HGroup width="100%">          <s:Label text="Out fields (CSV):"         width="150" />          <s:TextInput id="txtOuts"          width="100%"         text="STATE_NAME,STATE_FIPS,SUB_REGION,STATE_ABBR,POP2000,POP2007" />         </s:HGroup>        <s:HGroup width="100%">          <s:Button label="Execute" click="onExecuteButtonClick(event)" />          <s:Button label="Last result length" click="onGetLastResults(event)" />         </s:HGroup>       </s:VGroup>     </s:Panel>    <s:Panel title="Results"      width="100%"     height="100%">      <s:VGroup gap="5"        paddingLeft="5"        paddingTop="5"        paddingRight="5"        paddingBottom="5"        width="100%"       height="100%">        <s:Label id="lblResultsCount" />        <s:DataGrid width="100%"       height="100%"       dataProvider="{gridSource}"       columns="{gridColumns}"/>       </s:VGroup>     </s:Panel>   </s:Application>


or working without feature layer (using QueryTask)
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"        xmlns:s="library://ns.adobe.com/flex/spark"        xmlns:mx="library://ns.adobe.com/flex/mx">    <fx:Script>   <![CDATA[    import com.esri.ags.FeatureSet;    import com.esri.ags.Graphic;    import com.esri.ags.layers.supportClasses.Field;    import com.esri.ags.tasks.QueryTask;    import com.esri.ags.tasks.supportClasses.Query;        import mx.collections.ArrayCollection;    import mx.controls.Alert;    import mx.rpc.AsyncResponder;    import mx.rpc.Fault;    import mx.utils.StringUtil;        import spark.components.gridClasses.GridColumn;        [Bindable]    private var gridSource:ArrayCollection = new ArrayCollection();        [Bindable]    private var gridColumns:ArrayCollection = new ArrayCollection();        private var queryTask:QueryTask;        protected function onExecuteButtonClick(event:MouseEvent):void    {     executeQuery();    }        private function executeQuery():void    {     var query:Query = new Query();     query.where = txtWhere.text;     query.returnGeometry = false;          var outs:Array = StringUtil.trim(txtOuts.text).split(",");     query.outFields = outs;          queryTask = new QueryTask(txtServiceUrl.text);     queryTask.execute(query, new AsyncResponder(onQueryResult, onQueryFault));    }        protected function onQueryResult(featureSet:FeatureSet, token:Object = null):void    {     gridSource = new ArrayCollection();     lblResultsCount.text = StringUtil.substitute("Found: {0} features.", featureSet.features.length);     for each(var gr:Graphic in featureSet.features)     {      gridSource.addItem(gr.attributes);     }     gridSource.refresh();          gridColumns = new ArrayCollection();     for each (var field:Field in featureSet.fields)     {      var gridColumn:GridColumn = new GridColumn(field.alias);      gridColumns.addItem(gridColumn);     }     gridColumns.refresh();           }        protected function onQueryFault(fault:Fault, token:Object = null):void    {     trace(fault.getStackTrace());     Alert.show("Query faults.", "Error");    }        protected function onGetLastResults(event:MouseEvent):void    {     if (queryTask && queryTask.executeLastResult) // != null     {      var results:Array = queryTask.executeLastResult.attributes;      var messgae:String = StringUtil.substitute("Last results length is {0}", results.length);      Alert.show(messgae, "Message");     }     else     {      Alert.show("Query task is null or has no results!", "Warning");     }    }   ]]>  </fx:Script>      <s:Panel width="100%"      title="Query parameters">      <s:VGroup gap="5"        paddingLeft="5"        paddingTop="5"        paddingRight="5"        paddingBottom="5"        width="100%">        <s:HGroup width="100%">          <s:Label text="Url:"         width="150" />          <s:TextInput id="txtServiceUrl"         width="100%"         text="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5" />         </s:HGroup>        <s:HGroup width="100%">          <s:Label text="Where clause:"         width="150" />          <s:TextInput id="txtWhere"          width="100%"         text="1=1" />         </s:HGroup>        <s:HGroup width="100%">          <s:Label text="Out fields (CSV):"         width="150" />          <s:TextInput id="txtOuts"          width="100%"         text="STATE_NAME,STATE_FIPS,SUB_REGION,STATE_ABBR,POP2000,POP2007" />         </s:HGroup>        <s:HGroup width="100%">          <s:Button label="Execute" click="onExecuteButtonClick(event)" />          <s:Button label="Last result length" click="onGetLastResults(event)" />         </s:HGroup>       </s:VGroup>     </s:Panel>    <s:Panel title="Results"      width="100%"     height="100%">      <s:VGroup gap="5"        paddingLeft="5"        paddingTop="5"        paddingRight="5"        paddingBottom="5"        width="100%"       height="100%">        <s:Label id="lblResultsCount" />        <s:DataGrid width="100%"       height="100%"       dataProvider="{gridSource}"       columns="{gridColumns}"/>       </s:VGroup>     </s:Panel>   </s:Application>


Query execution returns:

  • query faults (wrong query params, url, ...)

  • query complete, but no results returned (nothing found ...)

  • query complete, and results returned

so you must listen/handle query complete/fault events to know "what happens?", to know "where happens?", to know "why queryTask.executeLastResult.attributes is null?"...

good luck

View solution in original post

0 Kudos
AlaeddineSaadaoui
Occasional Contributor
Hi ibespalov,
Thank you very much for your help.
Alaeddine Saadaoui
0 Kudos