Common CustomDataSource
Common_CustomDataSource_CSharp\CustomDataSource_CSharp\TiledMapDataSource_CSharp\MapInformation.cs
// Copyright 2007 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
//

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using ESRI.ArcGIS.ADF.Web.Display.Graphics;
using ESRI.ArcGIS.ADF.Web.Geometry;
using ESRI.ArcGIS.ADF.Web.SpatialReference;
using ESRI.ArcGIS.ADF.Web.DataSources;

namespace TiledMapDataSource_CSharp
{
    public class MapInformation : IMapInformation
    {
        public MapInformation(string dataSourceDefinition, string resourceDefinition)
        {
            this.dataSourceConfig = dataSourceDefinition;
            resourceConfig = resourceDefinition;
            parseConfig();
        }

        private string dataSourceConfig = string.Empty;
        private string resourceConfig = string.Empty;
        private SpatialReference defaultSpatialReference = null;
        private TileCacheInfo tileCacheInfo = null;
        private Envelope defaultExtent, fullExtent;

        public string DataFrame
        {
            get { return "(default)"; }
        }

        public SpatialReference DefaultSpatialReference
        {
            get { return defaultSpatialReference; }
        }

        public Envelope DefaultExtent
        {
            get
            {
                return defaultExtent;
            }
        }

        public Envelope FullExtent
        {
            get 
            {
                return fullExtent;
            }
        }

        public ESRI.ArcGIS.ADF.Web.DataSources.TileCacheInfo TileCacheInfo
        {
            get { return tileCacheInfo; }
            set { tileCacheInfo = new TileCacheInfo(value); }
        }

