/Main/src/DynamicDataDisplay.Maps/Servers/ResourcesServers/ResourcesTileServer.cs
C# | 126 lines | 106 code | 20 blank | 0 comment | 4 complexity | a7a04653eed6d7fd5664de47fe12b162 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using Microsoft.Research.DynamicDataDisplay.Charts.Maps; 6using System.Reflection; 7using System.Windows.Media.Imaging; 8using System.IO; 9using System.Diagnostics; 10 11namespace Microsoft.Research.DynamicDataDisplay.Maps.Servers 12{ 13 public abstract class ResourcesTileServer : ITileServer 14 { 15 private const string prefix = "Microsoft.Research.DynamicDataDisplay.Maps.Tiles"; 16 17 private static readonly Assembly currentAssembly; 18 private static readonly string[] resourceNames; 19 static ResourcesTileServer() 20 { 21 currentAssembly = typeof(ResourcesTileServer).Assembly; 22 resourceNames = currentAssembly.GetManifestResourceNames(); 23 } 24 25 private int minLevel = 1; 26 private int maxLevel = 2; 27 28 #region ITileServer Members 29 30 public bool Contains(TileIndex id) 31 { 32 return (minLevel <= id.Level && id.Level <= maxLevel); 33 } 34 35 private string currentPrefix; 36 public void BeginLoadImage(TileIndex id) 37 { 38 BitmapImage bmp = new BitmapImage(); 39 string resourceName = CreateResourceName(id); 40 using (Stream stream = currentAssembly.GetManifestResourceStream(resourceName)) 41 { 42 if (stream == null) 43 { 44 ReportFailure(id); 45 return; 46 } 47 48 bmp.BeginInit(); 49 bmp.StreamSource = stream; 50 bmp.CacheOption = BitmapCacheOption.OnLoad; 51 bmp.EndInit(); 52 if (bmp.IsDownloading) { } 53 } 54 55 ReportSuccess(bmp, id); 56 } 57 58 protected void ReportSuccess(BitmapImage bmp, TileIndex id) 59 { 60 Debug.WriteLine(String.Format("{0}: loaded id = {1}", ServerName, id)); 61 RaiseDataLoaded(new TileLoadResultEventArgs 62 { 63 Image = bmp, 64 Result = TileLoadResult.Success, 65 ID = id 66 }); 67 } 68 69 protected void ReportFailure(TileIndex id) 70 { 71 Debug.WriteLine(String.Format("{0}: failed id = {1}", ServerName, id)); 72 RaiseDataLoaded(new TileLoadResultEventArgs 73 { 74 Image = null, 75 ID = id, 76 Result = TileLoadResult.Failure 77 }); 78 } 79 80 81 private string CreateResourceName(TileIndex id) 82 { 83 StringBuilder builder = new StringBuilder(currentPrefix); 84 builder = builder.Append(id.Level).Append(".").Append(id.X).Append("x") 85 .Append(id.Y).Append(fileExtension); 86 87 return builder.ToString(); 88 } 89 90 private void RaiseDataLoaded(TileLoadResultEventArgs args) 91 { 92 ImageLoaded.Raise(this, args); 93 } 94 public event EventHandler<TileLoadResultEventArgs> ImageLoaded; 95 96 private string name = "Resource tile server"; 97 public string ServerName 98 { 99 get { return name; } 100 protected set 101 { 102 name = value; 103 currentPrefix = prefix + "." + name + ".z"; 104 } 105 } 106 107 private string fileExtension = ".png"; 108 public string FileExtension 109 { 110 get { return fileExtension; } 111 set { fileExtension = value; } 112 } 113 114 #endregion 115 116 #region ITileServer Members 117 118 protected void RaiseChangedEvent() 119 { 120 Changed.Raise(this); 121 } 122 public event EventHandler Changed; 123 124 #endregion 125 } 126}