ArcEngine开发:创建shp数据文件,并存入Geometry类型的要素到shp数据文件

首先是一个创建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;
            }
        }

经过以上两步,即可完成此过程。将此两步分开,便于单独需要利用的情况。

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值