IFeature.Shape throws an error

735
2
04-21-2011 01:05 PM
ReneRubalcava
Frequent Contributor
I don't usually need to deal with geometries too often in ArcObjects, but this is giving me quite a hard time.

I can get a IFeature object from my SDE. I can access the attributes fields just fine and pass them as needed. The problem is, when I try to access the IFeature.Shape property, I get the following error.
Error HRESULT E_FAIL has been returned from a call to a COM component.

stacktrace
at ESRI.ArcGIS.Geodatabase.IFeature.get_Shape()


This is a code snippet below.
Type sde_type = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory");
//IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)new SdeWorkspaceFactoryClass();

IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(sde_type);
IWorkspace workSpace = workspaceFactory.Open(propertySet, 0);
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workSpace;
#endregion

IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("sde.LA_Parcels");

// prepare the query filter
IQueryFilter qf = new QueryFilterClass();
qf.SubFields = "AIN";
qf.WhereClause = "AIN = '" + ain + "'";

IFeatureCursor fc = featureClass.Search(qf, true);

IFeature feat = fc.NextFeature();
string s_ain = "NOT FOUND";
if (feat != null)
{
 int field = feat.Fields.FindField("AIN");
 s_ain = feat.get_Value(field) as string;
 // grab some more field data ...
 
 IGeometry fgeom = null;
 try
 {
  fgeom = feat.Shape; // error gets thrown here
 }
 catch (Exception e)
 {
  throw new Exception(String.Format("ParcelLocationData Geometry from Shape Error: {0}", e.Message), e);
 }
}


I had read on a blog that it was good practice when creating ArcObjects objects on server to not use "new", but use Activator.CreateInstance, so I switched that up as it came up in my search on the COM error, but still nothing works.

I can view this data in ArcMap just fine, it works on MapServices that we have.
I have other C# dll's using ArcObjects running on this server just fine for sewer tracing, none of which actually access the IFeature.Shape property though.

Is there a step I am missing to try and do this?
My goal here is to use the IFeature.Shape as a basis for some spatial queries on other features.
Something like this
IArea area = (IArea)fgeom;
spatialFilter.Geometry = area.Centroid


Thanks in advance, and I'm sorry if this is something simple I am missing.
0 Kudos
2 Replies
ReneRubalcava
Frequent Contributor
Ok, I should kick myself.
this line...
qf.SubFields = "AIN";


needs to change to
qf.SubFields = "*";


Apparently, by explicitly defining the returned fields in the queryfilter, if you leave out Shape and OID, they will not be returned either. That's why IFeature.Shape failed, the Shape field was not included in my query. I just assumed Shape would be a default field regardless of what I specified.

Ok, that was quite a learning experience.
Thanks for looking.
0 Kudos
PrashantDhuri
New Contributor
Thanks a bunch... That solved my problem with shape and shape copy property.
0 Kudos