// Copyright (C) Stichting Deltares and State of the Netherlands 2025. All rights reserved. // // This file is part of Riskeer. // // Riskeer 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.ComponentModel; 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) { KnownTileSource knownTileSourceEquivalent = WellKnownTileSourceToKnownTileSource(wellKnownTileSource); ITileSource tileSource = TileSourceFactory.Instance.GetKnownTileSource(knownTileSourceEquivalent); return new WellKnownTileSourceLayerConfiguration(knownTileSourceEquivalent, tileSource); } protected override IConfiguration OnClone() { return new WellKnownTileSourceLayerConfiguration(PersistentCacheDirectoryPath, knownTileSource); } protected override void OnInitialize() { Initialized = true; } /// /// Returns the equivalent of . /// /// The tile provider to be used. /// The equivalent of the . /// Thrown when /// is not a valid enum value of . /// Thrown when /// is not a supported member. private static KnownTileSource WellKnownTileSourceToKnownTileSource(WellKnownTileSource wellKnownTileSource) { if (!Enum.IsDefined(typeof(WellKnownTileSource), wellKnownTileSource)) { throw new InvalidEnumArgumentException(nameof(wellKnownTileSource), (int) wellKnownTileSource, typeof(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(); } } private static string SuggestTileCachePath(ITileSource tileSource, KnownTileSource knownTileSource) { ITileSchema tileSchema = tileSource.Schema; string host = knownTileSource.ToString(); string format = tileSchema.Format; foreach (char c in Path.GetInvalidFileNameChars()) { host = host.Replace(c, '$'); } return Path.Combine(BruTileSettings.PersistentCacheDirectoryRoot, "WellKnown", host, format); } } }