首先是一个创建shp数据文件的函数,具体参数能根据变量名称看出来。函数如下:
public static void CreatShpFile(string shpFullFilePath, ISpatialReference spatialReference, esriGeometryType pGeometryType, string shpFileName)
{
string pFileName = shpFullFilePath + shpFileName + ".shp";
try
{
string shpFolder = System.IO.Path.GetDirectoryName(shpFullFilePath);
IWorkspaceFactory pWorkspaceFac = new ShapefileWorkspaceFactoryClass();
IWorkspace pWorkSpace = pWorkspaceFac.OpenFromFile(shpFolder, 0);
IFeatureWorkspace pFeatureWorkSpace = pWorkSpace as IFeatureWorkspace;
//如果文件已存在
if (System.IO.File.Exists(pFileName))
{
if (MessageBox.Show("文件已存在,是否覆盖?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
{
IFeatureClass pFCChecker = pFeatureWorkSpace.OpenFeatureClass(shpFileName);
if (pFCChecker != null)
{
IDataset pds = pFCChecker as IDataset;
pds.Delete();
}
}
else
{
return;
}
}
IFeatureClassDescription fcDescription = new FeatureClassDescriptionClass();
IObjectClassDescription pObjectDescription = (IObjectClassDescription)fcDescription;
IFields fields = pObjectDescription.RequiredFields;
int shapeFieldIndex = fields.FindField(fcDescription.ShapeFieldName);
IField field = fields.get_Field(shapeFieldIndex);
IGeometryDef geometryDef = field.GeometryDef;
IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
//线
geometryDefEdit.GeometryType_2 = pGeometryType; //geometry类型
geometryDefEdit.HasZ_2 = true; //使图层具有Z值(无,则创建一个二维文件,不具备Z值)
geometryDefEdit.SpatialReference_2 = spatialReference;
IFieldChecker fieldChecker = new FieldCheckerClass();
IEnumFieldError enumFieldError = null;
IFields validatedFields = null; //将传入字段 转成 validatedFields
fieldChecker.ValidateWorkspace = pWorkSpace;
fieldChecker.Validate(fields, out enumFieldError, out validatedFields);
pFeatureWorkSpace.CreateFeatureClass(shpFileName, validatedFields, pObjectDescription.InstanceCLSID, pObjectDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple, fcDescription.ShapeFieldName, "");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
当创建了Geometry,想将其存入shp数据文件中,具体函数如下所示:
public static void GenerateSHPFile(IGeometry pResultGeometry, string filename)
{
IWorkspaceFactory wsf = new ShapefileWorkspaceFactory();
IFeatureWorkspace fwp;
IFeatureLayer flay = new FeatureLayer();
fwp = (IFeatureWorkspace)wsf.OpenFromFile(System.IO.Path.GetDirectoryName(filename), 0);
IFeatureClass fc = fwp.OpenFeatureClass(System.IO.Path.GetFileName(filename));
flay.FeatureClass = fc;
flay.Name = flay.FeatureClass.AliasName;
IDataset pDataSet = flay.FeatureClass as IDataset;
IWorkspaceEdit m_WorkSpaceEdit = (IWorkspaceEdit)pDataSet.Workspace;
if (!m_WorkSpaceEdit.IsBeingEdited())
{
m_WorkSpaceEdit.StartEditing(true);
m_WorkSpaceEdit.EnableUndoRedo();
}
ITopologicalOperator pTop = pResultGeometry as ITopologicalOperator;
pTop.Simplify();
m_WorkSpaceEdit.StartEditOperation();
ITable pTable = flay.FeatureClass as ITable;
pTable.DeleteSearchedRows(null);
IFeature pFeature = fc.CreateFeature();
pFeature.Shape = ModifyGeomtryZMValue(fc, pResultGeometry);
pFeature.Store();
m_WorkSpaceEdit.StopEditOperation();
m_WorkSpaceEdit.StopEditing(true);
}
上函数里,用到的函数如下:
private static IGeometry ModifyGeomtryZMValue(IObjectClass featureClass, IGeometry modifiedGeo)
{
try
{
IFeatureClass trgFtCls = featureClass as IFeatureClass;
if (trgFtCls == null) return null;
string shapeFieldName = trgFtCls.ShapeFieldName;
IFields fields = trgFtCls.Fields;
int geometryIndex = fields.FindField(shapeFieldName);
IField field = fields.get_Field(geometryIndex);
IGeometryDef pGeometryDef = field.GeometryDef;
IPointCollection pPointCollection = modifiedGeo as IPointCollection;
if (pGeometryDef.HasZ)
{
IZAware pZAware = modifiedGeo as IZAware;
pZAware.ZAware = true;
IZ iz1 = modifiedGeo as IZ; //若报iz1为空的错误,则将设置Z值的这两句改成IPoint point = (IPoint)pGeo; point.Z = 0;
iz1.SetConstantZ((modifiedGeo as IPoint).Z);//如果此处报错,说明该几何体的点本身都没有Z值,在此处可以自己手动设置Z值,比如0,也就算将此句改成iz1.SetConstantZ(0);
}
else
{
IZAware pZAware = modifiedGeo as IZAware;
pZAware.ZAware = false;
}
if (pGeometryDef.HasM)
{
IMAware pMAware = modifiedGeo as IMAware;
pMAware.MAware = true;
}
else
{
IMAware pMAware = modifiedGeo as IMAware;
pMAware.MAware = false;
}
return modifiedGeo;
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message, "ModifyGeomtryZMValue error");
return modifiedGeo;
}
}
经过以上两步,即可完成此过程。将此两步分开,便于单独需要利用的情况。