Select to view content in your preferred language

Use Geometry Filter as Query Filter for Query Task

3449
5
Jump to solution
02-27-2012 07:00 AM
CameronIrvine
Emerging Contributor
Hi,

In the REST query operation you are able to filter the geometry to a point so that the query only takes place at that location.  I want to be able to do the same with the javascript api so that I can return the results of a field of a polygon at a specific point.  I don't want to use a map for this but just return the text of the field.  I have tried to
use this example and modify it but without any success.http://help.arcgis.com/en/webapi/javascript/arcgis/demos/query/query_nomap.html

This is probably due in large part to my lack of javascript knowledge!  The plan is to pass in the XY location via url into the queryfilter which I believe I have working its just the actual query task that I am unsure about.

So I have taken the same code as the 'no map' example above and just changed the query part (see below).  I am sure I need to do something more with the geometry point but I can't find any examples in code.

Any help with this would be much appreciated!

Thanks
Cameron

 //build query task          myQueryTask = new esri.tasks.QueryTask("http://thgistest01/ArcGIS/rest/services/PollingDistrict/MapServer/1");          //Can listen for onComplete event to process results or can use the callback option in the queryTask.execute method.         //dojo.connect(queryTask, "onComplete", showResults);          //build query filter         myQuery = new esri.tasks.Query();   new esri.geometry.Point ([x,y], new esri.SpatialReference({ wkid: sr }));   mQuery.returnGeometry = "true";   myQuery.outFields = ["Polling_Station_UPRN", "District_Code"];   myQueryTask.execute(myQuery,showResults); 
0 Kudos
1 Solution

Accepted Solutions
derekswingley1
Deactivated User
That's strange...when I run your code:
new esri.geometry.Point (535487,182880, new esri.SpatialReference({ wkid: 2770 }));


It creates a Point object. But that's not what is being sent via REST. Can you double-check your code posted here matches the code in your app?

View solution in original post

0 Kudos
5 Replies
derekswingley1
Deactivated User
Build an esri.geometry.Point() from your query string parameters and set that as query.geometry. And you probably don't need returnGeometry = true if you're not using a map.
0 Kudos
CameronIrvine
Emerging Contributor
Build an esri.geometry.Point() from your query string parameters and set that as query.geometry. And you probably don't need returnGeometry = true if you're not using a map.


Hi Derek,

I have done as you suggested and set the xy values as the query.geometry but its not working.

query = new esri.tasks.Query();
  var XY = new esri.geometry.Point (535487,182880, new esri.SpatialReference({ wkid: 2770 }));
  query.geometry = XY;
  query.outFields = ["Polling_Station_UPRN", "District_Code"];
  queryTask.execute(query,showResults);


When I look at the call to the REST service I can see the XY are all screwed up.  I am sure this is because I am not declaring the point to the query.geometry correctly.  Can you help me with this?  Sorry my Javascript skills are non-existent.

Thanks

[HTML]http://thgistest01/ArcGIS/rest/services/PollingDistrict/MapServer/1/query?f=json&where=&returnGeomet...{%22x%22%3A535487%2C%22y%22%3A182880%2C%22spatialReference%22%3A{%22wkid%22%3A2770}}&geometryType=esriGeometryPoint&inSR=2770&outFields=Polling_Station_UPRN%2CDistrict_Code&outSR=2770&callback=dojo.io.script.jsonp_dojoIoScript1._jsonpCallback[/HTML]
0 Kudos
derekswingley1
Deactivated User
That's strange...when I run your code:
new esri.geometry.Point (535487,182880, new esri.SpatialReference({ wkid: 2770 }));


It creates a Point object. But that's not what is being sent via REST. Can you double-check your code posted here matches the code in your app?
0 Kudos
CameronIrvine
Emerging Contributor
Hi Derek,

hmmm this is rather embarrassing but it was the spatial reference code that was wrong ie 27700 rather than 2770!

One more quick question, do you have any idea why the variable uprn returns as undefined?  I am trying to get the value of the field "Polling_Station_UPRN" so that I can use it as an input in another application but for some reason the value comes back as undefined outside of the function it is called in.  I have created it as a global variable but it's still undefined.  Any ideas?

Thanks
Cameron

<script type="text/javascript">$.loadSite('http://cms.esriuk.com/TowerHamlets/', 'ab066578-6325-495c-ac12-da7a9696ac02', '#mygaz', configLoaded);
 
 dojo.require("esri.tasks.query");
 dojo.require("esri.tasks.geometry");
 dojo.require("dojo.parser");
 dojo.require("esri.map");
 
  
 var queryTask;
    var uprn;
 
   
 function configLoaded(config /* site config */) {
        config = config;
        
        $("#lhDiv").bind({
            "locatorhub.pickList": function (evt, locatorhub, picklist) {
                // added to fix bug in release 6 (SP5) of LVF. Will be fixed in release 7
                $(".jquery-locatorhub-picklistrow").removeAttr("href");
                $(".jquery-locatorhub-results").css("display", "block");
            },
            "locatorhub.searchDialogLocationFound": function (evt, locatorhub, match) {

                // Insert what you want to do with an address match here!!!!
                AddressFound(match)

            },
            "locatorhub.matchedLocationClick": function (evt, locatorhub, match) {
                // Insert what you want to do with an address match here!!!!
                AddressFound(match);
            },
            "locatorhub.addressSearchError": function () {

                alert("Address Search error");
            },
            "locatorhub.error": function (evt, error) {
                alert("locator hub error")
            }

        })

    }
 
 function AddressFound(match) {
       
       x = match.Point.x;
       y = match.Point.y;
    sr = match.Point.spatialReference.wkid; ;
  
    
  queryTask = new esri.tasks.QueryTask("http://thgistest01/ArcGIS/rest/services/PollingDistrict/MapServer/1");

        //Can listen for onComplete event to process results or can use the callback option in the queryTask.execute method.
        //dojo.connect(queryTask, "onComplete", showResults);
  
        //build query filter
        query = new esri.tasks.Query();
  var XY = new esri.geometry.Point (x,y, new esri.SpatialReference({ wkid: 27700 }));
  query.geometry = XY;
  query.outFields = ["Polling_Station_UPRN"];
  //query.text = "Bow East 2";
  queryTask.execute(query);
         
     
  dojo.connect(queryTask, "onComplete", function (myFeatureSet) {
      
  var s = "";
    
        for (var i=0, il=myFeatureSet.features.length; i<il; i++) {
          var featureAttributes = myFeatureSet.features.attributes;
          for (att in featureAttributes) {
            s = s + "<strong>" + att + ": </strong>" +
   featureAttributes[att] + "<br />";
  
          }
    
        }
        dojo.byId("info").innerHTML = s;
 uprn = featureAttributes[att];
  
  
      }  
    
  );
  
 alert (uprn)
  
   
  }
0 Kudos
derekswingley1
Deactivated User
featureAttributes is a local variable in your for loop. If you want to reference it outside of your loop, define it before your loop. In the future, please start a new thread for additional questions.
0 Kudos