使用C# + httpWebRequest 解析WMTS服務後設資料

weixin_34041003發表於2017-09-04

解析http://219.142.81.86/igserver/ogc/kvp/TAS10R52000J49/WMTSServer服務的後設資料

// 測試httpWebRequest讀取 wmts後設資料
string address = "http://219.142.81.86/igserver/ogc/kvp/TAS10R52000J49/WMTSServer";
var request = (HttpWebRequest)WebRequest.Create(address);
var response = (HttpWebResponse)request.GetResponse();
var stream = response.GetResponseStream();

if (stream == null) return;

var xmlReader = new XmlTextReader(stream);
var xmlDocument = new XmlDocument();
xmlDocument.Load(xmlReader);

string namespace_uri = xmlDocument.DocumentElement.NamespaceURI;
var nsmgr = new XmlNamespaceManager(xmlDocument.NameTable);
nsmgr.AddNamespace("wmts", namespace_uri);

XmlNode node = xmlDocument.SelectSingleNode("//wmts:Layer", nsmgr);
if (node == null)    return;        
string LowerCorner, UpperCorner;
// 得到layer節點的所有子節點
XmlNodeList xnl = node.ChildNodes;

double west = 0;
double east = 0;
double south = 0;
double north = 0;
foreach (XmlNode xn1 in xnl)
 {
  if (xn1.Name == "ows:BoundingBox" || xn1.Name == "ows:WGS84BoundingBox")
  {
            XmlElement xe = (XmlElement)xn1;
            //得到crs屬性
            string crs = xe.GetAttribute("crs").ToString();

            // 得到BoundBox節點的所有子節點                   
            XmlNodeList xn10 = xn1.ChildNodes;
            LowerCorner = xn10.Item(0).InnerText;
            UpperCorner = xn10.Item(1).InnerText;
            string[] cc = LowerCorner.Split(' ');
            west = Convert.ToDouble(cc[0]);
            south = Convert.ToDouble(cc[1]);
            string[] uu = UpperCorner.Split(' ');
            east = Convert.ToDouble(uu[0]);
            north = Convert.ToDouble(uu[1]);
  }                
       // 將節點轉換為元素,便於得到節點的屬性值
       //XmlElement xe = (XmlElement)xn1;
       // 得到Type和ISBN兩個屬性的屬性值
       //string type = xe.GetAttribute("ows:Type").ToString();   //<book Type="必修課" ISBN="7-111-19149-2">
}

            XmlNodeList nodes = xmlDocument.SelectNodes("//wmts:TileMatrixSet", nsmgr);
            if (nodes.Count <= 0) return;

            Dictionary<int, _tileMatrix> tileMatrixSet = new Dictionary<int,_tileMatrix>();  
            int minLevel = 9999;
            int maxLevel = -9999;
            
            foreach (XmlNode xn1 in nodes)
            {
                if (xn1.ParentNode == null || xn1.ParentNode.Name != "Contents")
                    continue;
                
                XmlNodeList xn11 = xn1.ChildNodes;
                foreach (XmlNode xn110 in xn11)
                {
                    if (xn110.Name == "TileMatrix")
                    {
                        XmlNodeList xn10 = xn110.ChildNodes;
                        string strlevel = xn10.Item(0).InnerText;
                        string[] temp = strlevel.Split(':');
                        int cn = temp.Count();
                        int level = 0;
                        if (cn == 1)
                            level = Convert.ToInt32(temp[0]);
                        else
                            level = Convert.ToInt32(temp[cn-1]);
                        
                        double ScaleDenominator = Convert.ToDouble(xn10.Item(1).InnerText);
                        string[] TopLeftCorner = xn10.Item(2).InnerText.Split(' ');
                        double top = Convert.ToDouble(TopLeftCorner[0]);
                        double left = Convert.ToDouble(TopLeftCorner[1]);
                        int TileWidth = Convert.ToInt32(xn10.Item(3).InnerText);
                        int TileHeight = Convert.ToInt32(xn10.Item(4).InnerText);
                        int cols = Convert.ToInt32(xn10.Item(5).InnerText);
                        int rows = Convert.ToInt32(xn10.Item(6).InnerText);

                        _tileMatrix ts = new _tileMatrix();
                        ts.level = level;
                        ts.cornerTop = top;
                        ts.cornerLeft = level;
                        ts.tileWidth = TileWidth;
                        ts.tileHeight = TileHeight;
                        ts.tileCols = cols;
                        ts.tileRows = rows;
                        tileMatrixSet.Add(level, ts);

                        minLevel = Math.Min(minLevel, level);
                        maxLevel = Math.Max(maxLevel, level);
                    }
                }                 
            }
          
            xmlReader.Close();

相關文章