private bool CopyDirectly()//ref IWorkspace wsSource) { // VCT数据不允许直接复制 if (m_DataType == enumDataType.VCT) { SendMessage(enumMessageType.Exception, "导入VCT数据不允许直接复制"); return(false); } // MDB使用文件复制 if (m_DataType == enumDataType.PGDB) { try { string strDBPath = this.m_TargetPath + "\\" + this.m_TargetName; System.IO.File.Copy(this.m_Datasource, strDBPath); //if (!Hy.Common.Utility.Esri.AEAccessFactory.OpenPGDB(ref wsSource, strDBPath)) //{ // SendMessage(enumMessageType.Exception, "导入数据(复制文件)后打开出错,请确认数据源为正确的PGDB文件"); // return false; //} } catch { SendMessage(enumMessageType.Exception, "导入数据(复制文件)出错"); return(false); } } else // Shp和FileGDB使用文件夹复制 { try { if (m_DataType == enumDataType.SHP) { CopyDirectory(this.m_Datasource, this.m_TargetPath, this.m_TargetName); } else { Hy.Common.Utility.Esri.GPTool gpTool = new Hy.Common.Utility.Esri.GPTool(); gpTool.Copy(this.m_Datasource, this.m_TargetPath + "\\" + this.m_TargetName); } //wsSource = AEAccessFactory.OpenWorkspace(m_DataType, this.m_TargetPath + "\\" + this.m_TargetName); } catch { SendMessage(enumMessageType.Exception, "导入数据(复制文件夹)出错"); return(false); } } return(true); }
/// <summary> /// (从Base库)生成Query库 /// 默认的实现是从Base库导入 /// </summary> /// <returns></returns> protected virtual bool GenerateQuery(IWorkspace wsBase) { string strWorkspace = this.m_TargetPath + "\\" + COMMONCONST.DB_Name_Query; IWorkspace wsQuery = null; if (!Hy.Common.Utility.Esri.AEAccessFactory.OpenPGDB(ref wsQuery, strWorkspace)) { SendMessage(enumMessageType.Exception, "导入数据失败:无法打开Query库,请确认在创建任务文件结构时已创建Query库"); return(false); } //Hy.Common.Utility.Esri.AEAccessFactory.CreatePGDB(this.m_TargetPath, COMMONCONST.DB_Name_Query); //IFeatureDataset fdsTarget = CreateFeatureDataset(wsQuery, this.m_SpatialReference); Hy.Common.Utility.Esri.GPTool gpTool = new Hy.Common.Utility.Esri.GPTool(); // 打开数据源 if (wsBase == null) { SendMessage(enumMessageType.Exception, "创建Query库出错:无法打开Base库"); return(false); } // 获取FeatureClass名列表 IEnumDataset enDataset = wsBase.get_Datasets(esriDatasetType.esriDTAny); IDataset dataset = enDataset.Next(); while (dataset != null) { switch (dataset.Type) { case esriDatasetType.esriDTTable: case esriDatasetType.esriDTFeatureClass: SendEvent(dataset.Name); Hy.Common.Utility.Esri.DataConverter.ConvertTable(wsBase, wsQuery, dataset, GetObjectName(dataset.Name)); break; case esriDatasetType.esriDTFeatureDataset: IFeatureClassContainer fcContianer = dataset as IFeatureClassContainer; for (int i = 0; i < fcContianer.ClassCount; i++) { IDataset dsSub = fcContianer.get_Class(i) as IDataset; SendEvent(dsSub.Name); Hy.Common.Utility.Esri.DataConverter.ConvertTable(wsBase, wsQuery, dsSub, GetObjectName(dsSub.Name)); } break; default: break; } dataset = enDataset.Next(); } // 释放 enDataset = null; dataset = null; System.Runtime.InteropServices.Marshal.ReleaseComObject(wsBase); System.Runtime.InteropServices.Marshal.ReleaseComObject(wsQuery); wsBase = null; wsQuery = null; GC.Collect(); return(true); }
/// <summary> /// (从数据源)导入到Base库,并返回Base库Workspace对象 /// </summary> /// <param name="wsSource"></param> /// <returns></returns> protected virtual bool ImportToBase(ref IWorkspace wsBase) { // 直接复制的方式 if (m_JustCopy) { return(CopyToBase(ref wsBase)); } // 导入的方式 string strWorkspace = this.m_TargetPath + "\\" + COMMONCONST.DB_Name_Base; if (!Hy.Common.Utility.Esri.AEAccessFactory.OpenFGDB(ref wsBase, strWorkspace)) { SendMessage(enumMessageType.Exception, "导入数据失败:无法打开Base库,请确认在创建任务文件结构时已创建Base库"); return(false); }// Hy.Common.Utility.Esri.AEAccessFactory.CreateFGDB(this.m_TargetPath, COMMONCONST.DB_Name_Base); IFeatureDataset fdsTarget = CreateFeatureDataset(wsBase, this.m_SpatialReference); if (fdsTarget == null) { SendMessage(enumMessageType.Exception, "“Dataset”没有创建成功,无法继续导入"); return(false); } Hy.Common.Utility.Esri.GPTool gpTool = new Hy.Common.Utility.Esri.GPTool(); // 打开数据源 IWorkspace wsSource = null; try { wsSource = AEAccessFactory.OpenWorkspace(this.m_DataType, this.m_Datasource); if (wsSource == null) { SendMessage(enumMessageType.Exception, "打开数据源出错"); return(false); } // 获取FeatureClass名列表 IEnumDataset enDataset = wsSource.get_Datasets(esriDatasetType.esriDTAny); IDataset dataset = enDataset.Next(); while (dataset != null) { switch (dataset.Type) { case esriDatasetType.esriDTTable: SendEvent(dataset.Name); Hy.Common.Utility.Esri.DataConverter.ConvertTable(wsSource, wsBase, dataset, GetObjectName(dataset.Name)); break; case esriDatasetType.esriDTFeatureClass: SendEvent(dataset.Name); gpTool.CopyFeatureClass(string.Format("{0}\\{1}", this.m_Datasource, dataset.Name + (this.m_DataType == enumDataType.SHP ? ".shp" : "")), string.Format("{0}\\{1}\\{2}", strWorkspace, fdsTarget.Name, GetObjectName(dataset.Name))); //Hy.Common.Utility.Esri.DataConverter.ConvertFeatureClass(wsSource as IDataset,fdsTarget as IDataset, dataset as IFeatureClass, GetObjectName(dataset.Name)); break; case esriDatasetType.esriDTFeatureDataset: IFeatureClassContainer fsContainer = dataset as IFeatureClassContainer; for (int i = 0; i < fsContainer.ClassCount; i++) { string strFcName = (fsContainer.get_Class(i) as IDataset).Name; SendEvent(strFcName); gpTool.CopyFeatureClass(string.Format("{0}\\{1}\\{2}", this.m_Datasource, dataset.Name, strFcName), string.Format("{0}\\{1}\\{2}", strWorkspace, fdsTarget.Name, GetObjectName(strFcName))); //Hy.Common.Utility.Esri.DataConverter.ConvertFeatureClass(dataset, fdsTarget as IDataset, fsContainer.get_Class(i), (fsContainer.get_Class(i) as IDataset).Name); } break; default: break; } dataset = enDataset.Next(); } // 释放 enDataset = null; dataset = null; // 改别名 this.RenameClassObjects(wsBase); } catch (Exception exp) { SendMessage(enumMessageType.Exception, "导入Base库出错:" + exp.ToString()); } finally { if (wsSource != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(wsSource); wsSource = null; } } return(true); }
private bool CopyToBase(ref IWorkspace wsBase) { // VCT数据不允许直接复制 if (m_DataType == enumDataType.VCT) { SendMessage(enumMessageType.Exception, "导入VCT数据不允许直接复制"); return(false); } //if (ImportingObjectChanged != null) //{ // ImportingObjectChanged("数据复制"); //} // MDB使用文件复制 if (m_DataType == enumDataType.PGDB) { try { string strDBPath = this.m_TargetPath + "\\Base.MDB"; System.IO.File.Copy(this.m_Datasource, strDBPath);//+ COMMONCONST.DB_Name_Base); if (!Hy.Common.Utility.Esri.AEAccessFactory.OpenPGDB(ref wsBase, strDBPath)) { SendMessage(enumMessageType.Exception, "导入数据(复制文件)后打开出错,请确认数据源为正确的PGDB文件"); return(false); } } catch { SendMessage(enumMessageType.Exception, "导入数据(复制文件)出错"); return(false); } } else // Shp和FileGDB使用文件夹复制 { try { string strFolderName = "Base"; if (m_DataType == enumDataType.SHP) { strFolderName = "Base"; CopyDirectory(this.m_Datasource, this.m_TargetPath, strFolderName); } else { strFolderName = COMMONCONST.DB_Name_Base; Hy.Common.Utility.Esri.GPTool gpTool = new Hy.Common.Utility.Esri.GPTool(); gpTool.Copy(this.m_Datasource, this.m_TargetPath + "\\" + strFolderName); } wsBase = AEAccessFactory.OpenWorkspace(m_DataType, this.m_TargetPath + "\\" + strFolderName); } catch { SendMessage(enumMessageType.Exception, "导入数据(复制文件夹)出错"); return(false); } } return(true); }
//ref IWorkspace wsSource) private bool CopyDirectly() { // VCT数据不允许直接复制 if (m_DataType == enumDataType.VCT) { SendMessage(enumMessageType.Exception, "导入VCT数据不允许直接复制"); return false; } // MDB使用文件复制 if (m_DataType == enumDataType.PGDB) { try { string strDBPath = this.m_TargetPath + "\\"+this.m_TargetName; System.IO.File.Copy(this.m_Datasource, strDBPath); //if (!Hy.Common.Utility.Esri.AEAccessFactory.OpenPGDB(ref wsSource, strDBPath)) //{ // SendMessage(enumMessageType.Exception, "导入数据(复制文件)后打开出错,请确认数据源为正确的PGDB文件"); // return false; //} } catch { SendMessage(enumMessageType.Exception, "导入数据(复制文件)出错"); return false; } } else // Shp和FileGDB使用文件夹复制 { try { if (m_DataType == enumDataType.SHP) { CopyDirectory(this.m_Datasource, this.m_TargetPath, this.m_TargetName); } else { Hy.Common.Utility.Esri.GPTool gpTool = new Hy.Common.Utility.Esri.GPTool(); gpTool.Copy(this.m_Datasource, this.m_TargetPath + "\\" + this.m_TargetName); } //wsSource = AEAccessFactory.OpenWorkspace(m_DataType, this.m_TargetPath + "\\" + this.m_TargetName); } catch { SendMessage(enumMessageType.Exception, "导入数据(复制文件夹)出错"); return false; } } return true; }
/// <summary> /// 导入 /// </summary> public virtual bool Import() { IWorkspace wsTarget = null; try { // 直接复制的方式 if (m_JustCopy) { return CopyDirectly();//ref wsSource); } // 导入的方式 string strWorkspace = this.m_TargetPath + "\\" + this.m_TargetName; if (!Hy.Common.Utility.Esri.AEAccessFactory.OpenFGDB(ref wsTarget, strWorkspace)) { SendMessage(enumMessageType.Exception, "导入数据失败:无法打开目标库,请确认目标库已经创建"); return false; } IFeatureDataset fdsTarget = CreateFeatureDataset(wsTarget, this.m_SpatialReference); if (fdsTarget == null) { SendMessage(enumMessageType.Exception, "“Dataset”没有创建成功,无法继续导入"); return false; } Hy.Common.Utility.Esri.GPTool gpTool = new Hy.Common.Utility.Esri.GPTool(); // 打开数据源 try { IWorkspace wsSource = AEAccessFactory.OpenWorkspace(this.m_DataType, this.m_Datasource); if (wsSource == null) { SendMessage(enumMessageType.Exception, "打开数据源出错"); return false; } // 获取FeatureClass名列表 IEnumDataset enDataset = wsSource.get_Datasets(esriDatasetType.esriDTAny); IDataset dataset = enDataset.Next(); while (dataset != null) { switch (dataset.Type) { case esriDatasetType.esriDTTable: SendEvent(dataset.Name); Hy.Common.Utility.Esri.DataConverter.ConvertTable(wsSource, wsTarget, dataset, GetObjectName(dataset.Name)); break; case esriDatasetType.esriDTFeatureClass: SendEvent(dataset.Name); gpTool.CopyFeatureClass(string.Format("{0}\\{1}", this.m_Datasource, dataset.Name + (this.m_DataType == enumDataType.SHP ? ".shp" : "")), string.Format("{0}\\{1}\\{2}", strWorkspace, fdsTarget.Name, GetObjectName(dataset.Name))); break; case esriDatasetType.esriDTFeatureDataset: IFeatureClassContainer fsContainer = dataset as IFeatureClassContainer; for (int i = 0; i < fsContainer.ClassCount; i++) { string strFcName = (fsContainer.get_Class(i) as IDataset).Name; SendEvent(strFcName); gpTool.CopyFeatureClass(string.Format("{0}\\{1}\\{2}", this.m_Datasource, dataset.Name, strFcName), string.Format("{0}\\{1}\\{2}", strWorkspace, fdsTarget.Name, GetObjectName(strFcName))); } break; default: break; } dataset = enDataset.Next(); } // 释放 enDataset = null; dataset = null; } catch (Exception exp) { SendMessage(enumMessageType.Exception, "导入数据出错:" + exp.ToString()); return false; } finally { if (wsTarget != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(wsTarget); wsTarget = null; } } return true; } catch (Exception exp) { SendMessage(enumMessageType.Exception, "数据导入失败,错误信息:" + exp.Message); return false; } }
private bool CopyToBase(ref IWorkspace wsBase) { // VCT数据不允许直接复制 if (m_DataType == enumDataType.VCT) { SendMessage(enumMessageType.Exception, "导入VCT数据不允许直接复制"); return false; } //if (ImportingObjectChanged != null) //{ // ImportingObjectChanged("数据复制"); //} // MDB使用文件复制 if (m_DataType == enumDataType.PGDB) { try { string strDBPath = this.m_TargetPath + "\\Base.MDB"; System.IO.File.Copy(this.m_Datasource, strDBPath);//+ COMMONCONST.DB_Name_Base); if (!Hy.Common.Utility.Esri.AEAccessFactory.OpenPGDB(ref wsBase, strDBPath)) { SendMessage(enumMessageType.Exception, "导入数据(复制文件)后打开出错,请确认数据源为正确的PGDB文件"); return false; } } catch { SendMessage(enumMessageType.Exception, "导入数据(复制文件)出错"); return false; } } else // Shp和FileGDB使用文件夹复制 { try { string strFolderName = "Base"; if (m_DataType == enumDataType.SHP) { strFolderName = "Base"; CopyDirectory(this.m_Datasource, this.m_TargetPath, strFolderName); } else { strFolderName = COMMONCONST.DB_Name_Base; Hy.Common.Utility.Esri.GPTool gpTool = new Hy.Common.Utility.Esri.GPTool(); gpTool.Copy(this.m_Datasource, this.m_TargetPath + "\\" + strFolderName); } wsBase =AEAccessFactory.OpenWorkspace(m_DataType, this.m_TargetPath + "\\" + strFolderName); } catch { SendMessage(enumMessageType.Exception, "导入数据(复制文件夹)出错"); return false; } } return true; }
/// <summary> /// (从Base库)生成Query库 /// 默认的实现是从Base库导入 /// </summary> /// <returns></returns> protected virtual bool GenerateQuery(IWorkspace wsBase) { string strWorkspace = this.m_TargetPath + "\\" + COMMONCONST.DB_Name_Query; IWorkspace wsQuery = null; if (!Hy.Common.Utility.Esri.AEAccessFactory.OpenPGDB(ref wsQuery, strWorkspace)) { SendMessage(enumMessageType.Exception, "导入数据失败:无法打开Query库,请确认在创建任务文件结构时已创建Query库"); return false; } //Hy.Common.Utility.Esri.AEAccessFactory.CreatePGDB(this.m_TargetPath, COMMONCONST.DB_Name_Query); //IFeatureDataset fdsTarget = CreateFeatureDataset(wsQuery, this.m_SpatialReference); Hy.Common.Utility.Esri.GPTool gpTool = new Hy.Common.Utility.Esri.GPTool(); // 打开数据源 if (wsBase == null) { SendMessage(enumMessageType.Exception, "创建Query库出错:无法打开Base库"); return false; } // 获取FeatureClass名列表 IEnumDataset enDataset = wsBase.get_Datasets(esriDatasetType.esriDTAny); IDataset dataset = enDataset.Next(); while (dataset != null) { switch (dataset.Type) { case esriDatasetType.esriDTTable: case esriDatasetType.esriDTFeatureClass: SendEvent(dataset.Name); Hy.Common.Utility.Esri.DataConverter.ConvertTable(wsBase, wsQuery, dataset, GetObjectName(dataset.Name)); break; case esriDatasetType.esriDTFeatureDataset: IFeatureClassContainer fcContianer = dataset as IFeatureClassContainer; for (int i = 0; i < fcContianer.ClassCount; i++) { IDataset dsSub = fcContianer.get_Class(i) as IDataset; SendEvent(dsSub.Name); Hy.Common.Utility.Esri.DataConverter.ConvertTable(wsBase, wsQuery, dsSub, GetObjectName(dsSub.Name)); } break; default: break; } dataset = enDataset.Next(); } // 释放 enDataset = null; dataset = null; System.Runtime.InteropServices.Marshal.ReleaseComObject(wsBase); System.Runtime.InteropServices.Marshal.ReleaseComObject(wsQuery); wsBase = null; wsQuery = null; GC.Collect(); return true; }
private bool PrepareForTable(IDataset dsTarget, out string strBaseName, out string strQueryName) { strQueryName = strBaseName = "Base.mdb"; try { AEAccessFactory.CreateFGDB(TempPath, strBaseName); IWorkspace wsBase = AEAccessFactory.OpenWorkspace(enumDataType.FileGDB, string.Format("{0}\\{1}", TempPath, strBaseName)); GPTool gpTool = new GPTool(); DataConverter.ConvertTable(dsTarget.Workspace, wsBase, dsTarget, dsTarget.Name); return true; } catch { return false; } }