Convert a coverage to a shapefile
Cov2Shape.cpp
// Copyright 2010 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 

  

// Cov2Shape.cpp
//
// This sample shows how to convert a coverage to a shapefile.

#include "Cov2Shape.h"

int main(int argc, char* argv[])
{
  char* fullPathToCoverage = 0;
  char* fullPathToShapefile = 0;
  int paramCount = 0;

  // Parse the command line
  for (int i = 1; i < argc; i++)
  {
    if (strcmp(argv[i], "-h") == 0)
    {
      Usage();
      AoExit(0);
    }
    if (strcmp(argv[i], "-c") == 0)
    {
      fullPathToCoverage = argv[i+1];
      paramCount++;
      i++;
    }
    if (strcmp(argv[i], "-s") == 0)
    {
      fullPathToShapefile = argv[i+1];
      paramCount++;
      i++;
    }
  }

  // Check we got all parameters and they are valid
  if ((paramCount != 2) || (fullPathToCoverage == 0) || (fullPathToShapefile == 0))
  {
    Usage();
    AoExit(0);
  }

  // Initialize ArcEngine
  if (!InitializeApp())
  {
    ShutdownApp();
    AoExit(0);
  }

  // Perform conversion
  HRESULT hr = ConvertCov2Shape(fullPathToCoverage, fullPathToShapefile);

  // Check returned HRESULT
  switch (hr)
  {
  case S_OK:
    std::cerr << "hr is S_OK" << std::endl;
    break;
  case E_FAIL:
    std::cerr << "hr is E_FAIL" << std::endl;
    break;
  case E_POINTER:
    std::cerr << "hr is E_POINTER" << std::endl;
    break;
  default:
    std::cerr << "hr is 0x" << std::hex << hr << std::dec << " " << hr << std::endl;
    break;
  }

  // Uninitialize ArcEngine
  ShutdownApp();
  AoExit(0);
}
HRESULT ConvertCov2Shape(char *fullPathToCoverage, char *fullPathToShapefile)
{
  using std::cerr;
  using std::endl;

  // Parse full path to input into directory and file name
  CComBSTR bstrCoveragePath;
  CComBSTR bstrCoverageName;
  HRESULT hr = GetParentDirFromFullPath(fullPathToCoverage, &bstrCoveragePath, false);
  if (FAILED(hr) || bstrCoveragePath.Length() == 0) 
  {
    cerr << "Error parsing path to coverage" << endl;
    return hr;
  }
  hr = GetFileFromFullPath(fullPathToCoverage, &bstrCoverageName);
  if (FAILED(hr) || bstrCoverageName.Length() == 0) 
  {
    cerr << "Error parsing path to coverage" << endl;
    return hr;
  }

  IWorkspaceFactoryPtr ipWorkFact(CLSID_ArcInfoWorkspaceFactory);
  IWorkspacePtr ipWork;
  hr = ipWorkFact->OpenFromFile(bstrCoveragePath, 0, &ipWork);
  if (FAILED(hr) || ipWork == 0)
  {
    cerr << "Couldn't find workspace\n";
    if (FAILED(hr)) 
      return hr;
    else
      return E_FAIL;
  }

  IFeatureWorkspacePtr ipFeatWork (ipWork);
  IFeatureClassPtr ipFeatClass;
  hr = ipFeatWork->OpenFeatureClass(bstrCoverageName, &ipFeatClass);
  if (FAILED(hr) || ipFeatClass == 0) 
  {
    cerr << "Couldn't open coverage\n";
    if (FAILED(hr)) 
      return hr;
    else
      return E_FAIL;
  }
  IDatasetPtr ipDataset (ipFeatClass);
  INamePtr ipName;
  ipDataset->get_FullName(&ipName);
  IFeatureClassNamePtr ipCovFeatClassName (ipName);

  // Parse full path to output into directory and file name
  CComBSTR bstrShapefilePath;
  CComBSTR bstrShapefileName;
  hr = GetParentDirFromFullPath(fullPathToShapefile, &bstrShapefilePath, false);
  if (FAILED(hr) || bstrShapefilePath.Length() == 0) 
  {
    cerr << "Error parsing path to shapefile" << endl;
    return hr;
  }
  hr = GetFileFromFullPath(fullPathToShapefile, &bstrShapefileName);
  if (FAILED(hr) || bstrShapefileName.Length() == 0) 
  {
    cerr << "Error parsing path to shapefile" << endl;
    return hr;
  }

  IWorkspaceNamePtr ipShapeWorkName(CLSID_WorkspaceName);
  ipShapeWorkName->put_WorkspaceFactoryProgID(CComBSTR(L"esriDataSourcesFile.ShapefileWorkspaceFactory"));
  ipShapeWorkName->put_PathName(bstrShapefilePath);
  IFeatureClassNamePtr ipShapeFeatClassName(CLSID_FeatureClassName);
  ((IDatasetNamePtr)ipShapeFeatClassName)->put_Name(bstrShapefileName);
  ((IDatasetNamePtr)ipShapeFeatClassName)->putref_WorkspaceName(ipShapeWorkName);

  IFeatureDataConverterPtr ipFeatDataConv(CLSID_FeatureDataConverter);
  IEnumInvalidObjectPtr ipEnumInvalidObject;
  hr = ipFeatDataConv->ConvertFeatureClass(ipCovFeatClassName, 0, 0, ipShapeFeatClassName,
    0, 0, 0, 1000, 0, &ipEnumInvalidObject);
  return hr;
}

// Write out the usage details
void Usage(void)
{
  std::cout << "Usage:  Cov2Shape -c [full path to coverage] -s [full path to output shapefile]\n";
}