Пример #1
0
        /// <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();
        }
Пример #2
0
        /// <summary>
        /// Click event handler of the buttonProxy control.
        /// </summary>
        /// <param name="sender">The source object of this event.</param>
        /// <param name="e">The event parameters.</param>
        private void buttonConnection_Click(object sender, EventArgs e)
        {
            CredentialsForm form = new CredentialsForm("Specify Connection Settings", resolver);

            form.ShowDialog(this);
        }
Пример #3
0
 /// <summary>
 /// Click event handler of the buttonProxy control.
 /// </summary>
 /// <param name="sender">The source object of this event.</param>
 /// <param name="e">The event parameters.</param>
 private void buttonConnection_Click(object sender, EventArgs e)
 {
     CredentialsForm form = new CredentialsForm("Specify Connection Settings", resolver);
     form.ShowDialog(this);
 }
Пример #4
0
        /// <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();
            }
        }