krfournier

Polygon area always returning same result.

Discussion created by krfournier on Feb 23, 2011
I have a feature layer containing polygons, and a method that allows me to request a particular polygon's area. The code seems straight forward to me, but I always get the same value regardless of the polygon.

Here is the ATL/C++ code I am using. Does anything immediately wrong stand out to anyone?

double CalculatePolygonArea(LPCTSTR lpszLayer, LPCTSTR lpszFeature)
{
 double dResult = 0.0;

 int nIndex = m_mapLayers.GetAt( lpszLayer );
 if( nIndex >= 0 && m_pMapControl )
 {
  CComPtr<ILayer> pLayer = NULL;
  if( SUCCEEDED(m_pMapControl->get_Layer(nIndex, &pLayer)) && pLayer )
  {
   CComQIPtr<IFeatureLayer> pFeatureLayer(pLayer);
   if( pFeatureLayer )
   {
    CComPtr<IFeatureClass> pFeatureClass = NULL;
    if( SUCCEEDED(pFeatureLayer->get_FeatureClass(&pFeatureClass)) && pFeatureClass )
    {
     CComPtr<IQueryFilter> pQueryFilter = NULL;
     if( SUCCEEDED(pQueryFilter.CoCreateInstance(CLSID_QueryFilter)) && pQueryFilter )
     {
      pQueryFilter->AddField(CComBSTR("SHAPE"));

      CString strWhereClause;
      strWhereClause.Format( _T("KEY = '%s'"), lpszFeature );

      CComPtr<IFeatureCursor> pFeatureCursor = NULL;
      if( SUCCEEDED(pFeatureLayer->Search(pQueryFilter, VARIANT_FALSE, &pFeatureCursor)) && pFeatureCursor )
      {
       CComPtr<IFeature> pFeature = NULL;
       if( SUCCEEDED(pFeatureCursor->NextFeature(&pFeature)) && pFeature )
       {
        CComPtr<IGeometry> pGeometry = NULL;
        if( SUCCEEDED(pFeature->get_Shape(&pGeometry)) && pGeometry )
        {
         CComQIPtr<IArea> pArea( pGeometry );
         if( pArea )
          pArea->get_Area( &dResult );
        }
       }
      }
     }
    }
   }
  }
 }

 return dResult;
}

Outcomes