How to get a single feature from an object id

983
2
04-27-2022 07:24 AM
DaveLewis73
Occasional Contributor

I have what I believe to be a very simple question, but I cannot find an answer for it.  Is there a method of getting a single feature from an object id (OID)?  In ArcObjects this can be accomplished by simply calling GetFeature() on a feature class object and passing in an object id.  An example would be IFeature feature = featClass.GetFeature(100).

0 Kudos
2 Replies
Aashis
by Esri Contributor
Esri Contributor

You can search by ObjectId using QueryFilter.

 

public void SearchFC(FeatureClass featureClass, IReadOnlyList<long> objectIDs)
{
      QueryFilter queryFilter = new QueryFilter()
      {
        ObjectIDs = objectIDs
      };

      using (RowCursor rowCursor = featureClass.Search(queryFilter))
      {
        while (rowCursor.MoveNext())
        {
          using (Feature feature = rowCursor.Current as Feature)
          {
            // use feature
          }
        }
      }
}

 

0 Kudos
DaveLewis73
Occasional Contributor

Yeah, that is the direction that I figured that I would have to go.  I ended up writing a function that uses a QueryFilter:

internal Task<int> GetMaxCellNumber()
{
    return QueuedTask.Run<int>(() =>
    {
        FeatureClass featClass = GridLayer.GetFeatureClass();
        int num = featClass.GetCount();
        List<long> oids = new List<long>();
        oids.Add(checked(num - 1));
        IReadOnlyList<long> objectIDs = oids.AsReadOnly();
        QueryFilter queryFilter = new QueryFilter()
        {
            ObjectIDs = objectIDs
        };
        RowCursor cursor = featClass.Search(queryFilter);
        Row row = null;
        if (cursor.MoveNext())
            row = cursor.Current;

        if (row == null)
            return 0;

        return (int)row["Cell_Num"];

    });
}

Does this seem like a good solution or can it be made simpler?  It just seems a little clunky.

0 Kudos