PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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