⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newshapefile.cpp

📁 地理信息系统ArcEngine二次开发:实现建立新矢量图层功能。
💻 CPP
字号:
// Dont include .shp extension
HRESULT CreateShapefile(BSTR bstrPath, BSTR bstrName, IFeatureClass** ppFeatureClass)
{
  if (!ppFeatureClass)
    return E_POINTER;

  HRESULT hr;

  // Open the folder to contain the shapefile as a workspace
  IWorkspaceFactoryPtr ipWorkspaceFactory(CLSID_ShapefileWorkspaceFactory);
  IWorkspacePtr ipWS;
  hr = ipWorkspaceFactory->OpenFromFile(bstrPath, 0, &ipWS);
  IFeatureWorkspacePtr ipFWS(ipWS);

  // Set up a simple fields collection
  IFieldsPtr ipFields(CLSID_Fields);
  IFieldsEditPtr ipFieldsEdit(ipFields);

  // Make the shape field
  // it will need a geometry definition, with a spatial reference
  IFieldPtr ipField(CLSID_Field);
  IFieldEditPtr ipFieldEdit(ipField);
  hr = ipFieldEdit->put_Name(CComBSTR(L"Shape"));
  hr = ipFieldEdit->put_Type(esriFieldTypeGeometry);

  IGeometryDefPtr ipGeomDef(CLSID_GeometryDef);
  IGeometryDefEditPtr ipGeomDefEdit(ipGeomDef);
  hr = ipGeomDefEdit->put_GeometryType(esriGeometryPolygon);
  ISpatialReferencePtr ipSpatialRef(CLSID_UnknownCoordinateSystem);
  hr = ipGeomDefEdit->putref_SpatialReference(ipSpatialRef);
  hr = ipFieldEdit->putref_GeometryDef(ipGeomDef);
  hr = ipFieldsEdit->AddField(ipField);

  // Add another miscellaneous text field
  ipField.CreateInstance(CLSID_Field);
  ipFieldEdit = ipField;
  hr = ipFieldEdit->put_Length(30);
  hr = ipFieldEdit->put_Name(CComBSTR(L"MiscText"));
  hr = ipFieldEdit->put_Type(esriFieldTypeString);
  hr = ipFieldsEdit->AddField(ipField);

  // Create the shapefile
  // (some parameters apply to geodatabase options and can be defaulted as Nothing)
  IFeatureClassPtr ipFeatClass;
  hr = ipFWS->CreateFeatureClass(bstrName, ipFields, 0, 0, esriFTSimple, CComBSTR(L"Shape"), CComBSTR(L""), &ipFeatClass);

  *ppFeatureClass = ipFeatClass;
  if (*ppFeatureClass)
    (*ppFeatureClass)->AddRef();
  else
    return E_FAIL;

  return S_OK;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -