Ejemplo n.º 1
0
        //<CSCM>
        //********************************************************************************
        //** 函 数 名: GetPDBOrGDBWorkspace
        //** 版    权: CopyRight (C)
        //** 创 建 人: 杨旭斌
        //** 功能描述: 根据用户提供的PDB的全路径,获得对应的MDB的工作空间
        //** 创建日期:
        //** 修 改 人:
        //** 修改日期:
        //** 修改时间: 20070818
        //** 参数列表: sPDBOrFDBRouteAndName (String)pdb或FDB的全路径,即包括文件名称
        //**           bCreateFDB标识是否是创建FDB
        //** 版    本:1.0
        //*********************************************************************************
        //</CSCM>
        public static IWorkspace GetPDBOrGDBWorkspace(string sPDBOrFDBRouteAndName, bool bCreateFDB)
        {
            IWorkspace functionReturnValue = default(IWorkspace);

            try
            {
                string            sFilePath = null;
                string            sFileName = null;
                IWorkspaceFactory pWorkspaceFactory;
                IWorkspace        pWorkspace = default(IWorkspace);



                if (string.IsNullOrEmpty(Strings.Trim(sPDBOrFDBRouteAndName)))
                {
                    functionReturnValue = null;
                    return(functionReturnValue);
                }

                if (bCreateFDB)
                {
                    pWorkspaceFactory = new FileGDBWorkspaceFactoryClass();
                }
                else
                {
                    pWorkspaceFactory = new AccessWorkspaceFactoryClass();
                }

                //获得文件名
                sFileName = Microsoft.VisualBasic.FileIO.FileSystem.GetName(sPDBOrFDBRouteAndName);
                //20110723  HSh 注释因为文件名称未加后缀名".gdb"而不能创建的问题
                sFileName = sFileName.Substring(0, sFileName.Length - 4);

                //获得路径
                sFilePath = Microsoft.VisualBasic.FileIO.FileSystem.GetParentPath(sPDBOrFDBRouteAndName);
                //如果路径不存在,则创建
                if (Microsoft.VisualBasic.FileIO.FileSystem.DirectoryExists(sFilePath) == false)
                {
                    Microsoft.VisualBasic.FileIO.FileSystem.CreateDirectory(sFilePath);
                }
                //判断MDB文件是否存在,如果不存在的话,则创建
                if (bCreateFDB == false)
                {
                    if (Microsoft.VisualBasic.FileIO.FileSystem.FileExists(sPDBOrFDBRouteAndName) == false)
                    {
                        pWorkspaceFactory.Create(sFilePath, sFileName, null, 0);
                    }
                }
                else
                {
                    if (Microsoft.VisualBasic.FileIO.FileSystem.DirectoryExists(sPDBOrFDBRouteAndName) == false)
                    {
                        pWorkspaceFactory.Create(sFilePath, sFileName, null, 0);
                    }
                }
                pWorkspace = pWorkspaceFactory.OpenFromFile(sPDBOrFDBRouteAndName, 0);

                ClsPublicFunction.DeleteAllFeatCls(pWorkspace);

                functionReturnValue = pWorkspace;
                return(functionReturnValue);
            }
            catch
            {
                functionReturnValue = null;
                return(functionReturnValue);
            }
        }
Ejemplo n.º 2
0
        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;
        }