Query a feature layer against another feature layer

5901
8
Jump to solution
07-06-2015 06:58 AM
TimWitt2
MVP Alum

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

Tags (2)
1 Solution

Accepted Solutions
thejuskambi
Occasional Contributor III

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         
    });       
} 

View solution in original post

0 Kudos
8 Replies
thejuskambi
Occasional Contributor III

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         
    });       
} 
0 Kudos
TimWitt2
MVP Alum

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.

0 Kudos
thejuskambi
Occasional Contributor III

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.

0 Kudos
TimWitt2
MVP Alum

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.

0 Kudos
thejuskambi
Occasional Contributor III

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.

TimWitt2
MVP Alum

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.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

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.

TimWitt2
MVP Alum

Ahh ok I didn't know that. I'll see if I can figure this out. thanks!

0 Kudos