Maybe I'm just not wording my searches correctly, but I can't seem to find an answer to this. Is there a way to run a simple true/false intersect on an entire FeatureLayer?
Essentially I have a single polygon feature and I want to see if it intersects with a different Feature Layer that has 10's of thousands of features. Is the only way to do this to loop over every feature, checking if each one intersects with my target feature or is there a simpler way?
Thanks!
Solved! Go to Solution.
queryFeatures with spatial query
https://developers.arcgis.com/javascript/latest/api-reference/esri-rest-support-Query.html
Using the example from the link,
view.on("pointer-move", function(event){
let query = featureLayer.createQuery(); // THE OTHER LAYER
query.geometry = YOUR POLYGON
query.distance = 2;
query.units = "miles";
query.spatialRelationship = "intersects"; // this is the default
query.returnGeometry = true;
query.outFields = [ "POPULATION" ];
featureLayerView.queryFeatures(query)
.then(function(response){
// returns a feature set with features containing the
// POPULATION attribute and each feature's geometry
});
});
queryFeatures with spatial query
https://developers.arcgis.com/javascript/latest/api-reference/esri-rest-support-Query.html
Using the example from the link,
view.on("pointer-move", function(event){
let query = featureLayer.createQuery(); // THE OTHER LAYER
query.geometry = YOUR POLYGON
query.distance = 2;
query.units = "miles";
query.spatialRelationship = "intersects"; // this is the default
query.returnGeometry = true;
query.outFields = [ "POPULATION" ];
featureLayerView.queryFeatures(query)
.then(function(response){
// returns a feature set with features containing the
// POPULATION attribute and each feature's geometry
});
});
If we're only interested in knowing whether any features intersect, and don't need to know details anout the individual intersecting features themselves, we could also consider using the queryFeatureCount() method of a FeatureLayer - any count greater than zero means your polygon intersects, and a count of zero means no intersection. This saves having to transfer and process geometry and attribute data. Depending on your use case, that may be beneficial.