/// <summary> /// Function to add to map serivce both IMS and WMS /// </summary> ///<param name="msi">The map service info</param> private void addMapServiceLayer(MapServiceInfo msi) { if (msi.ServiceType.Equals("wms", StringComparison.OrdinalIgnoreCase)) { if (!addLayerWMS2(msi, false)) addLayerWMS(msi, false); // MessageBox.Show("WMS service not supported in this version.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { addLayerArcIMS(msi); // MessageBox.Show("ArcIMS service not supported in this version.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
/// <summary> /// Adds WMS layer to map /// </summary> /// <param name="msi">map service information</param> /// <param name="fromServerUrl">service url</param> /// <returns></returns> private bool addLayerWMS2(MapServiceInfo msi, Boolean fromServerUrl) { bool flag = false; if (msi == null) { throw new ArgumentNullException("msi"); } string service = msi.Service; string url = AppendQuestionOrAmpersandToUrlString(msi.Server); // append serviceParam to server url? if (msi.ServiceParam.Length > 0 && !fromServerUrl) { url = url + msi.ServiceParam; url = AppendQuestionOrAmpersandToUrlString(url); } CswClient client = new CswClient(); string response = client.SubmitHttpRequest("GET", url + "request=GetCapabilities&service=WMS", ""); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(response); XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xmlDoc.NameTable); xmlnsManager.AddNamespace("wms", "http://www.opengis.net/wms"); XmlNodeList nl = null; if (xmlDoc.SelectSingleNode("//wms:Layer", xmlnsManager) != null) { nl = xmlDoc.SelectNodes("/wms:WMS_Capabilities/wms:Capability/wms:Layer/wms:Layer/wms:Title", xmlnsManager); } if (nl != null) { flag = true; for (int i = nl.Count - 1; i >= 0; i--) { AGXD.ServiceConnectionProperties conn = new AGXD.ServiceConnectionProperties (AGXD.ServiceType.Wms, new Uri(url), "", nl.Item(i).InnerText); AGXM.ServiceLayer sl = new AGXM.ServiceLayer(conn); bool connected = sl.Connect(); if (connected) { addLayer(sl); addedLayer.Add(DateTime.Now.Millisecond, sl); } } } return flag; }
/// <summary> /// Function to add ArcIMS map serivce /// </summary> ///<param name="msi">The map service info</param> private void addLayerArcIMS(MapServiceInfo msi) { if (msi == null) { throw new ArgumentNullException("msi"); } //IIMSConnection imsConnection; try { AGXD.ServiceConnectionProperties sc = new AGXD.ServiceConnectionProperties(); sc.ServiceType = (AGXD.ServiceType)Enum.Parse(typeof(AGXD.ServiceType), "Ims"); AGXM.ServiceLayer resultLayer = new AGXM.ServiceLayer(); sc.ServiceName = msi.Service; sc.SubServiceName = ""; sc.Url = new System.Uri(msi.Server); // resultLayer.Username = ""; // resultLayer.Password = ""; //resultLayer.Name = "Layer"; // resultLayer.Copyright = ""; resultLayer.Visible = true; resultLayer.ServiceConnectionProperties = sc; resultLayer.Connect(); addLayer(resultLayer); addedLayer.Add(DateTime.Now, resultLayer); } catch (Exception ex) { throw new Exception(resourceManager.GetString("failedIMSService"), ex); } return; }
/// <summary> /// Function to add to map /// </summary> private void AddToMap_Clicked() { try { /* if (addedLayer[lstSearchResults.SelectedItem.GetHashCode()] != null) { AGXM.ServiceLayer resultLayer = (AGXM.ServiceLayer)addedLayer[lstSearchResults.SelectedItem.GetHashCode()]; try { if (resultLayer.Extent.GetEnvelope() != null) { MessageBox.Show(resourceManager.GetString("layerAdded"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception e) { addedLayer.Remove(lstSearchResults.SelectedItem.GetHashCode()); } }*/ // if (addedLayer[lstSearchResults.SelectedItem.GetHashCode()] == null) { Cursor.Current = Cursors.WaitCursor; // retrieve metadata CswRecord record = (CswRecord)lstSearchResults.SelectedItem; if (record == null) throw new NullReferenceException("CswRecord is null."); if (record.MapServerURL == null || record.MapServerURL.Trim().Length == 0) { // retrieve metadata RetrieveAddToMapInfoFromCatalog(); } else { _mapServerUrl = record.MapServerURL; } // if (xmlDoc == null) return; // prepare metadata for service info // xmlDoc = PrepareMetadata(xmlDoc); // if (xmlDoc == null) return; if (_mapServerUrl != null && _mapServerUrl.Trim().Length > 0) { String serviceType = record.ServiceType; if (serviceType == null || serviceType.Length == 0) { serviceType = CswProfile.getServiceType(_mapServerUrl); } if (serviceType.Equals("ags")) { if (_mapServerUrl.ToLower().Contains("arcgis/rest")) { _mapServerUrl = _mapServerUrl + "?f=nmf"; CswClient client = new CswClient(); AddAGSService(client.SubmitHttpRequest("DOWNLOAD", _mapServerUrl, "")); } else { AddAGSService(_mapServerUrl); } } else if (serviceType.Equals("wms") || serviceType.Equals("aims")) { try { MapServiceInfo msinfo = new MapServiceInfo(); msinfo.Server = record.MapServerURL; msinfo.Service = record.ServiceName; msinfo.ServiceType = record.ServiceType; CswProfile.ParseServiceInfoFromUrl(msinfo, _mapServerUrl, serviceType); addMapServiceLayer(msinfo); } catch (Exception e) { AddAGSService(_mapServerUrl); } } } else { MapServiceInfo msi = new MapServiceInfo(); // parse out service information // ParseServiceInfoFromMetadata(xmlDoc, ref msi); if (msi.IsMapService()) { addMapServiceLayer(msi); } else { MessageBox.Show(resourceManager.GetString("invalidService"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } // } } catch (Exception ex) { MessageBox.Show(resourceManager.GetString("addMapFailed"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { Cursor.Current = Cursors.Default; } }
/// <summary> /// Function to add WMS map serivce /// </summary> ///<param name="msi">The map service info</param> ///<param name="fromServerUrl">from server url</param> private void addLayerWMS(MapServiceInfo msi, Boolean fromServerUrl) { if (msi == null) { throw new ArgumentNullException("msi"); } string service = msi.Service; string url = AppendQuestionOrAmpersandToUrlString(msi.Server); // append serviceParam to server url? if (msi.ServiceParam.Length > 0 && !fromServerUrl) { url = url + msi.ServiceParam; url = AppendQuestionOrAmpersandToUrlString(url); } // connect to wms service try { AGXD.ServiceConnectionProperties sc = new AGXD.ServiceConnectionProperties(); sc.ServiceType = (AGXD.ServiceType)Enum.Parse(typeof(AGXD.ServiceType), "Wms"); AGXM.ServiceLayer resultLayer = new AGXM.ServiceLayer(); if (msi.Service != null && msi.Service.Trim().Length > 0) sc.ServiceName = msi.Service; else sc.ServiceName = "Unknown Service Name"; sc.SubServiceName = ""; sc.Url = new System.Uri(url); // sc.Username = ""; // sc.Password = ""; resultLayer.Visible = true; resultLayer.ServiceConnectionProperties = sc; resultLayer.Connect(); addLayer(resultLayer); addedLayer.Add(DateTime.Now, resultLayer); } catch (Exception ex) { throw new Exception(resourceManager.GetString("failedWMSService"), ex); } return; }
/// <summary> /// Parse out service information (such as service type, server name, service name, etc) from metadta document /// </summary> /// <param name="xmlDoc">xml metadata doc to be parsed</param> /// <param name="msi">MapServiceInfo object as output</param> private void ParseServiceInfoFromMetadata(XmlDocument xmlDoc, ref MapServiceInfo msi) { // todo: service info as an object? // also, what about service param and isSecured? if (xmlDoc == null) { throw new ArgumentNullException("xmlDoc"); } msi = new MapServiceInfo(); XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDoc.NameTable); xmlNamespaceManager.AddNamespace("cat", "http://www.esri.com/metadata/csw/"); xmlNamespaceManager.AddNamespace("csw", "http://www.opengis.net/cat/csw"); XmlNode nodeMetadata = xmlDoc.SelectSingleNode("//metadata|//cat:metadata|//csw:metadata", xmlNamespaceManager); if (nodeMetadata == null) { throw new Exception("No metadata node was found in the XML"); } // parse out service information XmlNode nodeEsri = nodeMetadata.SelectSingleNode("Esri"); if (nodeEsri == null) throw new Exception("<Esri> node missing"); // server XmlNode node = nodeEsri.SelectSingleNode("Server"); if (node == null) throw new Exception("'//Esri/Server' node missing"); msi.Server = node.InnerText; // service node = nodeEsri.SelectSingleNode("Service"); if (node != null) { msi.Service = node.InnerText; } // service type node = nodeEsri.SelectSingleNode("ServiceType"); if (node != null) { msi.ServiceType = node.InnerText; } // service param node = nodeEsri.SelectSingleNode("ServiceParam"); if (node != null) { msi.ServiceParam = node.InnerText; } // issecured node = nodeEsri.SelectSingleNode("issecured"); if (node != null) { msi.IsSecured = (node.InnerText.Equals("True", StringComparison.OrdinalIgnoreCase)); } return; }
/// <summary> /// Parses service information from url /// </summary> /// <param name="msinfo">map service information</param> /// <param name="mapServerUrl">map service url</param> /// <param name="serviceType">map service type</param> public static void ParseServiceInfoFromUrl(MapServiceInfo msinfo, String mapServerUrl, String serviceType) { msinfo.Service = "Generic " + serviceType + " Service Name"; String[] urlParts = mapServerUrl.Trim().Split('?'); if (urlParts.Length > 0) msinfo.Server = urlParts[0]; else msinfo.Server = mapServerUrl; String[] s = msinfo.Server.Split(new String[] { "/servlet/com.esri.esrimap.Esrimap" }, StringSplitOptions.RemoveEmptyEntries); msinfo.Server = s[0]; if (urlParts.Length > 1) { String[] urlParams = urlParts[1].Trim().Split('&'); foreach (String param in urlParams) { String paramPrefix = param.ToLower().Trim(); if (paramPrefix.StartsWith("service=") || paramPrefix.StartsWith("servicename=")) { msinfo.Service = param.Trim().Split('=')[1]; } else if (paramPrefix.StartsWith("map=")) { msinfo.ServiceParam = param.Trim(); } } } }
/// <summary> /// Retrieve metadta for the selected record from server. /// Then add the live data or maps that the metadta describes to ArcMap as a layer. /// </summary> private void AddToMap_Clicked() { try { Cursor.Current = Cursors.WaitCursor; CswRecord record = (CswRecord)resultsListBox.SelectedItem; if (record == null) throw new NullReferenceException(StringResources.CswRecordIsNull); if (record.MapServerURL == null || record.MapServerURL.Trim().Length == 0) { // retrieve metadata RetrieveAddToMapInfoFromCatalog(); } else { _mapServerUrl = record.MapServerURL; } if (_mapServerUrl != null && _mapServerUrl.Trim().Length > 0) { String serviceType = record.ServiceType; if (serviceType == null || serviceType.Length == 0) { serviceType = CswProfile.getServiceType(_mapServerUrl); } if (serviceType.Equals("unknown")) { System.Diagnostics.Process.Start("IExplore", _mapServerUrl); Cursor.Current = Cursors.Default; return; } else if (serviceType.Equals("ags")) { if (_mapServerUrl.ToLower().Contains("arcgis/rest")) { var urlparts = _mapServerUrl.Split('/'); if (urlparts != null && urlparts.Length > 0) { var lastPartOfUrl = urlparts[urlparts.Length - 1]; if (lastPartOfUrl.Length > 0 && IsNumeric(lastPartOfUrl)) { // CswClient client = new CswClient(); AddAGSService( _mapServerUrl); } else { _mapServerUrl = _mapServerUrl + "?f=lyr"; CswClient client = new CswClient(); AddAGSService(client.SubmitHttpRequest("DOWNLOAD", _mapServerUrl, "")); } } } else { AddAGSService(_mapServerUrl); } } else if (serviceType.Equals("wms")) { MapServiceInfo msinfo = new MapServiceInfo(); msinfo.Server = record.MapServerURL; msinfo.Service = record.ServiceName; msinfo.ServiceType = record.ServiceType; CswProfile.ParseServiceInfoFromUrl(msinfo,_mapServerUrl, serviceType); AddLayerWMS(msinfo, true); } else if (serviceType.Equals("aims")) { MapServiceInfo msinfo = new MapServiceInfo(); msinfo.Server = record.MapServerURL; msinfo.Service = record.ServiceName; msinfo.ServiceType = record.ServiceType; CswProfile.ParseServiceInfoFromUrl(msinfo,_mapServerUrl, serviceType); AddLayerArcIMS(msinfo); } else if (serviceType.Equals("wcs")) { // MapServiceInfo msi = new MapServiceInfo(); String[] s = _mapServerUrl.Trim().Split('?'); _mapServerUrl = s[0] + "?request=GetCapabilities&service=WCS"; CswClient client = new CswClient(); String response = client.SubmitHttpRequest("GET", _mapServerUrl, ""); XmlDocument xmlDocument = new XmlDocument(); try { xmlDocument.LoadXml(response); } catch (XmlException xmlEx) { } XmlNodeList contentMetadata = xmlDocument.GetElementsByTagName("ContentMetadata"); if (contentMetadata != null && contentMetadata.Count > 0) { XmlNodeList coverageList = contentMetadata.Item(0).ChildNodes; foreach (XmlNode coverage in coverageList) { XmlNodeList nodes = coverage.ChildNodes; foreach (XmlNode node in nodes) { if (node.Name.ToLower().Equals("name")) { _mapServerUrl = s[0] + "?request=GetCoverage&service=WCS&format=GeoTIFF&coverage=" + node.InnerText; try { String filePath = client.SubmitHttpRequest("DOWNLOAD", _mapServerUrl, ""); AddAGSService(filePath); } catch (Exception e) { ShowErrorMessageBox(StringResources.wcsWithNoGeoTiffInterface); return; } } } } } else { ShowErrorMessageBox(StringResources.serviceTypeUnknown); return; /* contentMetadata = xmlDocument.GetElementsByTagName("CoverageSummary"); if (contentMetadata != null && contentMetadata.Count > 0) { XmlNodeList coverageList = contentMetadata.Item(0).ChildNodes; foreach (XmlNode coverage in coverageList) { if (coverage.Name.ToLower().Equals("identifier")) { _mapServerUrl = s[0] + "?request=GetCoverage&service=WCS&format=GeoTIFF&coverage=" + coverage.InnerText; try { String filePath = client.SubmitHttpRequest("DOWNLOAD", _mapServerUrl, ""); AddAGSService(filePath); } catch (Exception e) { ShowErrorMessageBox("WCS service with no GeoTiff interface"); return; } } } }*/ } } } } catch (Exception ex) { ShowErrorMessageBox(ex.Message); } finally { Cursor.Current = Cursors.Default; } }
/// <summary> /// Parse out service information (such as service type, server name, service name, etc) from metadta document /// </summary> /// <param name="xmlDoc">xml metadata doc to be parsed</param> /// <param name="msi">A MapServiceInfo object as output</param> private MapServiceInfo ParseServiceInfoFromMetadata(XmlDocument xmlDoc) { // note: some required node may missing if it isn't a metadata for liveData or map try { if (xmlDoc == null) { throw new ArgumentNullException(); } MapServiceInfo msi = new MapServiceInfo(); XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDoc.NameTable); xmlNamespaceManager.AddNamespace("cat", "http://www.esri.com/metadata/csw/"); xmlNamespaceManager.AddNamespace("csw", "http://www.opengis.net/cat/csw"); xmlNamespaceManager.AddNamespace("gmd", "http://www.isotc211.org/2005/gmd"); XmlNode nodeMetadata = xmlDoc.SelectSingleNode("//metadata|//cat:metadata|//csw:metadata|//gmd:MD_Metadata", xmlNamespaceManager); if (nodeMetadata == null) { throw new Exception(StringResources.MetadataNodeMissing); } // parse out service information XmlNode nodeEsri = nodeMetadata.SelectSingleNode("Esri"); if (nodeEsri == null) throw new Exception(StringResources.EsriNodeMissing); // server XmlNode node = nodeEsri.SelectSingleNode("Server"); if (node == null) throw new Exception(StringResources.ServerNodeMissing); msi.Server = node.InnerText; // service node = nodeEsri.SelectSingleNode("Service"); if (node != null) { msi.Service = node.InnerText; } // service type node = nodeEsri.SelectSingleNode("ServiceType"); if (node != null) { msi.ServiceType = node.InnerText; } // service param node = nodeEsri.SelectSingleNode("ServiceParam"); if (node != null) { msi.ServiceParam = node.InnerText; } // issecured node = nodeEsri.SelectSingleNode("issecured"); if (node != null) { msi.IsSecured = (node.InnerText.Equals("True", StringComparison.OrdinalIgnoreCase)); } return msi; } catch (Exception ex) { ShowErrorMessageBox(StringResources.MapServiceInfoNotAvailable + "\r\n" + ex.Message); return null; } }