1

My method must return closest feature attribute. But there's a problem, it finds closest feature but returns COM exception HRESULT 0x80010202

void selectStreet(IPoint point, IActiveView activeView)
{

..........

if (getLayerByName._GetLayerByName(settings.StreetsLayerName) != null) // Settings1 settings = new Settings1(); settings.StreetsLayerName = "Street"
            {
                var foundLayer = getLayerByName._GetLayerByName(settings.StreetsLayerName);
                IQueryFilterClass qFilter = new IQueryFilterClass () { WhereClause = string.Format("{0} IS NOT NULL", settings.StreetsName) };
                IFeatureCursor featureCursor = (foundLayer as IGeoFeatureLayer).Search(qFilter, false);
                while ((feature = featureCursor.NextFeature()) != null)
                {
                    double distance = ((IProximityOperator)spatialFilter.Geometry).ReturnDistance(feature.Shape);
                    IProximityOperator prox = spatialFilter.Geometry as IProximityOperator;
                    distance = prox.ReturnDistance(feature.Shape);

                    if (closesToId == -1 || distance < closestDistance)
                    {
                        closesToId = feature.OID;
                        closestDistance = distance;
                        resultFeature = feature;
                        var one = feature.get_Value(feature.Fields.FindField(settings.StreetsName)).ToString(); // StreetsName = "STR_NAME"
                        var two = feature.get_Value(feature.Fields.FindField(settings.StreetsStreetCode)).ToString(); //StreetsStreetCode = "STR_CODE"
                    }
                 }
                }
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message); // HRESULT 0x80010202 exception
            }
            return null;
        }
            Where could be a problem?
1

Your code looked ok to me.

I did simplify it a bit so I could run it to as an add-in Button and it worked fine in 10.2.2. You will be able to run this on two layers, The first layer in the Map will need to have a field called ID, and the Second layer needs a feature with OID 5 as written.

What line is the code failing on?

public class Button1 : ESRI.ArcGIS.Desktop.AddIns.Button
{
    public Button1()
    {
    }

    protected override void OnClick()
    {

        TestMethod();

        ArcMap.Application.CurrentTool = null;
    }
    protected override void OnUpdate()
    {
        Enabled = ArcMap.Application != null;
    }
    private void TestMethod() {
        try
        {
            IMxDocument mxd = ArcMap.Document;
            ISpatialFilter spatialFilter = new SpatialFilter();

            IQueryFilter qFilter = new QueryFilterClass() { WhereClause = string.Format("{0} IS NOT NULL", "ID") };
            ILayer foundLayer = mxd.FocusMap.get_Layer(0);
            IFeatureLayer layer2 = (IFeatureLayer)mxd.FocusMap.get_Layer(1);
            spatialFilter.Geometry = layer2.FeatureClass.GetFeature(5).Shape;

            IFeatureCursor featureCursor = (foundLayer as IGeoFeatureLayer).Search(qFilter, false);
            IFeature feature;

            List<DistanceTest> _list = new List<DistanceTest>();

            while ((feature = featureCursor.NextFeature()) != null)
            {
                double distance = ((IProximityOperator)spatialFilter.Geometry).ReturnDistance(feature.Shape);
                IProximityOperator prox = spatialFilter.Geometry as IProximityOperator;
                distance = prox.ReturnDistance(feature.Shape);
                //Simplified to Prove concept
                if (distance > 0)
                {

                     var one = feature.get_Value(feature.Fields.FindField("ID")); // StreetsName = "STR_NAME"

                     _list.Add(new DistanceTest(feature.OID, distance));


                }
            }
   foreach (var _dist in _list)
        {
            System.Diagnostics.Debug.WriteLine("ID: {0} Distance: {1}", _dist.ID.ToString(), _dist.Distance.ToString());
        }     }

        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine(e.Message); 
        }


        return;
    }
    private class DistanceTest {
        public int ID { get; set; }
        public Double Distance { get; set; }

        public DistanceTest(int _id, double _distance) {
            this.ID = _id;
            Distance = _distance;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.