管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2024-04-24 de78cc06152f9954f62d74cf451358dcf817e655
TEWin/FrmWin.cs
@@ -182,6 +182,8 @@
                    SG.Open(this.fly); break;
                case 0xd8: // 加载shp
                    LoadShp(); break;
                case 0xd9: // 读取shp
                    ReadShp(); break;
            }
        }
@@ -497,6 +499,7 @@
            RunExe(GetAbsolutePath("osgblabPath"), null, false);
        }
        #region 加载shp + 读取shp
        private void LoadShp()
        {
            OpenFileDialog dialog = new OpenFileDialog();
@@ -505,20 +508,33 @@
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                LoadShp(dialog.FileName);
                string shpFile = dialog.FileName;
                if (LoadShp(shpFile))
                {
                    sendShpFile(shpFile);
                }
            }
        }
        private void LoadShp(string shpFile)
        private bool LoadShp(string shpFile)
        {
            string gid = GetGroupIdByName(shpGroupName);
            string conn = "FileName=" + shpFile + ";TEPlugName=OGR;"; // 连接字符串
            string shpName = Path.GetFileNameWithoutExtension(shpFile);
            try
            {
                string gid = GetGroupIdByName(shpGroupName);
                string conn = "FileName=" + shpFile + ";TEPlugName=OGR;"; // 连接字符串
                string shpName = Path.GetFileNameWithoutExtension(shpFile);
            IFeatureLayer71 layer = SG.Creator.CreateFeatureLayer(shpName, conn, gid); // 加载shp
            layer.Refresh();
                IFeatureLayer71 layer = SG.Creator.CreateFeatureLayer(shpName, conn, gid); // 加载shp
                layer.Refresh();
                SG.Navigate.FlyTo(layer.ID);
            SG.Navigate.FlyTo(layer.ID);
                return true;
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                return false;
            }
        }
        private string GetGroupIdByName(string groupName)
@@ -531,5 +547,58 @@
            return gid;
        }
        private void sendShpFile(string shpFile)
        {
            try
            {
                string shpTxtPath = GetShpTxtPath();
                using (StreamWriter sw = new StreamWriter(shpTxtPath, false))
                {
                    sw.Write(shpFile.Replace("\\", "/"));
                }
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
            }
        }
        private void ReadShp()
        {
            try
            {
                string shpTxtPath = GetShpTxtPath();
                if (string.IsNullOrEmpty(shpTxtPath) || !File.Exists(shpTxtPath))
                {
                    return;
                }
                string shpFile = File.ReadAllText(shpTxtPath);
                if (string.IsNullOrEmpty(shpFile) || !shpFile.ToLower().EndsWith(".shp") || !File.Exists(shpFile))
                {
                    return;
                }
                LoadShp(shpFile);
                File.Delete(shpTxtPath);
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
            }
        }
        private string GetShpTxtPath()
        {
            string shpTxtPath = ConfigurationManager.AppSettings["shpTxtPath"];
            if (!shpTxtPath.Contains(":\\"))
            {
                shpTxtPath = Path.Combine(Application.StartupPath, shpTxtPath);
            }
            return shpTxtPath;
        }
        #endregion
    }
}