AddXY.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. // #include "AddXY.h" int main(int argc, char **argv) { std::cout << "AddXY - ArcObjects C++ SDK Developer Sample" << std::endl << std::endl; if (argc != 2) { std::cerr << "Usage: AddXY [input shapefile]" << std::endl; AoExit(0); } char* data = argv[1]; // Process arguments CComBSTR dataPath; CComBSTR dataName; HRESULT hr = GetParentDirFromFullPath(data, &dataPath); if (FAILED(hr) || dataPath.Length() <= 0) { std::cerr << "Couldn't get input data path." << std::endl; AoExit(0); } hr = GetFileFromFullPath(data, &dataName); if (FAILED(hr) || dataName.Length() <= 0) { std::cerr << "Couldn't get input data file name." << std::endl; AoExit(0); } if (!InitializeApp()) { AoExit(0); } // Add the x and y fields hr = AddXYValues(dataPath, dataName); if (FAILED(hr)) { std::cerr << "AddXY failed." << std::endl; AoExit(0); } else std::cerr << "Done!" << std::endl; ShutdownApp(); AoExit(0); } HRESULT AddXYValues(BSTR dataPath, BSTR dataName) { IWorkspaceFactoryPtr ipWkspFac(CLSID_ShapefileWorkspaceFactory); IWorkspacePtr ipWksp; HRESULT hr = ipWkspFac->OpenFromFile(dataPath, 0, &ipWksp); if (FAILED(hr) || ipWksp == 0) { std::cerr << "Couldn't open workspace factory." << std::endl; return E_FAIL; } IFeatureWorkspacePtr ipFeatureWksp(ipWksp); IFeatureClassPtr ipFeatureClass; hr = ipFeatureWksp->OpenFeatureClass(dataName, &ipFeatureClass); if (FAILED(hr) || ipFeatureClass == 0) { std::cerr << "Couldn't open feature class." << std::endl; return E_FAIL; } esriGeometryType shape; ipFeatureClass->get_ShapeType(&shape); if (shape != esriGeometryPoint) { std::cerr << "This is not a shapefile of points." << std::endl; return E_FAIL; } else std::cerr << "Adding X & Y value columns to the shapefile of points..." << std::endl; // Check to see if X, Y fields are on the class. If not, add them. long found; ipFeatureClass->FindField(CComBSTR(L"textX"), &found); if (found == -1) { std::cerr << "Making the shape field textX..." << std::endl; IFieldPtr ipField(CLSID_Field); IFieldEditPtr ipFieldEdit(ipField); ipFieldEdit->put_Name(CComBSTR(L"textX")); ipFieldEdit->put_Type(esriFieldTypeDouble); ipFeatureClass->AddField(ipField); } else std::cerr << "textX field already exists -- no X values being added." << std::endl; ipFeatureClass->FindField(CComBSTR(L"textY"), &found); if (found == -1) { std::cerr << "Making the shape field textY..." << std::endl; IFieldPtr ipField(CLSID_Field); IFieldEditPtr ipFieldEdit(ipField); ipFieldEdit->put_Name(CComBSTR(L"textY")); ipFieldEdit->put_Type(esriFieldTypeDouble); ipFeatureClass->AddField(ipField); } else std::cerr << "textY field already exists -- no Y values being added." << std::endl; // Loop through the features and populate the new fields IFeatureCursorPtr ipFeatureCursor; ipFeatureClass->Update(0, VARIANT_FALSE, &ipFeatureCursor); std::cerr << "Getting the positions of the fields to update..." << std::endl; long lXField, lYField; ipFeatureCursor->FindField(CComBSTR(L"textX"), &lXField); ipFeatureCursor->FindField(CComBSTR(L"textY"), &lYField); std::cerr << "Processing the features..." << std::endl; IFeaturePtr ipFeature; ipFeatureCursor->NextFeature(&ipFeature); IPointPtr ipPoint; IGeometryPtr ipGeom; while (ipFeature != 0) { HRESULT hr = ipFeature->get_Shape(&ipGeom); ipPoint = ipGeom; double x, y; ipPoint->get_X(&x); ipPoint->get_Y(&y); ipFeature->put_Value(lXField, CComVariant(x)); ipFeature->put_Value(lYField, CComVariant(y)); ipFeatureCursor->UpdateFeature(ipFeature); ipFeatureCursor->NextFeature(&ipFeature); } return S_OK; }