| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6374af7 commit 2443a43
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -60,13 +60,13 @@ public IEnvelope BoundingBox | |||
| 60 | 60 | } | |
| 61 | 61 | } | |
| 62 | 62 | ||
| 63 | - public int CrsEpsg | ||
| 63 | + public int Projection | ||
| 64 | 64 | { | |
| 65 | 65 | get { return _provider.CrsEpsg; } | |
| 66 | 66 | set { _provider.CrsEpsg = value; } | |
| 67 | 67 | } | |
| 68 | 68 | ||
| 69 | - public string LayersCsv | ||
| 69 | + public string Layers | ||
| 70 | 70 | { | |
| 71 | 71 | get { return _provider.LayersCsv; } | |
| 72 | 72 | set { _provider.LayersCsv = value; } | |
@@ -83,5 +83,11 @@ public int Id | |||
| 83 | 83 | get { return _provider.Id; } | |
| 84 | 84 | set { _provider.Id = value; } | |
| 85 | 85 | } | |
| 86 | + | ||
| 87 | + public string Format | ||
| 88 | + { | ||
| 89 | + get { return _provider.Format; } | ||
| 90 | + set { _provider.Format = value; } | ||
| 91 | + } | ||
| 86 | 92 | } | |
| 87 | 93 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,14 +41,17 @@ public static string GetToolbarConfigPath() | |||
| 41 | 41 | ||
| 42 | 42 | public static string GetToolsConfigPath() | |
| 43 | 43 | { | |
| 44 | - // TODO: change to app data folder after testing | ||
| 45 | - return AssemblyHelper.GetAppFolder() + @"\Config\Tools\"; | ||
| 44 | + return GetConfigPath() + @"\Tools\"; | ||
| 46 | 45 | } | |
| 47 | 46 | ||
| 48 | 47 | public static string GetDriversConfigPath() | |
| 49 | 48 | { | |
| 50 | - // TODO: change to app data folder after testing | ||
| 51 | - return AssemblyHelper.GetAppFolder() + @"\Config\Drivers\"; | ||
| 49 | + return GetConfigPath() + @"\Drivers\"; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + public static string GetWmsCachePath() | ||
| 53 | + { | ||
| 54 | + return GetConfigPath() + @"\WMS\"; | ||
| 52 | 55 | } | |
| 53 | 56 | } | |
| 54 | 57 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,10 @@ public static string GetTexturesPath() | |||
| 22 | 22 | return GetStylesPath() + @"Textures\"; | |
| 23 | 23 | } | |
| 24 | 24 | ||
| 25 | + /// <summary> | ||
| 26 | + /// Gets path of the styles folder. | ||
| 27 | + /// </summary> | ||
| 28 | + /// <returns></returns> | ||
| 25 | 29 | public static string GetStylesPath() | |
| 26 | 30 | { | |
| 27 | 31 | return AssemblyHelper.GetAppFolder() + @"\Styles\"; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,7 +8,9 @@ | |||
| 8 | 8 | using BruTile.Extensions; | |
| 9 | 9 | using BruTile.Wms; | |
| 10 | 10 | using MW5.Api.Concrete; | |
| 11 | + using MW5.Api.Interfaces; | ||
| 11 | 12 | using MW5.Shared; | |
| 13 | + using Layer = BruTile.Wms.Layer; | ||
| 12 | 14 | ||
| 13 | 15 | namespace MW5.Tiles.Helpers | |
| 14 | 16 | { | |
@@ -29,28 +31,87 @@ private static Stream GetRemoteXmlStream(Uri uri) | |||
| 29 | 31 | ||
| 30 | 32 | public static WmsCapabilities GetWmsCapabilities(string url) | |
| 31 | 33 | { | |
| 32 | - string requestUrl = GetRequestUrl(url); | ||
| 33 | - | ||
| 34 | - using (var stream = GetRemoteXmlStream(new Uri(requestUrl))) | ||
| 34 | + using (var stream = GetWmsCapabilitiesStream(url)) | ||
| 35 | 35 | { | |
| 36 | 36 | return new WmsCapabilities(stream); | |
| 37 | 37 | } | |
| 38 | 38 | } | |
| 39 | 39 | ||
| 40 | + public static Stream GetWmsCapabilitiesStream(string url) | ||
| 41 | + { | ||
| 42 | + string requestUrl = GetRequestUrl(url); | ||
| 43 | + | ||
| 44 | + return GetRemoteXmlStream(new Uri(requestUrl)); | ||
| 45 | + } | ||
| 46 | + | ||
| 40 | 47 | /// <summary> | |
| 41 | 48 | /// Creates MapWinGIS WMS provider based GetCapabilities definition and layers selected by user. | |
| 42 | 49 | /// </summary> | |
| 43 | 50 | public static WmsProviderDef CreateProvider( | |
| 44 | 51 | this WmsCapabilities capabilities, | |
| 45 | - IEnumerable<BruTile.Wms.Layer> layers) | ||
| 52 | + IEnumerable<Layer> layers, string serverUrl) | ||
| 46 | 53 | { | |
| 47 | - string layersCsv = StringHelper.Join(layers.Select(l => l.Name), ","); | ||
| 54 | + var layer = layers.FirstOrDefault(); | ||
| 55 | + if (layer == null) | ||
| 56 | + { | ||
| 57 | + return null; | ||
| 58 | + } | ||
| 48 | 59 | ||
| 49 | - // TODO: provide id and name in the UI | ||
| 50 | - var provider = new WmsProviderDef(5000, "Custom WMS provider") { LayersCsv = layersCsv }; | ||
| 60 | + // TODO: provide id and name in the UI | ||
| 61 | + var provider = new WmsProviderDef(5000, "Custom WMS provider") | ||
| 62 | + { | ||
| 63 | + Layers = GetLayers(layers), | ||
| 64 | + Projection = layer.GetEpsg(), | ||
| 65 | + BoundingBox = layer.GetBoundingBox(), | ||
| 66 | + BaseUrl = serverUrl, | ||
| 67 | + Format = capabilities.GetFormat() | ||
| 68 | + }; | ||
| 51 | 69 | ||
| 52 | - // TODO: add other parameters | ||
| 53 | 70 | return provider; | |
| 54 | 71 | } | |
| 72 | + | ||
| 73 | + private static string GetFormat(this WmsCapabilities capabilities) | ||
| 74 | + { | ||
| 75 | + var list = capabilities.Capability.Request.GetMap.Format; | ||
| 76 | + if (list.Count > 2) | ||
| 77 | + { | ||
| 78 | + return list[2]; | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + return list.FirstOrDefault(); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + private static string GetLayers(this IEnumerable<Layer> layers) | ||
| 85 | + { | ||
| 86 | + return StringHelper.Join(layers.Select(l => l.Name), ","); | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + private static IEnvelope GetBoundingBox(this Layer layer) | ||
| 90 | + { | ||
| 91 | + var box = layer.BoundingBox.FirstOrDefault(); | ||
| 92 | + if (box == null) | ||
| 93 | + { | ||
| 94 | + return null; | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + return new Envelope(box.MinX, box.MaxX, box.MinY, box.MaxY); | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + private static int GetEpsg(this Layer layer) | ||
| 101 | + { | ||
| 102 | + int epsg = -1; | ||
| 103 | + var crs = layer.SRS.FirstOrDefault(); | ||
| 104 | + | ||
| 105 | + if (!string.IsNullOrWhiteSpace(crs) && crs.Length > 5) | ||
| 106 | + { | ||
| 107 | + crs = crs.Substring(5); | ||
| 108 | + if (Int32.TryParse(crs, out epsg)) | ||
| 109 | + { | ||
| 110 | + return epsg; | ||
| 111 | + } | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + return epsg; | ||
| 115 | + } | ||
| 55 | 116 | } | |
| 56 | 117 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,68 @@ | |||
| 1 | + using System.Collections.Generic; | ||
| 2 | + using System.IO; | ||
| 3 | + using System.Linq; | ||
| 4 | + using System.Text; | ||
| 5 | + using System.Threading.Tasks; | ||
| 6 | + using BruTile.Wms; | ||
| 7 | + using MW5.Plugins.Helpers; | ||
| 8 | + using MW5.Shared; | ||
| 9 | + using Exception = System.Exception; | ||
| 10 | + | ||
| 11 | + namespace MW5.Tiles.Helpers | ||
| 12 | + { | ||
| 13 | + /// <summary> | ||
| 14 | + /// Saves results of GetCapabilities requests locally. | ||
| 15 | + /// </summary> | ||
| 16 | + internal static class WmsCapabilitiesCache | ||
| 17 | + { | ||
| 18 | + private static string GetXmlPath(string serverName) | ||
| 19 | + { | ||
| 20 | + return ConfigPathHelper.GetWmsCachePath() + serverName + ".xml"; | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + public static bool Save(string serverName, Stream stream) | ||
| 24 | + { | ||
| 25 | + string path = GetXmlPath(serverName); | ||
| 26 | + | ||
| 27 | + try | ||
| 28 | + { | ||
| 29 | + PathHelper.CreateFolder(path); | ||
| 30 | + | ||
| 31 | + using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write)) | ||
| 32 | + { | ||
| 33 | + stream.CopyTo(fileStream); | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + return true; | ||
| 37 | + } | ||
| 38 | + catch (Exception ex) | ||
| 39 | + { | ||
| 40 | + Logger.Current.Warn("Failed to save WMS capabilities to the disk: {0}", ex, path); | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + return false; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + public static WmsCapabilities Load(string serverName) | ||
| 47 | + { | ||
| 48 | + string path = GetXmlPath(serverName); | ||
| 49 | + | ||
| 50 | + if (File.Exists(path)) | ||
| 51 | + { | ||
| 52 | + try | ||
| 53 | + { | ||
| 54 | + using (var stream = File.OpenRead(path)) | ||
| 55 | + { | ||
| 56 | + return new WmsCapabilities(stream); | ||
| 57 | + } | ||
| 58 | + } | ||
| 59 | + catch (Exception ex) | ||
| 60 | + { | ||
| 61 | + Logger.Current.Warn("Failed to load WMS capabilities from the disk: {0}", ex, path); | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + return null; | ||
| 66 | + } | ||
| 67 | + } | ||
| 68 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -87,6 +87,7 @@ | |||
| 87 | 87 | <Compile Include="Enums\Enums.cs" /> | |
| 88 | 88 | <Compile Include="Helpers\BrutileHelper.cs" /> | |
| 89 | 89 | <Compile Include="Helpers\ProviderManager.cs" /> | |
| 90 | + <Compile Include="Helpers\WmsCapabilitiesCache.cs" /> | ||
| 90 | 91 | <Compile Include="Model\TmsProvider.cs" /> | |
| 91 | 92 | <Compile Include="Model\TmsProviderList.cs" /> | |
| 92 | 93 | <Compile Include="Properties\AssemblyInfo.cs" /> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,9 +1,11 @@ | |||
| 1 | 1 | using System; | |
| 2 | 2 | using System.Collections.Generic; | |
| 3 | 3 | using System.Diagnostics; | |
| 4 | + using System.IO; | ||
| 4 | 5 | using System.Linq; | |
| 5 | 6 | using System.Text; | |
| 6 | 7 | using System.Threading.Tasks; | |
| 8 | + using System.Windows.Forms; | ||
| 7 | 9 | using BruTile.Wms; | |
| 8 | 10 | using MW5.Plugins.Concrete; | |
| 9 | 11 | using MW5.Plugins.Interfaces; | |
@@ -93,11 +95,14 @@ private void AddProvider() | |||
| 93 | 95 | MessageService.Current.Info("No layers are selected"); | |
| 94 | 96 | } | |
| 95 | 97 | ||
| 96 | - var provider = Model.Capabilities.CreateProvider(layers); | ||
| 98 | + var provider = Model.Capabilities.CreateProvider(layers, server.Url); | ||
| 97 | 99 | ||
| 98 | 100 | var providers = _context.Map.Tiles.WmsProviders; | |
| 99 | 101 | providers.Add(provider); | |
| 100 | 102 | ||
| 103 | + _context.Map.Lock(); | ||
| 104 | + _context.Map.Unlock(); | ||
| 105 | + | ||
| 101 | 106 | Debug.Print("WMS providers count: " + providers.Count); | |
| 102 | 107 | } | |
| 103 | 108 | ||
@@ -109,25 +114,55 @@ private void ConnectServer() | |||
| 109 | 114 | var server = SelectedServer; | |
| 110 | 115 | if (server == null) return; | |
| 111 | 116 | ||
| 117 | + var capabilities = WmsCapabilitiesCache.Load(server.Name); | ||
| 118 | + if (capabilities != null) | ||
| 119 | + { | ||
| 120 | + Model.Capabilities = capabilities; | ||
| 121 | + View.UpdateView(); | ||
| 122 | + return; | ||
| 123 | + } | ||
| 124 | + | ||
| 112 | 125 | View.ShowHourglass(); | |
| 113 | 126 | ||
| 114 | - Task<WmsCapabilities>.Factory.StartNew(() => BruTileHelper.GetWmsCapabilities(server.Url)) | ||
| 115 | - .ContinueWith(task => | ||
| 127 | + Task<Stream>.Factory | ||
| 128 | + .StartNew(() => BruTileHelper.GetWmsCapabilitiesStream(server.Url)) | ||
| 129 | + .ContinueWith(ProcessWmsCapabilitiesResults, TaskScheduler.FromCurrentSynchronizationContext()); | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + /// <summary> | ||
| 133 | + /// Displayes and cached the results of GetCapabilities request. | ||
| 134 | + /// </summary> | ||
| 135 | + private void ProcessWmsCapabilitiesResults(Task<Stream> task) | ||
| 136 | + { | ||
| 137 | + var server = SelectedServer; | ||
| 138 | + | ||
| 139 | + try | ||
| 140 | + { | ||
| 141 | + using (var stream = task.Result) | ||
| 142 | + { | ||
| 143 | + if (WmsCapabilitiesCache.Save(server.Name, stream)) | ||
| 116 | 144 | { | |
| 117 | - try | ||
| 118 | - { | ||
| 119 | - Model.Capabilities = task.Result; | ||
| 120 | - View.UpdateView(); | ||
| 121 | - } | ||
| 122 | - catch (System.Exception ex) | ||
| 123 | - { | ||
| 124 | - Logger.Current.Warn("WMS server GetCapabilities request failed: {0}", ex, server.Url); | ||
| 125 | - } | ||
| 126 | - finally | ||
| 127 | - { | ||
| 128 | - View.HideHourglass(); | ||
| 129 | - } | ||
| 130 | - }, TaskScheduler.FromCurrentSynchronizationContext()); | ||
| 145 | + // ConnectStream returned by WebResponse doesn't support seek operation, | ||
| 146 | + // so we can't read it twice, therefore on successful saving to the disk | ||
| 147 | + // we are rereading it from file | ||
| 148 | + Model.Capabilities = WmsCapabilitiesCache.Load(server.Name); | ||
| 149 | + } | ||
| 150 | + else | ||
| 151 | + { | ||
| 152 | + Model.Capabilities = new WmsCapabilities(stream); | ||
| 153 | + } | ||
| 154 | + | ||
| 155 | + View.UpdateView(); | ||
| 156 | + } | ||
| 157 | + } | ||
| 158 | + catch (System.Exception ex) | ||
| 159 | + { | ||
| 160 | + Logger.Current.Warn("WMS server GetCapabilities request failed: {0}", ex, server.Url); | ||
| 161 | + } | ||
| 162 | + finally | ||
| 163 | + { | ||
| 164 | + View.HideHourglass(); | ||
| 165 | + } | ||
| 131 | 166 | } | |
| 132 | 167 | ||
| 133 | 168 | /// <summary> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -72,6 +72,7 @@ public static void ApplyConfig(this IMuteMap map, IConfigService configService) | |||
| 72 | 72 | map.ShowCoordinates = config.ShowCoordinates ? config.CoordinatesDisplay : CoordinatesDisplay.None; | |
| 73 | 73 | map.ShowCoordinatesFormat = config.CoordinateAngleFormat; | |
| 74 | 74 | ||
| 75 | + map.ReuseTileBuffer = config.ReuseTileBuffer; | ||
| 75 | 76 | map.InertiaOnPanning = config.InnertiaOnPanning; | |
| 76 | 77 | map.AnimationOnZooming = config.AnimationOnZooming; | |
| 77 | 78 | map.ZoomBoxStyle = config.ZoomBoxStyle; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments