public IWorkspace CreateOrOpenLoaclGeoDataBase(string path, EnumLoaclGeoDatabaseType type) { try { IWorkspaceFactory pTargetWsf = null; switch (type) { case EnumLoaclGeoDatabaseType.MDB: pTargetWsf = new AccessWorkspaceFactory(); break; case EnumLoaclGeoDatabaseType.GDB: pTargetWsf = new FileGDBWorkspaceFactory(); break; case EnumLoaclGeoDatabaseType.SHP: pTargetWsf = new ShapefileWorkspaceFactory(); break; } if (System.IO.Directory.Exists(path) || System.IO.File.Exists(path)) { if (pTargetWsf.IsWorkspace(path)) { return(pTargetWsf.OpenFromFile(path, 0)); } else { ErrorMessage = "当前工作空间已经损坏"; return(null); } } else { string sPath = System.IO.Path.GetDirectoryName(path); string sName = System.IO.Path.GetFileNameWithoutExtension(path); IWorkspaceName pWorkspaceName = pTargetWsf.Create(sPath, sName, null, 0); return(pTargetWsf.Open(pWorkspaceName.ConnectionProperties, 0)); } } catch (Exception ex) { ErrorMessage = ex.Message; return(null); } }
/// <summary> /// 将layer中的要素选择集selection转换为shape文件 /// </summary> /// <param name="strParaFullPath">除文件名之外的全路径</param> /// <param name="strTemLyrName">文件命名,用来创建shape工作空间,可以设置为空字符串</param> /// <param name="iParaFeaLayer">当前操作的要素层</param> public static bool ExportSelection2Shp(string strParaFullPath, string strTemLyrName, IFeatureLayer iParaFeaLayer) { //string ExportFilePath = System.IO.Path.GetDirectoryName(strParaFullPath); IFeatureClass m_InFeaClass; IDataset m_InDataSet; IDatasetName m_InDataSetName; IFeatureSelection m_FeaSelection; IWorkspaceFactory m_WorkspaceFac; IWorkspaceName m_OutWorkspaceName; IFeatureClassName m_FeaClassName; IDatasetName m_OutDataSetName; DateTime dt = DateTime.Now; string strTime = string.Format("{0:yyyyMMddHHmmss}", dt); string ExportFileName = strParaFullPath.Substring(strParaFullPath.LastIndexOf("\\") + 1);//当前文件夹名称 string ExportFilePath = strParaFullPath; if (string.IsNullOrEmpty(ExportFileName)) { ExportFileName = "导出Shape文件"; } else { ExportFilePath = ExportFilePath.Substring(0, ExportFilePath.LastIndexOf("\\"));//除了导出文件夹的路径 } m_InFeaClass = iParaFeaLayer.FeatureClass; m_InDataSet = m_InFeaClass as IDataset; m_InDataSetName = m_InDataSet.FullName as IDatasetName; m_FeaSelection = iParaFeaLayer as IFeatureSelection; ISelectionSet iSelecttionSet = m_FeaSelection.SelectionSet; if (strTemLyrName.Trim() == "") { strTemLyrName = iParaFeaLayer.Name; } m_WorkspaceFac = new ShapefileWorkspaceFactory(); //判断指定的文件夹是否已经存在,如果存在则删除之前的文件夹 //string path=string.Format(strParaFullPath+"\\"+strTemLyrName); //if(Directory.Exists(path)) // Directory.Delete(path,true); m_OutWorkspaceName = m_WorkspaceFac.Create(ExportFilePath, ExportFileName, null, 0); m_FeaClassName = new FeatureClassNameClass(); m_OutDataSetName = m_FeaClassName as IDatasetName; m_OutDataSetName.WorkspaceName = m_OutWorkspaceName; m_OutDataSetName.Name = strTemLyrName + strTime; IQueryFilter filter = null; string subset = ""; IFieldChecker fieldChecker = new FieldCheckerClass(); IFields shapefileFields = null; IEnumFieldError enumFieldError = null; fieldChecker.InputWorkspace = m_InDataSet.Workspace; fieldChecker.ValidateWorkspace = m_InDataSet.Workspace; fieldChecker.Validate(m_InFeaClass.Fields, out enumFieldError, out shapefileFields); int iCounter; IField pGeoField = null; for (iCounter = 0; iCounter < shapefileFields.FieldCount; iCounter++) { IField field = shapefileFields.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; //得到几何字段的几何定义 --- shape文件没有grid //IGeometryDef pOutGeometryDef; //IGeometryDefEdit pOutGeometryDefEdit; //pOutGeometryDef = pGeoField.GeometryDef; ////设置几何字段的空间参考和网格 //pOutGeometryDefEdit = (IGeometryDefEdit)pOutGeometryDef; //pOutGeometryDefEdit.GridCount_2 = 1; //pOutGeometryDefEdit.set_GridSize(0, 1500000); //pOutGeometryDefEdit.SpatialReference_2 = iParaFeaLayer.SpatialReference;iParaFeaLayer的空间参考只读 IFeatureDataConverter2 pConvetLayer2Shape = new FeatureDataConverterClass(); pConvetLayer2Shape.ConvertFeatureClass(m_InDataSetName, filter, iSelecttionSet, null, m_FeaClassName, null, shapefileFields, "", 1000, 0); return(true); }
/* function CreateShapefile * Usage: create a new shapefile and return its featureclass * Author: JL Ding * Time: 2019/04/10 */ public IFeatureClass CreateShapefile(String sParentDirectory, String sWorkspaceName, String sType) { //如果指定的路径下已有此文件夹,则删除 if (System.IO.Directory.Exists(sParentDirectory + sWorkspaceName)) { System.IO.Directory.Delete(sParentDirectory + sWorkspaceName, true); } //通过工作空间工厂创建新的工作空间文件夹 IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactory(); //【为什么是这个类用来new? IWorkspaceName workspaceName = workspaceFactory.Create(sParentDirectory, sWorkspaceName, null, 0); ESRI.ArcGIS.esriSystem.IName name = workspaceName as ESRI.ArcGIS.esriSystem.IName; IWorkspace workspace = (IWorkspace)name.Open(); //为什么IName还能转化成IWorkspace?虽然帮助文档说了Use IName to open a workspace object,但是还是不知道会是这么用啊? IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace; //IFeatureWorkspace和IWorkspace都属于workspace类,可以直接相互转换 IFields fields = new Fields(); //书上用的FieldsClass(),不知道两者有什么区别 IFieldsEdit fieldsEdit = fields as IFieldsEdit; IFieldEdit fieldEdit = new FieldClass(); //这边就只能用FieldClass()而不能用Fields() fieldEdit.Name_2 = "OID"; fieldEdit.AliasName_2 = "序号"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; fieldsEdit.AddField((IField)fieldEdit); //使用IFieldsEdit接口将新建的字段加入字段集 fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Name"; fieldEdit.AliasName_2 = "名称"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldsEdit.AddField((IField)fieldEdit); //IGeometryDef geometryDef = new GeometryDefClass(); IGeometryDefEdit geometryDefEdit = new GeometryDefClass();//geometryDef as IGeometryDefEdit; ////这边就只能用GeometryDefClass()而不能用GeometryDef() if (sType == "Point") { geometryDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint; } else if (sType == "Line") { geometryDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline; } //geometryDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryLine; else if (sType == "Polygon") { geometryDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon; } else { geometryDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryAny; } ISpatialReference spatialReference = m_map.SpatialReference; geometryDefEdit.SpatialReference_2 = spatialReference; fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Shape"; fieldEdit.AliasName_2 = "形状"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef geometryDef = geometryDefEdit; fieldEdit.GeometryDef_2 = geometryDef; fieldsEdit.AddField((IField)fieldEdit); //CreateFeatureClass创建要素 IFeatureClass featureClass = featureWorkspace.CreateFeatureClass(sWorkspaceName, fields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); if (featureWorkspace == null) { return(null); } else { return(featureClass); } }
//创建要素 public void ExportFeature(IFeatureClass apFeatureClass, string ExportFilePath, string ExportFileShortName) { if (apFeatureClass == null) { MessageBox.Show("分析出错,请检查路径分析结果", "系统提示"); return; } //设置导出要素类的参数 IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass(); IDataset pOutDataset = (IDataset)apFeatureClass; pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName; //创建一个输出shp文件的工作空间 IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactory(); 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; //自定义字段 AddField(apFeatureClass, "Elevation", "", esriFieldType.esriFieldTypeInteger); //通过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 { } }