how to set spatial filter for a featurelayer

4755
9
04-26-2010 09:08 AM
RaviKonaparthi
New Contributor II
Hi,

I am able to set spatial filters by providing input geometry and spatial relationship to a FeatureLayer in snapshot mode in silverlight api. How do i do this in JSAPI 2.0? The feature layer doesnt have any geometry property. Is there an elegant way of achieving this by some other means?

regards
Ravi
0 Kudos
9 Replies
KellyHutchins
Esri Frequent Contributor
Ravi,

You can use the queryFeatures method to query the feature layer using a spatial filter. The queryFeatures method takes in input query that has a geometry property you can use to define the filter geometry.

http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jsapi_start.htm#jsapi/query.htm
0 Kudos
RaviKonaparthi
New Contributor II
Hi kelly, thanks for the input.

I am actually looking for an equivalent of spatial filter (geometry) functionality provided in flex and silverlight api for a FeatureLayer. If I use queryFeatures method to query the feature layer, then I have to deal with results.

This is not so for a featureLayer in silverlight and flex api where I can set the geometry and spatialrelationship and it works!. Its pretty straightforward there.

I have noticed that jsapi 2.0 provides a datalayer class (esri.tasks.DataLayer) which has geometry and spatialrelationship property. But I am unable to get it to work and couldnt find any sample which uses a datalayer.

regards
Ravi
0 Kudos
KellyHutchins
Esri Frequent Contributor
Ravi,
We don't have a method directly on the feature layer to apply a spatial filter - I don't see one in Flex either.  However if you want to apply a spatial query directly to the feature layer one approach would be to add a feature layer in selection mode then use selectFeatures to perform a spatial query. Here's a sample that shows how this works, here only features within the specified extent are drawn on the map.
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
  <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
    <title>FeatureLayer On Demand</title> 
    <script type="text/javascript">djConfig = { parseOnLoad:true };</script> 
    <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.0"></script> 
    <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.0/js/dojo/dijit/themes/tundra/tundra.css"> 

    <script type="text/javascript"> 
      dojo.require("esri.map");
      dojo.require("esri.layers.FeatureLayer");
      dojo.require("dijit.form.Button");
      dojo.require("dijit.Dialog");

      var mapLayers = [];  //array of layers in client map
      var map;
      function init() {
        var extent = new esri.geometry.Extent({"xmin":-13337231.937022427,"ymin":3914529.1302062804,"xmax":-12909184.578625461,"ymax":4220277.2433469705,"spatialReference":{"wkid":102100}});
        map = new esri.Map("map", { extent:extent});
        dojo.connect(map, "onLoad", initOperationalLayer);

        var imagery = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer");
        map.addLayer(imagery);

      }

      function initOperationalLayer(map) {
      

        var featureLayer = new esri.layers.FeatureLayer("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer/0",{
          mode: esri.layers.FeatureLayer.MODE_SELECTION,
          outFields: ["*"]
        });
        map.addLayer(featureLayer);
        var extent = new esri.geometry.Extent({"xmin":-13228997.10497058,"ymin":3961002.843403707,"xmax":-13014973.425772188,"ymax":4113876.899973986,"spatialReference":{"wkid":102100}});
        var query = new esri.tasks.Query();
        query.geometry = extent;
        featureLayer.selectFeatures(query,esri.layers.FeatureLayer.SELECTION_NEW);

      }



      dojo.addOnLoad(init);
    </script> 
  </head> 
  <body class="tundra"> 
    <div style="position:relative;"> 
      <div id="map" style="width:700px; height:500px; border:1px solid #000;"></div> 
   
    </div> 
  </body> 
</html>

   
RaviKonaparthi
New Contributor II
Hi Kelly,

I have tried the following code and it was working fine in my scenario. But when i buffer a point for a radius of 10 KM using geometry service and pass the resultant polygon (in my case a circle) to the query filter it doesn't filter. But the same polygon if i convert to extent it works fine. Any idea why this behavior is?

Regards,
Ravi.
0 Kudos
BenStuder
New Contributor
Hi Kelly,

I have tried the following code and it was working fine in my scenario. But when i buffer a point for a radius of 10 KM using geometry service and pass the resultant polygon (in my case a circle) to the query filter it doesn't filter. But the same polygon if i convert to extent it works fine. Any idea why this behavior is?

Regards,
Ravi.


Ravi, did you set up a proxy server and reference it in your code?
http://resources.esri.com/help/9.3/ArcGISServer/apis/javascript/arcgis/help/jshelp_start.htm#jshelp/...

Any querystring longer than 2000 characters just won't work due to Web browser limitations.
0 Kudos
RaviKonaparthi
New Contributor II
Ben,

I am using proxy file for this functionality and i have fixed my issue, now i am able to apply spatial filter on a feature layer.

Thank you,
Ravi.
0 Kudos
AlessioDi_Lorenzo
New Contributor
Ben,
I am using proxy file for this functionality and i have fixed my issue, now i am able to apply spatial filter on a feature layer.


Ravi, I'll really appreciate if you would explain your solution.

I'm going to port my apps to arcgis 10 and js apis 2.1 and I need a way to use spatial filters and obtain a server-side rendering; if understood correctly this solution could work also for me.

thx
0 Kudos
JulianoKersting
New Contributor III

I have noticed that jsapi 2.0 provides a datalayer class (esri.tasks.DataLayer) which has geometry and spatialrelationship property. But I am unable to get it to work and couldnt find any sample which uses a datalayer.
Ravi


Yeah I am trying to use DataLayer as well but could not find any code samples using this.

Did you find Anything?
0 Kudos
HamidMokdes
New Contributor III
Hi,

I am able to set spatial filters by providing input geometry and spatial relationship to a FeatureLayer in snapshot mode in silverlight api. How do i do this in JSAPI 2.0? The feature layer doesnt have any geometry property. Is there an elegant way of achieving this by some other means?

regards
Ravi


Hi;

There is no spatialRelationship property exposed through FeatureLayer object. How did you set that in silverlight ? I can only see the Geometry property .

Thanks,
0 Kudos