        private void parseConfig()
        {
            #region Get resource config nodes from document
            XmlDocument doc = new XmlDocument();
            doc.Load(dataSourceConfig);
            XmlNode root = doc.DocumentElement; //TileCacheInfos
            XmlNodeList tileCacheInfoNodes = root.SelectNodes("TileCacheInfo");

            if (tileCacheInfoNodes == null || tileCacheInfoNodes.Count < 1)
                throw new Exception("Could not find configuration for resource " + resourceConfig);

            XmlNode tileCacheNode = null;
            System.Collections.Generic.Dictionary<int, string> tileUrls = new Dictionary<int, string>();
            System.Collections.Generic.Dictionary<string, string> layers = new Dictionary<string, string>();

            for (int t = 0; t < tileCacheInfoNodes.Count; ++t)
            {
                if (tileCacheInfoNodes[t].Attributes["Name"].InnerText == resourceConfig)
                {
                    tileCacheNode = tileCacheInfoNodes[t];
                    break;
                }
            }
            if (tileCacheNode == null)
                tileCacheNode = tileCacheInfoNodes[0];
            #endregion
            #region Tile Cache Info
            string srtext = tileCacheNode.Attributes["SpatialReference"].InnerText;
            int srid;
            if (Int32.TryParse(srtext, out srid))
            {
                defaultSpatialReference = new SpatialReference(srid);
            }
            else
            {
                defaultSpatialReference = new SpatialReference(srtext);
            }
                
            int dpi = Convert.ToInt32(tileCacheNode.Attributes["DPI"].InnerText);
            int height = Convert.ToInt32(tileCacheNode.Attributes["Height"].InnerText);
            int width = Convert.ToInt32(tileCacheNode.Attributes["Width"].InnerText);
            string[] originCoords = tileCacheNode.Attributes["TileOrigin"].InnerText.Split(new char[] { ',' });
            double xmin = Convert.ToDouble(originCoords[0]);
            double ymin = Convert.ToDouble(originCoords[1]);
            Point origin = new Point(xmin, ymin);

            XmlNode extentNode = tileCacheNode.SelectSingleNode("FullExtent");
            xmin = Convert.ToDouble(extentNode.Attributes["XMin"].InnerText);
            ymin = Convert.ToDouble(extentNode.Attributes["YMin"].InnerText);
            double xmax = Convert.ToDouble(extentNode.Attributes["XMax"].InnerText);
            double ymax = Convert.ToDouble(extentNode.Attributes["YMax"].InnerText);
            fullExtent = new Envelope(xmin, ymin, xmax, ymax);
            fullExtent.SpatialReference = defaultSpatialReference;

            extentNode = tileCacheNode.SelectSingleNode("DefaultExtent");
            xmin = Convert.ToDouble(extentNode.Attributes["XMin"].InnerText);
            ymin = Convert.ToDouble(extentNode.Attributes["YMin"].InnerText);
            xmax = Convert.ToDouble(extentNode.Attributes["XMax"].InnerText);
            ymax = Convert.ToDouble(extentNode.Attributes["YMax"].InnerText);
            defaultExtent = new Envelope(xmin, ymin, xmax, ymax);
            defaultExtent.SpatialReference = defaultSpatialReference;

#endregion
            #region Layers
            XmlNode layersNode = tileCacheNode.SelectSingleNode("Layers");
            if (layersNode != null)
            {
                XmlNodeList layerNodes = layersNode.SelectNodes("Layer");
                if (layerNodes != null)
                {
                    for (int l = 0; l < layerNodes.Count; ++l)
                    {
                        string layerId = layerNodes[l].Attributes["LayerID"].InnerText;
                        string layerName = layerNodes[l].Attributes["Name"].InnerText;
                        layers.Add(layerId, layerName);
                    }
                }
            }
            #endregion
            #region LOD Info
            System.Collections.Generic.List<LodInfo> lodInfos = new List<LodInfo>();
            System.Collections.Generic.Dictionary<int, int> levels = new Dictionary<int, int>();
            XmlNode lodInfoNode = tileCacheNode.SelectSingleNode("LodInfos");
            XmlNodeList lodInfoNodes = lodInfoNode.SelectNodes("LodInfo");
            for (int l = 0; l < lodInfoNodes.Count; ++l)
            {
                int levelid = Convert.ToInt32(lodInfoNodes[l].Attributes["LevelID"].InnerText);
                levels.Add(l, levelid);
                int rows = Convert.ToInt32(lodInfoNodes[l].Attributes["Rows"].InnerText);
                int columns = Convert.ToInt32(lodInfoNodes[l].Attributes["Columns"].InnerText);
                string tileUrl = lodInfoNodes[l].Attributes["TileUrl"].InnerText;
                double tilewidth = Convert.ToDouble(lodInfoNodes[l].Attributes["TileExtentWidth"].InnerText);
                double tileheight = Convert.ToDouble(lodInfoNodes[l].Attributes["TileExtentHeight"].InnerText);
                double scale = Convert.ToDouble(lodInfoNodes[l].Attributes["Scale"].InnerText);
                double resolution = Convert.ToDouble(lodInfoNodes[l].Attributes["Resolution"].InnerText);
                LodInfo lodInfo = new LodInfo(levelid, resolution, scale, columns, rows, tilewidth, tileheight);
                lodInfos.Add(lodInfo);
                tileUrls.Add(levelid, tileUrl);
            }
            #endregion

            tileCacheInfo = new TileCacheInfo(width, height,
                dpi, origin, lodInfos.ToArray());
            tileCacheInfo.TileUrls = tileUrls;
            tileCacheInfo.Layers = layers;
            tileCacheInfo.Levels = levels;

            XmlNode urlGenNode = tileCacheNode.SelectSingleNode("TileUrlGenerator");
            if (urlGenNode != null)
            {
                tileCacheInfo.TileUrlGeneratorAssembly = 
                    urlGenNode.Attributes["Assembly"].InnerText;
                tileCacheInfo.TileUrlGeneratorClass = 
                    urlGenNode.Attributes["Class"].InnerText;
            }

        }
    }
}