创建空白ShapeFile文件

从目标要素集中复制其字段,(图形字段不复制),复制到新建的空白ShapeFile文件中


        /// 
        /// 创建空白的ShpFile文件
        /// 
        /// 源要素集
        /// 文件夹路径
        /// 文件名称
        /// 
        private bool CreateShpFile(IFeatureClass pFeaClass, string sShpFilePath, string sShpName, bool bIsPoint)
        {
            bool bResult = false;
            const string sShapeFieldName = "SHAPE";

            try
            {
                //// 打开工作空间
                IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
                IFeatureWorkspace pWS = (IFeatureWorkspace)pWSF.OpenFromFile(sShpFilePath, 0);

                //// 设置字段集
                IFields pFields = new FieldsClass();
                IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;

                //// 设置字段
                IField pField = new FieldClass();
                IFieldEdit pFieldEdit = (IFieldEdit)pField;

                //// 创建几何类型字段
                pFieldEdit.Name_2 = sShapeFieldName;
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;

                IGeometryDef pGeoDef = new GeometryDefClass();
                IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef;

                if (bIsPoint)
                {
                    pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
                }
                else
                {
                    pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
                }
                //// 获得要素集的空间参考
                ISpatialReference pSpatialRef = (pFeaClass as IGeoDataset).SpatialReference;
                pGeoDefEdit.SpatialReference_2 = pSpatialRef;

                pFieldEdit.GeometryDef_2 = pGeoDef;
                pFieldsEdit.AddField(pField);

                //// 从源要素集中赋值字段到新的Shp文件
                for (int i = 0; i < pFeaClass.Fields.FieldCount; i++)
                {
                    IField pTempField = pFeaClass.Fields.get_Field(i);
                    if (!pTempField.Type.Equals(esriFieldType.esriFieldTypeGeometry))
                    {
                        pFieldsEdit.AddField(pTempField);
                    }
                }

                pWS.CreateFeatureClass(sShpName, pFields, null, null, esriFeatureType.esriFTSimple, sShapeFieldName, "");
                bResult = true;
            }
            catch (Exception ex)
            {
                GtMap.GxComHelper.LogWriter.WriteLog("创建空Shape文件失败", ex);
            }

            return bResult;
        }


你可能感兴趣的:(C#,ArcEngine)