Select to view content in your preferred language

How to get a single feature from an object id

1858
4
04-27-2022 07:24 AM
DaveLewis73
Regular 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).

4 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
          }
        }
      }
}

 

DaveLewis73
Regular 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.

Stefan_Offermann
Occasional Contributor

I also am looking for this one-liner as I know this was so easy to use in ArcObjects. Creating a filter and cursor object seems to bloating when comparing it to good ol' featClass.GetFeature(oid) 😐

0 Kudos
GKmieliauskas
Esri Regular Contributor

Hi,

You can use Inspector class Load method. There is Load method overload Load(Table,Int64) where parameters are table/featureclass and objectid. Then you can read attributes and geometry using inspector object.