Skip to content

Commit

Permalink
Use mapping in WmtsRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldendulk committed Jun 7, 2020
1 parent 95be015 commit 1d7e095
Showing 4 changed files with 37 additions and 20 deletions.
29 changes: 19 additions & 10 deletions BruTile/Wmts/WmtsParser.cs
Original file line number Diff line number Diff line change
@@ -70,7 +70,7 @@ public static IEnumerable<HttpTileSource> Parse(Stream source,
/// <param name="capabilties">The capabilities document</param>
/// <param name="tileSchemas">A set of</param>
/// <returns></returns>
private static IEnumerable<HttpTileSource> GetLayers(Capabilities capabilties, List<ITileSchema> tileSchemas)
private static IEnumerable<HttpTileSource> GetLayers(Capabilities capabilties, List<WmtsTileSchema> tileSchemas)
{
var tileSources = new List<HttpTileSource>();

@@ -88,28 +88,32 @@ private static IEnumerable<HttpTileSource> GetLayers(Capabilities capabilties, L
{
if (!format.StartsWith("image/")) continue;

var tileMatrixSet = tileMatrixLink.TileMatrixSet;
var tileSchema = tileSchemas.First(s => Equals(s.Name, tileMatrixSet));

IRequest wmtsRequest;

if (layer.ResourceURL == null)
{
wmtsRequest = new WmtsRequest(CreateResourceUrlsFromOperations(
var resourceUrls = CreateResourceUrlsFromOperations(
capabilties.OperationsMetadata.Operation,
format,
capabilties.ServiceIdentification.ServiceTypeVersion.First(),
layer.Identifier.Value,
style.Identifier.Value,
tileMatrixLink.TileMatrixSet));
tileMatrixLink.TileMatrixSet);

wmtsRequest = new WmtsRequest(resourceUrls, tileSchema.LevelToIdentifier);
}
else
{
wmtsRequest = new WmtsRequest(CreateResourceUrlsFromResourceUrlNode(
var resourceUrls = CreateResourceUrlsFromResourceUrlNode(
layer.ResourceURL,
style.Identifier.Value,
tileMatrixLink.TileMatrixSet));
}
tileMatrixLink.TileMatrixSet);

var tileMatrixSet = tileMatrixLink.TileMatrixSet;
var tileSchema = (WmtsTileSchema)tileSchemas.First(s => Equals(s.Name, tileMatrixSet));
wmtsRequest = new WmtsRequest(resourceUrls, tileSchema.LevelToIdentifier);
}

//var layerName = layer.Identifier.Value;
var styleName = style.Identifier.Value;
@@ -199,7 +203,7 @@ private static IEnumerable<ResourceUrl> CreateResourceUrlsFromResourceUrlNode(
return resourceUrls;
}

private static List<ITileSchema> GetTileMatrixSets(IEnumerable<TileMatrixSet> tileMatrixSets,
private static List<WmtsTileSchema> GetTileMatrixSets(IEnumerable<TileMatrixSet> tileMatrixSets,
BoundingBoxAxisOrderInterpretation bbaoi)
{
// Get a set of well known scale sets. For these we don't need to have
@@ -210,7 +214,7 @@ private static List<ITileSchema> GetTileMatrixSets(IEnumerable<TileMatrixSet> ti
// Unit of measure registry
var crsUnitOfMeasure = new CrsUnitOfMeasureRegistry();

var tileSchemas = new List<ITileSchema>();
var tileSchemas = new List<WmtsTileSchema>();
foreach (var tileMatrixSet in tileMatrixSets)
{
// Check if a Well-Known scale set is used, either by Identifier or WellKnownScaleSet property
@@ -256,6 +260,11 @@ private static List<ITileSchema> GetTileMatrixSets(IEnumerable<TileMatrixSet> ti
tileSchema.Srs = supportedCrs;
tileSchema.SupportedSRS = crs;

foreach (var tileMatrix in tileMatrices)
{
tileSchema.LevelToIdentifier[tileMatrix.Level] = tileMatrix.Identifier.Value;
}

// record the tile schema
tileSchemas.Add(tileSchema);
}
8 changes: 6 additions & 2 deletions BruTile/Wmts/WmtsRequest.cs
Original file line number Diff line number Diff line change
@@ -20,20 +20,24 @@ public class WmtsRequest : IRequest
private readonly List<ResourceUrl> _resourceUrls;
private int _resourceUrlCounter;
private readonly object _syncLock = new object();
private readonly IDictionary<int, string> _levelToIdentifier;

public WmtsRequest(IEnumerable<ResourceUrl> resourceUrls)
public WmtsRequest(IEnumerable<ResourceUrl> resourceUrls, IDictionary<int, string> levelToIdentifier)
{
_resourceUrls = resourceUrls.ToList();
_levelToIdentifier = levelToIdentifier;
}

public Uri GetUri(TileInfo info)
{
var urlFormatter = GetNextServerNode();
var stringBuilder = new StringBuilder(urlFormatter.Template);

// For wmts we need to map the level int to an identifier of type string.
var identifier = _levelToIdentifier[info.Index.Level];
stringBuilder.Replace(XTag, info.Index.Col.ToString(CultureInfo.InvariantCulture));
stringBuilder.Replace(YTag, info.Index.Row.ToString(CultureInfo.InvariantCulture));
stringBuilder.Replace(ZTag, info.Index.Level.ToString(CultureInfo.InvariantCulture));
stringBuilder.Replace(ZTag, identifier);

return new Uri(stringBuilder.ToString());
}
10 changes: 6 additions & 4 deletions BruTile/Wmts/WmtsTileSchema.cs
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ internal WmtsTileSchema()
Resolutions = new Dictionary<int, Resolution>();
YAxis = YAxis.OSM;
}

/// <summary>
/// Gets an identifier for the layer and tile matrix set.
/// </summary>
@@ -66,7 +66,7 @@ internal WmtsTileSchema CreateSpecific(string title, string layer, string @abstr
throw new ArgumentNullException("tileMatrixSet");
if (string.IsNullOrEmpty(format))
throw new ArgumentNullException("format");

if (@abstract == null) @abstract = string.Empty;
if (string.IsNullOrEmpty(style)) style = "null";

@@ -130,7 +130,7 @@ public Extent Extent
/// Gets a value indicating the orientation of the y-axis
/// </summary>
public YAxis YAxis { get; set; }

public IDictionary<int, Resolution> Resolutions { get; set; }

public int GetTileWidth(int level)
@@ -162,7 +162,7 @@ public long GetMatrixHeight(int level)
{
return Resolutions[level].MatrixHeight;
}

/// <summary>
/// Returns a List of TileInfos that cover the provided extent.
/// </summary>
@@ -191,5 +191,7 @@ public int GetMatrixFirstRow(int level)
{
return 0; // always zero because WMTS can not have a discrepancy between schema origin and bbox origin
}

public IDictionary<int, string> LevelToIdentifier { get; set;} = new Dictionary<int, string>();
}
}
10 changes: 6 additions & 4 deletions Tests/BruTile.Tests/Wmts/WmtsTests.cs
Original file line number Diff line number Diff line change
@@ -141,14 +141,15 @@ public void TestWmtsRequest()
{
// arrange
var resourceUrls = CreateResourceUrls();
var wmtsRequest = new WmtsRequest(resourceUrls);
var levelToIdentifier = new Dictionary<int, string> { [14] = "level-14" } ;
var wmtsRequest = new WmtsRequest(resourceUrls, levelToIdentifier);

// act
var url1 = wmtsRequest.GetUri(new TileInfo { Index = new TileIndex(8938, 5680, 14) });
var url2 = wmtsRequest.GetUri(new TileInfo { Index = new TileIndex(8938, 5680, 14) });

// assert
Assert.True(url1.ToString().Equals("http://maps1.wien.gv.at/wmts/lb/farbe/google3857/14/5680/8938.jpeg"));
Assert.True(url1.ToString().Equals("http://maps1.wien.gv.at/wmts/lb/farbe/google3857/level-14/5680/8938.jpeg"));
Assert.True(url2.ToString().Contains("maps2"));
}

@@ -157,7 +158,8 @@ public void TestWmtsRequestInParallel()
{
// arrange
var resourceUrls = CreateResourceUrls();
var request = new WmtsRequest(resourceUrls);
var levelToIdentifier = new Dictionary<int, string> { [14] = "level-14" };
var request = new WmtsRequest(resourceUrls, levelToIdentifier);
var urls = new ConcurrentBag<Uri>(); // List is not thread save
var tileInfo = new TileInfo {Index = new TileIndex(8938, 5680, 14)};

@@ -167,7 +169,7 @@ public void TestWmtsRequestInParallel()
Parallel.ForEach(requests, r => urls.Add(r()));

// assert
var count = urls.Count(u => u.ToString() == "http://maps1.wien.gv.at/wmts/lb/farbe/google3857/14/5680/8938.jpeg");
var count = urls.Count(u => u.ToString() == "http://maps1.wien.gv.at/wmts/lb/farbe/google3857/level-14/5680/8938.jpeg");
Assert.True(count == 50);
}

0 comments on commit 1d7e095

Please sign in to comment.