// Copyright (C) Stichting Deltares 2016. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.IO;
using BruTile;
using BruTile.Predefined;
using Core.Components.BruTile.Data;
using Core.Components.Gis.Data;
using Core.Components.Gis.Exceptions;
namespace Core.Components.BruTile.Configurations
{
///
/// A configuration for a connection to built-in web-based tile services.
///
///
/// Original source: https://github.com/FObermaier/DotSpatial.Plugins/blob/master/DotSpatial.Plugins.BruTileLayer/Configuration/KnownTileLayerConfiguration.cs
/// Original license: http://www.apache.org/licenses/LICENSE-2.0.html
///
public class WellKnownTileSourceLayerConfiguration : PersistentCacheConfiguration
{
private readonly KnownTileSource knownTileSource;
///
/// Creates an instance of .
///
/// The built-in tile provider to be used.
/// The directory path to the persistent tile cache.
/// Thrown when
/// is an invalid folder path.
/// Thrown when
/// isn't a supported member.
/// Thrown when creating the file
/// cache failed.
private WellKnownTileSourceLayerConfiguration(string persistentCacheDirectoryPath, KnownTileSource knownTileSource)
: base(persistentCacheDirectoryPath)
{
this.knownTileSource = knownTileSource;
ITileSource tileSource = TileSourceFactory.Instance.GetKnownTileSource(knownTileSource);
InitializeFromTileSource(tileSource);
}
///
/// Creates a new initialized instance of .
///
/// The built-in tile provider to be used.
/// The tile source corresponding to .
/// Thrown when creating the file
/// cache failed.
private WellKnownTileSourceLayerConfiguration(KnownTileSource knownTileSource, ITileSource tileSource)
: base(SuggestTileCachePath(tileSource, knownTileSource))
{
this.knownTileSource = knownTileSource;
InitializeFromTileSource(tileSource);
}
///
/// Creates a fully initialized instance of .
///
/// The tile provider to be used.
/// The new .
/// Thrown when
/// isn't a supported member.
/// Thrown when creating the file
/// cache failed.
public static WellKnownTileSourceLayerConfiguration CreateInitializedConfiguration(WellKnownTileSource wellKnownTileSource)
{
return CreateInitializedConfiguration(WellKnownTileSourceToKnownTileSource(wellKnownTileSource));
}
public override IConfiguration Clone()
{
ThrowExceptionIfDisposed();
return new WellKnownTileSourceLayerConfiguration(PersistentCacheDirectoryPath, knownTileSource);
}
public override void Initialize()
{
ThrowExceptionIfDisposed();
Initialized = true;
}
///
/// Creates a fully initialized instance of .
///
/// The built-in tile provider to be used.
/// The new .
/// Thrown when
/// isn't a supported member.
/// Thrown when creating the file
/// cache failed.
private static WellKnownTileSourceLayerConfiguration CreateInitializedConfiguration(KnownTileSource knownTileSource)
{
ITileSource tileSource = TileSourceFactory.Instance.GetKnownTileSource(knownTileSource);
return new WellKnownTileSourceLayerConfiguration(knownTileSource, tileSource);
}
///
/// Returns the equivalent of .
///
/// The tile provider to be used.
/// The equivalent of the .
/// Thrown when
/// is not a supported member.
private static KnownTileSource WellKnownTileSourceToKnownTileSource(WellKnownTileSource wellKnownTileSource)
{
switch (wellKnownTileSource)
{
case WellKnownTileSource.BingAerial:
return KnownTileSource.BingAerial;
case WellKnownTileSource.BingHybrid:
return KnownTileSource.BingHybrid;
case WellKnownTileSource.BingRoads:
return KnownTileSource.BingRoads;
case WellKnownTileSource.EsriWorldTopo:
return KnownTileSource.EsriWorldTopo;
case WellKnownTileSource.EsriWorldShadedRelief:
return KnownTileSource.EsriWorldShadedRelief;
case WellKnownTileSource.OpenStreetMap:
return KnownTileSource.OpenStreetMap;
default:
throw new NotSupportedException($"Unknown value '{wellKnownTileSource}' for '{nameof(wellKnownTileSource)}'");
}
}
private static string SuggestTileCachePath(ITileSource tileSource, KnownTileSource knownTileSource)
{
ITileSchema tileSchema = tileSource.Schema;
string host = knownTileSource.ToString();
string format = tileSchema.Format;
foreach (var c in Path.GetInvalidFileNameChars())
{
host = host.Replace(c, '$');
}
return Path.Combine(BruTileSettings.PersistentCacheDirectoryRoot, "WellKnown", host, format);
}
}
}