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

1046
16
01-08-2013 06:56 AM
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
Can you elaborate on this? Do you have a use case/user story?


I have an Editor set on my map, and have a toolbar that activates the editor for selection purposes only. The user will select features, then they can right-click and perform actions on the selected features.

There's also a "search" tool where the user can enter some criteria and the resulting graphics should get selected. The user can now right-click and perform the same actions as above on those selections.
0 Kudos
MichaelBranscomb
Esri Frequent Contributor
Hi,

Although the Editor is selecting the graphics/features, the actual selections exist in the layers. You can use the Editor.GraphicsLayers property to get a list of all the GraphicsLayers/FeatureLayers with which the Editor control is interacting then iterate their selection sets.

Cheers

Mike
0 Kudos
DominiqueBroux
Esri Frequent Contributor
   if (featureSet != null)
   {
    foreach (var feature in featureSet)
    {
     feature.Selected = true;
    }

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

The features returned by the query tasks and the features of your graphics layer are not the same, so setting feature.Selected to true has no effect on the feature in the graphics layer.

I think you should use the ObjectId to retrieve the features of the graphics layer having the same objectId as the features returned by your query.
0 Kudos
GeorgeFaraj
Occasional Contributor III
Hi Dominique,

Can you provide an example of what you're suggesting? I think I understand what you're saying, but not sure. Currently what I was doing was that I would add the graphic returned by the query if it didn't exist in the Graphics collection. But then it looks like the graphic gets duplicated by the layer.

Note: this is a dynamic feature layer.


var queryTask = new QueryTask(context.Layers.ActiveParcelLayer.Url);
queryTask.ExecuteCompleted += new EventHandler<QueryEventArgs>(queryTask_ExecuteCompleted);

var query = new Query();
query.OutFields.Add("*");
query.Source = layer.Source;
query.ReturnGeometry = true;
query.OutSpatialReference = map.SpatialReference;
query.Where = string.Format("PROP_ID in ({0})", string.Join(",", propIds));

var featureSet = queryTask.Execute(query);
if (featureSet != null)
{
 foreach (var feature in featureSet)
 {
  var propId = GisHelper.GetPropertyId(feature);
  var graphic = layer.Graphics.FirstOrDefault(
   g =>  GisHelper.GetPropertyId(g) == propId
  );
  if (graphic == null)
  {
   graphic = feature;
   layer.Graphics.Add(graphic);
  }
       
  graphic.Selected = true;
 }
}


Any suggestions?
0 Kudos
DominiqueBroux
Esri Frequent Contributor
You got the idea 🙂 I don't see anything wrong in your code.:confused:

I guess you checked that your GisHelper.GetPropertyId method was working well?
Check also that you included the PROP_ID field in the OutFields of your feature layer (in XAML).

Sorry no others ideas
0 Kudos
GeorgeFaraj
Occasional Contributor III
Hey Dominique,

You had mentioned using ObjectID, what did you mean by that exactly?

The problem with my code, apparently, is that I'm adding a feature to the Graphics collection of my layer, but since my layer is OnDemand, whenever I zoom to the region that contains the feature that I added, it seems to be duplicating that feature on top of it, so the selected graphic is hidden.

I might be understanding it incorrectly too. This does seem to be happening only if I do this in the Initialized event handler of my layer... If I do this moments after the event has finished, this doesn't happen. I'm able to see my selected graphics.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
You had mentioned using ObjectID, what did you mean by that exactly?

I meant an unique identifier in order to avoid feature duplications.
The ObjectId is a good candidate (the OnDemand mechanism uses it as well to avoid duplication).
Most generally the ObjectId field is called "OBJECTID" but to be more generic you can get the objectid field name from FeatureLayerInfo.ObjectIdField.
0 Kudos