How can I find features around a point using a QueryTask?

1334
2
Jump to solution
08-01-2013 12:09 PM
YohanBienvenue
Occasional Contributor II
I have a map with a ArcGISDynamicMapServiceLayer
I have added a graphic to the map, and now I want to query the map to get all the features around this point according to some radius in KM.

It's important that the features are not located further then the circle radius around the point, so I can't use a box geometry as a spatial filter for my Query.

So, in short, how can I search for features within a circle defined by a point plus a certain buffer around that point?
Can I do this with a QueryTask?

Thanks
0 Kudos
1 Solution

Accepted Solutions
DianaBenedict
Occasional Contributor III
You can do the following if you feel that this workflow works for you

1) Use the ESRI geometry task to create a buffer around the point at your specified distance
https://developers.arcgis.com/en/javascript/jsapi/geometryservice.html#buffer

2) use the returned geometry and pass in to your query.geometry property the new buffer geometry
https://developers.arcgis.com/en/javascript/jsapi/query.html
use the following query params to fine tune your query
query.geometry ==> your buffer geom
query.spatialRelationship ==> SPATIAL_REL_WITHIN
Default Value: SPATIAL_REL_INTERSECTS

3) use the results as needed

There should be plenty of examples out there for how to execute the buffer task and of course the query task as well. I have provided some sample code that I put together to show one of many ways to accomplish this task. The most important thing to keep in mind is that both the exectute buffer and execture query return deffered objects. You will want to utitlize this so that you can prevent the application/code from running away from you.

Below is a brief example of how you might accomplish this:

//buffer code that accepts the params for buffering and returns the dojo deferred object   bufferGeometry: function (inGeometryArray, distanceArray, inSpatialRef, outSpatialRef, geomServiceURL) {     var geomService = new esri.tasks.GeometryService(geomServiceURL);     var bufferParams = new esri.tasks.BufferParameters();     bufferParams.geometries = inGeometryArray;     bufferParams.bufferSpatialReference = inSpatialRef;     bufferParams.outSpatialReference = outSpatialRef;     bufferParams.distances = distanceArray; //units in feet     bufferParams.unit = esri.tasks.GeometryService.UNIT_METER;      var deferred = geomService.buffer(bufferParams);     return deferred;   }  //js code that is used to run the buffer and add the query task  var deferred = bufferGeometry([pointGeom], [50], pointGeom.spatialReference, map.spatialReference); deferred.then(function (results) { var query = new esri.tasks.Query(); query.geometry = results[0]; //or use one of the other constants to fine tune this query.spatialRelationship = esri.tasks.Query.SPATIAL_REL_WITHIN;  query.returnGeometry = true; query.outFields = ["CITY_NAME"]; var queryTask = new esri.tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0"); //exectute the query and call the queryComplete function/callback queryTask.execute(query, queryComplete); }, function (error) { alert("Error during buffer Geom: " + error.message); });  //another function to process your query results function queryComplete(featureSet) {     var features = featureSet.features;     //loop through your featureset and do something here! }  

View solution in original post

0 Kudos
2 Replies
DianaBenedict
Occasional Contributor III
You can do the following if you feel that this workflow works for you

1) Use the ESRI geometry task to create a buffer around the point at your specified distance
https://developers.arcgis.com/en/javascript/jsapi/geometryservice.html#buffer

2) use the returned geometry and pass in to your query.geometry property the new buffer geometry
https://developers.arcgis.com/en/javascript/jsapi/query.html
use the following query params to fine tune your query
query.geometry ==> your buffer geom
query.spatialRelationship ==> SPATIAL_REL_WITHIN
Default Value: SPATIAL_REL_INTERSECTS

3) use the results as needed

There should be plenty of examples out there for how to execute the buffer task and of course the query task as well. I have provided some sample code that I put together to show one of many ways to accomplish this task. The most important thing to keep in mind is that both the exectute buffer and execture query return deffered objects. You will want to utitlize this so that you can prevent the application/code from running away from you.

Below is a brief example of how you might accomplish this:

//buffer code that accepts the params for buffering and returns the dojo deferred object   bufferGeometry: function (inGeometryArray, distanceArray, inSpatialRef, outSpatialRef, geomServiceURL) {     var geomService = new esri.tasks.GeometryService(geomServiceURL);     var bufferParams = new esri.tasks.BufferParameters();     bufferParams.geometries = inGeometryArray;     bufferParams.bufferSpatialReference = inSpatialRef;     bufferParams.outSpatialReference = outSpatialRef;     bufferParams.distances = distanceArray; //units in feet     bufferParams.unit = esri.tasks.GeometryService.UNIT_METER;      var deferred = geomService.buffer(bufferParams);     return deferred;   }  //js code that is used to run the buffer and add the query task  var deferred = bufferGeometry([pointGeom], [50], pointGeom.spatialReference, map.spatialReference); deferred.then(function (results) { var query = new esri.tasks.Query(); query.geometry = results[0]; //or use one of the other constants to fine tune this query.spatialRelationship = esri.tasks.Query.SPATIAL_REL_WITHIN;  query.returnGeometry = true; query.outFields = ["CITY_NAME"]; var queryTask = new esri.tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0"); //exectute the query and call the queryComplete function/callback queryTask.execute(query, queryComplete); }, function (error) { alert("Error during buffer Geom: " + error.message); });  //another function to process your query results function queryComplete(featureSet) {     var features = featureSet.features;     //loop through your featureset and do something here! }  
0 Kudos
YohanBienvenue
Occasional Contributor II
Many thanks for this. I was wondering if I could avoid an extra request by using a geometry service first, but if it's the most efficient way I'll most likely implement this. I'm currently looking into running my own geometry service in ArcGIS Server

Thanks again
0 Kudos