Shape2Raster.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. // // Shape2Raster // // This sample shows how to use the IConversionOp interface to convert // a shapefile into a GRID raster. The output GRID is created in the // same directory as the input shapefile. #include "Shape2Raster.h" int main(int argc, char* argv[]) { char *shapeFileName = 0; char *rasterFileName = 0; char *fieldName = 0; double cellSize = 0.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], "-s") == 0) { shapeFileName = argv[i+1]; paramCount++; i++; } if (strcmp(argv[i], "-r") == 0) { rasterFileName = argv[i+1]; paramCount++; i++; } if (strcmp(argv[i], "-f") == 0) { fieldName = argv[i+1]; paramCount++; i++; } if (strcmp(argv[i], "-c") == 0) { cellSize = strtod(argv[i+1], NULL); paramCount++; i++; } } // Check we got all parameters and that they are valid if ((paramCount != 4) || (shapeFileName == 0) || (fieldName == 0) || (rasterFileName == 0) || (cellSize == 0.0)) { Usage(); AoExit(0); } // Initialize ArcEngine if (!InitializeApp()) { ShutdownApp(); AoExit(0); } // Perform conversion HRESULT hr = ConvertShape2Raster(shapeFileName, fieldName, rasterFileName, cellSize); // 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; case E_SPATIAL_ANALYST_INVALID_FIELD: std::cerr << "An invalid field name was supplied.\n"; break; default: std::cerr << "hr is 0x" << std::hex << hr << std::dec << " " << hr << std::endl; break; } // Uninitialize ArcEngine ShutdownApp(); AoExit(0); } HRESULT ConvertShape2Raster(char* fullPathToShape, char *fieldName, char* rasterFileName, double dblCellSize) { using std::cerr; using std::endl; // Parse full path into directory and file name CComBSTR bstrPath; CComBSTR bstrShapefileName; HRESULT hr = GetParentDirFromFullPath(fullPathToShape, &bstrPath, false); if (FAILED(hr) || bstrPath.Length() == 0) { cerr << "Error parsing path to shapefile" << endl; return hr; } hr = GetFileFromFullPath(fullPathToShape, &bstrShapefileName); if (FAILED(hr) || bstrShapefileName.Length() == 0) { cerr << "Error parsing path to shapefile" << endl; return hr; } // Open shapefile IWorkspaceFactoryPtr ipWorkFact(CLSID_ShapefileWorkspaceFactory); IWorkspacePtr ipWork; hr = ipWorkFact->OpenFromFile(bstrPath, 0, &ipWork); if (FAILED(hr) || ipWork == 0) { cerr << "Couldn't find workspace\n"; return E_FAIL; } IFeatureWorkspacePtr ipFeatWork (ipWork); IFeatureClassPtr ipFeatClass; hr = ipFeatWork->OpenFeatureClass(bstrShapefileName, &ipFeatClass); if (FAILED(hr) || ipFeatClass == 0) { cerr << "Couldn't open shapefile\n"; return E_FAIL; } // Get a raster workspace for output IWorkspaceFactoryPtr ipRastWorkFact(CLSID_RasterWorkspaceFactory); IWorkspacePtr ipRastWork; hr = ipRastWorkFact->OpenFromFile(bstrPath, 0, &ipRastWork); if (FAILED(hr) || ipRastWork == 0) { cerr << "Couldn't find output workspace\n"; return E_FAIL; } // Create a feature class descriptor. This is used to pick which field in the // shapefile supplies the values for the output GRID IFeatureClassDescriptorPtr ipFeatClassDescr(CLSID_FeatureClassDescriptor); hr = ipFeatClassDescr->Create(ipFeatClass, 0, CComBSTR(fieldName)); if (FAILED(hr)) return hr; IGeoDatasetPtr ipGeoData (ipFeatClassDescr); // Create raster conversion operator and set up analysis environment IConversionOpPtr ipConv(CLSID_RasterConversionOp); IRasterAnalysisEnvironmentPtr ipRastAnalEnv (ipConv); ipRastAnalEnv->putref_OutWorkspace(ipRastWork); esriRasterEnvSettingEnum envType = esriRasterEnvValue; CComVariant cellSize(dblCellSize); ipRastAnalEnv->SetCellSize(envType, &cellSize); IEnvelopePtr ipEnv; ipGeoData->get_Extent(&ipEnv); CComVariant varExtent(ipEnv); ipRastAnalEnv->SetExtent(esriRasterEnvValue, &varExtent, 0); // Perform conversion IRasterDatasetPtr ipRastData; hr = ipConv->ToRasterDataset(ipGeoData, CComBSTR(L"GRID"), ipRastWork, CComBSTR(rasterFileName), &ipRastData); return hr; } // Display usage details void Usage(void) { std::cerr << "Usage: Shape2Raster -s [path to shapefile] -f [field name for cell values] -r [name for output raster] -c [cell size]\n"; }