Filter a shapefile

3993
4
Jump to solution
03-04-2015 12:55 PM
VygintasCesnauskas
New Contributor III

Is it possible to filter a shapefile by its attributes once it's loaded onto the map?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
OwenEarley
Occasional Contributor III

There are no methods in the API to do this but you could write your own.

The first thing I would do is to make sure that your graphics layer gets an Id with it is created. Check out the options parameter when creating the GraphicsLayer. Then when you want to apply a definition query:

  • Get your graphics layer object
  • Loop through all of the graphics
  • Test your definition query against a property in the attributes
  • Set the visibility of each graphic (based on your test condition)
  • Redraw the graphics layer

Something like this:

var gl = map.getLayer('shapefileGL');  // < this needs to be your graphics layer id
var value = dom.byId('defValue').value;  // this is the value to filter on
var field = "STRAHLER"; // this is the field (attribute) to filter on
setSimpleDefinitionQuery(gl, field, value);

function setSimpleDefinitionQuery(gl, field, value){ 
    dojo.forEach(gl.graphics,function(gr){
          gr.visible = (gr.attributes[field] == value);
    });
    gl.redraw();
}

View solution in original post

4 Replies
KellyHutchins
Esri Frequent Contributor

How are you adding the shapefile?

0 Kudos
OwenEarley
Occasional Contributor III

This question is a bit vague - how are you adding the shapefile to a javascript map?

If you are talking about a FeatureLayer then you can use setDefinitionExpression:

Set's the definition expression for the FeatureLayer. Only the features that match the definition expression are displayed. A definition expression limits the features available for display and queries by applying constraints to the layer's attribute fields.

If you are talking about an ArcgisDynamicMapServiceLayer then you can use setlayerdefinitions:

Sets the layer definitions used to filter the features of individual layers in the map service.
0 Kudos
VygintasCesnauskas
New Contributor III

Apologies for the lack of detail, shapefile is uploaded using esri/request:

var uploadRequest = esriRequest({
     url: this._uploadUrl,
     form: dojo.byId("formUploadShapeFile")
});

Then it's added to the map as a GraphicsLayer.

0 Kudos
OwenEarley
Occasional Contributor III

There are no methods in the API to do this but you could write your own.

The first thing I would do is to make sure that your graphics layer gets an Id with it is created. Check out the options parameter when creating the GraphicsLayer. Then when you want to apply a definition query:

  • Get your graphics layer object
  • Loop through all of the graphics
  • Test your definition query against a property in the attributes
  • Set the visibility of each graphic (based on your test condition)
  • Redraw the graphics layer

Something like this:

var gl = map.getLayer('shapefileGL');  // < this needs to be your graphics layer id
var value = dom.byId('defValue').value;  // this is the value to filter on
var field = "STRAHLER"; // this is the field (attribute) to filter on
setSimpleDefinitionQuery(gl, field, value);

function setSimpleDefinitionQuery(gl, field, value){ 
    dojo.forEach(gl.graphics,function(gr){
          gr.visible = (gr.attributes[field] == value);
    });
    gl.redraw();
}