/// <summary> /// 创建FileGDB和PGDB Workspace /// @remark /// 1.Shp创建Workspace没有意义 /// 2.不支持SDE创建 /// </summary> /// <param name="wsType"></param> /// <param name="strPath"></param> /// <param name="strName"></param> /// <returns></returns> public static IWorkspace CreateWorkspace(enumWorkspaceType wsType, string strPath, string strName) { try { IWorkspaceFactory wsf = null; switch (wsType) { case enumWorkspaceType.SDE: throw new Exception("CreateWorkspace方法被设计为不支持SDE方式创建"); case enumWorkspaceType.FileGDB: wsf = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactoryClass(); break; case enumWorkspaceType.PGDB: wsf = new AccessWorkspaceFactoryClass(); break; case enumWorkspaceType.File: wsf = new ShapefileWorkspaceFactoryClass(); break; } IWorkspaceName wsName = wsf.Create(strPath, strName, null, 0); return (wsName as IName).Open() as IWorkspace; } catch { return null; } }
private void ExportFeature(IFeatureClass pInFeatureClass, string pPath) { //创建一个输出shp文件的工作空间 IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); string parentPath = pPath.Substring(0, pPath.LastIndexOf("\\")); string fileName = pPath.Substring(pPath.LastIndexOf("\\") + 1, pPath.Length - pPath.LastIndexOf("\\") - 1); IWorkspaceName pWorkspaceName = pWorkspaceFactory.Create(parentPath, fileName, null, 0); //创建一个要素类 IName name = (IName)pWorkspaceName; IWorkspace pOutWorkspace = (IWorkspace)name.Open(); IDataset pInDataset = pInFeatureClass as IDataset; IFeatureClassName pInFCName = pInDataset.FullName as IFeatureClassName; IWorkspace pInWorkspace = pInDataset.Workspace; IDataset pOutDataset = pOutWorkspace as IDataset; IWorkspaceName pOutWorkspaceName = pOutDataset.FullName as IWorkspaceName; IFeatureClassName pOutFCName = new FeatureClassNameClass(); IDatasetName pDatasetName = pOutFCName as IDatasetName; pDatasetName.WorkspaceName = pOutWorkspaceName; pDatasetName.Name = pInFeatureClass.AliasName; IFieldChecker pFieldChecker = new FieldCheckerClass(); pFieldChecker.InputWorkspace = pInWorkspace; pFieldChecker.ValidateWorkspace = pOutWorkspace; IFields pFields = pInFeatureClass.Fields; IFields pOutFields; IEnumFieldError pEnumFieldError; pFieldChecker.Validate(pFields, out pEnumFieldError, out pOutFields); IFeatureDataConverter pFeatureDataConverter = new FeatureDataConverterClass(); pFeatureDataConverter.ConvertFeatureClass(pInFCName, null, null, pOutFCName, null, pOutFields, "", 100, 0); }
public void ExportFeature(IFeatureClass pInFeatureClass, string pPath) { // create a new Access workspace factory IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); string parentPath = pPath.Substring(0, pPath.LastIndexOf('\\')); string fileName = pPath.Substring(pPath.LastIndexOf('\\') + 1, pPath.Length - pPath.LastIndexOf('\\') - 1); IWorkspaceName pWorkspaceName = pWorkspaceFactory.Create(parentPath, fileName, null, 0); // Cast for IName IName name = (IName)pWorkspaceName; //Open a reference to the access workspace through the name object IWorkspace pOutWorkspace = (IWorkspace)name.Open(); IDataset pInDataset = pInFeatureClass as IDataset; IFeatureClassName pInFCName = pInDataset.FullName as IFeatureClassName; IWorkspace pInWorkspace = pInDataset.Workspace; IDataset pOutDataset = pOutWorkspace as IDataset; IWorkspaceName pOutWorkspaceName = pOutDataset.FullName as IWorkspaceName; IFeatureClassName pOutFCName = new FeatureClassNameClass(); IDatasetName pDatasetName = pOutFCName as IDatasetName; pDatasetName.WorkspaceName = pOutWorkspaceName; pDatasetName.Name = pInFeatureClass.AliasName; IFieldChecker pFieldChecker = new FieldCheckerClass(); pFieldChecker.InputWorkspace = pInWorkspace; pFieldChecker.ValidateWorkspace = pOutWorkspace; IFields pFields = pInFeatureClass.Fields; IFields pOutFields; IEnumFieldError pEnumFieldError; pFieldChecker.Validate(pFields, out pEnumFieldError, out pOutFields); IFeatureDataConverter pFeatureDataConverter = new FeatureDataConverterClass(); pFeatureDataConverter.ConvertFeatureClass(pInFCName, null, null, pOutFCName, null, pOutFields, "", 100, 0); }
public void SaveShpToFile(IFeatureClass pFeatureClass, string ExportFilePath, string ExportFileShortName) { //设置导出要素类的参数 IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass(); IDataset pOutDataset = (IDataset)pFeatureClass; pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName; //创建一个输出shp文件的工作空间 IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspaceName pInWorkspaceName = new WorkspaceNameClass(); pInWorkspaceName = pShpWorkspaceFactory.Create(ExportFilePath, ExportFileShortName, null, 0); //创建一个要素类 IFeatureClassName pInFeatureClassName = new FeatureClassNameClass(); IDatasetName pInDatasetClassName; pInDatasetClassName = (IDatasetName)pInFeatureClassName; pInDatasetClassName.Name = ExportFileShortName;//作为输出参数 pInDatasetClassName.WorkspaceName = pInWorkspaceName; IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass(); pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, null, pInFeatureClassName, null, null, "", 1000, 0); MessageBox.Show("导出成功", "系统提示"); }
/// <summary> /// 创建FileGDB和PGDB Workspace /// @remark /// 1.Shp创建Workspace没有意义 /// 2.不支持SDE创建 /// </summary> /// <param name="wsType"></param> /// <param name="strPath"></param> /// <param name="strName"></param> /// <returns></returns> public static IWorkspace CreateWorkspace(enumWorkspaceType wsType, string strPath, string strName) { try { IWorkspaceFactory wsf = null; switch (wsType) { case enumWorkspaceType.SDE: throw new Exception("CreateWorkspace方法被设计为不支持SDE方式创建"); case enumWorkspaceType.FileGDB: wsf = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactoryClass(); break; case enumWorkspaceType.PGDB: wsf = new AccessWorkspaceFactoryClass(); break; case enumWorkspaceType.File: wsf = new ShapefileWorkspaceFactoryClass(); break; } IWorkspaceName wsName = wsf.Create(strPath, strName, null, 0); return((wsName as IName).Open() as IWorkspace); } catch { return(null); } }
public IFeatureClass CreateShapefile( string sParentDirectory, string sWorkspaceName, string sFileName) { if (System.IO.Directory.Exists(sParentDirectory + sWorkspaceName)) { System.IO.Directory.Delete(sParentDirectory + sWorkspaceName, true); } IWorkspaceFactory pWf = new ShapefileWorkspaceFactoryClass(); IWorkspaceName pWn = pWf.Create(sParentDirectory, sWorkspaceName, null, 0); ESRI.ArcGIS.esriSystem.IName name = pWn as ESRI.ArcGIS.esriSystem.IName; IWorkspace pW = (IWorkspace)name.Open(); IFeatureWorkspace pFw = pW as IFeatureWorkspace; IFields pFs = new FieldsClass(); IFieldsEdit pFse = pFs as IFieldsEdit; IFieldEdit pFe = new FieldClass(); pFe.Name_2 = "OID"; pFe.AliasName_2 = "序号"; pFe.Type_2 = esriFieldType.esriFieldTypeOID; pFse.AddField((IField)pFe);//IFieldEdit.AddField是AO隐藏属性 pFe = new FieldClass(); pFe.Name_2 = "Name"; pFe.AliasName_2 = "名称"; pFe.Type_2 = esriFieldType.esriFieldTypeString; pFse.AddField((IField)pFe); IGeometryDefEdit pGde = new GeometryDefClass(); ISpatialReference pSr = m_map.SpatialReference; pGde.SpatialReference_2 = pSr; pGde.GeometryType_2 = esriGeometryType.esriGeometryPoint; pFe = new FieldClass(); string sShapeFieldName = "Shape"; pFe.Name_2 = sShapeFieldName; pFe.AliasName_2 = "形状"; pFe.Type_2 = esriFieldType.esriFieldTypeGeometry; pFe.GeometryDef_2 = pGde; pFse.AddField((IField)pFe); IFeatureClass pFc = pFw.CreateFeatureClass(sFileName, pFs, null, null, esriFeatureType.esriFTSimple, "Shape", ""); if (pFc == null) { return(null); } return(pFc); }
public IFeatureClass CreateShapefile(String sParentDirectory, String sWorkspaceName, String sFileName) { IWorkspaceFactory workspaceFactory; IWorkspace workspace; IFeatureWorkspace featureWorkspace; IFeatureClass featureClass; if (System.IO.Directory.Exists(sParentDirectory + sWorkspaceName)) { workspaceFactory = new ShapefileWorkspaceFactoryClass(); workspace = workspaceFactory.OpenFromFile(sParentDirectory + sWorkspaceName, 0); featureWorkspace = workspace as IFeatureWorkspace; featureClass = featureWorkspace.OpenFeatureClass(sFileName); } else { workspaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create(sParentDirectory, sWorkspaceName, null, 0); ESRI.ArcGIS.esriSystem.IName name = workspaceName as ESRI.ArcGIS.esriSystem.IName; workspace = (IWorkspace)name.Open(); featureWorkspace = workspace as IFeatureWorkspace; IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = fields as IFieldsEdit; IFieldEdit fieldEdit = new FieldClass(); fieldEdit.Name_2 = "OID"; fieldEdit.AliasName_2 = "序号"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; fieldsEdit.AddField((IField)fieldEdit); fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Name"; fieldEdit.AliasName_2 = "名称"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldsEdit.AddField((IField)fieldEdit); IGeometryDefEdit geoDefEdit = new GeometryDefClass(); ISpatialReference spatialReference = m_map.SpatialReference; geoDefEdit.SpatialReference_2 = spatialReference; geoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Shape"; fieldEdit.AliasName_2 = "形状"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; fieldEdit.GeometryDef_2 = geoDefEdit; fieldsEdit.AddField((IField)fieldEdit); featureClass = featureWorkspace.CreateFeatureClass(sFileName, fields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); if (featureClass == null) { return(null); } } return(featureClass); }
/// <summary> /// 作用:创建要素类 /// 作者:汪建龙 /// 编写时间:2016年12月27日10:50:16 /// </summary> /// <param name="saveFilePath"></param> /// <param name="spatialReference"></param> /// <param name="esriGeometryType"></param> /// <returns></returns> public static IFeatureClass CreateFeatrueClass(string saveFilePath, ISpatialReference spatialReference, esriGeometryType esriGeometryType) { IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass(); var directory = System.IO.Path.GetDirectoryName(saveFilePath); var parent = System.IO.Path.GetDirectoryName(directory); var fileName = System.IO.Path.GetFileName(directory); IWorkspaceName workspaceName = workspaceFactory.Create(parent, fileName, null, 0); IName name = workspaceName as IName; IWorkspace workspace = name.Open() as IWorkspace; IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace; IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = fields as IFieldsEdit; IFieldEdit fieldEdit = new FieldClass(); fieldEdit.Name_2 = "OID"; fieldEdit.AliasName_2 = "序号"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; fieldsEdit.AddField(fieldEdit as IField); fieldEdit = new FieldClass(); fieldEdit.Name_2 = "TCMC"; fieldEdit.AliasName_2 = "图层名称"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldsEdit.AddField(fieldEdit as IField); IGeometryDefEdit geometryDefEdit = new GeometryDefClass(); geometryDefEdit.SpatialReference_2 = spatialReference; geometryDefEdit.GeometryType_2 = esriGeometryType; fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Shape"; fieldEdit.AliasName_2 = "形状"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; fieldEdit.GeometryDef_2 = geometryDefEdit; fieldsEdit.AddField(fieldEdit as IField); return(featureWorkspace.CreateFeatureClass(System.IO.Path.GetFileNameWithoutExtension(saveFilePath), fields, null, null, esriFeatureType.esriFTSimple, "shape", "")); }
/// <summary> /// 要素类转Shape /// </summary> /// <param name="apFeatureClass"></param> private void ExportFeatureClassToConTempShp(IFeatureClass apFeatureClass) { if (Directory.Exists(System.Windows.Forms.Application.StartupPath + "\\Convert\\DWGConvert\\ConTempShp")) { Directory.Delete(System.Windows.Forms.Application.StartupPath + "\\Convert\\DWGConvert\\ConTempShp", true); } //设置导出要素类的参数 IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass(); IDataset pOutDataset = (IDataset)apFeatureClass; pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName; //创建一个输出shp文件的工作空间 IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspaceName pInWorkspaceName = new WorkspaceNameClass(); pInWorkspaceName = pShpWorkspaceFactory.Create(System.Windows.Forms.Application.StartupPath + "\\Convert\\DWGConvert", "ConTempShp", null, 0); //创建一个要素集合 IFeatureDatasetName pInFeatureDatasetName = null; //创建一个要素类 IFeatureClassName pInFeatureClassName = new FeatureClassNameClass(); IDatasetName pInDatasetClassName; pInDatasetClassName = (IDatasetName)pInFeatureClassName; pInDatasetClassName.Name = "ConTempShp";//作为输出参数 pInDatasetClassName.WorkspaceName = pInWorkspaceName; //通过FIELDCHECKER检查字段的合法性,为输出SHP获得字段集合 long iCounter; IFields pOutFields, pInFields; IFieldChecker pFieldChecker; IField pGeoField; IEnumFieldError pEnumFieldError = null; pInFields = apFeatureClass.Fields; pFieldChecker = new FieldChecker(); pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields); //通过循环查找几何字段 pGeoField = null; for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++) { if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry) { pGeoField = pOutFields.get_Field((int)iCounter); break; } } //得到几何字段的几何定义 IGeometryDef pOutGeometryDef; IGeometryDefEdit pOutGeometryDefEdit; pOutGeometryDef = pGeoField.GeometryDef; //设置几何字段的空间参考和网格 pOutGeometryDefEdit = (IGeometryDefEdit)pOutGeometryDef; pOutGeometryDefEdit.GridCount_2 = 1; pOutGeometryDefEdit.set_GridSize(0, 1500000); //try //{ IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass(); pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0); // MessageBox.Show("导出成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); //} //catch (Exception ex) //{ //} /*****20130227杨漾(去掉trycatch,上层trycatch(MainForm)处理)*****/ }
public IWorkspace CreateWorkspaceGdbFile(string path, string name, string type) { try { IWorkspace wp; IWorkspaceName wpn; IName nwp; IWorkspaceFactory2 wspf; Directory.CreateDirectory(path); path = path + "\\"; switch (type) { case "mdb": wspf = new AccessWorkspaceFactoryClass(); name = name + ".mdb"; break; case "shp": wspf = new ShapefileWorkspaceFactoryClass(); break; default: wspf = new ShapefileWorkspaceFactoryClass(); break; } wpn = wspf.Create(path, name, null, 0); nwp = (IName)wpn; wp = (IWorkspace)nwp.Open(); return wp; } catch { return null; } }
public IFeatureClass CreateShapefile(string ParentDirectory, string sWorkspaceName, string FileName, string geotype) { if (System.IO.Directory.Exists(ParentDirectory + sWorkspaceName)) { System.IO.Directory.Delete(ParentDirectory + sWorkspaceName, true); } //创建针对shape文件的工作空间工场对象 //通过参数创建相关工作空间(文件夹),用于包含shape文件 IWorkspaceFactory WorkspaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspaceName WorkspaceName = WorkspaceFactory.Create(ParentDirectory, sWorkspaceName, null, 0); ESRI.ArcGIS.esriSystem.IName name = WorkspaceName as ESRI.ArcGIS.esriSystem.IName; //打开新建的工作空间,并通过IFeatureWorkspace接口访问它 IWorkspace workspace = (IWorkspace)name.Open(); IFeatureWorkspace featureworkspace = workspace as IFeatureWorkspace; //创建、编辑shape文件的字段 IFields fields = new FieldsClass(); IFieldsEdit fieldsedit = fields as IFieldsEdit; //创建并编辑“序号”字段 //“序号”字段是要素类必备字段 IFieldEdit fieldedit = new FieldClass(); fieldedit.Name_2 = "OID"; fieldedit.AliasName_2 = "序号"; fieldedit.Type_2 = esriFieldType.esriFieldTypeOID; fieldsedit.AddField((IField)fieldedit); //创建并编辑“名称”字段 fieldedit = new FieldClass(); fieldedit.Name_2 = "Name"; fieldedit.AliasName_2 = "名称"; fieldedit.Type_2 = esriFieldType.esriFieldTypeString; fieldsedit.AddField((IField)fieldedit); IGeometryDefEdit geodefedit = new GeometryDefClass(); ISpatialReference spatialreference = m_map.SpatialReference; geodefedit.SpatialReference_2 = spatialreference; //geotype这个字符串变量代表了要素类的几何类型 switch (geotype) { case "Point": geodefedit.GeometryType_2 = esriGeometryType.esriGeometryPoint; break; case "Line": geodefedit.GeometryType_2 = esriGeometryType.esriGeometryPolyline; break; case "Polygon": geodefedit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; break; default: geodefedit.GeometryType_2 = esriGeometryType.esriGeometryPoint; break; } fieldedit = new FieldClass(); //创建“形状”字段来表示要素类的集合类型 string shapename = "Shape"; fieldedit.Name_2 = shapename; fieldedit.AliasName_2 = "形状"; fieldedit.Type_2 = esriFieldType.esriFieldTypeGeometry; fieldedit.GeometryDef_2 = geodefedit; fieldsedit.AddField((IField)fieldedit); IFeatureClass featureclass = featureworkspace.CreateFeatureClass(FileName, fields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); if (featureclass == null) { return(null); } return(featureclass); }
/// <summary> /// 保存输出JointCount>1的点位图层 /// </summary> /// <param name="apFeatureClass"></param> public void ExportFeatureClassToShp(IFeatureClass apFeatureClass) { if (apFeatureClass == null) { MessageBox.Show("请选择", "系统提示"); return; } //调用保存文件函数 SaveFileDialog sa = new SaveFileDialog(); sa.Filter = "SHP文件(.shp)|*.shp"; sa.ShowDialog(); sa.CreatePrompt = true; string ExportShapeFileName = sa.FileName; // string StrFilter = "SHP文件(.shp)|*.shp"; // string ExportShapeFileName = SaveFileDialog(StrFilter); if (ExportShapeFileName == "") { return; } string ExportFileShortName = System.IO.Path.GetFileNameWithoutExtension(ExportShapeFileName); string ExportFilePath = System.IO.Path.GetDirectoryName(ExportShapeFileName); shpPath = ExportFilePath + "\\" + ExportFileShortName + "\\" + ExportFileShortName + ".shp"; //设置导出要素类的参数 IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass(); IDataset pOutDataset = (IDataset)apFeatureClass; pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName; //创建一个输出shp文件的工作空间 IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspaceName pInWorkspaceName = new WorkspaceNameClass(); pInWorkspaceName = pShpWorkspaceFactory.Create(ExportFilePath, ExportFileShortName, null, 0); //创建一个要素集合 IFeatureDatasetName pInFeatureDatasetName = null; //创建一个要素类 IFeatureClassName pInFeatureClassName = new FeatureClassNameClass(); IDatasetName pInDatasetClassName; pInDatasetClassName = (IDatasetName)pInFeatureClassName; pInDatasetClassName.Name = ExportFileShortName;//作为输出参数 pInDatasetClassName.WorkspaceName = pInWorkspaceName; //通过FIELDCHECKER检查字段的合法性,为输出SHP获得字段集合 long iCounter; IFields pOutFields, pInFields; IFieldChecker pFieldChecker; IField pGeoField; IEnumFieldError pEnumFieldError = null; pInFields = apFeatureClass.Fields; pFieldChecker = new FieldChecker(); pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields); //通过循环查找几何字段 pGeoField = null; for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++) { if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry) { pGeoField = pOutFields.get_Field((int)iCounter); break; } } //得到几何字段的几何定义 IGeometryDef pOutGeometryDef; IGeometryDefEdit pOutGeometryDefEdit; pOutGeometryDef = pGeoField.GeometryDef; //设置几何字段的空间参考和网格 pOutGeometryDefEdit = (IGeometryDefEdit)pOutGeometryDef; pOutGeometryDefEdit.GridCount_2 = 1; pOutGeometryDefEdit.set_GridSize(0, 1500000); try { //开始导入 IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass(); pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0); MessageBox.Show("导出成功", "系统提示"); } catch (Exception ex) { MessageBox.Show("the following exception occurred:" + ex.ToString()); } }
//internal void ExportFeatureClassToShp(ESRI.ArcGIS.Geodatabase.IFeatureClass pFeatureClass, string strFullPath, string filePath) //{ // //throw new NotImplementedException(); public void ExportFeatureClassToShp(IFeatureClass apFeatureClass, string ExportShapeFileName, string ExportFilePath) { if (apFeatureClass == null) { MessageBox.Show("请选择", "系统提示"); return; } if (ExportShapeFileName == "") return; string ExportFileShortName = System.IO.Path.GetFileNameWithoutExtension(ExportShapeFileName); ExportFilePath = System.IO.Path.GetDirectoryName(ExportShapeFileName); shpPath = ExportFilePath + "\\" + ExportFileShortName + "\\" + ExportFileShortName + ".shp"; //设置导出要素类的参数 IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass(); IDataset pOutDataset = (IDataset)apFeatureClass; pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName; //创建一个输出shp文件的工作空间 IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspaceName pInWorkspaceName = new WorkspaceNameClass(); pInWorkspaceName = pShpWorkspaceFactory.Create(ExportFilePath, ExportFileShortName, null, 0); //创建一个要素集合 IFeatureDatasetName pInFeatureDatasetName = null; //创建一个要素类 IFeatureClassName pInFeatureClassName = new FeatureClassNameClass(); IDatasetName pInDatasetClassName; pInDatasetClassName = (IDatasetName)pInFeatureClassName; pInDatasetClassName.Name = ExportFileShortName;//作为输出参数 pInDatasetClassName.WorkspaceName = pInWorkspaceName; //通过FIELDCHECKER检查字段的合法性,为输出SHP获得字段集合 long iCounter; IFields pOutFields, pInFields; IFieldChecker pFieldChecker; IField pGeoField; IEnumFieldError pEnumFieldError = null; pInFields = apFeatureClass.Fields; pFieldChecker = new FieldChecker(); pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields); //通过循环查找几何字段 pGeoField = null; for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++) { if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry) { pGeoField = pOutFields.get_Field((int)iCounter); break; } } //得到几何字段的几何定义 IGeometryDef pOutGeometryDef; IGeometryDefEdit pOutGeometryDefEdit; pOutGeometryDef = pGeoField.GeometryDef; //设置几何字段的空间参考和网格 pOutGeometryDefEdit = (IGeometryDefEdit)pOutGeometryDef; pOutGeometryDefEdit.GridCount_2 = 1; pOutGeometryDefEdit.set_GridSize(0, 1500000); try { //开始导入 IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass(); //pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0); pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0); MessageBox.Show("导出成功", "系统提示"); } catch (Exception ex) { MessageBox.Show("the following exception occurred:" + ex.ToString()); } }
/// <summary> /// 导出FeatureClass到Shapefile文件 /// </summary> /// <param name="sPath"></param> /// <param name="apFeatureClass"></param> public static bool ExportFeatureClassToShp(string sPath, IFeatureClass apFeatureClass) { try { DateTime dt = DateTime.Now; string strTime = string.Format("{0:yyyyMMddHHmmss}", dt); string fileName = System.IO.Path.GetFileNameWithoutExtension(sPath); //文件名称 string ExportFilePath = System.IO.Path.GetDirectoryName(sPath); //文件夹路径 string ExportFileName = ExportFilePath.Substring(ExportFilePath.LastIndexOf("\\") + 1); //最后选择的文件夹 //如果是c:\\这样的根目录,那么根日期建立一个导出文件夹的名称 if (string.IsNullOrEmpty(ExportFileName)) { ExportFileName = "导出Shape文件"; } else { ExportFilePath = ExportFilePath.Substring(0, ExportFilePath.LastIndexOf("\\"));//除了导出文件夹的路径 } //设置导出要素类的参数 IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass(); IDataset pOutDataset = (IDataset)apFeatureClass; pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName; //创建一个输出shp文件的工作空间 IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspaceName pInWorkspaceName = new WorkspaceNameClass(); pInWorkspaceName = pShpWorkspaceFactory.Create(ExportFilePath, ExportFileName, null, 0); //创建一个要素集合 IFeatureDatasetName pInFeatureDatasetName = null; //创建一个要素类 IFeatureClassName pInFeatureClassName = new FeatureClassNameClass(); IDatasetName pInDatasetClassName; pInDatasetClassName = (IDatasetName)pInFeatureClassName; pInDatasetClassName.Name = fileName + strTime;//作为输出参数 pInDatasetClassName.WorkspaceName = pInWorkspaceName; //通过FIELDCHECKER检查字段的合法性,为输出SHP获得字段集合 long iCounter; IFields pOutFields, pInFields; IField pGeoField; IEnumFieldError pEnumFieldError = null; IQueryFilter filter = null; string subset = ""; pInFields = apFeatureClass.Fields; IFieldChecker pFieldChecker = new FieldChecker(); pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields); //通过循环查找几何字段 pGeoField = null; for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++) { IField field = pOutFields.get_Field((int)iCounter); if (field.Type == esriFieldType.esriFieldTypeGeometry) { pGeoField = field; } if (!IsBlob(field)) { string fieldName = field.Name; if (fieldName.Contains("_")) { fieldName = fieldName.Replace("_", "."); } subset += (fieldName + ","); } } subset = subset.Substring(0, subset.LastIndexOf(",")); filter = new QueryFilterClass(); filter.SubFields = subset; //得到几何字段的几何定义 IGeometryDef pOutGeometryDef; IGeometryDefEdit pOutGeometryDefEdit; pOutGeometryDef = pGeoField.GeometryDef; //设置几何字段的空间参考和网格 pOutGeometryDefEdit = (IGeometryDefEdit)pOutGeometryDef; pOutGeometryDefEdit.GridCount_2 = 1; pOutGeometryDefEdit.set_GridSize(0, 1500000); //开始导入 IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass(); pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, filter, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0); return(true); } catch { return(false); } }
/// <summary> /// method: CreateShp /// </summary> /// <param name="sParendirectory"></param> /// <param name="sWorkSpaceName"></param> /// <param name="sFileName"></param> /// <param name="fType"></param> /// <param name="pSpatialRef"></param> /// <returns></returns> public static IFeatureClass CreateShp(string sParendirectory, string sWorkSpaceName, string sFileName, string fType, ISpatialReference pSpatialRef) { if (System.IO.Directory.Exists(sParendirectory + sWorkSpaceName)) { System.IO.Directory.Delete(sParendirectory + sWorkSpaceName, true); } //open() a Workspace through name IWorkspaceFactory workSpaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspaceName workSpaceName = workSpaceFactory.Create(sParendirectory, sWorkSpaceName, null, 0); IName pName = workSpaceName as IName; IWorkspace _WorkSpace = (IWorkspace)pName.Open(); //IFeatureWorkspace IFeatureWorkspace featureWorkSpace = _WorkSpace as IFeatureWorkspace; //prepare necessary fields: //fields IFields pFields = new FieldsClass(); IFieldsEdit fieldsEdit = pFields as IFieldsEdit; //NECESSARY field1: OID IFieldEdit fieldEdit = new FieldClass(); //field_name fieldEdit.Name_2 = "OID"; fieldEdit.AliasName_2 = "序号"; //field_type fieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; //IFieldsEdit.AddField fieldsEdit.AddField((IField)fieldEdit); //NECESSARY field2: geometry fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Shape"; fieldEdit.AliasName_2 = "形状"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; //IFieldsEdit.GeometryDef IGeometryDefEdit geometryDefEdit = new GeometryDefClass(); switch (fType) { case "Point": geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; break; case "Polyline": geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline; break; case "Polygon": geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; break; } //SpatialReference if (pSpatialRef != null) { geometryDefEdit.SpatialReference_2 = pSpatialRef; } fieldEdit.GeometryDef_2 = geometryDefEdit; fieldsEdit.AddField((IField)fieldEdit); //Another attribute field: Name fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Name"; fieldEdit.AliasName_2 = "名称"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldsEdit.AddField((IField)fieldEdit); //CORE Method //IFeatureWorkspace.CreateFeatureClass(...) to release the Shp IFeatureClass featureClass = featureWorkSpace.CreateFeatureClass(sFileName, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); if (featureClass == null) { MessageBox.Show("创建失败"); return(null); } else { MessageBox.Show("创建成功"); return(featureClass); } }
/// <summary> /// 导出Shp文件 /// </summary> /// <param name="path"></param> /// <param name="apFeatureClass"></param> /// <returns></returns> public static bool ExportFeatureClassToShp(string path, IFeatureClass apFeatureClass) { try { string exportFileShortName = System.IO.Path.GetFileNameWithoutExtension(path); if (exportFileShortName == "") { exportFileShortName = (apFeatureClass as IDataset).Name; if (exportFileShortName.LastIndexOf('.') >= 0) { exportFileShortName = exportFileShortName.Substring(exportFileShortName.LastIndexOf('.') + 1); } } string exportFilePath = System.IO.Path.GetDirectoryName(path); if (exportFilePath == null) { exportFilePath = path; } //设置导出要素类的参数 IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass(); IDataset pOutDataset = (IDataset)apFeatureClass; pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName; //创建一个输出shp文件的工作空间 IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspaceName pInWorkspaceName = new WorkspaceNameClass(); pInWorkspaceName = pShpWorkspaceFactory.Create(exportFilePath, exportFileShortName, null, 0); //创建一个要素集合 IFeatureDatasetName pInFeatureDatasetName = null; //创建一个要素类 IFeatureClassName pInFeatureClassName = new FeatureClassNameClass(); IDatasetName pInDatasetClassName; pInDatasetClassName = (IDatasetName)pInFeatureClassName; pInDatasetClassName.Name = exportFileShortName; pInDatasetClassName.WorkspaceName = pInWorkspaceName; //通过FIELDCHECKER检查字段的合法性,为输出SHP获得字段集合 IFields pInFields = apFeatureClass.Fields; pInFields = apFeatureClass.Fields; IFieldChecker pFieldChecker = new FieldChecker(); pFieldChecker.Validate(pInFields, out IEnumFieldError pEnumFieldError, out IFields pOutFields); //通过循环查找几何字段 IField pGeoField = null; for (long iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++) { if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry) { pGeoField = pOutFields.get_Field((int)iCounter); break; } } //得到几何字段的几何定义 IGeometryDef pOutGeometryDef; IGeometryDefEdit pOutGeometryDefEdit; pOutGeometryDef = pGeoField.GeometryDef; //设置几何字段的空间参考和网格 pOutGeometryDefEdit = (IGeometryDefEdit)pOutGeometryDef; pOutGeometryDefEdit.GridCount_2 = 1; pOutGeometryDefEdit.set_GridSize(0, 1500000); //开始导入 IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass(); pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0); return(true); } catch (Exception ex) { return(false); } }
//Function: Create Shapefile //Date: 2019/4/12 public IFeatureClass CreateShapefile(IFields fields, string sParentDirectory, string sWorkspaceName, //the name of the folder that contains the shapefile string sFilename, string sGeometryType) //the geometry type of shapefile { IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create(sParentDirectory, sWorkspaceName, null, 0); ESRI.ArcGIS.esriSystem.IName name = workspaceName as ESRI.ArcGIS.esriSystem.IName; IWorkspace workspace = (IWorkspace)name.Open(); IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace; IFieldsEdit fieldsEdit = fields as IFieldsEdit; IFieldEdit fieldEdit = new FieldClass(); fieldEdit.Name_2 = "OID"; fieldEdit.AliasName_2 = "序号"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; fieldsEdit.AddField((IField)fieldEdit); fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Name"; fieldEdit.AliasName_2 = "名称"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldsEdit.AddField((IField)fieldEdit); //Change shape type IGeometryDefEdit geoDefEdit = new GeometryDefClass(); ISpatialReference spatialReference = m_map.SpatialReference; geoDefEdit.SpatialReference_2 = spatialReference; if (sGeometryType == "Point") { geoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; } else if (sGeometryType == "Line") { geoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryLine; } else if (sGeometryType == "Polygon") { geoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; } //change shapetype fieldEdit = new FieldClass(); string sShapeFieldName = "Shape"; fieldEdit.Name_2 = sShapeFieldName; fieldEdit.AliasName_2 = "形状"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; fieldEdit.GeometryDef_2 = geoDefEdit; fieldsEdit.AddField((IField)fieldEdit); //create feature class IFeatureClass featureClass = featureWorkspace.CreateFeatureClass(sFilename, fields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); if (featureClass == null) { return(null); } return(featureClass); }