Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I need to perform search on a FeatureClass using ISpatialFilter for a given Point (x,y). I wrote a program in C# to do that and its retrieving the features as expected. But the similar program in C++ is failing for some feature classes and working for some. I am wondering where it went wrong.

The below c++ code works fine for featureclass cbsa. But doesnt work for BlockGroups.

    //Create Point
    IPointPtr ipPoint = NULL;
    ipPoint.CreateInstance(CLSID_Point);
    ipPoint->PutCoords( dX, xY);

    //Create Envelope
    IEnvelopePtr envelope(CLSID_Envelope);
    hr = ipPoint->get_Envelope(&envelope);

    //Open Feature Class
    IWorkspaceFactoryPtr pWorkspacefactory(CLSID_FileGDBWorkspaceFactory);
    IFeatureWorkspacePtr featureWorkspace;
    IWorkspacePtr workspace;
    hr = pWorkspacefactory->OpenFromFile(CComBSTR(gdbpath,0,&workspace);
    featureWorkspace = workspace;
    IFeatureClassPtr featureClass;
    hr = featureWorkspace->OpenFeatureClass(CComBSTR("CensusBGroups"),&featureClass);

    // Set ISpatialFilter Properties
    BSTR shapeFieldName;
    hr = featureClass->get_ShapeFieldName(&shapeFieldName);

    ISpatialFilterPtr spatialFilter(CLSID_SpatialFilter);
    spatialFilter->putref_Geometry(envelope);
    spatialFilter->put_SpatialRel(esriSpatialRelIntersects);
    spatialFilter->put_GeometryField(shapeFieldName);

    // Search
    IFeatureCursorPtr featureCursor;
    featureClass->Search(spatialFilter, false, &featureCursor); // featurecursor doesnt contain any featueres
    IFeaturePtr feature;

    // Get First Feature
    hr = featureCursor->NextFeature(&feature); //feature is null
    IFieldsPtr totalfields;
    hr = feature->get_Fields(&totalfields);
share|improve this question
    
Hi, What is the shape type of "cbsa" and "BlockGroups"? –  Darksanta May 11 '13 at 5:26
    
Both are of type Polygon –  John Nocom May 11 '13 at 9:28
    
Does the spatial reference of the envelope match that of the featureclass being searched? –  Kirk Kuykendall May 23 '13 at 1:46

1 Answer 1

I'm not sure that it works fine in C#, however, Point is 1 dimension geometry which doesn't contain valid extent. So, I recommended to use Point geometry instead of using Envelope geometry. Here is the sample code, and also I fixed some part of code which was verbose.

//Create Point
IPointPtr ipPoint(CLSID_Point);
ipPoint->PutCoords( dX, xY);

//Open Feature Class
IWorkspaceFactoryPtr pWorkspacefactory(CLSID_FileGDBWorkspaceFactory);
IFeatureWorkspacePtr featureWorkspace;
IWorkspacePtr workspace;
hr = pWorkspacefactory->OpenFromFile(CComBSTR(gdbpath),0,&workspace);
featureWorkspace = workspace;
IFeatureClassPtr featureClass;
hr = featureWorkspace->OpenFeatureClass(CComBSTR(L"CensusBGroups"),&featureClass);

// Set ISpatialFilter Properties
BSTR shapeFieldName;
hr = featureClass->get_ShapeFieldName(&shapeFieldName);

//Use Point geometry directly for SpatialFilter
ISpatialFilterPtr spatialFilter(CLSID_SpatialFilter);
spatialFilter->putref_Geometry(ipPoint);
spatialFilter->put_SpatialRel(esriSpatialRelIntersects);
//If the search origin is Point and search destination is Polygon,
//you can also use esriSpatialRelWithin
//spatialFilter->put_SpatialRel(esriSpatialRelWithin);
spatialFilter->put_GeometryField(shapeFieldName);

// Search
IFeatureCursorPtr featureCursor;
featureClass->Search(spatialFilter, false, &featureCursor); // featurecursor doesnt contain any featueres
IFeaturePtr feature;

// Get First Feature
hr = featureCursor->NextFeature(&feature); //feature is null
IFieldsPtr totalfields;
hr = feature->get_Fields(&totalfields);

Hope this helps you.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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