/// <summary> /// 导出要素类到目标工作空间 /// </summary> /// <param name="featureClass"></param> /// <param name="workspace"></param> /// <param name="queryGeometry"></param> /// <param name="spatialRelEnum"></param> /// <param name="whereClause"></param> /// <returns></returns> public static Boolean ExportToWorkspace(IFeatureClass featureClass, IWorkspace workspace, IGeometry queryGeometry = null, esriSpatialRelEnum spatialRelEnum = esriSpatialRelEnum.esriSpatialRelIntersects, string whereClause = "") { IDataset inDataSet = featureClass as IDataset; IFeatureClassName inFCName = inDataSet.FullName as IFeatureClassName; IWorkspace inWorkspace = inDataSet.Workspace; IDataset outDataSet = workspace as IDataset; IWorkspaceName outWorkspaceName = outDataSet.FullName as IWorkspaceName; IFeatureClassName outFCName = new FeatureClassNameClass(); IDatasetName dataSetName = outFCName as IDatasetName; dataSetName.WorkspaceName = outWorkspaceName; dataSetName.Name = inDataSet.Name; IFieldChecker fieldChecker = new FieldCheckerClass { InputWorkspace = inWorkspace, ValidateWorkspace = workspace }; IFields fields = featureClass.Fields; fieldChecker.Validate(fields, out IEnumFieldError enumFieldError, out IFields outFields); IFeatureDataConverter featureDataConverter = null; IField geometryField; try { IGeometryDef geometryDef = null; ISpatialFilter pSF = new SpatialFilterClass(); for (int i = 0; i < outFields.FieldCount; i++) { if (outFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry) { geometryField = outFields.get_Field(i); geometryDef = geometryField.GeometryDef; IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef; targetFCGeoDefEdit.GridCount_2 = 1; targetFCGeoDefEdit.set_GridSize(0, 0); targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference; pSF.Geometry = queryGeometry; pSF.GeometryField = featureClass.ShapeFieldName; pSF.SpatialRel = spatialRelEnum; pSF.WhereClause = whereClause; break; } } featureDataConverter = new FeatureDataConverterClass(); featureDataConverter.ConvertFeatureClass(inFCName, pSF, null, outFCName, geometryDef, outFields, " ", 1000, 0); return(true); } catch (Exception ex) { LOG.Error("图层数据导出出错!" + ex.Message); throw ex; } }
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> /// Converts the event route source to a stand-alone feature class. /// </summary> /// <param name="outputTableName">The name of the output feature class.</param> /// <param name="filter">The filter used to limit the number of features converted.</param> /// <param name="workspace">The workspace that will contain the feature class.</param> /// <returns> /// Returns a <see cref="IFeatureClass" /> representing the results of the conversion. /// </returns> public IFeatureClass Convert(string outputTableName, IQueryFilter filter, IWorkspace workspace) { var source = (IFeatureClass)this.FeatureClass; var geometryDef = ((IClone)source.GetGeometryDef()) as IGeometryDef; IFieldChecker checker = new FieldCheckerClass(); IEnumFieldError errors; IFields fields; checker.InputWorkspace = workspace; checker.ValidateWorkspace = workspace; checker.Validate(source.Fields, out errors, out fields); var outputClassName = workspace.Define(outputTableName, new FeatureClassNameClass()); workspace.Delete(outputClassName); var dataConverter = new FeatureDataConverterClass(); var invalidObjects = dataConverter.ConvertFeatureClass((IDatasetName)this.Name, filter, null, null, outputClassName, geometryDef, fields, "", 1000, 0); if (invalidObjects.AsEnumerable().Any()) { return(null); } var route = workspace.GetFeatureClass(outputTableName); return(route); }
/// <summary> /// 复制要素集(ConvertFeatureDataset) /// </summary> /// <param name="sourceWorkspace"></param> /// <param name="targetWorkspace"></param> /// <param name="nameOfSourceFeatureDataset"></param> /// <param name="nameOfTargetFeatureDataset"></param> public void IFeatureDataConverter_ConvertFeatureDataset(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureDataset, string nameOfTargetFeatureDataset) { //create source workspace name IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; //create source dataset name IFeatureDatasetName sourceFeatureDatasetName = new FeatureDatasetNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureDatasetName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = nameOfSourceFeatureDataset; //create target workspace name IDataset targetWorkspaceDataset = (IDataset)targetWorkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; //create target dataset name IFeatureDatasetName targetFeatureDatasetName = new FeatureDatasetNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureDatasetName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = nameOfTargetFeatureDataset; //Convert feature dataset IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass(); featureDataConverter.ConvertFeatureDataset(sourceFeatureDatasetName, targetFeatureDatasetName, null, "", 1000, 0); //Console.WriteLine("Conversion Complete"); }
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 ConvertMDBFeatureDatasetToSDE(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureDataset, string nameOfTargetFeatureDataset) { if (sourceWorkspace == null || targetWorkspace == null) { return; } //创建源工作空间名 IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; //创建源数据集名 IFeatureDatasetName sourceFeatureDatasetName = new FeatureDatasetNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureDatasetName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = nameOfSourceFeatureDataset; //创建目标工作空间名 IDataset targetWorkspaceDataset = (IDataset)targetWorkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; //创建目标数据集名 IFeatureDatasetName targetFeatureDatasetName = new FeatureDatasetNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureDatasetName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = nameOfTargetFeatureDataset; //转换(复制)源数据集到目标数据集 IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass(); featureDataConverter.ConvertFeatureDataset(sourceFeatureDatasetName, targetFeatureDatasetName, null, "", 1000, 0); }
/// <summary> /// Exports the source table using the query filter to the table in the output workspace. /// </summary> /// <param name="source">The source.</param> /// <param name="filter">The filter.</param> /// <param name="tableName">Name of the output table.</param> /// <param name="workspace">The output workspace.</param> /// <param name="requiredFields">The required fields.</param> /// <param name="handle">The handle.</param> /// <param name="surrogate">The surrogate for the event handler.</param> /// <param name="invalid">The errors that occured during the export.</param> /// <param name="errors">The field errors.</param> /// <returns> /// Returns a <see cref="IFeatureClass" /> representing the feature class that was exported. /// </returns> public static ITable Export(this ITable source, IQueryFilter filter, string tableName, IWorkspace workspace, IFields requiredFields, int handle, FeatureProgress surrogate, out IEnumInvalidObject invalid, out IEnumFieldError errors) { var ds = (IDataset)source; var input = ds.Workspace.Define(ds.Name, new TableNameClass()); var output = workspace.Define(tableName, new TableNameClass()); workspace.Delete(output); IFieldChecker fieldChecker = new FieldCheckerClass(); fieldChecker.InputWorkspace = ds.Workspace; fieldChecker.ValidateWorkspace = workspace; IFields targetFields; fieldChecker.Validate(requiredFields, out errors, out targetFields); var featureDataConverter = new FeatureDataConverterClass(); if (surrogate != null) { surrogate.Advise(featureDataConverter); } invalid = featureDataConverter.ConvertTable(input, filter, null, output, targetFields, "", 1000, handle); return(((IName)output).Open() as ITable); }
/// <summary> /// Saves as shpfile. /// </summary> /// <param name="inputFeatureClass">The input feature class.</param> /// <param name="pInDataset">The p in dataset.</param> /// <param name="fileName">Name of the file.</param> /// <param name="pOutWorkspace">The p out workspace.</param> private void SaveAsShpfile(IFeatureClass inputFeatureClass, IDataset pInDataset, string fileName, IWorkspace pOutWorkspace) { IFeatureClassName inputDatasetName = pInDataset.FullName as IFeatureClassName; IWorkspace workspace = pInDataset.Workspace; IDataset dataset = pOutWorkspace as IDataset; IWorkspaceName workspaceName = dataset.FullName as IWorkspaceName; IFeatureClassName featureClassName = new FeatureClassNameClass(); IDatasetName datasetName = featureClassName as IDatasetName; datasetName.WorkspaceName = workspaceName; datasetName.Name = fileName; IFieldChecker fieldChecker = new FieldCheckerClass(); fieldChecker.InputWorkspace = workspace; fieldChecker.ValidateWorkspace = pOutWorkspace; IFields fields = inputFeatureClass.Fields; IEnumFieldError enumFieldError; IFields outputFields; fieldChecker.Validate(fields, out enumFieldError, out outputFields); IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass(); featureDataConverter.ConvertFeatureClass(inputDatasetName, null, null, featureClassName, null, outputFields, "", 100, 0); XtraMessageBox.Show(AppMessage.MSG0029, AppMessage.MSG0000, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Asterisk); }
public static bool ConvertFeatureDataset(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureDataset, string nameOfTargetFeatureDataset) { try { //create source workspace name IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; //create source dataset name IFeatureDatasetName sourceFeatureDatasetName = new FeatureDatasetNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureDatasetName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = nameOfSourceFeatureDataset; //create target workspace name IDataset targetWorkspaceDataset = (IDataset)targetWorkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; //create target dataset name IFeatureDatasetName targetFeatureDatasetName = new FeatureDatasetNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureDatasetName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = nameOfTargetFeatureDataset; //Convert feature dataset IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass(); featureDataConverter.ConvertFeatureDataset(sourceFeatureDatasetName, targetFeatureDatasetName, null, "", 1000, 0); return(true); } catch { return(false); } }
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); }
/// <summary> /// SDE要素类导出mdb(要素集下要素类) /// </summary> /// <param name="IN_SourceWorkspace">源工作空间</param> /// <param name="IN_TargetWorkspace">目标要素数据集</param> /// <param name="IN_SourceFeatureClassName">源要素类名</param> /// <param name="IN_TargetFeatureClassName">目标要素类名</param> private void ConvertFeatureClass(IWorkspace IN_SourceWorkspace, IFeatureDataset IN_TargetWorkspace, string IN_SourceFeatureClassName, string IN_TargetFeatureClassName) { #region 环境配置 //创建源工作空间名 IDataset Temp_SourceWorkspaceDataset = (IDataset)IN_SourceWorkspace; IWorkspaceName Temp_SourceWorkspaceName = (IWorkspaceName)Temp_SourceWorkspaceDataset.FullName; //创建源要素数据集名 IFeatureClassName Temp_SourceFeatureClassName = new FeatureClassNameClass(); IDatasetName Temp_SourceDatasetName = (IDatasetName)Temp_SourceFeatureClassName; Temp_SourceDatasetName.WorkspaceName = Temp_SourceWorkspaceName; Temp_SourceDatasetName.Name = IN_SourceFeatureClassName; //创建目标工作空间名 IDataset Temp_TargetWorkspaceDataset = (IDataset)IN_TargetWorkspace.Workspace; IWorkspaceName Temp_TargetWorkspaceName = (IWorkspaceName)(Temp_TargetWorkspaceDataset.FullName); //创建目标要素类名 IFeatureClassName Temp_TargetFeatureClassName = new FeatureClassNameClass(); IDatasetName Temp_TargetDatasetName = (IDatasetName)Temp_TargetFeatureClassName; Temp_TargetDatasetName.WorkspaceName = Temp_TargetWorkspaceName; Temp_TargetDatasetName.Name = IN_TargetFeatureClassName; //创建目标要素数据集名 IFeatureDatasetName Temp_TargetFeatureDatasetName = new FeatureDatasetNameClass(); IDatasetName Temp_TargetDatasetName2 = (IDatasetName)Temp_TargetFeatureDatasetName; Temp_TargetDatasetName2.WorkspaceName = Temp_TargetWorkspaceName; Temp_TargetDatasetName2.Name = IN_TargetWorkspace.Name; #endregion //打开源要素类获取字段定义 ESRI.ArcGIS.esriSystem.IName Temp_SourceName = (ESRI.ArcGIS.esriSystem.IName)Temp_SourceFeatureClassName; IFeatureClass Temp_SourceFeatureClass = (IFeatureClass)Temp_SourceName.Open(); //验证字段 IFieldChecker Temp_FieldChecker = new FieldCheckerClass(); IFields Temp_TargetFeatureClassFields; IFields Temp_SourceFeatureClassFields = Temp_SourceFeatureClass.Fields; IEnumFieldError enumFieldError; Temp_FieldChecker.InputWorkspace = IN_SourceWorkspace; Temp_FieldChecker.ValidateWorkspace = IN_TargetWorkspace.Workspace; Temp_FieldChecker.Validate(Temp_SourceFeatureClassFields, out enumFieldError, out Temp_TargetFeatureClassFields); //批量导入 IField Temp_GeometryField; for (int i = 0; i < Temp_TargetFeatureClassFields.FieldCount; i++) { if (Temp_TargetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry) { Temp_GeometryField = Temp_TargetFeatureClassFields.get_Field(i); //获取空间定义 IGeometryDef Temp_GeometryDef = Temp_GeometryField.GeometryDef; IGeometryDefEdit Temp_TargetFCGeoDefEdit = (IGeometryDefEdit)Temp_GeometryDef; Temp_TargetFCGeoDefEdit.GridCount_2 = 1; Temp_TargetFCGeoDefEdit.set_GridSize(0, 0); Temp_TargetFCGeoDefEdit.SpatialReference_2 = Temp_GeometryField.GeometryDef.SpatialReference; //定义筛选条件 IQueryFilter Temp_QueryFilter = new QueryFilterClass(); Temp_QueryFilter.WhereClause = ""; //导入要素类至要素数据集 IFeatureDataConverter Temp_FeatureDataConverter = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = Temp_FeatureDataConverter.ConvertFeatureClass(Temp_SourceFeatureClassName, Temp_QueryFilter, Temp_TargetFeatureDatasetName, Temp_TargetFeatureClassName, Temp_GeometryDef, Temp_TargetFeatureClassFields, "", 1000, 0); break; } } }
/// <summary> /// 将一个要素类从一个工作空间转移到另外一个工作空间 /// 注意目标工作空间不能有改要素类,必须先清除 /// </summary> /// <param name="sourceWorkspace">源工作空间</param> /// <param name="targetWorkspace">目标工作空间</param> /// <param name="nameOfSourceFeatureClass">源要素类名</param> /// <param name="nameOfTargetFeatureClass">目标要素类名</param> public void IFeatureDataConverter_ConvertFeatureClass(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureClass, string nameOfTargetFeatureClass) { //create source workspace name IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; //create source dataset name IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = nameOfSourceFeatureClass; //create target workspace name IDataset targetWorkspaceDataset = (IDataset)targetWorkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; //create target dataset name IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = nameOfTargetFeatureClass; //Open input Featureclass to get field definitions. ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName; IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open(); //Validate the field names because you are converting between different workspace types. IFieldChecker fieldChecker = new FieldCheckerClass(); IFields targetFeatureClassFields; IFields sourceFeatureClassFields = sourceFeatureClass.Fields; IEnumFieldError enumFieldError; // Most importantly set the input and validate workspaces! fieldChecker.InputWorkspace = sourceWorkspace; fieldChecker.ValidateWorkspace = targetWorkspace; fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields); // Loop through the output fields to find the geomerty field IField geometryField; for (int i = 0; i < targetFeatureClassFields.FieldCount; i++) { if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry) { geometryField = targetFeatureClassFields.get_Field(i); // Get the geometry field's geometry defenition IGeometryDef geometryDef = geometryField.GeometryDef; //Give the geometry definition a spatial index grid count and grid size IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef; targetFCGeoDefEdit.GridCount_2 = 1; targetFCGeoDefEdit.set_GridSize(0, 0); //Allow ArcGIS to determine a valid grid size for the data loaded targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference; // we want to convert all of the features IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; // Load the feature class IFeatureDataConverter fctofc = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName, queryFilter, null, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0); break; } } }
/// <summary> /// Transfers data from an input FeatureClass to an output FeatureClass with data filtered with the specified query filter. /// </summary> /// <param name="inputWorkspace">The input workspace.</param> /// <param name="inputFeatureClassName">Name of the input feature class.</param> /// <param name="outputWorkspace">The output workspace.</param> /// <param name="outputFeatureClassName">Name of the output feature class.</param> /// <param name="outputFeatureDatasetName">The output FeatureDataset</param> /// <param name="queryFilter">The query filter used to filter data to be transfered.</param> public static IEnumInvalidObject TransferData(IFeatureWorkspace inputWorkspace, IFeatureClass inputFeatureClass, IFeatureWorkspace outputWorkspace, IFeatureClassName outputFeatureClassName, IFeatureDatasetName outputFeatureDatasetName, IQueryFilter queryFilter, ISpatialReference outputSpatialReference) { if (inputWorkspace == null) { throw new ArgumentNullException("A valid input feature workspace reference [inputWorkspace] is required."); } if (inputFeatureClass == null) { throw new ArgumentNullException("A valid input featureclass [inputFeatureClass] is required."); } if (outputWorkspace == null) { throw new ArgumentNullException("A valid output feature workspace reference [outputWorkspace] is required."); } if (outputFeatureClassName == null) { throw new ArgumentNullException("A valid output featureclass name reference [outputFeatureClassName] is required."); } IDataset dataset = (IDataset)inputFeatureClass; IFeatureClassName inputFeatureClassName = (IFeatureClassName)dataset.FullName; IEnumFieldError error = null; IFields outFields = null; IFields inFields = inputFeatureClass.Fields; FieldCheckerClass fieldChecker = new FieldCheckerClass(); fieldChecker.InputWorkspace = (IWorkspace)inputWorkspace; fieldChecker.ValidateWorkspace = (IWorkspace)outputWorkspace; fieldChecker.Validate(inFields, out error, out outFields); IField shapeField = null; for (int i = 0; i < outFields.FieldCount; i++) { if (outFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry) { shapeField = outFields.get_Field(i); break; } } IGeometryDef shapeGeoDef = shapeField.GeometryDef; IGeometryDefEdit shapeGeoDefEdit = shapeGeoDef as IGeometryDefEdit; shapeGeoDefEdit.GridCount_2 = 1; shapeGeoDefEdit.set_GridSize(0, DefaultIndexGrid(ref inputFeatureClass)); shapeGeoDefEdit.SpatialReference_2 = outputSpatialReference; FeatureDataConverterClass fdc = new FeatureDataConverterClass(); return(fdc.ConvertFeatureClass(inputFeatureClassName, queryFilter, outputFeatureDatasetName, outputFeatureClassName, shapeGeoDefEdit, outFields, "", 1000, 0)); }
public void IFeatureDataConverter(IWorkspace pSourceWorkspace, IWorkspace pTargetWorkspace, string pSFeatureClass, string pDFeatureClass) { IDataset sourceWorkspaceDataset = (IDataset)pSourceWorkspace; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = pSFeatureClass; IDataset targetWorkspaceDataset = (IDataset)pTargetWorkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = pDFeatureClass; ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName; IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open(); IFieldChecker fieldChecker = new FieldCheckerClass(); IFields targetFeatureClassFields; IFields sourceFeatureClassFields = sourceFeatureClass.Fields; IEnumFieldError enumFieldError; fieldChecker.InputWorkspace = pSourceWorkspace; fieldChecker.ValidateWorkspace = pTargetWorkspace; fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields); IField geometryField; for (int i = 0; i < targetFeatureClassFields.FieldCount; i++) { if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry) { geometryField = targetFeatureClassFields.get_Field(i); IGeometryDef geometryDef = geometryField.GeometryDef; IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef; targetFCGeoDefEdit.GridCount_2 = 1; targetFCGeoDefEdit.set_GridSize(0, 0); targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference; IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; IFeatureDataConverter fctofc = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName, queryFilter, null, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0); break; } } }
/// <summary> /// 复制源图层到目标dataset中的目标图层 /// </summary> /// <param name="sourceWorkspaceDataset">源图层集</param> /// <param name="targetWorkspaceDataset">目标图层集</param> /// <param name="sourceFeatureClass">源图层</param> /// <param name="nameOfTargetFeatureClass">待创建的图层名</param> public static bool ConvertFeatureClass(IDataset sourceWorkspaceDataset, IDataset targetWorkspaceDataset, IFeatureClass sourceFeatureClass, string nameOfTargetFeatureClass) { try { IFeatureClassName nameOfSourceFeatureClass = ((IDataset)sourceFeatureClass).FullName as IFeatureClassName; IDatasetName pSourceDsName = (IDatasetName)sourceWorkspaceDataset.FullName; IDatasetName pTargetDsName = (IDatasetName)targetWorkspaceDataset.FullName; IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); targetFeatureClassName.FeatureDatasetName = pTargetDsName; IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.Name = nameOfTargetFeatureClass; IFields targetFeatureClassFields; IFields sourceFeatureClassFields = sourceFeatureClass.Fields; IEnumFieldError enumFieldError; IFieldChecker fieldChecker = new FieldCheckerClass(); fieldChecker.InputWorkspace = sourceWorkspaceDataset.Workspace; fieldChecker.ValidateWorkspace = targetWorkspaceDataset.Workspace; fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields); IField geometryField = targetFeatureClassFields.get_Field(targetFeatureClassFields.FindField(sourceFeatureClass.ShapeFieldName)); //for (int i = 0; i < targetFeatureClassFields.FieldCount; i++) //{ // if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry) // { //geometryField = targetFeatureClassFields.get_Field(i); IGeometryDef geometryDef = geometryField.GeometryDef; IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef; targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference; IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; IFeatureDataConverter fConverter = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = fConverter.ConvertFeatureClass(nameOfSourceFeatureClass, queryFilter, pTargetDsName as IFeatureDatasetName, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0); // break; // } //} return(true); } catch (Exception exp) { return(false); } }
/// <summary> /// 拷贝源表到目标空间 /// </summary> /// <param name="sourceWorkspace"></param> /// <param name="targetWorkspace"></param> /// <param name="SourceDataset"></param> /// <param name="nameOfTargetDataset"></param> public static void ConvertTable(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, IDataset SourceDataset, string nameOfTargetDataset) { try { //create source workspace name ITable pSourceTab = (ITable)SourceDataset; IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; IDatasetName sourceDatasetName = (IDatasetName)SourceDataset.FullName; //create target workspace name IDataset targetWorkspaceDataset = (IDataset)targetWorkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; //create target dataset name ITableName targetTableName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetTableName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = nameOfTargetDataset; ////Open input Featureclass to get field definitions. //ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceDatasetName; //ITable sourceTable = (ITable)sourceName.Open(); // we want to convert all of the features IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; //Validate the field names because you are converting between different workspace types. IFieldChecker fieldChecker = new FieldCheckerClass(); IFields targetFields; IFields sourceFields = pSourceTab.Fields; IEnumFieldError enumFieldError; // Most importantly set the input and validate workspaces! fieldChecker.InputWorkspace = sourceWorkspace; fieldChecker.ValidateWorkspace = targetWorkspace; fieldChecker.Validate(sourceFields, out enumFieldError, out targetFields); if (enumFieldError == null) { IFeatureDataConverter fctofc = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = fctofc.ConvertTable(sourceDatasetName, queryFilter, targetDatasetName, pSourceTab.Fields, "", 1000, 0); } } catch (Exception exp) { Hy.Common.Utility.Log.OperationalLogManager.AppendMessage(exp.ToString()); return; } }
public void FclssToShp(IFeatureClass fteClss, string lsshp) { if (fteClss == null || string.IsNullOrEmpty(lsshp)) { return; } string filename = System.IO.Path.GetFileName(lsshp); string filepath = System.IO.Path.GetDirectoryName(lsshp); if (!System.IO.Directory.Exists(filepath)) { System.IO.Directory.CreateDirectory(filepath); } //释放空间 using (ComReleaser comreleaser = new ComReleaser()) { IDataset indataset = fteClss as IDataset; IWorkspaceFactory pWorkfactory = new ShapefileWorkspaceFactoryClass(); IFeatureWorkspace fteWsp = pWorkfactory.OpenFromFile(filepath, 0) as IFeatureWorkspace; IWorkspace inwsp = indataset.Workspace; IFeatureClassName inFCName = indataset.FullName as IFeatureClassName; // IWorkspace outWsp = fteWsp as IWorkspace; IDataset outdataset = outWsp as IDataset; IWorkspaceName outWspName = outdataset.FullName as IWorkspaceName; IFeatureClassName outFCName = new FeatureClassNameClass(); IDatasetName outDstName = outFCName as IDatasetName; outDstName.WorkspaceName = outWspName; outDstName.Name = fteClss.AliasName.ToString(); //检查字段的合法性 IFieldChecker checker = new FieldCheckerClass(); checker.InputWorkspace = inwsp; checker.ValidateWorkspace = outWsp; IFields fileds = fteClss.Fields; IFields outfields = null; IEnumFieldError error = null; checker.Validate(fileds, out error, out outfields); //转换为SHP IFeatureDataConverter convert = new FeatureDataConverterClass(); convert.ConvertFeatureClass(inFCName, null, null, outFCName, null, outfields, "", 100, 0); } }
/// <summary> /// 拷贝源表到目标空间 /// </summary> /// <param name="sourceWorkspace"></param> /// <param name="targetWorkspace"></param> /// <param name="SourceDataset"></param> /// <param name="nameOfTargetDataset"></param> public static bool ConvertTable(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, IDataset SourceDataset, string nameOfTargetDataset) { try { ITable pSourceTab = (ITable)SourceDataset; IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; IDatasetName sourceDatasetName = (IDatasetName)SourceDataset.FullName; IDataset targetWorkspaceDataset = (IDataset)targetWorkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; ITableName targetTableName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetTableName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = nameOfTargetDataset; IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; IFieldChecker fieldChecker = new FieldCheckerClass(); IFields targetFields; IFields sourceFields = pSourceTab.Fields; IEnumFieldError enumFieldError; fieldChecker.InputWorkspace = sourceWorkspace; fieldChecker.ValidateWorkspace = targetWorkspace; fieldChecker.Validate(sourceFields, out enumFieldError, out targetFields); if (enumFieldError == null) { IFeatureDataConverter fConverter = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = fConverter.ConvertTable(sourceDatasetName, queryFilter, targetDatasetName, pSourceTab.Fields, "", 1000, 0); } return(true); } catch (Exception exp) { return(false); } }
/// <summary> /// 复制数据到另一张表 /// </summary> /// <param name="outZWorkspace">目标工作空间</param> /// <param name="name">目标图层名</param> /// <param name="filter">查询条件</param> public void CopyTo(ZWorkspace outZWorkspace, string name, ZFilter filter) { // 数据源 IFeatureClassName inputFClassName = ((IDataset)featureClass).FullName as IFeatureClassName; // 目标数据集 IDataset outDataSet = outZWorkspace.workspace as IDataset; IWorkspaceName outWorkspaceName = outDataSet.FullName as IWorkspaceName; // 目标图层 IFeatureClassName outputFClassName = new FeatureClassNameClass(); IDatasetName dataSetName = outputFClassName as IDatasetName; dataSetName.WorkspaceName = outWorkspaceName; dataSetName.Name = name; // 导出 FeatureDataConverterClass converter = new FeatureDataConverterClass(); converter.ConvertFeatureClass(inputFClassName, filter.Value, null, outputFClassName, null, featureClass.Fields, "", 100, 0); // 释放资源 Marshal.ReleaseComObject(converter); }
private IFeatureClass CopyFeatureClassIntoNewWorkspace(IFeatureClass inFeatureClass, IWorkspace outWorkspace, string newName) { // get FeatureClassName for input var inDataset = inFeatureClass as IDataset; var inFeatureClassName = inDataset.FullName as IFeatureClassName; var inWorkspace = inDataset.Workspace; // get WorkSpaceName for output var outDataset = outWorkspace as IDataset; var outWorkspaceName = outDataset.FullName as IWorkspaceName; // Create new FeatureClassName IFeatureClassName outFeatureClassName = new FeatureClassNameClass(); // Assign it a name and a workspace var datasetName = outFeatureClassName as IDatasetName; datasetName.Name = newName == String.Empty ? (inFeatureClassName as IDatasetName).Name : newName; datasetName.WorkspaceName = outWorkspaceName; // Check for field conflicts. IFieldChecker fieldChecker = new FieldCheckerClass(); IFields inFields = inFeatureClass.Fields; IFields outFields; IEnumFieldError enumFieldError; fieldChecker.InputWorkspace = inWorkspace; fieldChecker.ValidateWorkspace = outWorkspace; fieldChecker.Validate(inFields, out enumFieldError, out outFields); // Check enumFieldError for field naming conflicts //Convert the data. IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass(); featureDataConverter.ConvertFeatureClass(inFeatureClassName, null, null, outFeatureClassName, null, outFields, "", 100, 0); var pFeatureWS = (IFeatureWorkspace)outWorkspace; return(pFeatureWS.OpenFeatureClass(newName)); }
/// <summary> /// Exports the source table using the query filter to the table in the output workspace. /// </summary> /// <param name="source">The source.</param> /// <param name="filter">The filter.</param> /// <param name="tableName">Name of the output table.</param> /// <param name="workspace">The output workspace.</param> /// <param name="requiredFields">The required fields.</param> /// <param name="handle">The handle.</param> /// <param name="keepFeatureDatset">if set to <c>true</c> when the feature class should be created within feature datset.</param> /// <param name="surrogate">The surrogate for the event handler.</param> /// <param name="invalid">The errors that occured during the export.</param> /// <param name="errors">The field errors.</param> /// <returns> /// Returns a <see cref="IFeatureClass" /> representing the feature class that was exported. /// </returns> public static IFeatureClass Export(this IFeatureClass source, IQueryFilter filter, string tableName, IWorkspace workspace, IFields requiredFields, int handle, bool keepFeatureDatset, FeatureProgress surrogate, out IEnumInvalidObject invalid, out IEnumFieldError errors) { var ds = (IDataset)source; var input = ds.Workspace.Define(ds.Name, new FeatureClassNameClass()); var output = workspace.Define(tableName, new FeatureClassNameClass()); if (source.FeatureDataset != null && keepFeatureDatset) { output.FeatureDatasetName = source.FeatureDataset.FullName as IDatasetName; } var i = source.Fields.FindField(source.ShapeFieldName); var field = source.Fields.Field[i]; var clone = (IClone)field.GeometryDef; var geometryDef = (IGeometryDef)clone.Clone(); workspace.Delete(output); IFieldChecker fieldChecker = new FieldCheckerClass(); fieldChecker.InputWorkspace = ds.Workspace; fieldChecker.ValidateWorkspace = workspace; IFields targetFields; fieldChecker.Validate(requiredFields, out errors, out targetFields); var featureDataConverter = new FeatureDataConverterClass(); if (surrogate != null) { surrogate.Advise(featureDataConverter); } invalid = featureDataConverter.ConvertFeatureClass(input, filter, null, output, geometryDef, targetFields, "", 1000, handle); return(((IName)output).Open() as IFeatureClass); }
public static void Project(IFeatureDataset ifeatureDataset_0, ISpatialReference ispatialReference_0, IWorkspace iworkspace_0, string string_0) { IGeometryDef outputGeometryDef = new GeometryDefClass(); ISpatialReference spatialReference = ((IGeoDataset)ifeatureDataset_0).SpatialReference; ((IGeometryDefEdit)outputGeometryDef).SpatialReference_2 = ispatialReference_0; IWorkspace workspace = ifeatureDataset_0.Workspace; IFeatureDataConverter converter = new FeatureDataConverterClass(); IWorkspaceName name = new WorkspaceNameClass { ConnectionProperties = workspace.ConnectionProperties, WorkspaceFactoryProgID = workspace.WorkspaceFactory.GetClassID().Value.ToString() }; IWorkspaceName name2 = new WorkspaceNameClass(); new PropertySetClass(); name2.ConnectionProperties = iworkspace_0.ConnectionProperties; name2.WorkspaceFactoryProgID = iworkspace_0.WorkspaceFactory.GetClassID().Value.ToString(); IDatasetName name3 = new FeatureDatasetNameClass(); string str = ifeatureDataset_0.Name; int num = str.LastIndexOf("."); if (num != -1) { str = str.Substring(num + 1); } name3.Name = str; name3.WorkspaceName = name; IDatasetName name4 = new FeatureDatasetNameClass { WorkspaceName = name2, Name = string_0 }; converter.ConvertFeatureDataset((IFeatureDatasetName)name3, (IFeatureDatasetName)name4, outputGeometryDef, "", 1000, 0); }
/// <summary> /// 拷贝源图层到空间数据集下的目标图层 /// </summary> /// <param name="sourceWorkspace">源Workspace</param> /// <param name="targetWorkspace">目标Workspace</param> /// <param name="sourceClassName">源图层</param> /// <param name="destClassName">待创建的图层名</param> public static bool ConvertFeatureClass(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string sourceClassName, string destClassName) { try { IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = sourceClassName; IDataset targetWorkspaceDataset = (IDataset)targetWorkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = destClassName; IName sourceName = (IName)sourceFeatureClassName; IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open(); IFields targetFeatureClassFields; IFields sourceFeatureClassFields = sourceFeatureClass.Fields; IEnumFieldError enumFieldError; IFieldChecker fieldChecker = new FieldCheckerClass(); fieldChecker.InputWorkspace = sourceWorkspace; fieldChecker.ValidateWorkspace = targetWorkspace; fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields); IField geometryField = targetFeatureClassFields.get_Field(targetFeatureClassFields.FindField(sourceFeatureClass.ShapeFieldName)); //for (int i = 0; i < targetFeatureClassFields.FieldCount; i++) //{ // geometryField = targetFeatureClassFields.get_Field(i); // if (geometryField.Type == esriFieldType.esriFieldTypeGeometry) // { IGeometryDef geometryDef = geometryField.GeometryDef; IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef; targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference; IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; IEnumDataset pEnumDataset = targetWorkspace.get_Datasets(esriDatasetType.esriDTFeatureDataset); IFeatureDataConverter fConverter = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = fConverter.ConvertFeatureClass(sourceFeatureClassName, queryFilter, null, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0); IInvalidObjectInfo obj = enumErrors.Next(); // break; // } //} return true; } catch (Exception exp) { return false; } }
/// <summary> /// 将一个要素类从一个工作空间转移到另外一个工作空间 /// 注意目标工作空间不能有改要素类,必须先清除 /// </summary> /// <params name="sourceWorkspace">源工作空间</params> /// <params name="targetWorkspace">目标工作空间</params> /// <params name="nameOfSourceFeatureClass">源要素类名</params> /// <params name="nameOfTargetFeatureClass">目标要素类名</params> public bool IFeatureDataConverter_ConvertFeatureClass(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureClass, string nameOfTargetFeatureClass) { bool change = false; //create source workspace name IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; //create source dataset name IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = nameOfSourceFeatureClass; //create target workspace name IDataset targetWorkspaceDataset = (IDataset)targetWorkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; //create target dataset name IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = nameOfTargetFeatureClass; //Open input Featureclass to get field definitions. ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName; IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open(); //Validate the field names because you are converting between different workspace types. IFieldChecker fieldChecker = new FieldCheckerClass(); IFields targetFeatureClassFields; IFields sourceFeatureClassFields = sourceFeatureClass.Fields; IEnumFieldError enumFieldError; // Most importantly set the input and validate workspaces! fieldChecker.InputWorkspace = sourceWorkspace; fieldChecker.ValidateWorkspace = targetWorkspace; fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields); // Loop through the output fields to find the geomerty field IField geometryField; for (int i = 0; i < targetFeatureClassFields.FieldCount; i++) { if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry) { geometryField = targetFeatureClassFields.get_Field(i); // Get the geometry field's geometry defenition IGeometryDef geometryDef = geometryField.GeometryDef; //Give the geometry definition a spatial index grid count and grid size IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef; targetFCGeoDefEdit.GridCount_2 = 1; targetFCGeoDefEdit.set_GridSize(0, 0); //Allow ArcGIS to determine a valid grid size for the data loaded targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference; // we want to convert all of the features IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; // Load the feature class IFeatureDataConverter fctofc = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName, queryFilter, null, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0); if (enumErrors.Next() == null) change = true; break; } } return change; }
/// <summary> /// 复制源图层到目标dataset中的目标图层 /// </summary> /// <param name="sourceWorkspaceDataset">源图层集</param> /// <param name="targetWorkspaceDataset">目标图层集</param> /// <param name="sourceFeatureClass">源图层</param> /// <param name="nameOfTargetFeatureClass">待创建的图层名</param> public static bool ConvertFeatureClass(IDataset sourceWorkspaceDataset, IDataset targetWorkspaceDataset, IFeatureClass sourceFeatureClass, string nameOfTargetFeatureClass) { try { IFeatureClassName nameOfSourceFeatureClass = ((IDataset)sourceFeatureClass).FullName as IFeatureClassName; IDatasetName pSourceDsName = (IDatasetName)sourceWorkspaceDataset.FullName; IDatasetName pTargetDsName = (IDatasetName)targetWorkspaceDataset.FullName; IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); targetFeatureClassName.FeatureDatasetName = pTargetDsName; IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.Name = nameOfTargetFeatureClass; IFields targetFeatureClassFields; IFields sourceFeatureClassFields = sourceFeatureClass.Fields; IEnumFieldError enumFieldError; IFieldChecker fieldChecker = new FieldCheckerClass(); fieldChecker.InputWorkspace = sourceWorkspaceDataset.Workspace; fieldChecker.ValidateWorkspace = targetWorkspaceDataset.Workspace; fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields); IField geometryField = targetFeatureClassFields.get_Field(targetFeatureClassFields.FindField(sourceFeatureClass.ShapeFieldName)); //for (int i = 0; i < targetFeatureClassFields.FieldCount; i++) //{ // if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry) // { //geometryField = targetFeatureClassFields.get_Field(i); IGeometryDef geometryDef = geometryField.GeometryDef; IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef; targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference; IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; IFeatureDataConverter fConverter = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = fConverter.ConvertFeatureClass(nameOfSourceFeatureClass, queryFilter, pTargetDsName as IFeatureDatasetName, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0); // break; // } //} return true; } catch (Exception exp) { return false; } }
/// <summary> /// 转格式 /// </summary> /// <param name="sourceWorkspacePath"></param> /// <param name="targetWorkspacePath"></param> /// <param name="sourceDataName"></param> /// <param name="targetDataName"></param> public static void ConvertFeatureClassToShapefile(string sourceWorkspacePath, string targetWorkspacePath, string sourceDataName, string targetDataName) { // Open the source and target workspaces. IWorkspaceFactory sourceWorkspaceFactory = new FileGDBWorkspaceFactoryClass(); IWorkspaceFactory targetWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspace sourceWorkspace = sourceWorkspaceFactory.OpenFromFile(sourceWorkspacePath, 0); IWorkspace targetWorkspace = targetWorkspaceFactory.OpenFromFile(targetWorkspacePath, 0); // Cast the workspaces to the IDataset interface and get name objects. IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace; IDataset targetWorkspaceDataset = (IDataset)targetWorkspace; IName sourceWorkspaceDatasetName = sourceWorkspaceDataset.FullName; IName targetWorkspaceDatasetName = targetWorkspaceDataset.FullName; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDatasetName; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDatasetName; // Create a name object for the shapefile and cast it to the IDatasetName interface. IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName; sourceDatasetName.Name = sourceDataName;//"streets"; sourceDatasetName.WorkspaceName = sourceWorkspaceName; // Create a name object for the FGDB feature class and cast it to the IDatasetName interface. IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.Name = targetDataName;// "AtlantaStreets"; targetDatasetName.WorkspaceName = targetWorkspaceName; // Open source feature class to get field definitions. IName sourceName = (IName)sourceFeatureClassName; IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open(); // Create the objects and references necessary for field validation. IFieldChecker fieldChecker = new FieldCheckerClass(); IFields sourceFields = sourceFeatureClass.Fields; IFields targetFields = null; IEnumFieldError enumFieldError = null; // Set the required properties for the IFieldChecker interface. fieldChecker.InputWorkspace = sourceWorkspace; fieldChecker.ValidateWorkspace = targetWorkspace; // Validate the fields and check for errors. fieldChecker.Validate(sourceFields, out enumFieldError, out targetFields); if (enumFieldError != null) { // Handle the errors in a way appropriate to your application. Console.WriteLine("Errors were encountered during field validation."); } // Find the shape field. String shapeFieldName = sourceFeatureClass.ShapeFieldName; int shapeFieldIndex = sourceFeatureClass.FindField(shapeFieldName); IField shapeField = sourceFields.get_Field(shapeFieldIndex); // Get the geometry definition from the shape field and clone it. IGeometryDef geometryDef = shapeField.GeometryDef; IClone geometryDefClone = (IClone)geometryDef; IClone targetGeometryDefClone = geometryDefClone.Clone(); IGeometryDef targetGeometryDef = (IGeometryDef)targetGeometryDefClone; // Create a query filter to remove ramps, interstates and highways. IQueryFilter queryFilter = new QueryFilterClass(); //queryFilter.WhereClause = "NAME <> 'Ramp' AND PRE_TYPE NOT IN ('I', 'Hwy')"; queryFilter.WhereClause = "1=1"; // Create the converter and run the conversion. IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass(); IEnumInvalidObject enumInvalidObject = featureDataConverter.ConvertFeatureClass(sourceFeatureClassName, queryFilter, null, targetFeatureClassName, targetGeometryDef, targetFields, "", 1000, 0); // Check for errors. IInvalidObjectInfo invalidObjectInfo = null; enumInvalidObject.Reset(); while ((invalidObjectInfo = enumInvalidObject.Next()) != null) { // Handle the errors in a way appropriate to the application. Console.WriteLine("Errors occurred for the following feature: {0}", invalidObjectInfo.InvalidObjectID); } }
/// <summary> /// 拷贝源表到目标空间 /// </summary> /// <param name="sourceWorkspace"></param> /// <param name="targetWorkspace"></param> /// <param name="SourceDataset"></param> /// <param name="nameOfTargetDataset"></param> public static void ConvertTable(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, IDataset SourceDataset, string nameOfTargetDataset) { try { //create source workspace name ITable pSourceTab = (ITable)SourceDataset; IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; IDatasetName sourceDatasetName = (IDatasetName)SourceDataset.FullName; //create target workspace name IDataset targetWorkspaceDataset = (IDataset)targetWorkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; //create target dataset name ITableName targetTableName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetTableName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = nameOfTargetDataset; ////Open input Featureclass to get field definitions. //ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceDatasetName; //ITable sourceTable = (ITable)sourceName.Open(); // we want to convert all of the features IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; //Validate the field names because you are converting between different workspace types. IFieldChecker fieldChecker = new FieldCheckerClass(); IFields targetFields; IFields sourceFields = pSourceTab.Fields; IEnumFieldError enumFieldError; // Most importantly set the input and validate workspaces! fieldChecker.InputWorkspace = sourceWorkspace; fieldChecker.ValidateWorkspace = targetWorkspace; fieldChecker.Validate(sourceFields, out enumFieldError, out targetFields); if (enumFieldError == null) { IFeatureDataConverter fctofc = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = fctofc.ConvertTable(sourceDatasetName, queryFilter, targetDatasetName, pSourceTab.Fields, "", 1000, 0); } } catch (Exception exp) { Hy.Common.Utility.Log.OperationalLogManager.AppendMessage(exp.ToString()); return; } }
/// <summary> /// SDE要素类导出mdb /// </summary> /// <param name="sourceWorkspace">源工作空间</param> /// <param name="targetWorkspace">目标工作空间</param> /// <param name="nameOfSourceFeatureClass">源要素类名</param> /// <param name="nameOfTargetFeatureClass">目标要素类名</param> /// <param name="queryFilter"></param> /// <returns></returns> private bool ConvertFeatureClass(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureClass, string nameOfTargetFeatureClass) { //创建一个源数据的工作空间的name IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; //创建源数据Name作为转换参数 IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = nameOfSourceFeatureClass; //创建目标(导出)数据空间的name IDataset targetWorkspaceDataset = (IDataset)targetWorkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; //创建目标数据Name作为转换参数 IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.WorkspaceName = targetWorkspaceName; //这个命名也很重要,如果是mdb,那么这个命名在数据库中必须是本来不存在的(当然你可以先清空数据库) targetDatasetName.Name = nameOfTargetFeatureClass; //根据FetureClassName打开数据 ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName; IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open(); //在两个不同的工作空间转换数据要检查字段的有效性 //一些检查有效性的参数 IFieldChecker fieldChecker = new FieldCheckerClass(); IFields targetFeatureClassFields; IFields sourceFeatureClassFields = sourceFeatureClass.Fields; IEnumFieldError enumFieldError; //设置检查有效性的源数据工作空间 fieldChecker.InputWorkspace = sourceWorkspace; fieldChecker.ValidateWorkspace = targetWorkspace; fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields); //通过输出字段循环找出几何字段 IField geometryField; //遍历字段 for (int i = 0; i < targetFeatureClassFields.FieldCount; i++) { //找到定义几何要素类型的字段 if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry) { //获取当前字段 geometryField = targetFeatureClassFields.get_Field(i); //编辑几何要素类型字段 IGeometryDef geometryDef = geometryField.GeometryDef; IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef; targetFCGeoDefEdit.GridCount_2 = 1; targetFCGeoDefEdit.set_GridSize(0, 0); //保证空间参考 SpatialReference_2属性字段才是可写的,SpatialReference属性只可读 targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference; //开始转换 IFeatureDataConverter myConvert = new FeatureDataConverterClass(); myConvert.ConvertFeatureClass(sourceFeatureClassName, null, null, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0); return(true); } } return(false); }
/// <summary> /// 复制源图层到目标dataset中的目标图层 /// </summary> /// <param name="sourceWorkspaceDataset">源图层集</param> /// <param name="targetWorkspaceDataset">目标图层集</param> /// <param name="sourceFeatureClass">源图层</param> /// <param name="nameOfTargetFeatureClass">待创建的图层名</param> public static IFeatureClass ConvertFeatureClass(IDataset sourceWorkspaceDataset, IDataset targetWorkspaceDataset, IFeatureClass sourceFeatureClass, string nameOfTargetFeatureClass) { IFeatureClass TragetFeatureClass = null; try { IFeatureClassName nameOfSourceFeatureClass = ((IDataset)sourceFeatureClass).FullName as IFeatureClassName; IDatasetName pSourceDsName = (IDatasetName)sourceWorkspaceDataset.FullName; IDatasetName pTargetDsName = (IDatasetName)targetWorkspaceDataset.FullName; IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); targetFeatureClassName.FeatureDatasetName = pTargetDsName; IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.Name = nameOfTargetFeatureClass; //Validate the field names because you are converting between different workspace types. IFieldChecker fieldChecker = new FieldCheckerClass(); IFields targetFeatureClassFields; IFields sourceFeatureClassFields = sourceFeatureClass.Fields; IEnumFieldError enumFieldError; // Most importantly set the input and validate workspaces! fieldChecker.InputWorkspace = sourceWorkspaceDataset.Workspace; fieldChecker.ValidateWorkspace = targetWorkspaceDataset.Workspace; fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields); // Loop through the output fields to find the geomerty field IField geometryField; for (int i = 0; i < targetFeatureClassFields.FieldCount; i++) { if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry) { geometryField = targetFeatureClassFields.get_Field(i); // Get the geometry field's geometry defenition IGeometryDef geometryDef = geometryField.GeometryDef; //Give the geometry definition a spatial index grid count and grid size IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef; //targetFCGeoDefEdit.GridCount_2 = 1; //targetFCGeoDefEdit.set_GridSize(0, 0); //Allow ArcGIS to determine a valid grid size for the data loaded targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference; // we want to convert all of the features IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; // Load the feature class IFeatureDataConverter fctofc = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(nameOfSourceFeatureClass, queryFilter, pTargetDsName as IFeatureDatasetName, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0); break; } } string strTargetFtCls = ((IDatasetName)targetFeatureClassName).Name; TragetFeatureClass = ((IFeatureWorkspace)targetWorkspaceDataset.Workspace).OpenFeatureClass(strTargetFtCls); } catch (Exception exp) { Hy.Common.Utility.Log.OperationalLogManager.AppendMessage(exp.ToString()); return(null); } return(TragetFeatureClass); }
/// <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)处理)*****/ }
/// <summary> /// convert featureclass in shapefile /// </summary> /// <param name="sourceWorkspace">oggetto workspace</param> /// <param name="outputName">nome feature class e nome shapefile</param> /// <param name="targetWorkspacePath">cartella shapefile</param> /// <param name="errorField">lista degli eventuali errori nella creazione dei campi</param> /// <param name="invalidObject">lista degli eventuale errori di creazione record</param> private void ConvertFeatureClassToShapefile(IWorkspace sourceWorkspace, string outputName, string targetWorkspacePath, ref List <string> errorField, ref List <string> invalidObject) { IWorkspace targetWorkspace = null; try { Type factoryType = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory"); IWorkspaceFactory targetWorkspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType); targetWorkspace = targetWorkspaceFactory.OpenFromFile(targetWorkspacePath, 0); // Cast the workspaces to the IDataset interface and get name objects. IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace; IDataset targetWorkspaceDataset = (IDataset)targetWorkspace; IName sourceWorkspaceDatasetName = sourceWorkspaceDataset.FullName; IName targetWorkspaceDatasetName = targetWorkspaceDataset.FullName; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDatasetName; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDatasetName; // Create a name object for the shapefile and cast it to the IDatasetName interface. IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName; sourceDatasetName.Name = outputName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; // Create a name object for the FGDB feature class and cast it to the IDatasetName interface. IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.Name = outputName; targetDatasetName.WorkspaceName = targetWorkspaceName; // Open source feature class to get field definitions. IName sourceName = (IName)sourceFeatureClassName; IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open(); // Create the objects and references necessary for field validation. IFieldChecker fieldChecker = new FieldCheckerClass(); IFields sourceFields = sourceFeatureClass.Fields; IFields targetFields = null; IEnumFieldError enumFieldError = null; // Set the required properties for the IFieldChecker interface. fieldChecker.InputWorkspace = sourceWorkspace; fieldChecker.ValidateWorkspace = targetWorkspace; // Validate the fields and check for errors. fieldChecker.Validate(sourceFields, out enumFieldError, out targetFields); if (enumFieldError != null) { IFieldError fieldError = null; enumFieldError.Reset(); while ((fieldError = enumFieldError.Next()) != null) { errorField.Add($"Errore: {Enum.GetName(typeof(esriFieldNameErrorType), fieldError.FieldError)} - Campo: {targetFields.get_Field(fieldError.FieldIndex).Name}"); } } // Find the shape field. string shapeFieldName = sourceFeatureClass.ShapeFieldName; int shapeFieldIndex = sourceFeatureClass.FindField(shapeFieldName); IField shapeField = sourceFields.get_Field(shapeFieldIndex); // Get the geometry definition from the shape field and clone it. IGeometryDef geometryDef = shapeField.GeometryDef; IClone geometryDefClone = (IClone)geometryDef; IClone targetGeometryDefClone = geometryDefClone.Clone(); IGeometryDef targetGeometryDef = (IGeometryDef)targetGeometryDefClone; // Create the converter and run the conversion. IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass(); IEnumInvalidObject enumInvalidObject = featureDataConverter.ConvertFeatureClass(sourceFeatureClassName, null, null, targetFeatureClassName, targetGeometryDef, targetFields, string.Empty, 1000, 0); // Check for errors. IInvalidObjectInfo invalidObjectInfo = null; enumInvalidObject.Reset(); while ((invalidObjectInfo = enumInvalidObject.Next()) != null) { invalidObject.Add($"{invalidObjectInfo.InvalidObjectID}"); } } catch { throw; } finally { if (targetWorkspace != null) { Marshal.FinalReleaseComObject(targetWorkspace); } } }
public static void Project(IFeatureClass ifeatureClass_0, ISpatialReference ispatialReference_0, IWorkspace iworkspace_0, string string_0, double double_0) { string str2; double num3; double num4; double num5; double num6; IEnumFieldError error; IFields fields2; IWorkspace workspace = ((IDataset)ifeatureClass_0).Workspace; IFeatureDataConverter converter = new FeatureDataConverterClass(); IWorkspaceName name = new WorkspaceNameClass { ConnectionProperties = workspace.ConnectionProperties, WorkspaceFactoryProgID = workspace.WorkspaceFactory.GetClassID().Value.ToString() }; IWorkspaceName name2 = new WorkspaceNameClass(); new PropertySetClass(); name2.ConnectionProperties = iworkspace_0.ConnectionProperties; name2.WorkspaceFactoryProgID = iworkspace_0.WorkspaceFactory.GetClassID().Value.ToString(); IDatasetName name3 = new FeatureClassNameClass(); string aliasName = ifeatureClass_0.AliasName; int index = aliasName.LastIndexOf("."); if (index != -1) { aliasName = aliasName.Substring(index + 1); } name3.Name = aliasName; name3.WorkspaceName = name; IDatasetName name4 = new FeatureClassNameClass { WorkspaceName = name2 }; IFieldChecker checker = new FieldCheckerClass { ValidateWorkspace = iworkspace_0 }; string[] strArray = string_0.Split(new char[] { '.' }); string_0 = strArray[strArray.Length - 1] + "_Project"; checker.ValidateTableName(string_0, out str2); string str3 = str2; int num2 = 1; if ((iworkspace_0.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace) || (iworkspace_0.Type == esriWorkspaceType.esriLocalDatabaseWorkspace)) { while (((IWorkspace2)iworkspace_0).get_NameExists(esriDatasetType.esriDTFeatureClass, str3)) { str3 = str2 + "_" + num2.ToString(); num2++; } } else if (iworkspace_0.Type == esriWorkspaceType.esriFileSystemWorkspace) { while (File.Exists(str3 + ".shp")) { str3 = str2 + "_" + num2.ToString(); num2++; } } name4.Name = str3; IFields inputField = new FieldsClass(); index = ifeatureClass_0.Fields.FindField(ifeatureClass_0.ShapeFieldName); IField field = ifeatureClass_0.Fields.get_Field(index); IGeometryDef geometryDef = field.GeometryDef; ISpatialReference spatialReference = geometryDef.SpatialReference; ispatialReference_0.GetDomain(out num3, out num4, out num5, out num6); ((IGeometryDefEdit)geometryDef).GridCount_2 = 1; ((IGeometryDefEdit)geometryDef).set_GridSize(0, double_0); ((IGeometryDefEdit)geometryDef).SpatialReference_2 = ispatialReference_0; ((IFieldEdit)field).GeometryDef_2 = geometryDef; for (int i = 0; i < ifeatureClass_0.Fields.FieldCount; i++) { if (i == index) { ((IFieldsEdit)inputField).AddField(field); } else { IField field2 = ifeatureClass_0.Fields.get_Field(i); ((IFieldsEdit)inputField).AddField(field2); } } checker.Validate(inputField, out error, out fields2); if (m_pfrm != null) { m_pfrm.FeatureProgress = converter; } try { converter.ConvertFeatureClass((IFeatureClassName)name3, null, null, (IFeatureClassName)name4, geometryDef, fields2, "", 1000, 0); } catch (Exception exception) { MessageBox.Show(exception.Message); } }
public ITable exportTable(ITable inputTable, string outPath, IQueryFilter filter) { // Create a name object for the source workspace and open it. IDataset inDset = (IDataset)inputTable; IWorkspace sourceWorkspace = (inDset).Workspace; // Create a name object for the target (file GDB) workspace and open it. string outDbStr = geoUtil.parseDbStr(outPath); string outName = System.IO.Path.GetFileName(outPath); IWorkspace targetWorkspace = geoUtil.OpenWorkSpace(outDbStr); outName = geoUtil.getSafeOutputNameNonRaster(targetWorkspace, outName); // Create a name object for the source dataset. ITableName sourceTableName = new TableNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceTableName; sourceDatasetName.Name = inDset.Name; sourceDatasetName.WorkspaceName = (IWorkspaceName)((IDataset)sourceWorkspace).FullName; // Create a name object for the target dataset. ITableName targetTableName = new TableNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetTableName; targetDatasetName.Name = outName; targetDatasetName.WorkspaceName = (IWorkspaceName)((IDataset)targetWorkspace).FullName; ; // Open source feature class to get field definitions. //IName sourceName = (IName)sourceFeatureClassName; ITable sourceTable = inputTable; // Create the objects and references necessary for field validation. IFieldChecker fieldChecker = new FieldCheckerClass(); IFields sourceFields = sourceTable.Fields; IFields targetFields = null; IEnumFieldError enumFieldError = null; // Set the required properties for the IFieldChecker interface. fieldChecker.InputWorkspace = sourceWorkspace; fieldChecker.ValidateWorkspace = targetWorkspace; // Validate the fields and check for errors. fieldChecker.Validate(sourceFields, out enumFieldError, out targetFields); if (enumFieldError != null) { // Handle the errors in a way appropriate to your application. Console.WriteLine("Errors were encountered during field validation."); } // Create the converter and run the conversion. IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass(); IEnumInvalidObject enumInvalidObject = featureDataConverter.ConvertTable(sourceDatasetName, filter, targetDatasetName, targetFields,"",1000,0); // Check for errors. IInvalidObjectInfo invalidObjectInfo = null; enumInvalidObject.Reset(); while ((invalidObjectInfo = enumInvalidObject.Next()) != null) { // Handle the errors in a way appropriate to the application. Console.WriteLine("Errors occurred for the following feature: {0}", invalidObjectInfo.InvalidObjectID); } return (Table)((IName)targetTableName).Open(); }
public void ConvertMDBFeatureDatasetToSDE(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureDataset, string nameOfTargetFeatureDataset) { if (sourceWorkspace == null || targetWorkspace == null) { return; } //创建源工作空间名 IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; //创建源数据集名 IFeatureDatasetName sourceFeatureDatasetName =new FeatureDatasetNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureDatasetName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = nameOfSourceFeatureDataset; //创建目标工作空间名 IDataset targetWorkspaceDataset = (IDataset)targetWorkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; //创建目标数据集名 IFeatureDatasetName targetFeatureDatasetName = new FeatureDatasetNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureDatasetName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = nameOfTargetFeatureDataset; //转换(复制)源数据集到目标数据集 IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass(); featureDataConverter.ConvertFeatureDataset(sourceFeatureDatasetName, targetFeatureDatasetName, null, "", 1000, 0); }
/// <summary> /// 复制源图层到目标dataset中的目标图层 /// </summary> /// <param name="sourceWorkspaceDataset">源图层集</param> /// <param name="targetWorkspaceDataset">目标图层集</param> /// <param name="sourceFeatureClass">源图层</param> /// <param name="nameOfTargetFeatureClass">待创建的图层名</param> public static IFeatureClass ConvertFeatureClass(IDataset sourceWorkspaceDataset, IDataset targetWorkspaceDataset, IFeatureClass sourceFeatureClass, string nameOfTargetFeatureClass) { IFeatureClass TragetFeatureClass = null; try { IFeatureClassName nameOfSourceFeatureClass = ((IDataset)sourceFeatureClass).FullName as IFeatureClassName; IDatasetName pSourceDsName = (IDatasetName)sourceWorkspaceDataset.FullName; IDatasetName pTargetDsName = (IDatasetName)targetWorkspaceDataset.FullName; IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); targetFeatureClassName.FeatureDatasetName = pTargetDsName; IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.Name = nameOfTargetFeatureClass; //Validate the field names because you are converting between different workspace types. IFieldChecker fieldChecker = new FieldCheckerClass(); IFields targetFeatureClassFields; IFields sourceFeatureClassFields = sourceFeatureClass.Fields; IEnumFieldError enumFieldError; // Most importantly set the input and validate workspaces! fieldChecker.InputWorkspace = sourceWorkspaceDataset.Workspace; fieldChecker.ValidateWorkspace = targetWorkspaceDataset.Workspace; fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields); // Loop through the output fields to find the geomerty field IField geometryField; for (int i = 0; i < targetFeatureClassFields.FieldCount; i++) { if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry) { geometryField = targetFeatureClassFields.get_Field(i); // Get the geometry field's geometry defenition IGeometryDef geometryDef = geometryField.GeometryDef; //Give the geometry definition a spatial index grid count and grid size IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef; //targetFCGeoDefEdit.GridCount_2 = 1; //targetFCGeoDefEdit.set_GridSize(0, 0); //Allow ArcGIS to determine a valid grid size for the data loaded targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference; // we want to convert all of the features IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; // Load the feature class IFeatureDataConverter fctofc = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(nameOfSourceFeatureClass, queryFilter, pTargetDsName as IFeatureDatasetName, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0); break; } } string strTargetFtCls = ((IDatasetName)targetFeatureClassName).Name; TragetFeatureClass = ((IFeatureWorkspace)targetWorkspaceDataset.Workspace).OpenFeatureClass(strTargetFtCls); } catch (Exception exp) { Hy.Common.Utility.Log.OperationalLogManager.AppendMessage(exp.ToString()); return null; } return TragetFeatureClass; }
/// <summary> /// 拷贝源表到目标空间 /// </summary> /// <param name="sourceWorkspace"></param> /// <param name="targetWorkspace"></param> /// <param name="SourceDataset"></param> /// <param name="nameOfTargetDataset"></param> public static bool ConvertTable(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, IDataset SourceDataset, string nameOfTargetDataset) { try { ITable pSourceTab = (ITable)SourceDataset; IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; IDatasetName sourceDatasetName = (IDatasetName)SourceDataset.FullName; IDataset targetWorkspaceDataset = (IDataset)targetWorkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; ITableName targetTableName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetTableName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = nameOfTargetDataset; IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; IFieldChecker fieldChecker = new FieldCheckerClass(); IFields targetFields; IFields sourceFields = pSourceTab.Fields; IEnumFieldError enumFieldError; fieldChecker.InputWorkspace = sourceWorkspace; fieldChecker.ValidateWorkspace = targetWorkspace; fieldChecker.Validate(sourceFields, out enumFieldError, out targetFields); if (enumFieldError == null) { IFeatureDataConverter fConverter = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = fConverter.ConvertTable(sourceDatasetName, queryFilter, targetDatasetName, pSourceTab.Fields, "", 1000, 0); } return true; } catch (Exception exp) { return false; } }
/// <summary> /// shape图层入库 /// </summary> /// <params name="sourceworkspace"></params> /// <params name="targetworkspace"></params> /// <params name="nameOfsourceFeatureClass"></params> /// <params name="nameOftargetFeatureClass"></params> /// <returns></returns> public IFeatureClass ShapeFileIntoGDB(IWorkspace sourceworkspace,IWorkspace targetworkspace, string nameOfsourceFeatureClass, string nameOftargetFeatureClass) { try { //创建源工作空间 IDataset sourceWorkspaceDataset = (IDataset)sourceworkspace; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; //创建源数据集 IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = nameOfsourceFeatureClass; //创建目标工作空间 IDataset targetWorkspaceDataset = (IDataset)targetworkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; //创建目标数据集 IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = nameOftargetFeatureClass; //源数据集的字段集 IName sourceName = (IName)sourceFeatureClassName; IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open(); //验证字段 IFieldChecker fieldChecker = new FieldCheckerClass(); IFields targetFeatureClassFields; IFields sourceFeatureClassFields = sourceFeatureClass.Fields; IEnumFieldError enumFieldError; //设置验证的对象 fieldChecker.InputWorkspace = sourceworkspace; fieldChecker.ValidateWorkspace = targetworkspace; fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields); //找到空间对象字段 IField geometryField; for (int i = 0; i < targetFeatureClassFields.FieldCount; i++) { if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry) { geometryField = targetFeatureClassFields.get_Field(i); //得到空间字段的定义 IGeometryDef geometryDef = geometryField.GeometryDef; //得到空间字段的索引 IGeometryDefEdit targetFCGeometryDefEdit = (IGeometryDefEdit)geometryDef; targetFCGeometryDefEdit.GridCount_2 = 1; targetFCGeometryDefEdit.set_GridSize(0, 0); //targetFCGeometryDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference; ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass(); ISpatialReference spatialReference = DataEditCommon.g_pMap.SpatialReference; //spatialReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_NAD1983UTM_20N); ISpatialReferenceResolution spatialReferenceResolution = (ISpatialReferenceResolution)spatialReference; spatialReferenceResolution.ConstructFromHorizon(); spatialReferenceResolution.SetDefaultXYResolution(); spatialReferenceResolution.SetDefaultZResolution(); ISpatialReferenceTolerance spatialReferenceTolerance = (ISpatialReferenceTolerance)spatialReference; spatialReferenceTolerance.SetMinimumXYTolerance(); spatialReferenceTolerance.SetMinimumZTolerance(); double XMin, XMax, YMin, YMax, ZMin, ZMax, MinXYTolerance, MinZTolerance; XMin = 4054.3603997438; XMax = 78088.6926632544; YMin = 14424.8510028409; YMax = 59609.4812606697; ZMin = 30330.1483519995; ZMax = 38389.3283520005; //spatialReference.SetDomain(XMin, XMax, YMin, YMax); //spatialReference.SetZDomain(ZMin, ZMax); MinXYTolerance = 0.000000000008219; MinZTolerance = 0.000000000007629; //spatialReferenceTolerance.SetMinimumZTolerance( MinXYTolerance); //spatialReference.GetDomain(out XMin, out XMax, out YMin, out YMax); //spatialReference.GetZDomain(out ZMin, out ZMax); targetFCGeometryDefEdit.SpatialReference_2 = spatialReference; //开始导入 IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; //导入所有的输入对象 IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass(); IEnumInvalidObject enumInvalidObject = featureDataConverter.ConvertFeatureClass(sourceFeatureClassName, queryFilter, null, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0); break; } } //导入后数据集的字段集 IName targetName = (IName)targetFeatureClassName; IFeatureClass targetFeatureClass = (IFeatureClass)targetName.Open(); return targetFeatureClass; } catch (Exception ex) { return null; } }
//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()); } }
public IFeatureClass exportFeatures(IFeatureClass inputFeatureClass, string outPath, ISpatialFilter filter) { // Create a name object for the source (shapefile) workspace and open it. IDataset inDset = (IDataset)inputFeatureClass; IWorkspace sourceWorkspace = (inDset).Workspace; // Create a name object for the target (file GDB) workspace and open it. string outDbStr = geoUtil.parseDbStr(outPath); string outName = System.IO.Path.GetFileName(outPath); IWorkspace targetWorkspace = geoUtil.OpenWorkSpace(outDbStr); outName = geoUtil.getSafeOutputNameNonRaster(targetWorkspace, outName); // Create a name object for the source dataset. IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName; sourceDatasetName.Name = inDset.Name; sourceDatasetName.WorkspaceName = (IWorkspaceName)((IDataset)sourceWorkspace).FullName; // Create a name object for the target dataset. IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.Name = outName; targetDatasetName.WorkspaceName = (IWorkspaceName)((IDataset)targetWorkspace).FullName; ; // Open source feature class to get field definitions. //IName sourceName = (IName)sourceFeatureClassName; IFeatureClass sourceFeatureClass = inputFeatureClass; // Create the objects and references necessary for field validation. IFieldChecker fieldChecker = new FieldCheckerClass(); IFields sourceFields = sourceFeatureClass.Fields; IFields targetFields = null; IEnumFieldError enumFieldError = null; // Set the required properties for the IFieldChecker interface. fieldChecker.InputWorkspace = sourceWorkspace; fieldChecker.ValidateWorkspace = targetWorkspace; // Validate the fields and check for errors. fieldChecker.Validate(sourceFields, out enumFieldError, out targetFields); if (enumFieldError != null) { // Handle the errors in a way appropriate to your application. Console.WriteLine("Errors were encountered during field validation."); } // Find the shape field. String shapeFieldName = sourceFeatureClass.ShapeFieldName; int shapeFieldIndex = sourceFeatureClass.FindField(shapeFieldName); IField shapeField = sourceFields.get_Field(shapeFieldIndex); // Get the geometry definition from the shape field and clone it. IGeometryDef geometryDef = shapeField.GeometryDef; IClone geometryDefClone = (IClone)geometryDef; IClone targetGeometryDefClone = geometryDefClone.Clone(); IGeometryDef targetGeometryDef = (IGeometryDef)targetGeometryDefClone; // Cast the IGeometryDef to the IGeometryDefEdit interface. IGeometryDefEdit targetGeometryDefEdit = (IGeometryDefEdit)targetGeometryDef; // Set the IGeometryDefEdit properties. targetGeometryDefEdit.GridCount_2 = 1; targetGeometryDefEdit.set_GridSize(0, 0.75); // Create the converter and run the conversion. IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass(); IEnumInvalidObject enumInvalidObject = featureDataConverter.ConvertFeatureClass(sourceFeatureClassName, filter, null, targetFeatureClassName, targetGeometryDef, targetFields, "", 1000, 0); // Check for errors. IInvalidObjectInfo invalidObjectInfo = null; enumInvalidObject.Reset(); while ((invalidObjectInfo = enumInvalidObject.Next()) != null) { // Handle the errors in a way appropriate to the application. Console.WriteLine("Errors occurred for the following feature: {0}", invalidObjectInfo.InvalidObjectID); } return (IFeatureClass)((IName)targetFeatureClassName).Open(); }
//将mdb中要素类转换复制到GDB数据库中 private void ImportMDBToGDB(string file, string outfilename) { m_success = false;//初始化 try { string filepath = file.Substring(0, file.LastIndexOf("---")); string filename = file.Substring(file.LastIndexOf("-") + 1); //打开mdb文件所在的工作空间 ESRI.ArcGIS.Geodatabase.IWorkspaceFactory wf = new AccessWorkspaceFactory(); IFeatureWorkspace pFeatureWorkspaceMDB = wf.OpenFromFile(@filepath, 0) as IFeatureWorkspace; IWorkspace pWorkspaceMDB = pFeatureWorkspaceMDB as IWorkspace; // 创建源工作空间名称 IDataset sourceWorkspaceDataset = (IDataset)pWorkspaceMDB; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; //创建源数据集名称 //IFeatureClassName sourceFeatureClassName = serverContext.CreateObject("esriGeoDatabase.FeatureClassName") as IFeatureClassName; IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = filename; //打开存在的工作空间,作为导入的空间 IWorkspaceFactory Pwf = new FileGDBWorkspaceFactoryClass(); //IWorkspace pWorkspaceGDB = Pwf.OpenFromFile(GetSourcePath(comboBoxSource.Text), 0); //IWorkspace2 pWorkspace2 = (IWorkspace2)(Pwf.OpenFromFile(GetSourcePath(comboBoxSource.Text), 0)); IWorkspace pWorkspaceGDB = GetWorkspace(comboBoxSource.Text); if (pWorkspaceGDB == null) { m_strErr = "数据源未找到"; m_success = false; return; } string username = GetSourceUser(comboBoxSource.Text); if (username.Trim() != "") { outfilename = username + "." + outfilename; } IWorkspace2 pWorkspace2 = pWorkspaceGDB as IWorkspace2; IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceGDB; //创建目标工作空间名称 IDataset targetWorkspaceDataset = (IDataset)pWorkspaceGDB; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; //创建目标数据集名称 // IFeatureClassName targetFeatureClassName = serverContext.CreateObject("esriGeoDatabase.FeatureClassName") as IFeatureClassName; //判断要素是否存在,若存在将删除源文件 if (pWorkspace2.get_NameExists(esriDatasetType.esriDTFeatureClass, outfilename)) { if (m_newfile == true) { IFeatureClass tmpfeatureclass; tmpfeatureclass = pFeatureWorkspace.OpenFeatureClass(outfilename); IDataset set = tmpfeatureclass as IDataset; fdr.DeleteSql(filename); set.CanDelete(); set.Delete(); } else { m_strErr = "存在相同文件名"; m_success = false; return; } } IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = outfilename; //打开输入的要素类以得到字段定义 ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName; IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open();//打开源要素类 //验证字段名称,因为你正在不同类型的工作空间之间进行数据转换 //IFieldChecker fieldChecker = serverContext.CreateObject("esriGeoDatabase.FieldChecker") as IFieldChecker; IFieldChecker fieldChecker = new FieldCheckerClass(); IFields sourceFeatureClassFields = sourceFeatureClass.Fields; IFields targetFeatureClassFields; IEnumFieldError enumFieldError; //最重要的设置输入和验证工作空间 fieldChecker.InputWorkspace = pWorkspaceMDB; fieldChecker.ValidateWorkspace = pWorkspaceGDB; fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields); //遍历所有输出字段找到几何字段 IField geometryField; for (int i = 0; i < targetFeatureClassFields.FieldCount; i++) { if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry) { geometryField = targetFeatureClassFields.get_Field(i); //得到几何字段的几何定义 IGeometryDef geometryDef = geometryField.GeometryDef; //赋予几何定义一个空间索引格网数目和格网大小值 IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef; targetFCGeoDefEdit.GridCount_2 = 1; targetFCGeoDefEdit.set_GridSize(0, 0); //允许ArcGIS为数据加载确定一个有效的格网大小 targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference; //转换要素类中所有的要素 //IQueryFilter queryFilter = serverContext.CreateObject("esriGeoDatabase.QueryFilter") as IQueryFilter; ; QueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; //加载要素类 //IFeatureDataConverter fctofc = serverContext.CreateObject("esriGeoDatabase.FeatureDataConverter") as IFeatureDataConverter; if (InsertIntoDatabase(outfilename))//更新数据库 { IFeatureDataConverter fctofc = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName, queryFilter, null, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0); } } } m_success = true; } catch { m_success = false; m_strErr = ""; } }
/// <summary> /// 拷贝源图层到空间数据集下的目标图层 /// </summary> /// <param name="sourceWorkspace"></param> /// <param name="targetWorkspace"></param> /// <param name="nameOfSourceFeatureClass"></param> /// <param name="nameOfTargetFeatureClass"></param> public static void ConvertFeatureClass(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureClass, string nameOfTargetFeatureClass) { try { //create source workspace name IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; //create source dataset name IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = nameOfSourceFeatureClass; //create target workspace name IDataset targetWorkspaceDataset = (IDataset)targetWorkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; //create target dataset name IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = nameOfTargetFeatureClass; //Open input Featureclass to get field definitions. IName sourceName = (IName)sourceFeatureClassName; IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open(); //Validate the field names because you are converting between different workspace types. IFieldChecker fieldChecker = new FieldCheckerClass(); IFields targetFeatureClassFields; IFields sourceFeatureClassFields = sourceFeatureClass.Fields; IEnumFieldError enumFieldError; // Most importantly set the input and validate workspaces! fieldChecker.InputWorkspace = sourceWorkspace; fieldChecker.ValidateWorkspace = targetWorkspace; fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields); // Loop through the output fields to find the geomerty field IField geometryField; for (int i = 0; i < targetFeatureClassFields.FieldCount; i++) { geometryField = targetFeatureClassFields.get_Field(i); if (geometryField.Type == esriFieldType.esriFieldTypeGeometry) { // Get the geometry field's geometry defenition IGeometryDef geometryDef = geometryField.GeometryDef; //Give the geometry definition a spatial index grid count and grid size IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef; //targetFCGeoDefEdit.GridCount_2 = 1; //targetFCGeoDefEdit.set_GridSize(0, 1000); //Allow ArcGIS to determine a valid grid size for the data loaded targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference; // we want to convert all of the features IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; IEnumDataset pEnumDataset = targetWorkspace.get_Datasets(esriDatasetType.esriDTFeatureDataset); IFeatureDataset ipDataset = (IFeatureDataset)pEnumDataset.Next(); IDatasetName pTargetDsName = (IDatasetName)ipDataset.FullName; // Load the feature class IFeatureDataConverter fctofc = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName, queryFilter, pTargetDsName as IFeatureDatasetName, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0); IInvalidObjectInfo obj = enumErrors.Next(); break; } } } catch (Exception exp) { Hy.Common.Utility.Log.OperationalLogManager.AppendMessage(exp.ToString()); return; } }
/// <summary> /// 将GDB的数据入到SDE中 xisheng 20110919 /// </summary> private void ImportGDBToSDE(string file, string outfilename, out int featurecount) { m_success = false;//初始化 try { string filepath = file.Substring(0, file.LastIndexOf("---")); string filename = file.Substring(file.LastIndexOf("-") + 1); //打开mdb文件所在的工作空间 ESRI.ArcGIS.Geodatabase.IWorkspaceFactory wf = new FileGDBWorkspaceFactory(); IFeatureWorkspace pFeatureWorkspaceGDB = wf.OpenFromFile(@filepath, 0) as IFeatureWorkspace; IWorkspace pWorkspaceGDB = pFeatureWorkspaceGDB as IWorkspace; // 创建源工作空间名称 IDataset sourceWorkspaceDataset = (IDataset)pFeatureWorkspaceGDB; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; //创建源数据集名称 //IFeatureClassName sourceFeatureClassName = serverContext.CreateObject("esriGeoDatabase.FeatureClassName") as IFeatureClassName; IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = filename; //打开存在的工作空间,作为导入的空间; IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pTargetworkspace; //创建目标工作空间名称 IDataset targetWorkspaceDataset = (IDataset)pTargetworkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; IWorkspace2 pWorkspace2 = pTargetworkspace as IWorkspace2; IFeatureDataset tmpfeaturedataset; //创建目标数据集名称 // IFeatureClassName targetFeatureClassName = serverContext.CreateObject("esriGeoDatabase.FeatureClassName") as IFeatureClassName; //判断要素是否存在,若存在将删除源文件 if (pWorkspace2.get_NameExists(esriDatasetType.esriDTFeatureDataset, textBox.Text)) { tmpfeaturedataset = pFeatureWorkspace.OpenFeatureDataset(textBox.Text); if (text_prj.Text != "") { IGeoDatasetSchemaEdit pgeodataset = tmpfeaturedataset as IGeoDatasetSchemaEdit; if (pgeodataset.CanAlterSpatialReference) { pgeodataset.AlterSpatialReference(GetSpatialReferenceformFile(text_prj.Text)); } } } else { tmpfeaturedataset = CreateFeatureDataset(pTargetworkspace as IFeatureWorkspace, textBox.Text, text_prj.Text); } if (pWorkspace2.get_NameExists(esriDatasetType.esriDTFeatureClass, outfilename)) { IFeatureClass tmpfeatureclass; tmpfeatureclass = pFeatureWorkspace.OpenFeatureClass(outfilename); IDataset tempset = tmpfeatureclass as IDataset; tempset.Delete(); } IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = outfilename; //目标数据集 IFeatureDatasetName outfeaturedatasetname = tmpfeaturedataset.FullName as IFeatureDatasetName; //打开输入的要素类以得到字段定义 ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName; IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open();//打开源要素类 //验证字段名称,因为你正在不同类型的工作空间之间进行数据转换 //IFieldChecker fieldChecker = serverContext.CreateObject("esriGeoDatabase.FieldChecker") as IFieldChecker; IFieldChecker fieldChecker = new FieldCheckerClass(); IFields sourceFeatureClassFields = sourceFeatureClass.Fields; IFields targetFeatureClassFields; IEnumFieldError enumFieldError; //最重要的设置输入和验证工作空间 fieldChecker.InputWorkspace = pWorkspaceGDB; fieldChecker.ValidateWorkspace = pTargetworkspace; fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields); //遍历所有输出字段找到几何字段 IField geometryField; for (int i = 0; i < targetFeatureClassFields.FieldCount; i++) { if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry) { geometryField = targetFeatureClassFields.get_Field(i); //得到几何字段的几何定义 IGeometryDef geometryDef = geometryField.GeometryDef; //赋予几何定义一个空间索引格网数目和格网大小值 IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef; targetFCGeoDefEdit.GridCount_2 = 1; targetFCGeoDefEdit.set_GridSize(0, 0); //允许ArcGIS为数据加载确定一个有效的格网大小 targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference; //转换要素类中所有的要素 //IQueryFilter queryFilter = serverContext.CreateObject("esriGeoDatabase.QueryFilter") as IQueryFilter; ; QueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; //加载要素类 //IFeatureDataConverter fctofc = serverContext.CreateObject("esriGeoDatabase.FeatureDataConverter") as IFeatureDataConverter; IFeatureDataConverter fctofc = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName, queryFilter, outfeaturedatasetname, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0); } } featurecount = sourceFeatureClass.FeatureCount(null); m_success = true; } catch (Exception ee) { m_success = false; m_strErr = ee.Message; featurecount = 0; } }
string m_strErr; //错误信息提醒 //SHP数据入库到GDB数据库的方法 private void ImportFeatureClassToNewWorkSpace(string filename, string outfilename) { //try //{ m_success = false; //初始化 string ImportShapeFileName = filename; string ExportFileShortName = outfilename; if (ImportShapeFileName == "") { return; } string ImportFileShortName = System.IO.Path.GetFileNameWithoutExtension(ImportShapeFileName); string ImportFilePath = System.IO.Path.GetDirectoryName(ImportShapeFileName); //打开存在的工作空间,作为导入的空间 IWorkspaceFactory Pwf = new FileGDBWorkspaceFactoryClass(); //IWorkspace pWorkspace = Pwf.OpenFromFile(GetSourcePath(comboBoxSource.Text), 0); // IWorkspace2 pWorkspace2 =(IWorkspace2)(Pwf.OpenFromFile(GetSourcePath(comboBoxSource.Text), 0)); IWorkspace pWorkspace = GetWorkspace(comboBoxSource.Text); if (pWorkspace == null) { m_strErr = "数据源未找到"; m_success = false; return; } string username = GetSourceUser(comboBoxSource.Text); if (username.Trim() != "") { ExportFileShortName = username + "." + ExportFileShortName; } IWorkspace2 pWorkspace2 = pWorkspace as IWorkspace2; //判断要素是否存在,若存在将删除源文件 if (pWorkspace2.get_NameExists(esriDatasetType.esriDTFeatureClass, ExportFileShortName)) { if (m_newfile == true) { IFeatureClass tmpfeatureclass; IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspace; tmpfeatureclass = pFeatureWorkspace.OpenFeatureClass(ExportFileShortName); IDataset set = tmpfeatureclass as IDataset; fdr.DeleteSql(ExportFileShortName); set.CanDelete(); set.Delete(); } else { //MessageBox.Show("存在相同文件名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); m_strErr = "存在相同文件名"; m_success = false; return; } } IWorkspaceName pInWorkspaceName; IFeatureDatasetName pOutFeatureDSName; IFeatureClassName pInFeatureClassName; IDatasetName pInDatasetName; IFeatureClassName pOutFeatureClassName; IDatasetName pOutDatasetName; long iCounter; IFields pOutFields, pInFields; IFieldChecker pFieldChecker; IField pGeoField; IGeometryDef pOutGeometryDef; IGeometryDefEdit pOutGeometryDefEdit; IName pName; IFeatureClass pInFeatureClass; IFeatureDataConverter pShpToClsConverter; IEnumFieldError pEnumFieldError = null; //得到一个输入SHP文件的工作空间, pInWorkspaceName = new WorkspaceNameClass(); pInWorkspaceName.PathName = ImportFilePath; pInWorkspaceName.WorkspaceFactoryProgID = "esriCore.ShapefileWorkspaceFactory.1"; //创建一个新的要素类名称,目的是为了以来PNAME接口的OPEN方法打开SHP文件 pInFeatureClassName = new FeatureClassNameClass(); pInDatasetName = (IDatasetName)pInFeatureClassName; pInDatasetName.Name = ImportFileShortName; pInDatasetName.WorkspaceName = pInWorkspaceName; //打开一个SHP文件,将要读取它的字段集合 pName = (IName)pInFeatureClassName; pInFeatureClass = (IFeatureClass)pName.Open(); //通过FIELDCHECKER检查字段的合法性,为输入要素类获得字段集合 pInFields = pInFeatureClass.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; } } //得到几何字段的几何定义 pOutGeometryDef = pGeoField.GeometryDef; //设置几何字段的空间参考和网格 pOutGeometryDefEdit = (IGeometryDefEdit)pOutGeometryDef; pOutGeometryDefEdit.GridCount_2 = 1; pOutGeometryDefEdit.set_GridSize(0, 1500000); //创建一个新的要素类名称作为可用的参数 pOutFeatureClassName = new FeatureClassNameClass(); pOutDatasetName = (IDatasetName)pOutFeatureClassName; pOutDatasetName.Name = ExportFileShortName; //创建一个新的数据集名称作为可用的参数 pOutFeatureDSName = (IFeatureDatasetName) new FeatureDatasetName(); //如果参数的值是NULL,说明要创建独立要素类 //创建一个不存在的要素集合pFDN,通过它将IFeatureClassName和工作空间连接起来,而ConvertFeatureClass函数并不使用该变量作为参数, IFeatureDatasetName pFDN = new FeatureDatasetNameClass(); IDatasetName pDN = (IDatasetName)pFDN; IDataset pDS = (IDataset)pWorkspace; pDN.WorkspaceName = (IWorkspaceName)pDS.FullName; pOutFeatureClassName.FeatureDatasetName = (IDatasetName)pFDN; //将pOutFeatureDSName设置为Null,将它做为参数给ConvertFeatureClass函数,因为IFeatureClassName本身已经和工作空间关联了,生成的 //要素类在工作空间的根目录下,即独立要素类 pOutFeatureDSName = null; //开始导入 if (InsertIntoDatabase(ExportFileShortName)) { pShpToClsConverter = new FeatureDataConverterClass(); pShpToClsConverter.ConvertFeatureClass(pInFeatureClassName, null, pOutFeatureDSName, pOutFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0); //MessageBox.Show("导入成功", "提示"); m_success = true; } //} //catch //{ // m_success = false; //} }
/// <summary> /// 拷贝源图层到空间数据集下的目标图层 /// </summary> /// <param name="sourceWorkspace"></param> /// <param name="targetWorkspace"></param> /// <param name="nameOfSourceFeatureClass"></param> /// <param name="nameOfTargetFeatureClass"></param> public static void ConvertFeatureClass(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureClass, string nameOfTargetFeatureClass) { try { //create source workspace name IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; //create source dataset name IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = nameOfSourceFeatureClass; //create target workspace name IDataset targetWorkspaceDataset = (IDataset)targetWorkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; //create target dataset name IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = nameOfTargetFeatureClass; //Open input Featureclass to get field definitions. IName sourceName = (IName)sourceFeatureClassName; IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open(); //Validate the field names because you are converting between different workspace types. IFieldChecker fieldChecker = new FieldCheckerClass(); IFields targetFeatureClassFields; IFields sourceFeatureClassFields = sourceFeatureClass.Fields; IEnumFieldError enumFieldError; // Most importantly set the input and validate workspaces! fieldChecker.InputWorkspace = sourceWorkspace; fieldChecker.ValidateWorkspace = targetWorkspace; fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields); // Loop through the output fields to find the geomerty field IField geometryField; for (int i = 0; i < targetFeatureClassFields.FieldCount; i++) { geometryField = targetFeatureClassFields.get_Field(i); if (geometryField.Type == esriFieldType.esriFieldTypeGeometry) { // Get the geometry field's geometry defenition IGeometryDef geometryDef = geometryField.GeometryDef; //Give the geometry definition a spatial index grid count and grid size IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef; //targetFCGeoDefEdit.GridCount_2 = 1; //targetFCGeoDefEdit.set_GridSize(0, 1000); //Allow ArcGIS to determine a valid grid size for the data loaded targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference; // we want to convert all of the features IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; IEnumDataset pEnumDataset = targetWorkspace.get_Datasets(esriDatasetType.esriDTFeatureDataset); IFeatureDataset ipDataset = (IFeatureDataset)pEnumDataset.Next(); IDatasetName pTargetDsName = (IDatasetName)ipDataset.FullName; // Load the feature class IFeatureDataConverter fctofc = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName, queryFilter, pTargetDsName as IFeatureDatasetName, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0); IInvalidObjectInfo obj = enumErrors.Next(); break; } } } catch (Exception exp) { Hy.Common.Utility.Log.OperationalLogManager.AppendMessage(exp.ToString()); return; } }
public void IFeatureDataConverter(IWorkspace pSourceWorkspace, IWorkspace pTargetWorkspace, string pSFeatureClass, string pDFeatureClass) { IDataset sourceWorkspaceDataset = (IDataset)pSourceWorkspace; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = pSFeatureClass; IDataset targetWorkspaceDataset = (IDataset)pTargetWorkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = pDFeatureClass; ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName; IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open(); IFieldChecker fieldChecker = new FieldCheckerClass(); IFields targetFeatureClassFields; IFields sourceFeatureClassFields = sourceFeatureClass.Fields; IEnumFieldError enumFieldError; fieldChecker.InputWorkspace = pSourceWorkspace; fieldChecker.ValidateWorkspace = pTargetWorkspace; fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields); IField geometryField; for (int i = 0; i < targetFeatureClassFields.FieldCount; i++) { if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry) { geometryField = targetFeatureClassFields.get_Field(i); IGeometryDef geometryDef = geometryField.GeometryDef; IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef; targetFCGeoDefEdit.GridCount_2 = 1; targetFCGeoDefEdit.set_GridSize(0, 0); targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference; IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; IFeatureDataConverter fctofc = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName, queryFilter, null, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0); break; } } }
/// <summary> /// /// </summary> /// <param name="pSourceFeatureClass"></param> /// <param name="_pTWorkspaceFactory"></param> /// <param name="_pTWs"></param> /// <param name="_pTName"></param> public void ConvertQuery2FeatureClass( IFeatureClass pSourceFeatureClass , IWorkspaceFactory _pTWorkspaceFactory, String _pTWs, string _pTName) { IWorkspace pTWorkspace = _pTWorkspaceFactory.OpenFromFile(_pTWs, 0); IDataset pSDataset = pSourceFeatureClass as IDataset; IFeatureClassName pSourceFeatureClassName = pSDataset.FullName as IFeatureClassName; IDataset pTDataset = (IDataset)pTWorkspace; IName pTDatasetName = pTDataset.FullName; IWorkspaceName pTargetWorkspaceName = (IWorkspaceName)pTDatasetName; IFeatureClassName pTargetFeatureClassName = new FeatureClassNameClass(); IDatasetName pTargetDatasetName = (IDatasetName)pTargetFeatureClassName; // 验证要素类的名称是否合法,但是并没有对这个名称是否存在做检查 string pTableName = null; IFieldChecker pNameChecker = new FieldCheckerClass(); pNameChecker.ValidateWorkspace = pTWorkspace; int pFlag = pNameChecker.ValidateTableName(_pTName, out pTableName); pTargetDatasetName.Name = pTableName; pTargetDatasetName.WorkspaceName = pTargetWorkspaceName; // 创建字段检查对象 IFieldChecker pFieldChecker = new FieldCheckerClass(); IFields sourceFields = pSourceFeatureClass.Fields; IFields pTargetFields = null; IEnumFieldError pEnumFieldError = null; pFieldChecker.InputWorkspace = ((IDataset)pSourceFeatureClass).Workspace; pFieldChecker.ValidateWorkspace = pTWorkspace; // 验证字段 pFieldChecker.Validate(sourceFields, out pEnumFieldError, out pTargetFields); if (pEnumFieldError != null) { // 报错提示 Console.WriteLine("Errors were encountered during field validation."); } String pShapeFieldName = pSourceFeatureClass.ShapeFieldName; int pFieldIndex = pSourceFeatureClass.FindField(pShapeFieldName); IField pShapeField = sourceFields.get_Field(pFieldIndex); IGeometryDef pTargetGeometryDef = pShapeField.GeometryDef; // 创建要素转换对象 IFeatureDataConverter pFDConverter = new FeatureDataConverterClass(); IEnumInvalidObject pEnumInvalidObject = pFDConverter.ConvertFeatureClass(pSourceFeatureClassName, null, null, pTargetFeatureClassName, pTargetGeometryDef, pTargetFields, "", 1000, 0); // 检查是否有错误 IInvalidObjectInfo pInvalidInfo = null; pEnumInvalidObject.Reset(); while ((pInvalidInfo = pEnumInvalidObject.Next()) != null) { // 报错提示. Console.WriteLine("Errors occurred for the following feature: {0}", pInvalidInfo.InvalidObjectID); } }
/// <summary> /// performs a simple copy of database object /// </summary> /// <param name="parentObjClass">the parent object</param> /// <param name="outWks">out Workspace</param> /// <returns>the new feature object</returns> public IObjectClass simpleCopyObj(object parentObjClass, IWorkspace outWks) { IObjectClass outCls = null; IQueryFilter qyf = new QueryFilterClass(); IFeatureDataConverter2 fdConv2 = new FeatureDataConverterClass(); IDataset dSet = (IDataset)parentObjClass; esriDatasetType pDsetType = dSet.Type; string bn = dSet.BrowseName; string sbn = bn.Substring(bn.LastIndexOf(".")+1); IDatasetName dSetN = (IDatasetName)dSet.FullName; IDatasetName dSetN2; IWorkspaceName wksN = (IWorkspaceName)((IDataset)outWks).FullName; IFields pfs; if (pDsetType == esriDatasetType.esriDTTable) { ITableName tbn = new TableNameClass(); dSetN2 = (IDatasetName)tbn; dSetN2.Name = sbn; dSetN2.WorkspaceName = wksN; pfs = (IFields)((IClone)((IObjectClass)parentObjClass).Fields).Clone(); updateDomains(ref pfs, outWks); fdConv2.ConvertTable(dSetN,qyf, null, dSetN2, pfs, "",1000, 0); outCls = (IObjectClass)((IName)dSetN2).Open(); } else if (pDsetType == esriDatasetType.esriDTFeatureClass) { IFeatureClass pFtCls = (IFeatureClass)dSet; IFeatureClassName fcn = new FeatureClassNameClass(); IFeatureDatasetName fdn = new FeatureDatasetNameClass(); IDatasetName dSetN3 = (IDatasetName)fdn; dSetN3.WorkspaceName = wksN; IFeatureDataset pFtDs = pFtCls.FeatureDataset; if (pFtDs == null) { dSetN3 = null; fdn = null; } else { string ftDN = pFtCls.FeatureDataset.Name; string ftDNs = ftDN.Substring(ftDN.LastIndexOf(".") + 1); dSetN3.Name = ftDNs; } dSetN2 = (IDatasetName)fcn; dSetN2.Name = sbn; dSetN2.WorkspaceName = wksN; IField pSf = pFtCls.Fields.get_Field(pFtCls.FindField(pFtCls.ShapeFieldName)); pfs = (IFields)((IClone)pFtCls.Fields).Clone(); updateDomains(ref pfs, outWks); fdConv2.ConvertFeatureClass(dSetN, qyf, null, fdn, fcn, (IGeometryDef)((IClone)pSf.GeometryDef).Clone(), pfs, "", 1000, 0); outCls = (IObjectClass)((IName)dSetN2).Open(); } else { } return outCls; }