Hey everybody,
I am trying to query a point feature layer against a polygon feature layer. In ArcGIS Desktop this is very easy, just go to select by location and then query the point feature layer to see when it intersects with the polygon feature layer.
What is the best approach to do this via ArcGIS Javascript API? I am trying to find an example but the closest I can find is this Select with Feature Layer | ArcGIS API for JavaScript
Is there an example that uses 2 feature layers?
Thanks for any help!
Tim
Solved! Go to Solution.
You can use two Feature layers instead of one, similar to same example like this
function selectInBuffer(response){ var feature; var features = response.features; var inBuffer = []; //filter out features that are not actually in buffer, since we got all points in the buffer's bounding box for (var i = 0; i < features.length; i++) { feature = features; if(circle.contains(feature.geometry)){ //TODO: if the feature.geometry is point then buffer the point here inBuffer.push(feature.geometry); } } var query = new Query(); query.geometry =geometryEngine.union(inBuffer); featureLayer2.selectFeatures(query, function(results){ //TODO: do something with the second layer result }); }
You can use two Feature layers instead of one, similar to same example like this
function selectInBuffer(response){ var feature; var features = response.features; var inBuffer = []; //filter out features that are not actually in buffer, since we got all points in the buffer's bounding box for (var i = 0; i < features.length; i++) { feature = features; if(circle.contains(feature.geometry)){ //TODO: if the feature.geometry is point then buffer the point here inBuffer.push(feature.geometry); } } var query = new Query(); query.geometry =geometryEngine.union(inBuffer); featureLayer2.selectFeatures(query, function(results){ //TODO: do something with the second layer result }); }
Instead of the buffer I would like to use a polygon feature layer to select all the points from feature layer 1 that fall within the polygon feature layer.
the sample is not buffering any geometry. it is union of all the geometries. you could use where clause "1=1" on the first layer. and then combine all the polygons and query on the second layer.
PS: You need to watch out for the map service limit.
Before the code that you posted the actual query happens, which I have a problem with.
var query = new Query(); query.geometry = circle.getExtent(); //use a fast bounding box query. will only go to the server if bounding box is outside of the visible map featureLayer.queryFeatures(query, selectInBuffer);
Instead of the Circle extent I want to use a polygon feature layer.
My understanding is featureLayer is polygon feature layer and featureLayer2 is point feature layer. so when in query the 1st layer use query.where instead of circle
var query = new Query();
query.where= "1=1"; // or the condition of you polygon selection.
//use a fast bounding box query. will only go to the server if bounding box is outside of the visible map featureLayer.queryFeatures(query, selectInBuffer);
and on the result, combine all the polygons and get the 2nd layers.
I think we are talking about two different things.
I want to do a spatial query, like the "Select by Location" query in ArcGIS Desktop. I want all points that fall within another feature layer.
Tim,
There is no equivalent to select by location for a whole layer in the JS API. The method of unioning all geometries of one layer and querying the second using this unioned geom as thejus kambi is suggesting is the only option I am aware of.
Ahh ok I didn't know that. I'll see if I can figure this out. thanks!