/// <summary> /// Polulate the projection tree based on the EPSG file. /// </summary> private void PopulateList() { treeView.Nodes.Clear(); using (Stream s = File.OpenRead(MapUtils.GetPROJ_LIB() + "\\epsg")) { using (StreamReader reader = new StreamReader(s)) { string line; string name = ""; string proj4 = ""; int row = 0; while ((line = reader.ReadLine()) != null) { ++row; if (line.StartsWith("#")) { if (proj4 != "") { // adding the previous section string[] names = name.Split(new string[] { " / " }, StringSplitOptions.None); string[] proj_defs = proj4.Split(new char[] { '<', '>' }); if (proj_defs.Length > 3) { if (names.Length > 1) { AddListItem(names[0].Trim(), names[1].Trim(), proj_defs[1].Trim(), proj_defs[2].Trim()); } else { if (proj_defs[2].Contains("longlat")) { AddListItem("Longitude-Latitude", names[0].Trim(), proj_defs[1].Trim(), proj_defs[2].Trim()); } else { AddListItem("Other Non Geographic", names[0].Trim(), proj_defs[1].Trim(), proj_defs[2].Trim()); } } } } proj4 = ""; name = line.Substring(1).Trim(); } else { proj4 += line; } } } } treeView.Sort(); }
/// <summary> /// Polulate the projection tree based on the EPSG file. /// </summary> private void PopulateList() { treeView.Nodes.Clear(); using (Stream s = File.OpenRead(MapUtils.GetPROJ_LIB() + "\\epsg")) { using (StreamReader reader = new StreamReader(s)) { string line; string name = ""; string proj4 = ""; int row = 0; while ((line = reader.ReadLine()) != null) { ++row; if (line.StartsWith("#")) { if (proj4 != "") { ProcessLine(name, proj4); } proj4 = ""; name = line.Substring(1).Trim(); } else { proj4 += line; } } //process last line if (proj4 != "" && name != "") { ProcessLine(name, proj4); } } } treeView.Sort(); }
/// <summary> /// This function tries to find out the projection from the EPSG file based on the name or the proj4 parameters /// </summary> /// <param name="projection">input projection in proj.4 fromat</param> /// <param name="proj4">the matching projection in the epsg file</param> /// <param name="proj4">epsg</param> /// <returns>Projection Name</returns> public static string FindProjection(string projection, out string proj4, out int epsg) { string[] def = null; if (projection.Contains("+proj")) { def = projection.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); } // todo: epsg based search string projName = ""; proj4 = ""; epsg = 0; using (Stream s = File.OpenRead(MapUtils.GetPROJ_LIB() + "\\epsg")) { using (StreamReader reader = new StreamReader(s)) { string line; int i; int minrank = 100; string line2 = ""; while ((line = reader.ReadLine()) != null) { int rank = 0; if (def != null) { // proj4 based search for (i = 0; i < def.Length; i++) { if (!line.Contains(def[i])) { if (def[i].StartsWith("+proj")) { break; } if (def[i].StartsWith("+ellps")) { break; } if (def[i].StartsWith("+zone")) { break; } if (def[i].StartsWith("+datum")) { break; } if (def[i].StartsWith("+units")) { break; } if (def[i].StartsWith("+south")) { break; } if (def[i].StartsWith("+north")) { break; } else { ++rank; } } } if (i == def.Length) { if (rank < minrank) { minrank = rank; projName = line2.Substring(2); proj4 = line; if (rank == 0) { // found break; } } } } else { // name based search if (line.Contains(projection)) { break; } } if (line.StartsWith("#")) { line2 = line; } } } } string[] proj_defs = proj4.Split(new char[] { '<', '>' }); if (proj_defs.Length >= 3) { proj4 = proj_defs[2].Trim(); int.TryParse(proj_defs[1].Trim(), out epsg); } if (projName != "") { if (projName.Contains(" / ")) { return(projName); } if (proj4.Contains("longlat")) { return("Longitude-Latitude / " + projName); } else { return("Other Non Geographic / " + projName); } } else { return(projection); } }
/// <summary> /// Refresh the layer list by submitting a GetCapabilities request. /// </summary> public void LoadLayers() { //Create the XmlDocument. doc = new XmlDocument(); doc.XmlResolver = null; //serverURL = textBoxServer.Text.Trim().Split(new char[] { '?' })[0]; serverURL = textBoxServer.Text.Trim(); //steph: remove the check as it force user to encode \ in their URL (map=c:\data\mymap.map) //if url is not valid, let the app report it //if (!Uri.IsWellFormedUriString(serverURL, UriKind.Absolute)) //{ // MessageBox.Show("The specified server URL is invalid", // "MapManager", MessageBoxButtons.OK, MessageBoxIcon.Error); // return; //} while (true) { try { this.Cursor = Cursors.WaitCursor; XmlTextReader reader; if (serverURL.Contains("?")) { reader = new XmlTextReader(serverURL + "&SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.3.0"); } else { reader = new XmlTextReader(serverURL + "?SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.3.0"); } reader.Namespaces = false; reader.XmlResolver = resolver; doc.Load(reader); break; } catch (WebException wex) { if (wex.Status == WebExceptionStatus.ProtocolError) { // Get HttpWebResponse to check the HTTP status code. HttpWebResponse httpResponse = (HttpWebResponse)wex.Response; if (httpResponse.StatusCode == HttpStatusCode.Unauthorized || httpResponse.StatusCode == HttpStatusCode.ProxyAuthenticationRequired) { CredentialsForm form = new CredentialsForm("Authentication required for " + serverURL, resolver); if (form.ShowDialog(this) == DialogResult.OK) { continue; } return; } } MessageBox.Show("Unable to load WMS capabilities of the layer, " + wex.Message, "MapManager", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } catch (Exception ex) { MessageBox.Show("Unable to load WMS capabilities of the layer, " + ex.Message, "MapManager", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } finally { this.Cursor = Cursors.Default; } } // server version XmlAttribute att = doc.DocumentElement.Attributes["version"]; wms_server_version = "1.1.1"; if (att != null) { wms_server_version = att.Value.Trim(); } if (!wms_server_version.StartsWith("1.0") && !wms_server_version.StartsWith("1.1")) { wms_server_version = "1.1.1"; } // load supported image types comboBoxImageFormat.Items.Clear(); foreach (XmlNode node in doc.SelectNodes("//Request/GetMap/Format")) { int index = comboBoxImageFormat.Items.Add(node.InnerText.Trim()); if (comboBoxImageFormat.Items[index].ToString() == map.outputformat.mimetype || (comboBoxImageFormat.SelectedIndex < 0 && map.outputformat.mimetype.StartsWith(comboBoxImageFormat.Items[index].ToString())) || comboBoxImageFormat.Items[index].ToString().ToLower().StartsWith("image/png")) { comboBoxImageFormat.SelectedIndex = index; } } // load epsg values Hashtable epsg = new Hashtable(); using (Stream s = File.OpenRead(MapUtils.GetPROJ_LIB() + "\\epsg")) { using (StreamReader reader = new StreamReader(s)) { string line; string projName = ""; while ((line = reader.ReadLine()) != null) { if (line.StartsWith("#") && line.Length > 2) { projName = line.Substring(2); } else if (line.StartsWith("<")) { string[] items = line.Split(new char[] { '<', '>' }, StringSplitOptions.RemoveEmptyEntries); if (items.Length > 0) { epsg.Add("EPSG:" + items[0], projName); } } } } } // load projections Dictionary <string, string> projections = new Dictionary <string, string>(); string selectedProj = null; foreach (XmlNode srs in doc.SelectNodes("//CRS | //SRS")) { string[] srs2 = srs.InnerText.Split(); foreach (string s in srs2) { if (!projections.ContainsKey(s)) { if (epsg.ContainsKey(s)) { projections.Add(s, epsg[s].ToString()); } else { projections.Add(s, s); } if (s.Contains("EPSG:4326")) { selectedProj = epsg[s].ToString(); } } } } sortedProj = new List <KeyValuePair <string, string> >(projections); bs = new BindingSource(); bs.DataSource = sortedProj; comboBoxProj.DataSource = bs; UpdateProjBinding(); if (selectedProj != null) { comboBoxProj.SelectedValue = selectedProj; } if (comboBoxImageFormat.SelectedIndex < 0 && comboBoxImageFormat.Items.Count > 0) { comboBoxImageFormat.SelectedIndex = 0; } // ensure default imageindices are invalid if (treeViewLayers.ImageList != null) { treeViewLayers.ImageIndex = treeViewLayers.ImageList.Images.Count; treeViewLayers.SelectedImageIndex = treeViewLayers.ImageList.Images.Count; } // set up the treeView treeViewLayers.BeginUpdate(); treeViewLayers.Nodes.Clear(); listViewLayers.Items.Clear(); foreach (XmlNode node in doc.SelectNodes("//Capability/Layer")) { AddLayerNode(treeViewLayers.Nodes, node); } treeViewLayers.EndUpdate(); treeViewLayers.ExpandAll(); if (treeViewLayers.Nodes.Count > 0) { treeViewLayers.Nodes[0].EnsureVisible(); } }