//获得提取的工作区间 private IWorkspace GetWorkspace() { IWorkspace functionReturnValue = default(IWorkspace); string sFileRoute = null; string sFileStyle = null; IWorkspace pWorkspace = default(IWorkspace); sFileRoute = this.txtSaveFileRoute.Text; sFileStyle = this.cmbFileStyle.Text; if (string.IsNullOrEmpty(sFileRoute.Trim())) { //g_clsErrorHandle.DisplayInformation("请设置提取路径!", false); MessageBoxEx.Show("请设置提取路径!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); functionReturnValue = null; return(functionReturnValue); } pWorkspace = null; if (sFileStyle == "SHP") { pWorkspace = ClsCreatAndGetDataset.CreatOrGetSHPWorkspace(sFileRoute); } else if (sFileStyle == "FDB") { pWorkspace = ClsCreatAndGetDataset.GetPDBOrGDBWorkspace(sFileRoute, true); } else if (sFileStyle == "PDB") { pWorkspace = ClsCreatAndGetDataset.GetPDBOrGDBWorkspace(sFileRoute, false); } functionReturnValue = pWorkspace; return(functionReturnValue); }
private void Deal() { IWorkspace pWorkspace = default(IWorkspace); string sDesFeatClsName = null; IFeatureClass pDesFeatCls = default(IFeatureClass); string sInfo = null; ISpatialReference pSpatRef = null; int i = 0; string sTxtPath = ""; bool bExist = false; //判断Log文件夹是否存在 //印骅 20081201 是否选择了要素类 if (dgvData.Rows.Count == 0) { //g_clsErrorHandle.DisplayInformation("没有添加任何数据,请添加数据!", false); MessageBoxEx.Show("没有添加任何数据,请添加数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //是否进行投影转换,只有选择路径和投影参数以后才可转换 if (m_bHasSpatRef == false) { if (string.IsNullOrEmpty(txtSaveFileRoute.Text.Trim())) { MessageBoxEx.Show("请设置提取路径!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { //g_clsErrorHandle.DisplayInformation("没有设置投影参数!请选择*.prj文件!", false); MessageBoxEx.Show("没有设置投影参数!请选择*.prj文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } pWorkspace = GetWorkspace(); if (pWorkspace == null) { return; } this.ProgressBar.Visible = true; // 创建PrjLog.txt到bin\Log文件夹 sTxtPath = System.Windows.Forms.Application.StartupPath.Replace("\\bin", ""); bExist = Directory.Exists(sTxtPath + "\\Log"); if (!bExist) { Directory.CreateDirectory(sTxtPath + "\\Log"); } FileStream fsStream = new FileStream(sTxtPath + "\\Log" + "\\PrjLog.txt", FileMode.Append, FileAccess.Write); StreamWriter swWriter = new StreamWriter(fsStream); swWriter.WriteLine("下列要素类不能投影到指定坐标系,转换失败,要素类创建错误!"); swWriter.WriteLine("记录数" + Strings.Chr(9) + "要素类路径"); ////批量投影转换并提取 for (i = 0; i <= m_PathCollection.Count - 1; i++) { //// 获得要素类 m_pFeatureCls = (IFeatureClass)m_FeatClsCollection[i]; sDesFeatClsName = m_pFeatureCls.AliasName; sInfo = "当前操作层:" + m_pFeatureCls.AliasName; this.lblInfo.Text = sInfo; Application.DoEvents(); if (!string.IsNullOrEmpty(sDesFeatClsName.Trim())) { if (m_pFeatureCls != null) { this.lblInfo.Text = sInfo + "正在获得目标要素类,请稍候...."; lblInfo.Refresh(); Application.DoEvents(); ////读prj,获得空间参考 if (m_bHasSpatRef == true) { pSpatRef = ClsPublicFunction.LoadPRJ(txtPreDefinedPrj.Text); if (sDesFeatClsName.Contains(".")) { int dotIndex = 0; dotIndex = sDesFeatClsName.IndexOf("."); sDesFeatClsName = sDesFeatClsName.Substring(dotIndex + 1, sDesFeatClsName.Length - dotIndex - 1); } IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspace; IFields pFields = m_pFeatureCls.Fields; pDesFeatCls = ClsCreatAndGetDataset.CreatOrOpenFeatClsByName(ref pFeatureWorkspace, sDesFeatClsName, ref pFields, pSpatRef, null); pWorkspace = (IWorkspace)pFeatureWorkspace; //m_pFeatureCls.Fields = pFields; } else { if (sDesFeatClsName.Contains(".")) { int dotIndex = 0; dotIndex = sDesFeatClsName.IndexOf("."); sDesFeatClsName = sDesFeatClsName.Substring(dotIndex + 1, sDesFeatClsName.Length - dotIndex - 1); } IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspace; IFields pFields = m_pFeatureCls.Fields; pDesFeatCls = ClsCreatAndGetDataset.CreatOrOpenFeatClsByName(ref pFeatureWorkspace, sDesFeatClsName, ref pFields, null, null); pWorkspace = (IWorkspace)pFeatureWorkspace; //m_pFeatureCls.Fields = pFields; } if (pDesFeatCls != null) { //如果不剪切的,则使用该方法,需要剪切,则使用选择集,分位于内部要素和位于外部要素或直接使用pfeatcls搜索二遍 //如果数据量大的话,搜索二遍的方法是不可行的 m_lResult = 0; if (m_bHasSpatRef == true) { m_lResult = ClsCreateFeat.OutPutFeat(ref pDesFeatCls, ref m_pFeatureCls, "", null, false, ref pSpatRef, null, ref this.ProgressBar, ref this.lblInfo, sInfo); } else { ISpatialReference pSpatialReference = null; ClsCreateFeat.OutPutFeat(ref pDesFeatCls, ref m_pFeatureCls, "", null, false, ref pSpatialReference, null, ref this.ProgressBar, ref this.lblInfo, sInfo); } // 投影转换失败则将日志写入PrjLog.txt if (m_lResult == -1) { m_bAllTransferDone = false; swWriter.WriteLine(Convert.ToInt32(m_iTNum).ToString() + Strings.Chr(9).ToString() + Strings.Chr(9).ToString() + m_PathCollection[i].ToString()); m_iTNum += 1; } //// 更新featureclass的范围 ISchemaLock schemaLock = default(ISchemaLock); schemaLock = (ISchemaLock)pDesFeatCls; IFeatureClassManage featureClassManage = default(IFeatureClassManage); featureClassManage = (IFeatureClassManage)pDesFeatCls; featureClassManage.UpdateExtent(); schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } } } } lblInfo.Text = "投影转换完成"; m_iTNum = 1; //要素类记录数归1 this.Refresh(); Application.DoEvents(); this.ProgressBar.Visible = false; MessageBoxEx.Show("投影转换完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtSaveFileRoute.Text = ""; //写入转换完成时间 DateTime Now = DateTime.Now; swWriter.Write("转换完成时间:" + Strings.Chr(9)); swWriter.Write(Strings.Chr(9) + Now.ToString() + Constants.vbCrLf); swWriter.WriteLine(); swWriter.Close(); fsStream.Close(); // 有转换不成功时,弹出提示信息 if (m_bAllTransferDone == false) { MessageBoxEx.Show("有要素类投影转换失败!详情请查看\"Log\\PrjLog.txt\"!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } m_bAllTransferDone = true; }