/// <summary> /// Loads a wmc file with file name provided /// </summary> /// <param name="strFileName">the file name</param> public void LoadFromFile(string strFileName) { XmlAttributeCollection theAttributes = null; XmlNode theLayer = null; XmlNode theNameNode = null; XmlNode theTitleNode = null; XmlNode theServerNode = null; XmlNode theOnlineResourceNode = null; XmlDocument theWMC = new XmlDocument(); try { string xml = ReadFromFile(strFileName); theWMC.LoadXml(xml); } catch (Exception e) { OpenWMC.logger.writeLog(e.StackTrace); } XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(theWMC.NameTable); xmlnsManager.AddNamespace("context", "http://www.opengis.net/context"); xmlnsManager.AddNamespace("sld", "http://www.opengis.net/sld"); xmlnsManager.AddNamespace("xlink", "http://www.w3.org/1999/xlink"); xmlnsManager.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema"); XmlNodeList theLayers = theWMC.SelectNodes("/context:ViewContext/context:LayerList/context:Layer", xmlnsManager); if (theLayers.Count == 0) { xmlnsManager = new XmlNamespaceManager(theWMC.NameTable); xmlnsManager.AddNamespace("sld", "http://www.opengis.net/sld"); xmlnsManager.AddNamespace("xlink", "http://www.w3.org/1999/xlink"); xmlnsManager.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema"); xmlnsManager.AddNamespace("context", "http://www.opengeospatial.net/context"); theLayers = theWMC.SelectNodes("/context:ViewContext/context:LayerList/context:Layer", xmlnsManager); if (theLayers.Count == 0) { sErr = StringResources.InvalidWMCDocument + StringResources.NoLayers; return; } } for (int i = theLayers.Count - 1; i >= 0; i--) { theLayer = theLayers.Item(i); WMCLayer myLayer = new WMCLayer(); XmlNodeList children = theLayer.ChildNodes; foreach (XmlNode child in children) { if (child.Name.ToLower() == "name") { theNameNode = child; } else if (child.Name.ToLower() == "title") { theTitleNode = child; } else if (child.Name.ToLower() == "server") { theServerNode = child; XmlNodeList nds = theServerNode.ChildNodes; foreach (XmlNode c in nds) { if (c.Name.ToLower() == "onlineresource") { theOnlineResourceNode = c; } } if (theTitleNode == null) { myLayer.Title = getAttribute(theServerNode.Attributes, "title"); } } } //1 - get Server info theAttributes = theServerNode.Attributes; myLayer.Server.Service = getAttribute(theAttributes, "service"); myLayer.Server.Version = getAttribute(theAttributes, "version"); myLayer.Server.Title = getAttribute(theAttributes, "title"); //2 - get online info myLayer.Server.OnlineResource.Href = getAttributeFromNode(theOnlineResourceNode, "xlink:href"); myLayer.Server.OnlineResource.ResourceType = getAttributeFromNode(theOnlineResourceNode, "xlink:type"); myLayer.Server.OnlineResource.Role = getAttributeFromNode(theOnlineResourceNode, "xlink:role"); myLayer.Server.OnlineResource.ArcRole = getAttributeFromNode(theOnlineResourceNode, "xlink:arcrole"); myLayer.Server.OnlineResource.Title = getAttributeFromNode(theOnlineResourceNode, "xlink:title"); myLayer.Server.OnlineResource.Show = getAttributeFromNode(theOnlineResourceNode, "xlink:show"); myLayer.Server.OnlineResource.Actuate = getAttributeFromNode(theOnlineResourceNode, "xlink:actuate"); //3 - get layer name, title and info if (myLayer.Server.Service == "OGC:WMS") { myLayer.Name = theNameNode.InnerText; myLayer.SecretName = theNameNode.InnerText; } else if (myLayer.Server.Service == "ESRI:ARCIMS") { if (theNameNode.InnerText.Contains(":")) { myLayer.Name = theNameNode.InnerText.Substring(0, theNameNode.InnerText.IndexOf(":") - 1); myLayer.SecretName = theNameNode.InnerText.Substring(theNameNode.InnerText.IndexOf(":") + 1); } else { myLayer.Name = theNameNode.InnerText; myLayer.SecretName = theNameNode.InnerText; } } else if (myLayer.Server.Service == "ESRI:ARCIMS:HTTP") { myLayer.Name = myLayer.Server.Title; myLayer.SecretName = theTitleNode.InnerText; } else if (myLayer.Server.Service == "ESRI:AGS:MAP:SOAP") { if (theNameNode.InnerText.Contains(":")) { myLayer.Name = theNameNode.InnerText.Substring(0, theNameNode.InnerText.IndexOf(":") - 1); myLayer.SecretName = theNameNode.InnerText.Substring(theNameNode.InnerText.IndexOf(":") + 1); } else { myLayer.Name = theNameNode.InnerText; myLayer.SecretName = theNameNode.InnerText; } } else { myLayer.Name = theNameNode.InnerText; } if (theTitleNode != null) { myLayer.Title = theTitleNode.InnerText; } theAttributes = theLayer.Attributes; myLayer.IsHidden = theAttributes.GetNamedItem("hidden").Value; myLayer.IsQueryable = theAttributes.GetNamedItem("queryable").Value; layers.Add(myLayer); } if (theWMC.SelectNodes("/context:ViewContext/context:General/context:BoundingBox", xmlnsManager).Count == 0) { HasExtent = false; } HasExtent = true; XmlNode theGeneral = theWMC.SelectNodes("/context:ViewContext/context:General/context:BoundingBox", xmlnsManager).Item(0); theAttributes = theGeneral.Attributes; Double xmax, xmin, ymax, ymin; CultureInfo culture = new CultureInfo("us"); xmax = Double.Parse(getAttribute(theAttributes, "maxx"), culture); xmin = Double.Parse(getAttribute(theAttributes, "minx"), culture); ymax = Double.Parse(getAttribute(theAttributes, "maxy"), culture); ymin = Double.Parse(getAttribute(theAttributes, "miny"), culture); extent = new EnvelopeClass(); extent.XMax = xmax; extent.XMin = xmin; extent.YMax = ymax; extent.YMin = ymin; sSRS = getAttribute(theAttributes, "SRS"); }
/// <summary> /// Adds wmc layer to current map document /// </summary> /// <param name="theLayer">the WMCLayer object</param> /// <param name="ipMxDoc">the map document</param> private void AddTheLayer(WMCLayer theLayer, IMxDocument ipMxDoc) { bool bIsLoaded = false; bool bIsArcIMS = false; bool bIsAGS = false; string strServiceType; strServiceType = theLayer.Server.Service.ToUpper(); string strThisServerID = null; // check the service type and retrieve the service ID if (strServiceType == "ESRI:ARCIMS" || strServiceType == "ESRI:ARCIMS:HTTP") { strThisServerID = theLayer.Server.OnlineResource.Href + ":" + theLayer.Name; bIsArcIMS = true; } else if (strServiceType == "ESRI:AGS:MAP:SOAP") { strThisServerID = theLayer.Server.OnlineResource.Href + ":" + theLayer.Name; bIsAGS = true; } else if (strServiceType.Contains("WMS")) { strThisServerID = theLayer.Server.OnlineResource.Href; bIsArcIMS = false; } else { strThisServerID = ""; } logger.writeLog("strServiceType = " + strServiceType + " strThisServerID = " + strThisServerID); // 'was there something? if (strThisServerID.Length == 0) { logger.writeLog(StringResources.UnrecognizedServiceType + strServiceType + StringResources.Exiting); MessageBox.Show(StringResources.UnrecognizedServiceType + strServiceType + StringResources.Exiting); return; } // has it been added to the map already? string strServer; string strLayerName; string strSecretName; string layer = null; foreach (string lyr in colServiceList) { layer = lyr; string[] s = DecodeServiceList(layer); strServer = s[0]; strLayerName = s[1]; strSecretName = s[2]; if (strServer == strThisServerID) { bIsLoaded = true; break; } else if ((strServer + strLayerName) == strThisServerID) { bIsLoaded = true; break; } else if ((strServer + ":" + strLayerName) == strThisServerID) { bIsLoaded = true; break; } } bool bAddedToList = false; //it its not loaded if (!bIsLoaded) { //add it to map (and service list) if (bIsArcIMS) { // arcims - .name is the group layername in arcmap. .title is the sublayer display name bAddedToList = addLayerArcIMS(ipMxDoc, theLayer.Server.OnlineResource.Href, theLayer.Name, theLayer.SecretName); } else if (bIsAGS) { bAddedToList = addLayerAGS(ipMxDoc, theLayer.Server.OnlineResource.Href, theLayer.Server.Title, theLayer.SecretName); } else { // wms wants the .Title to be displayed as layername in arcmap. thelayer.name is the WMSLayername - internal // wms group layer name comes from servicedescription bAddedToList = addLayerWMS(ipMxDoc, theLayer.Server.OnlineResource.Href, theLayer.Title, theLayer.SecretName); } } else { // get the service that matches string[] s = DecodeServiceList(layer); strServer = s[0]; strLayerName = s[1]; strSecretName = s[2]; if (bIsArcIMS) { // use layer.title for arcims logger.writeLog(StringResources.DontNeedToAddService + strServer + StringResources.Colon + strLayerName + StringResources.Colon + theLayer.SecretName); colServiceList.Add(EncodeServiceList(strServer, strLayerName, theLayer.SecretName)); } else if (bIsAGS) { // use layer.title for ags logger.writeLog(StringResources.DontNeedToAddService + strServer + StringResources.Colon + strLayerName + StringResources.Colon + theLayer.SecretName); colServiceList.Add(EncodeServiceList(strServer, strLayerName, theLayer.SecretName)); } else { // create a NEW entry using the new layers internal Name logger.writeLog(StringResources.DontNeedToAddService + strServer + StringResources.Colon + strLayerName + StringResources.Colon + theLayer.SecretName); colServiceList.Add(EncodeServiceList(strServer, strLayerName, theLayer.SecretName)); } bAddedToList = true; } // add to layer visibility if (bAddedToList && (theLayer.IsHidden == "false" || theLayer.IsHidden == "0")) { colVisibleLayers.Add(colServiceList.ToArray()[(colServiceList.Count - 1)]); } }
/// <summary> /// Loads a wmc file with file name provided /// </summary> /// <param name="strFileName">the file name</param> public void LoadFromFile(string strFileName) { XmlAttributeCollection theAttributes = null; XmlNode theLayer = null; XmlNode theNameNode = null; XmlNode theTitleNode = null; XmlNode theServerNode = null; XmlNode theOnlineResourceNode = null; XmlDocument theWMC = new XmlDocument(); try { string xml = ReadFromFile(strFileName); theWMC.LoadXml(xml); } catch (Exception e) { OpenWMC.logger.writeLog(e.StackTrace); } XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(theWMC.NameTable); xmlnsManager.AddNamespace("context", "http://www.opengis.net/context"); xmlnsManager.AddNamespace("sld", "http://www.opengis.net/sld"); xmlnsManager.AddNamespace("xlink", "http://www.w3.org/1999/xlink"); xmlnsManager.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema"); XmlNodeList theLayers = theWMC.SelectNodes("/context:ViewContext/context:LayerList/context:Layer",xmlnsManager); if (theLayers.Count == 0) { xmlnsManager = new XmlNamespaceManager(theWMC.NameTable); xmlnsManager.AddNamespace("sld", "http://www.opengis.net/sld"); xmlnsManager.AddNamespace("xlink", "http://www.w3.org/1999/xlink"); xmlnsManager.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema"); xmlnsManager.AddNamespace("context", "http://www.opengeospatial.net/context"); theLayers = theWMC.SelectNodes("/context:ViewContext/context:LayerList/context:Layer",xmlnsManager); if (theLayers.Count == 0) { sErr = StringResources.InvalidWMCDocument + StringResources.NoLayers; return; } } for (int i = theLayers.Count -1; i >= 0 ; i--) { theLayer = theLayers.Item(i); WMCLayer myLayer = new WMCLayer(); XmlNodeList children = theLayer.ChildNodes; foreach (XmlNode child in children) { if (child.Name.ToLower() == "name") theNameNode = child; else if (child.Name.ToLower() == "title") theTitleNode = child; else if (child.Name.ToLower() == "server") { theServerNode = child; XmlNodeList nds = theServerNode.ChildNodes; foreach (XmlNode c in nds) { if (c.Name.ToLower() == "onlineresource") theOnlineResourceNode = c; } if (theTitleNode == null) { myLayer.Title = getAttribute(theServerNode.Attributes, "title"); } } } //1 - get Server info theAttributes = theServerNode.Attributes; myLayer.Server.Service = getAttribute(theAttributes, "service"); myLayer.Server.Version = getAttribute(theAttributes, "version"); myLayer.Server.Title = getAttribute(theAttributes, "title"); //2 - get online info myLayer.Server.OnlineResource.Href = getAttributeFromNode(theOnlineResourceNode, "xlink:href"); myLayer.Server.OnlineResource.ResourceType = getAttributeFromNode(theOnlineResourceNode, "xlink:type"); myLayer.Server.OnlineResource.Role = getAttributeFromNode(theOnlineResourceNode, "xlink:role"); myLayer.Server.OnlineResource.ArcRole = getAttributeFromNode(theOnlineResourceNode, "xlink:arcrole"); myLayer.Server.OnlineResource.Title = getAttributeFromNode(theOnlineResourceNode, "xlink:title"); myLayer.Server.OnlineResource.Show = getAttributeFromNode(theOnlineResourceNode, "xlink:show"); myLayer.Server.OnlineResource.Actuate = getAttributeFromNode(theOnlineResourceNode, "xlink:actuate"); //3 - get layer name, title and info if (myLayer.Server.Service == "OGC:WMS") { myLayer.Name = theNameNode.InnerText; myLayer.SecretName = theNameNode.InnerText; } else if (myLayer.Server.Service == "ESRI:ARCIMS") { if (theNameNode.InnerText.Contains(":")) { myLayer.Name = theNameNode.InnerText.Substring(0, theNameNode.InnerText.IndexOf(":") - 1); myLayer.SecretName = theNameNode.InnerText.Substring(theNameNode.InnerText.IndexOf(":") + 1); } else { myLayer.Name = theNameNode.InnerText; myLayer.SecretName = theNameNode.InnerText; } } else if (myLayer.Server.Service == "ESRI:ARCIMS:HTTP") { myLayer.Name = myLayer.Server.Title; myLayer.SecretName = theTitleNode.InnerText; } else if (myLayer.Server.Service == "ESRI:AGS:MAP:SOAP") { if (theNameNode.InnerText.Contains(":")) { myLayer.Name = theNameNode.InnerText.Substring(0, theNameNode.InnerText.IndexOf(":") - 1); myLayer.SecretName = theNameNode.InnerText.Substring(theNameNode.InnerText.IndexOf(":") + 1); } else { myLayer.Name = theNameNode.InnerText; myLayer.SecretName = theNameNode.InnerText; } } else { myLayer.Name = theNameNode.InnerText; } if (theTitleNode != null) { myLayer.Title = theTitleNode.InnerText; } theAttributes = theLayer.Attributes; myLayer.IsHidden =theAttributes.GetNamedItem("hidden").Value; myLayer.IsQueryable = theAttributes.GetNamedItem("queryable").Value; layers.Add(myLayer); } if (theWMC.SelectNodes("/context:ViewContext/context:General/context:BoundingBox",xmlnsManager).Count == 0) { HasExtent = false; } HasExtent = true; XmlNode theGeneral = theWMC.SelectNodes("/context:ViewContext/context:General/context:BoundingBox",xmlnsManager).Item(0); theAttributes = theGeneral.Attributes; Double xmax, xmin, ymax, ymin; xmax = Double.Parse(getAttribute(theAttributes, "maxx")); xmin = Double.Parse(getAttribute(theAttributes, "minx")); ymax = Double.Parse(getAttribute(theAttributes, "maxy")); ymin = Double.Parse(getAttribute(theAttributes, "miny")); extent = new EnvelopeClass(); extent.XMax = xmax; extent.XMin = xmin; extent.YMax = ymax; extent.YMin = ymin; sSRS = getAttribute(theAttributes, "SRS"); }