FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

a) Caching of WMS capabilities request; b) Setting properties of WMS … · MapWindow/MapWindow5@2443a43 · GitHub

Commit 2443a43

Browse files
sleschinski
committed
a) Caching of WMS capabilities request; b) Setting properties of WMS provider.
1 parent 6374af7 commit 2443a43

8 files changed

Lines changed: 210 additions & 31 deletions

File tree

‎src/MW5.Api/Concrete/WmsProviderDef.cs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ public IEnvelope BoundingBox
6060
}
6161
}
6262

63-
public int CrsEpsg
63+
public int Projection
6464
{
6565
get { return _provider.CrsEpsg; }
6666
set { _provider.CrsEpsg = value; }
6767
}
6868

69-
public string LayersCsv
69+
public string Layers
7070
{
7171
get { return _provider.LayersCsv; }
7272
set { _provider.LayersCsv = value; }
@@ -83,5 +83,11 @@ public int Id
8383
get { return _provider.Id; }
8484
set { _provider.Id = value; }
8585
}
86+
87+
public string Format
88+
{
89+
get { return _provider.Format; }
90+
set { _provider.Format = value; }
91+
}
8692
}
8793
}

‎src/MW5.Plugins/Helpers/ConfigPathHelper.cs‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,17 @@ public static string GetToolbarConfigPath()
4141

4242
public static string GetToolsConfigPath()
4343
{
44-
// TODO: change to app data folder after testing
45-
return AssemblyHelper.GetAppFolder() + @"\Config\Tools\";
44+
return GetConfigPath() + @"\Tools\";
4645
}
4746

4847
public static string GetDriversConfigPath()
4948
{
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\";
5255
}
5356
}
5457
}

‎src/MW5.Plugins/Helpers/ResourcePathHelper.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public static string GetTexturesPath()
2222
return GetStylesPath() + @"Textures\";
2323
}
2424

25+
/// <summary>
26+
/// Gets path of the styles folder.
27+
/// </summary>
28+
/// <returns></returns>
2529
public static string GetStylesPath()
2630
{
2731
return AssemblyHelper.GetAppFolder() + @"\Styles\";

‎src/MW5.Tiles/Helpers/BrutileHelper.cs‎

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
using BruTile.Extensions;
99
using BruTile.Wms;
1010
using MW5.Api.Concrete;
11+
using MW5.Api.Interfaces;
1112
using MW5.Shared;
13+
using Layer = BruTile.Wms.Layer;
1214

1315
namespace MW5.Tiles.Helpers
1416
{
@@ -29,28 +31,87 @@ private static Stream GetRemoteXmlStream(Uri uri)
2931

3032
public static WmsCapabilities GetWmsCapabilities(string url)
3133
{
32-
string requestUrl = GetRequestUrl(url);
33-
34-
using (var stream = GetRemoteXmlStream(new Uri(requestUrl)))
34+
using (var stream = GetWmsCapabilitiesStream(url))
3535
{
3636
return new WmsCapabilities(stream);
3737
}
3838
}
3939

40+
public static Stream GetWmsCapabilitiesStream(string url)
41+
{
42+
string requestUrl = GetRequestUrl(url);
43+
44+
return GetRemoteXmlStream(new Uri(requestUrl));
45+
}
46+
4047
/// <summary>
4148
/// Creates MapWinGIS WMS provider based GetCapabilities definition and layers selected by user.
4249
/// </summary>
4350
public static WmsProviderDef CreateProvider(
4451
this WmsCapabilities capabilities,
45-
IEnumerable<BruTile.Wms.Layer> layers)
52+
IEnumerable<Layer> layers, string serverUrl)
4653
{
47-
string layersCsv = StringHelper.Join(layers.Select(l => l.Name), ",");
54+
var layer = layers.FirstOrDefault();
55+
if (layer == null)
56+
{
57+
return null;
58+
}
4859

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+
};
5169

52-
// TODO: add other parameters
5370
return provider;
5471
}
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+
}
55116
}
56117
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

‎src/MW5.Tiles/MW5.Tiles.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
<Compile Include="Enums\Enums.cs" />
8888
<Compile Include="Helpers\BrutileHelper.cs" />
8989
<Compile Include="Helpers\ProviderManager.cs" />
90+
<Compile Include="Helpers\WmsCapabilitiesCache.cs" />
9091
<Compile Include="Model\TmsProvider.cs" />
9192
<Compile Include="Model\TmsProviderList.cs" />
9293
<Compile Include="Properties\AssemblyInfo.cs" />

‎src/MW5.Tiles/Views/WmsCapabilitiesPresenter.cs‎

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.IO;
45
using System.Linq;
56
using System.Text;
67
using System.Threading.Tasks;
8+
using System.Windows.Forms;
79
using BruTile.Wms;
810
using MW5.Plugins.Concrete;
911
using MW5.Plugins.Interfaces;
@@ -93,11 +95,14 @@ private void AddProvider()
9395
MessageService.Current.Info("No layers are selected");
9496
}
9597

96-
var provider = Model.Capabilities.CreateProvider(layers);
98+
var provider = Model.Capabilities.CreateProvider(layers, server.Url);
9799

98100
var providers = _context.Map.Tiles.WmsProviders;
99101
providers.Add(provider);
100102

103+
_context.Map.Lock();
104+
_context.Map.Unlock();
105+
101106
Debug.Print("WMS providers count: " + providers.Count);
102107
}
103108

@@ -109,25 +114,55 @@ private void ConnectServer()
109114
var server = SelectedServer;
110115
if (server == null) return;
111116

117+
var capabilities = WmsCapabilitiesCache.Load(server.Name);
118+
if (capabilities != null)
119+
{
120+
Model.Capabilities = capabilities;
121+
View.UpdateView();
122+
return;
123+
}
124+
112125
View.ShowHourglass();
113126

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))
116144
{
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+
}
131166
}
132167

133168
/// <summary>

‎src/MW5/Helpers/MapInitializer.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public static void ApplyConfig(this IMuteMap map, IConfigService configService)
7272
map.ShowCoordinates = config.ShowCoordinates ? config.CoordinatesDisplay : CoordinatesDisplay.None;
7373
map.ShowCoordinatesFormat = config.CoordinateAngleFormat;
7474

75+
map.ReuseTileBuffer = config.ReuseTileBuffer;
7576
map.InertiaOnPanning = config.InnertiaOnPanning;
7677
map.AnimationOnZooming = config.AnimationOnZooming;
7778
map.ZoomBoxStyle = config.ZoomBoxStyle;

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL