using System; using System.Collections.Generic; using System.Text; using System.Drawing; using GeoScene.Data; using GeoScene.Engine; using GeoScene.Globe; using System.IO; using System.Windows.Forms; using System.Collections; using System.Data; using System.Xml; using System.Net.NetworkInformation; using System.Net; using System.Security.Cryptography; namespace Cyberpipe { public class Utility { //public static string ConnectionString; public static string dbdatabase; public static string userID; public static Hashtable Pipe_QueryFields; public static string DBPassword; public static string DBServer; public static string DBBackUp; public static string serverip; public static int serverport; public static string localicenseserverip; public static int localicenseserverport; public static GeoScene.Engine.GSODataSource dataSource; public static List<LayerChild> LayerChilds; public static ArrayList LayerLabels; public static ArrayList LayerNames; public static ArrayList TerrainsName; public static Hashtable LayerLabel_LayerIDs; public static Hashtable Query_Fields = new Hashtable(); public static Hashtable Pipe_Code = new Hashtable(); public static List<PipelineType> listPipelineType; public static string PicRootURL; public static string PicDefaultURL; public static double dFlyAboveLine = 2; public static string userName; public static string projectStr; //传感器配置 public static string sensorConnectString; public static string sensorDatabase; public static List<MarkerLayer> sensorMarkerLayers; public static void SetBallons(GSOBalloon featureTooltip, GSOBalloonEx balloonEx) { featureTooltip.CacheFilePath = Application.StartupPath + "/GeoScene/Globe/Temp"; featureTooltip.SetSize(EnumSizeIndex.ROUNDED_CX, 0); featureTooltip.SetSize(EnumSizeIndex.ROUNDED_CY, 0); featureTooltip.SetSize(EnumSizeIndex.MARGIN_CX, 3); featureTooltip.SetSize(EnumSizeIndex.MARGIN_CY, 3); featureTooltip.SetSize(EnumSizeIndex.ANCHOR_HEIGHT, 0); featureTooltip.SetSize(EnumSizeIndex.ANCHOR_WIDTH, 0); featureTooltip.SetSize(EnumSizeIndex.ANCHOR_MARGIN, 0); featureTooltip.SetSize(EnumSizeIndex.ANCHOR_OFFSET_CX, 1); featureTooltip.SetSize(EnumSizeIndex.ANCHOR_OFFSET_CY, -1); featureTooltip.SetDirection(EnumToolTipDirection.TD_BOTTOMEDGE_LEFT); featureTooltip.EscapeSequencesEnabled = true; featureTooltip.HyperlinkEnabled = true; featureTooltip.Opaque = 30; featureTooltip.MaxWidth = 300; featureTooltip.SetShadow(0, 0, 50, true, 0, 0); balloonEx.SetSize(EnumSizeIndexEx.CONTENT_CX, 500); balloonEx.SetSize(EnumSizeIndexEx.CONTENT_CY, 300); balloonEx.Opaque = 5; balloonEx.SetBorder(Color.White, 1, 1); balloonEx.SetColorBkType(EnumBkColorTypeEx.SKY); } public static GSOFeature FindFeatureByUserID(GSOFeatureDataset featdataset, string fieldname, string value) { GSOFeatures feats = featdataset.GetAllFeatures(); for (int i = 0; i < feats.Length; i++) { GSOFeature feat = feats[i]; if (feat.GetFieldAsString(fieldname) == value) { return feat; } } return null; } public static GeoScene.Data.GSOPoint2d XYZ_2_Latlon(double x, double y) { int id = GeoScene.Data.GSOProjectManager.AddProject(Utility.projectStr); // GeoScene.Data.GSOProjectManager.SetCurProject(id); GeoScene.Data.GSOPoint2d pt2d = new GeoScene.Data.GSOPoint2d(x, y); GeoScene.Data.GSOPoint2d result = GeoScene.Data.GSOProjectManager.Inverse(pt2d, id); return result; } public static GeoScene.Data.GSOPoint2d Latlon_2_XYZ(double lon, double lat) { int id = GeoScene.Data.GSOProjectManager.AddProject(Utility.projectStr); GeoScene.Data.GSOPoint2d pt2d = new GeoScene.Data.GSOPoint2d(lon, lat); GeoScene.Data.GSOPoint2d result = GeoScene.Data.GSOProjectManager.Forward(pt2d, id); return result; } /// <summary> /// MD5加密 /// </summary> /// <param name="pToEncrypt">要加密字符串</param> /// <returns></returns> public static string MD5Encrypt(string pToEncrypt) { string sKey = "12345678";//默认8位加密密匙 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } ret.ToString(); return ret.ToString(); } public static GSOFeatures Table2Features(DataTable table, string currentQueryLayer, GeoScene.Globe.GSOGlobeControl globeControl1) { GSOFeatures features = new GSOFeatures(); for (int i = 0; i < table.Rows.Count; i++) { DataRow row = table.Rows[i]; string featureName = row["编号"].ToString(); featureName = featureName.Trim(); //GSOLayer layer = globeControl1.Globe.Layers.GetLayerByID((int)(Utility.LayerLabel_LayerIDs[currentQueryLayer])); GSOLayer layer = globeControl1.Globe.Layers.GetLayerByCaption(currentQueryLayer); GSOFeatures tempfeatures = layer.GetFeatureByName(featureName, false); if (tempfeatures.Length > 0) { features.Add(tempfeatures[0]); } } return features; } public static string filename = Application.StartupPath + "\\Config.xml"; public static string filenameSensor = Application.StartupPath + "\\sensorConfig.xml"; public static string filenamePipelineType = Application.StartupPath + "\\PipelineType.xml"; //程序启动的时候读取配置文件 public static void SetParams() { #region 读取 filename "Config.xml" Utility.LayerChilds = new List<LayerChild>(); Utility.LayerLabels = new System.Collections.ArrayList(); Utility.TerrainsName = new System.Collections.ArrayList(); Utility.LayerNames = new System.Collections.ArrayList(); Utility.LayerLabel_LayerIDs = new System.Collections.Hashtable(); if (File.Exists(filename)) { XmlTextReader XmlReader = new XmlTextReader(filename); try { while (XmlReader.Read()) { if (XmlReader.Name == "database") { Utility.dbdatabase = XmlReader.ReadElementString(); } else if (XmlReader.Name == "dbpassword") { Utility.DBPassword = XmlReader.ReadElementString(); } else if (XmlReader.Name == "user") { Utility.userID = XmlReader.ReadElementString(); } else if (XmlReader.Name == "locaserverip") { Utility.serverip = XmlReader.ReadElementString(); } else if (XmlReader.Name == "locaserverport") { Utility.serverport = Convert.ToInt32(XmlReader.ReadElementString()); } else if (XmlReader.Name == "localicenseserverip") { Utility.localicenseserverip = XmlReader.ReadElementString(); } else if (XmlReader.Name == "localicenseserverport") { Utility.localicenseserverport = Convert.ToInt32(XmlReader.ReadElementString()); } else if (XmlReader.Name == "dbbackuppath") { Utility.DBBackUp = XmlReader.ReadElementString(); } else if (XmlReader.Name == "layerchild") { LayerChild child = new LayerChild(); child.attriLabel = XmlReader["label"].Trim(); child.attriLayer = XmlReader["layer"].Trim(); child.attriType = XmlReader["type"].Trim(); Utility.LayerChilds.Add(child); Utility.LayerLabels.Add(XmlReader["label"].Trim()); Utility.LayerNames.Add(XmlReader["layer"].Trim()); } else if (XmlReader.Name == "Terrain") { Utility.TerrainsName.Add(XmlReader.ReadElementString()); } else if (XmlReader.Name == "dbserver") { Utility.DBServer = XmlReader.ReadElementString(); } else if (XmlReader.Name == "rooturl") { Utility.PicRootURL = XmlReader.ReadElementString(); } else if (XmlReader.Name == "defaultimgurl") { Utility.PicDefaultURL = XmlReader.ReadElementString(); } else if (XmlReader.Name == "queryfield") { Utility.Query_Fields.Add(XmlReader["label"], XmlReader.ReadElementString()); } else if (XmlReader.Name == "pipelineCode") { Utility.Pipe_Code.Add(XmlReader["label"], XmlReader.ReadElementString()); } else if (XmlReader.Name == "projectStr") { Utility.projectStr = XmlReader.ReadElementString(); } } } catch (Exception ex) { //LogError.PublishError(ex); } finally { XmlReader.Close(); } } else { MessageBox.Show("配置文件" + filename + "不存在!", "提示"); } #endregion #region 读取 filenameSensor "sensorConfig.xml" sensorMarkerLayers = new List<MarkerLayer>(); if (File.Exists(filenameSensor)) { XmlTextReader XmlReader = new XmlTextReader(filenameSensor); try { while (XmlReader.Read()) { if (XmlReader.Name == "ConnectString") { string dbserver = XmlReader["dbserver"].Trim(); string dbserverport = XmlReader["dbserverport"].Trim(); string database = XmlReader["database"].Trim(); string username = XmlReader["username"].Trim(); string password = XmlReader["password"].Trim(); Utility.sensorDatabase = database; Utility.sensorConnectString = "Server=" + dbserver + ";Port=" + dbserverport + ";Database=" + database + ";Uid=" + username + ";Pwd=" + password + ""; } else if (XmlReader.Name == "MarkerLayer") { MarkerLayer markerLayer = new MarkerLayer(); string layerName = XmlReader["layerName"].Trim(); string tableName = XmlReader["tableName"].Trim(); markerLayer.layerName = layerName; markerLayer.tableName = tableName; Dictionary<string, string> fields = new Dictionary<string, string>(); XmlReader.ReadSubtree(); while (XmlReader.Read()) { if (XmlReader.Name == "field") { string name = XmlReader["name"].Trim(); string label = XmlReader["label"].Trim(); fields.Add(name, label); } else if (XmlReader.Name == "MarkerLayer") { break; } } markerLayer.fields = fields; sensorMarkerLayers.Add(markerLayer); } } } catch (Exception ex) { //LogError.PublishError(ex); } finally { XmlReader.Close(); } } else { MessageBox.Show("配置文件" + filenameSensor + "不存在!", "提示"); } #endregion #region 读取 filenamePipelineType "PipelineType.xml" listPipelineType = new List<PipelineType>(); if (File.Exists(filenamePipelineType)) { XmlTextReader XmlReader = new XmlTextReader(filenamePipelineType); try { while (XmlReader.Read()) { if (XmlReader.Name == "PipelineCode") { string pipelinecode = XmlReader["code"] == null ? null : XmlReader["code"].Trim(); string pipelinename = XmlReader["name"] == null ? null : XmlReader["name"].Trim(); string pipelinetype = XmlReader["type"] == null ? null : XmlReader["type"].Trim(); if (pipelinecode != null && pipelinename != null && pipelinetype != null) { PipelineType pipelineType = new PipelineType(pipelinecode, pipelinename, pipelinetype); if (listPipelineType.Contains(pipelineType) == false) { listPipelineType.Add(pipelineType); } } } } } catch (Exception ex) { //LogError.PublishError(ex); } finally { XmlReader.Close(); } } else { MessageBox.Show("配置文件" + filenamePipelineType + "不存在!", "提示"); } #endregion } //判断网络是否连接 public static bool isNetworkConnectionSuccess(string ipAdress) { if (ipAdress == null) { return false; } if (ipAdress.Contains("\\")) { ipAdress = ipAdress.Substring(0, ipAdress.LastIndexOf('\\')); } if (ipAdress.Trim() == "localhost" || ipAdress.Trim() == "127.0.0.1" || ipAdress.Trim() == Dns.GetHostName()) { return true; } Ping ping = new Ping(); try { PingReply pr = ping.Send(ipAdress.Trim(), 3000); if (pr.Status == IPStatus.Success) { return true; } else { return false; } } catch (Exception ex) { LogError.PublishError(ex); return false; } } //删除内存图层中指定名称的feature public static void RemoveFeatureFromName(GSOGlobeControl globeControl1, string featureName) { GSOFeatures features = globeControl1.Globe.MemoryLayer.GetFeatureByName(featureName, true); if (features != null) { for (int i = features.Length - 1; i >= 0; i--) { GSOFeature feature = features[i]; if (feature != null) { globeControl1.Globe.MemoryLayer.RemoveFeatureByID(feature.ID); } } } globeControl1.Refresh(); } } }