Select to view content in your preferred language

I need an example of how to use a spatial relationship in a query.

2527
12
06-22-2010 02:06 PM
TracySchloss
Honored Contributor
I've seen posts where the goal is to let the user draw a polygon and select features within it.  However, I have a twist  on that.  I have a find task that will select a particular district number.  This is displayed to the user in a graphicslayer as a single polygon (will only ever have one polygon in it,  it's just a highlight and I continually clear it out).  I would then like to select all the points in another layer that fall within that district

I see that I can define my query to have a spatialRelationship of "SPATIAL_REL_WITHIN", but I can't find a good example of how I might use this.  I"m not even sure this is my best approach.  I've also been looking at the geometry service and the relations it returns.
Tags (2)
0 Kudos
12 Replies
RobertScheitlin__GISP
MVP Emeritus
Tracy,

   Here is a complete sample app that using find to get the state polygon and then uses that polygon in the spatial query to get the cities that intersect it.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:esri="http://www.esri.com/2008/ags"
    pageTitle="Find features in Map Layers"
    styleName="plain">

    <mx:Script>
        <![CDATA[
         import com.esri.ags.events.QueryEvent;
            import com.esri.ags.Graphic;
            import com.esri.ags.events.FindEvent;
            import com.esri.ags.geometry.Geometry;
            import com.esri.ags.tasks.Query;
            import com.esri.ags.tasks.FeatureSet;

            private function doFind():void
            {
                findTask.execute(myFindParams);
            }

            private function findCompleteHandler(event:FindEvent):void
            {
                myGraphicsLayer.clear();
                var graphic:Graphic;
                var query:Query = new Query();
                resultSummary.text = event.findResults.length + " State Found using Find Task";
                var resultCount:int = event.findResults.length;
                for (var i:Number = 0; i < resultCount; i++)
                {
                 query.returnGeometry = true;
                 query.outFields = ["CITY_NAME","POP1990"];
                 query.outSpatialReference = myMap.spatialReference;
                    graphic = event.findResults.feature;
                    query.geometry = graphic.geometry;
     graphic.symbol = sfsFind;
                    myGraphicsLayer.add(graphic);
                    queryTask.execute(query);
                }
            }
            
            private function queryCompleteHandler(event:QueryEvent):void
            {
             myGraphicsLayer.clear();
             var fset:FeatureSet = event.featureSet;
             resultSummary.text += " and " + fset.features.length + " cities found using spatial query."
    for each (var graphic:Graphic in fset.features)
    {
     graphic.symbol = smsFind;
     graphic.toolTip = graphic.attributes["CITY_NAME"] + " - Population: " + graphic.attributes["POP1990"]
     myGraphicsLayer.add(graphic);
    }
            }
        ]]>
    </mx:Script>

    <!-- Symbol for Find Result as Point -->
    <esri:SimpleMarkerSymbol id="smsFind" style="square" color="0xFFFF00" size="11" alpha="0.9">
        <esri:SimpleLineSymbol color="0x000000"/>
    </esri:SimpleMarkerSymbol>

    <!-- Symbol for Find Result as Polygon -->
    <esri:SimpleFillSymbol id="sfsFind" color="0xFFFF00" alpha="0.7"/>

    <!-- Find Task -->
    <esri:FindTask
        id="findTask"
        executeComplete="findCompleteHandler(event)"
        url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer"
        />
    <!-- Query Task -->
    <esri:QueryTask
     id="queryTask"
     executeComplete="queryCompleteHandler(event)"
     url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0"
        />
    <esri:FindParameters id="myFindParams"
        returnGeometry="true"
        contains="true"
        searchText="{fText.text}"
        layerIds="[2]"
        searchFields="['STATE_NAME']"
        />
    <mx:HBox width="100%" height="40" backgroundColor="0xDDDDFF" paddingTop="10" horizontalAlign="center">
        <mx:Text text="Search for names of States:"/>
        <mx:TextInput maxWidth="400" id="fText" enter="doFind()" text="Alabama"/>
        <mx:Button label="Find" click="doFind()"/>
    </mx:HBox>
    <mx:Text id="resultSummary" height="15"/>
        <esri:Map id="myMap">
            <esri:extent>
                <esri:Extent xmin="-126" ymin="24" xmax="-67" ymax="50">
                    <esri:SpatialReference wkid="4326"/>
                </esri:Extent>
            </esri:extent>
            <esri:ArcGISDynamicMapServiceLayer
                url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer"/>
            <esri:GraphicsLayer id="myGraphicsLayer"/>
        </esri:Map>
</mx:Application>
0 Kudos
ShannonMartel
Emerging Contributor
Is there a way to modify this code to allow for a spatial query of several different map services layers;  Like floodplain, zoning, school district?  I want to put the results in a datagrid.  I assume there would be multiple querytasks but after that I am lost.  I have this code in Flash 4.  Thank you for your help.

Shannon
0 Kudos
tanyabisen
Emerging Contributor
Hi Robert,

I am using the sample which you have given in this thread..Its working fine for few layers but the same code fails when i try to do attribute query on a particular layer. can u please help me with the issue.

I am using Bing maps as the basemap and the code shows wrong output for an attribute query.
0 Kudos
ZahidChaudhry
Deactivated User
Hi Robert,

I am using the sample which you have given in this thread..Its working fine for few layers but the same code fails when i try to do attribute query on a particular layer. can u please help me with the issue.

I am using Bing maps as the basemap and the code shows wrong output for an attribute query.


Basemap doesnt matter, You need to make sure that you are getting geometry back from Find as that is the geometry you are using to query data..
0 Kudos
tanyabisen
Emerging Contributor
Its returning a geometry but still it not working for few of my queries...
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Tanya,

   Does the layers that fails have the ObjectID and Shape fields visible?
0 Kudos
tanyabisen
Emerging Contributor
Yes Robert,

I can give u d application link..n wen u select division nos D1-D5..find task is showing unusual behavior..for othr divisions..it is cmpletley wrking fine.. e.g D6,D45,D22.I have provided the link..

http://maps.ijanaagraha.org/Queries_15th/MergedQueries_Find/bin-debug/MergedQueries_Find.html.

I am struggling since 2 weeks to figure out the problem...anyhelp would be really appreciated
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Tanya,

   The issue is that the find task is the same as use a queryTask with a where clause like "Divisions LIKE 'D1*'". Is there a reason you are using Find Task instead of Query Task?
0 Kudos
tanyabisen
Emerging Contributor
No..I tried using the query task also but still I am getting the same results for those first 5 divisions..and I am not able to figure put what is the problem.
0 Kudos