📄 newshapefile.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 + -