Skip to content

Commit

Permalink
Adding temporary workaround for user agent issue #668
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldendulk committed Jul 26, 2019
1 parent 5340359 commit 6954069
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 2 deletions.
48 changes: 48 additions & 0 deletions Mapsui/Utilities/CustomHttpTileProvider.cs
@@ -0,0 +1,48 @@
// Copyright (c) BruTile developers team. All rights reserved. See License.txt in the project root for license information.

using System;
using System.Net.Http;
using BruTile;
using BruTile.Cache;
using BruTile.Web;

namespace Mapsui.Utilities
{
[Obsolete("workaround for user agent issue")]
public class CustomHttpTileProvider : ITileProvider, IRequest
{
private readonly Func<Uri, byte[]> _fetchTile;
private readonly IRequest _request;
private readonly HttpClient _httpClient = new HttpClient();

public CustomHttpTileProvider(IRequest request = null, IPersistentCache<byte[]> persistentCache = null,
Func<Uri, byte[]> fetchTile = null)
{
_request = request;
PersistentCache = persistentCache ?? new NullCache();
_fetchTile = fetchTile ?? FetchTile;
_httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mapsui");
}

private byte[] FetchTile(Uri arg)
{
return _httpClient.GetByteArrayAsync(arg).ConfigureAwait(false).GetAwaiter().GetResult();
}

public IPersistentCache<byte[]> PersistentCache { get; }

public Uri GetUri(TileInfo tileInfo)
{
return _request.GetUri(tileInfo);
}

public byte[] GetTile(TileInfo tileInfo)
{
var bytes = PersistentCache.Find(tileInfo.Index);
if (bytes != null) return bytes;
bytes = _fetchTile(_request.GetUri(tileInfo));
if (bytes != null) PersistentCache.Add(tileInfo.Index, bytes);
return bytes;
}
}
}
53 changes: 53 additions & 0 deletions Mapsui/Utilities/CustomHttpTileSource.cs
@@ -0,0 +1,53 @@
// Copyright (c) BruTile developers team. All rights reserved. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using BruTile;
using BruTile.Cache;
using BruTile.Web;

namespace Mapsui.Utilities
{
[Obsolete("workaround for user agent issue")]
public class CustomHttpTileSource : ITileSource, IRequest
{
private readonly CustomHttpTileProvider _provider;

public CustomHttpTileSource(ITileSchema tileSchema, string urlFormatter, IEnumerable<string> serverNodes = null,
string apiKey = null, string name = null, IPersistentCache<byte[]> persistentCache = null,
Func<Uri, byte[]> tileFetcher = null, Attribution attribution = null)
: this(tileSchema, new BasicRequest(urlFormatter, serverNodes, apiKey), name, persistentCache, tileFetcher, attribution)
{
}

public CustomHttpTileSource(ITileSchema tileSchema, IRequest request, string name = null,
IPersistentCache<byte[]> persistentCache = null, Func<Uri, byte[]> tileFetcher = null, Attribution attibution = null)
{
_provider = new CustomHttpTileProvider(request, persistentCache, tileFetcher);
Schema = tileSchema;
Name = name ?? string.Empty;
Attribution = attibution ?? new Attribution();
}

public IPersistentCache<byte[]> PersistentCache => _provider.PersistentCache;

public Uri GetUri(TileInfo tileInfo)
{
return _provider.GetUri(tileInfo);
}

public ITileSchema Schema { get; }

public string Name { get; set; }

public Attribution Attribution { get; set; }

/// <summary>
/// Gets the actual image content of the tile as byte array
/// </summary>
public virtual byte[] GetTile(TileInfo tileInfo)
{
return _provider.GetTile(tileInfo);
}
}
}
4 changes: 2 additions & 2 deletions Mapsui/Utilities/OpenStreetMap.cs
Expand Up @@ -14,9 +14,9 @@ public static TileLayer CreateTileLayer()
return new TileLayer(CreateTileSource()) { Name = "OpenStreetMap" };
}

private static HttpTileSource CreateTileSource()
private static CustomHttpTileSource CreateTileSource()
{
return new HttpTileSource(new GlobalSphericalMercator(),
return new CustomHttpTileSource(new GlobalSphericalMercator(),
"https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
new[] { "a", "b", "c" }, name: "OpenStreetMap",
attribution: OpenStreetMapAttribution);
Expand Down

0 comments on commit 6954069

Please sign in to comment.