Select points within polygon of separate layers

2638
8
02-23-2018 11:10 AM
ClintonLunn
New Contributor III

Hi all, 

I am trying to achieve something similar to a Select by Location operation. I am using the Javascript API 4.6. I have 2 layers: 1 is a point layer, and the second is a polygon layer of city boundaries. Essentially, I would like to select a city and return all of the points within that city boundary.

What I have done: 

I am using the search widget to enable me to text search the layer, where I have connected my layer of points. In this search widget, I can filter on a where statement, or geometry.

I have been able to successfully use the where statement when searching for objects within the same layer, but have not been able to get the filter by geometry to work. Here is a snippet of what I have come up with so far. In the console, it is showing that I am returning geometry objects, and it doesn't throw an error when I try to set the searchWidget geometry to the response geometry. However, it still returns points outside of my city boundary. 

     //on change, get the value (name) of the city
    var city = document.getElementById("filterCityPanel");
    var cityStr = query("#filterCityPanel").on("change", function(e) {
            
      var task = new QueryTask({
        url: url,
      });
    
      var params = new Query();
      params.returnGeometry = true;
      params.where = "name = '" + e.target.value + "'";
      params.outFields = ["name"];

      task.execute(params)
      .then(function(response) {
      searchWidget.sources.items[0].filter.geometry = response.features[0].geometry;
      }) 
    });‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Thank you! 

Clinton Lunn

8 Replies
RobertScheitlin__GISP
MVP Emeritus

Clinton,

   Once you have applied the filter to the source you still need to execute a search for the filter to take affect.

0 Kudos
ClintonLunn
New Contributor III

Robert, 


I am not sure if I understand what you mean. Do you mean once I filter to a city and then do a text search inside of my search widget?

I have been able to successfully get the where filter to execute. However, this was done on the same layer. See below for example: 

// build select panel for quad (url, field name, dropdown label, dom ID)    
buildSelectPanel(URL, "quad", "Filter by Quad", "filterQuadPanel");

    var quad = document.getElementById("filterQuadPanel");
    var quadStr = query("#filterQuadPanel").on("change", function(e) {
      quadStr = e.target.value.toUpperCase();
      var searchQuery = "quad = '" + quadStr + "'";
      searchWidget.sources.items[0].filter.where = searchQuery;
    
    });‍‍‍‍‍‍‍‍‍

Thank you for the speedy response!

Clinton

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Do you mean once I filter to a city and then do a text search inside of my search widget? Yes

0 Kudos
ClintonLunn
New Contributor III

Robert,

If I am understanding you correctly, I have done that. Once I apply a filter from the first post, I then go to my search widget and enter an ID number of the field that I am searching for, and in the suggested results, it will still return from outside of my selection area. 

Thank you,

Clinton 

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Is the filter geometry the same spatial reference as the data being searched?

0 Kudos
ClintonLunn
New Contributor III

Robert, 

 

Yes, they are both the same spatial reference. 

 

Thank you,

Clinton

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Hmm...  Sounds like there may be an issue with the search widget then.

I would get away from using the search widget and Just use a filtering select populated using the results of the first query.

0 Kudos
mukulchavan
New Contributor

Hi Clinton,

I am looking for API which will return boundary coordinates of cities. You have mentioned in your question that you are drawing polygon for city boundaries, can you help me here? I need an api which provide those details of jurisdictional boundary of city.

Thanks & Regards

Mukul 

0 Kudos