Using Editor to select features, how to query for selections?

1030
16
01-08-2013 06:56 AM
Labels (1)
GeorgeFaraj
Occasional Contributor III
I'm using Editor for selecting features in my map. Now I want to retrieve the features that are selected, how can I do this? Also, is there a way to add features to the selection using code?
0 Kudos
16 Replies
GeorgeFaraj
Occasional Contributor III
I found FeatureLayer.SelectedGraphics, and this seems to work when I use the Editor to select features. But now I'm trying to select features through code, by using a QueryTask and setting the Selected property to true for the results. The FeatureLayer.SelectedGraphics collection does not get updated if I do this, and the features do not appear selected in the map. What am I missing here?

Thanks.
0 Kudos
GeorgeFaraj
Occasional Contributor III
Can anyone tell me how to select features through code?
0 Kudos
cristiBJ
New Contributor III
For example:

    var query = new esri.tasks.Query();
    query.where = "SR_ID = '" + requestID + "'"; //REQUESTID
    query.outFields = ["*"];
    //execute query
    map.getLayer(yourLayerId).selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW, function (features) {

    if (features.length > 0) {
            for (var i = 0; i < features.length; i++) {
               //do your coding
            }//for
    }//if
0 Kudos
GeorgeFaraj
Occasional Contributor III
For example:

    var query = new esri.tasks.Query();
    query.where = "SR_ID = '" + requestID + "'"; //REQUESTID
    query.outFields = ["*"];
    //execute query
    map.getLayer(yourLayerId).selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW, function (features) {

    if (features.length > 0) {
            for (var i = 0; i < features.length; i++) {
               //do your coding
            }//for
    }//if


I need to do this in C#/WPF.

Thanks.
0 Kudos
MatthewBrown1
Occasional Contributor
Hi Charles,

There could be a number of reasons for this, perhaps you could post the code for QueryTask and QueryTask_ExecuteCompleted?

Also, the QueryTask samples in the SDK Sample app are pretty comprehensive - looks like the Attribute Query example is what you are trying to replicate to some extent.
0 Kudos
GeorgeFaraj
Occasional Contributor III
Hi Mathew,

Here's my code:

public void SelectProperties(IEnumerable<int> propIds)
{
 UnselectAll();

 if (context.Layers.ActiveParcelLayer != null)
 {
  if (propIds.Count() > 0)
  {
   var queryTask = new QueryTask(context.Layers.ActiveParcelLayer.Url);
   queryTask.ExecuteCompleted += new EventHandler<QueryEventArgs>(queryTask_ExecuteCompleted);

   var query = new Query();
   query.Where = string.Format("PROP_ID in ({0})", string.Join(",", propIds));

   var featureSet = queryTask.Execute(query);
   if (featureSet != null)
   {
    foreach (var feature in featureSet)
    {
     feature.Selected = true;
    }

    context.Layers.ActiveParcelLayer.Refresh();
    OnSelectionChanged();
   }
  }
 }
 else
 {
  pendingSelection = new List<int>(propIds);
 }
}

void queryTask_ExecuteCompleted(object sender, QueryEventArgs e)
{
 foreach (var feature in e.FeatureSet)
 {
  feature.Selected = true;
 }

 context.Layers.ActiveParcelLayer.Refresh();
 OnSelectionChanged();
}


The QueryTask returns a set of features, and they have the right attribute values, but setting their Selected property to true (or calling the Select() method) is not doing anything that I can see.

Thanks!
0 Kudos
MatthewBrown1
Occasional Contributor
Hi Charles,

I think it might be an idea to first alter the foreach in ExecutedComplete to add the result to a graphics layer. This seems to be the easiest method to use and the graphics will still have attributes that you can manipulate or display.

So you'll have something like:


foreach (var feature in e.FeatureSet)
    {
        // Add graphics to a GraphicsLayer you have already created
        // e.g. GraphicsLayer graphicsLayer = new GraphicsLayer();
        graphicsLayer.Graphics.Add(feature);
    }

// Add layer to map
map.Layers.Add(graphicsLayer);


Then you can implement ZoomTo:


// Zoom to selected features (define expand percentage)
Envelope selectedFeatureExtent = graphicsLayer.FullExtent.Extent;

double expandPercentage = 30;
double widthExpand = selectedFeatureExtent.Width * (expandPercentage / 100);
double heightExpand = selectedFeatureExtent.Height * (expandPercentage / 100);

ESRI.ArcGIS.Client.Geometry.Envelope displayExtent = new ESRI.ArcGIS.Client.Geometry.Envelope(
selectedFeatureExtent.XMin - (widthExpand / 2),
selectedFeatureExtent.YMin - (heightExpand / 2),
selectedFeatureExtent.XMax + (widthExpand / 2),
selectedFeatureExtent.YMax + (heightExpand / 2));

map.ZoomTo(displayExtent);


As I said, this is pretty simple and you may have to take a different approach if your workflow is complicated.
0 Kudos
GeorgeFaraj
Occasional Contributor III
But adding them to another layer wouldn't get them to be listed in the SelectedGraphics collection of my feature layer.

I have an Editor assigned to my map, and the user can select features using their mouse. This all works nicely and the SelectedGraphics collection of the layer gets updated perfectly. All I want to do now is do this through code. After that, the user could use the Editor to add more features to the selection.
0 Kudos
MatthewBrown1
Occasional Contributor
I have an Editor assigned to my map, and the user can select features using their mouse. This all works nicely and the SelectedGraphics collection of the layer gets updated perfectly. All I want to do now is do this through code. After that, the user could use the Editor to add more features to the selection.


Can you elaborate on this? Do you have a use case/user story?

You could use an on click event to capture the subsequent user selection and add that to the graphics layer holding the query result. But I wonder if query task is actually the best option. Manipulating the graphics layer for the feature service layer might be better.
0 Kudos