Select to view content in your preferred language

Spatial Query

1538
4
Jump to solution
03-05-2013 03:23 PM
RobertPincus
Regular Contributor
I want to select a polygon in one layer, run a spatial query on another layer and list the attributes of the selected features.

For example, using the service at "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/", I can select the State of California's feature (layer 5). I then want to take this feature and run a spatial query on the Counties layer (layer 3) and list the names of the selected counties.


Thanks,
Robert
0 Kudos
1 Solution

Accepted Solutions
TracySchloss
Honored Contributor
The first query function finds the geometry, which is then passed to another querytask, this one based on geometry as a search instead of by attributes.  In my example, I am using region, but you should be able to follow the logic.  I have a dropdown list instead of a text input field to make sure the input is valid.

function findRegion(){         var e = document.getElementById("regionSelect");  //the component name of the region selection         var regionQueryTask = new esri.tasks.QueryTask("http://myserverpathName/ArcGIS/rest/services/myService/MapServer/3");         var regionQuery = new esri.tasks.Query;         regionQuery.text = e.options[e.selectedIndex].text;//this is my primary display field  if you needed another field, use a where clause         regionQuery.returnGeometry = true;         regionQueryTask.execute(regionQuery,showRegionResults);     }


The result function of the initial query finds the geometry of the feature that is selected in findRegion
function showRegionResults (results) {         region = results.features[0].geometry;         map.setExtent(region.getExtent(), true);  //zooms to the region         findFeatureInRegion(region);     }  function findFeatureInRegion(region) {    var newQueryTask = new esri.tasks.QueryTask("http://myserverpathName/arcgis/rest/services/myService/MapServer/2"); //EUSstaff    var newQuery = new esri.tasks.Query;    newQuery.outFields = ["*"];    if (region) {    newQuery.geometry = region;    newQuery.spatialRelationship = esri.tasks.Query.SPATIAL_REL_CONTAINS;    newQueryTask.execute(newQuery);  dojo.connect(newQueryTask, "onComplete", function(results){  //whatever you wanted to do with the features that were selected in the 2nd query } }

View solution in original post

0 Kudos
4 Replies
RahulMetangale1
Frequent Contributor
Hi Robert,

This can be done with two spatial queries:
1. query the state using the map click point.
2. use the result of first query(state polygon) to query the counties layer

I hope this helps.

Rahul
0 Kudos
RobertPincus
Regular Contributor
I have seen the code examples that do what you suggest.

The way I want to go is to start the selection process by entering a state name in a text box.
And I can select a state by entering the state name in a text box.
My problem is getting the code that will create a spatial query on another layer.

Thanks,
Robert
0 Kudos
TracySchloss
Honored Contributor
The first query function finds the geometry, which is then passed to another querytask, this one based on geometry as a search instead of by attributes.  In my example, I am using region, but you should be able to follow the logic.  I have a dropdown list instead of a text input field to make sure the input is valid.

function findRegion(){         var e = document.getElementById("regionSelect");  //the component name of the region selection         var regionQueryTask = new esri.tasks.QueryTask("http://myserverpathName/ArcGIS/rest/services/myService/MapServer/3");         var regionQuery = new esri.tasks.Query;         regionQuery.text = e.options[e.selectedIndex].text;//this is my primary display field  if you needed another field, use a where clause         regionQuery.returnGeometry = true;         regionQueryTask.execute(regionQuery,showRegionResults);     }


The result function of the initial query finds the geometry of the feature that is selected in findRegion
function showRegionResults (results) {         region = results.features[0].geometry;         map.setExtent(region.getExtent(), true);  //zooms to the region         findFeatureInRegion(region);     }  function findFeatureInRegion(region) {    var newQueryTask = new esri.tasks.QueryTask("http://myserverpathName/arcgis/rest/services/myService/MapServer/2"); //EUSstaff    var newQuery = new esri.tasks.Query;    newQuery.outFields = ["*"];    if (region) {    newQuery.geometry = region;    newQuery.spatialRelationship = esri.tasks.Query.SPATIAL_REL_CONTAINS;    newQueryTask.execute(newQuery);  dojo.connect(newQueryTask, "onComplete", function(results){  //whatever you wanted to do with the features that were selected in the 2nd query } }
0 Kudos
RobertPincus
Regular Contributor
This works. Thanks.

Robert
0 Kudos