#region Disclaimer / License // Copyright (C) 2014, Jackie Ng // http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie@gmail.com // // This library 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 2.1 of the License, or (at your option) any later version. // // This library 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 library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // #endregion Disclaimer / License using OSGeo.MapGuide.ObjectModels.Common; using OSGeo.MapGuide.ObjectModels.LayerDefinition; using OSGeo.MapGuide.ObjectModels.MapDefinition; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; namespace OSGeo.MapGuide.ObjectModels.TileSetDefinition { public interface ITileSetDefinition : IResource, ITileSetAbstract { ITileStoreParameters TileStoreParameters { get; } IEnvelope Extents { get; set; } } public interface ITileStoreParameters { string TileProvider { get; set; } void AddParameter(string name, string value); void SetParameter(string name, string value); IEnumerable Parameters { get; } void ClearParameters(); event EventHandler ParametersChanged; } public interface ITileSetAbstract { void SetFiniteDisplayScales(IEnumerable scales); /// /// Adds the finite display scale. The implementation may internally sort after adding the added item /// /// The value. void AddFiniteDisplayScale(double value); /// /// Removes the finite display scale. /// /// The value. void RemoveFiniteDisplayScale(double value); /// /// Gets the scale count. /// /// The scale count. int ScaleCount { get; } /// /// Removes the scale at the specified index /// /// The index. void RemoveScaleAt(int index); /// /// Gets the scale at the specified index /// /// The index. /// double GetScaleAt(int index); /// /// Removes all scales. /// void RemoveAllScales(); IEnumerable FiniteDisplayScale { get; } /// /// Gets whether this tile set supports under certain conditions. If false, the caller /// should check if is true in order to /// safely invoke any scale based operations /// bool SupportsCustomFiniteDisplayScalesUnconditionally { get; } /// /// Gets whether this tile set supports custom finite display scales. If false, none /// of the scale operations should be used /// bool SupportsCustomFiniteDisplayScales { get; } IEnumerable BaseMapLayerGroups { get; } IEnumerable GetLayersForGroup(string groupName); /// /// Gets the group count. /// /// The group count. int GroupCount { get; } /// /// Gets the group at the specified index /// /// The index. /// IBaseMapGroup GetGroupAt(int index); /// /// Adds the base layer group. /// /// The name. /// IBaseMapGroup AddBaseLayerGroup(string name); /// /// Removes the base layer group. /// /// The group. void RemoveBaseLayerGroup(IBaseMapGroup group); } public static class ExtensionMethods { /// /// Gets the number of base layers /// /// /// public static int GetBaseLayerCount(this ITileSetAbstract map) { Check.ArgumentNotNull(map, nameof(map)); return map.BaseMapLayerGroups.SelectMany(g => g.BaseMapLayer).Count(); } /// /// Gets the minimum finite display scale /// /// /// public static double GetMinScale(this ITileSetAbstract map) { Check.ArgumentNotNull(map, nameof(map)); if (map.ScaleCount == 0) return 0.0; return map.FiniteDisplayScale.First(); } /// /// Gets the maximum finite display scale /// /// /// public static double GetMaxScale(this ITileSetAbstract map) { Check.ArgumentNotNull(map, nameof(map)); if (map.ScaleCount == 0) return 0.0; return map.FiniteDisplayScale.Last(); } /// /// Gets the parent group for the specified layer /// /// /// /// public static IBaseMapGroup GetGroupForLayer(this ITileSetAbstract map, IBaseMapLayer layer) { Check.ArgumentNotNull(map, nameof(map)); foreach (var group in map.BaseMapLayerGroups) { foreach (var tl in group.BaseMapLayer) { if (tl == layer) return group; } } return null; } /// /// Gets whether this base map group has tiled layers /// /// /// public static bool HasLayers(this IBaseMapGroup grp) { Check.ArgumentNotNull(grp, nameof(grp)); return new List(grp.BaseMapLayer).Count > 0; } /// /// Gets whether this base map has tiled layers /// /// /// public static bool HasLayers(this ITileSetAbstract map) { Check.ArgumentNotNull(map, nameof(map)); if (!map.HasGroups()) return false; foreach (var group in map.BaseMapLayerGroups) { if (group.HasLayers()) return true; } return false; } /// /// Gets whether this base map has groups /// /// /// public static bool HasGroups(this ITileSetAbstract map) { Check.ArgumentNotNull(map, nameof(map)); return new List(map.BaseMapLayerGroups).Count > 0; } /// /// Gets the first base map group /// /// /// public static IBaseMapGroup GetFirstGroup(this ITileSetAbstract map) { Check.ArgumentNotNull(map, nameof(map)); var list = new List(map.BaseMapLayerGroups); if (list.Count > 0) return list[0]; return null; } /// /// Gets the base layer of the specified name /// /// /// /// public static IBaseMapLayer GetBaseLayerByName(this ITileSetAbstract map, string layerName) { Check.ArgumentNotNull(map, nameof(map)); Check.ArgumentNotEmpty(layerName, nameof(layerName)); foreach (var group in map.BaseMapLayerGroups) { foreach (var layer in group.BaseMapLayer) { if (layerName.Equals(layer.Name)) return layer; } } return null; } /// /// Gets whether a base layer of the specified name exists. /// /// /// /// public static bool LayerExists(this ITileSetAbstract map, string layerName) { Check.ArgumentNotNull(map, nameof(map)); Check.ArgumentNotEmpty(layerName, nameof(layerName)); return map.GetBaseLayerByName(layerName) != null; } /// /// Gets the base map group of the specified name /// /// /// /// public static IBaseMapGroup GetGroup(this ITileSetAbstract map, string groupName) { Check.ArgumentNotNull(map, nameof(map)); Check.ArgumentNotEmpty(groupName, nameof(groupName)); foreach (var group in map.BaseMapLayerGroups) { if (groupName.Equals(group.Name)) return group; } return null; } /// /// Gets whether the specified base map group exists /// /// /// /// public static bool GroupExists(this ITileSetAbstract map, string groupName) { Check.ArgumentNotNull(map, nameof(map)); Check.ArgumentNotEmpty(groupName, nameof(groupName)); foreach (var group in map.BaseMapLayerGroups) { if (groupName.Equals(group.Name)) return true; } return false; } /// /// Gets the tiled layers for the specified base map group /// /// /// /// public static IEnumerable GetLayersForGroup(this ITileSetAbstract map, string groupName) { Check.ArgumentNotNull(map, nameof(map)); Check.ArgumentNotEmpty(groupName, nameof(groupName)); foreach (var group in map.BaseMapLayerGroups) { if (groupName.Equals(group.Name)) { return group.BaseMapLayer; } } return new IBaseMapLayer[0]; } /// /// Removes the given base layer group from the Map Definition /// /// /// /// public static void RemoveBaseLayerGroup(this ITileSetDefinition map, IBaseMapGroup group) { Check.ArgumentNotNull(map, nameof(map)); if (null == group) return; map.RemoveBaseLayerGroup(group); } /// /// Gets whether the specified base group exists in the tile set /// /// /// /// public static bool GroupExists(this ITileSetDefinition tileSet, string groupName) { Check.ArgumentNotNull(tileSet, nameof(tileSet)); Check.ArgumentNotEmpty(groupName, nameof(groupName)); foreach (var group in tileSet.BaseMapLayerGroups) { if (groupName.Equals(group.Name)) return true; } return false; } /// /// Gets the tile set parameter of the specified name /// /// /// /// public static INameStringPair GetParameter(this ITileSetDefinition tileSet, string name) { Check.ArgumentNotNull(tileSet, nameof(tileSet)); return tileSet.TileStoreParameters.Parameters.FirstOrDefault(p => p.Name == name); } /// /// Gets the coordinate system of this tile set. Must be using the default tile provider /// /// /// public static string GetDefaultCoordinateSystem(this ITileSetDefinition tileSet) { Check.ArgumentNotNull(tileSet, nameof(tileSet)); var p = tileSet.GetParameter("CoordinateSystem"); //NOXLATE if (p != null) return p.Value; return null; } /// /// Sets the coordinate system of this tile set. Must be using the default tile provider. /// /// /// public static void SetDefaultCoordinateSystem(this ITileSetDefinition tileSet, string coordinateSystem) { Check.ArgumentNotNull(tileSet, nameof(tileSet)); Check.ArgumentNotEmpty(coordinateSystem, nameof(coordinateSystem)); if (tileSet.TileStoreParameters.TileProvider != "Default") //NOXLATE throw new InvalidOperationException(string.Format(Strings.ParameterNotApplicableForTileProvider, "CoordinateSystem", tileSet.TileStoreParameters.TileProvider)); tileSet.TileStoreParameters.SetParameter("CoordinateSystem", coordinateSystem); //NOXLATE } /// /// Gets the finite scale list of this tile set. Must be using the default tile provider. /// /// /// public static double[] GetDefaultFiniteScaleList(this ITileSetDefinition tileSet) { Check.ArgumentNotNull(tileSet, nameof(tileSet)); var p = tileSet.GetParameter("FiniteScaleList"); //NOXLATE if (p != null && !string.IsNullOrEmpty(p.Value)) return p.Value.Split(',').Select(x => x.Trim()).Select(x => Convert.ToDouble(x)).OrderBy(s => s).ToArray(); return new double[0]; } /// /// Sets the finite scale list of this tile set. Must be using the default tile provider. /// /// /// public static void SetDefaultFiniteScaleList(this ITileSetDefinition tileSet, IEnumerable scales) { Check.ArgumentNotNull(tileSet, nameof(tileSet)); Check.ArgumentNotNull(scales, nameof(scales)); if (tileSet.TileStoreParameters.TileProvider != "Default") //NOXLATE throw new InvalidOperationException(string.Format(Strings.ParameterNotApplicableForTileProvider, "FiniteScaleList", tileSet.TileStoreParameters.TileProvider)); //NOXLATE string str = string.Join(",", scales.OrderByDescending(x => x).Select(x => x.ToString(CultureInfo.InvariantCulture)).ToArray()); tileSet.TileStoreParameters.SetParameter("FiniteScaleList", str); //NOXLATE } /// /// Gets the tile path of this tile set /// /// /// public static string GetTilePath(this ITileSetDefinition tileSet) { Check.ArgumentNotNull(tileSet, nameof(tileSet)); var p = tileSet.GetParameter("TilePath"); //NOXLATE if (p != null) return p.Value; return null; } /// /// Sets the path of this tile set /// /// /// public static void SetTilePath(this ITileSetDefinition tileSet, string path) { Check.ArgumentNotNull(tileSet, nameof(tileSet)); Check.ArgumentNotEmpty(path, nameof(path)); tileSet.TileStoreParameters.SetParameter("TilePath", path); //NOXLATE } /// /// Gets the width of this tile set. Must be using the default tile provider. /// /// /// public static int? GetDefaultTileWidth(this ITileSetDefinition tileSet) { Check.ArgumentNotNull(tileSet, nameof(tileSet)); var p = tileSet.GetParameter("TileWidth"); //NOXLATE if (p != null) return Convert.ToInt32(p.Value); return null; } /// /// Sets the width of this tile set. Must be using the default tile provider. /// /// /// public static void SetDefaultTileWidth(this ITileSetDefinition tileSet, int value) { Check.ArgumentNotNull(tileSet, nameof(tileSet)); if (tileSet.TileStoreParameters.TileProvider == "XYZ") //NOXLATE throw new InvalidOperationException(string.Format(Strings.ParameterNotApplicableForTileProvider, "TileWidth", "XYZ")); //NOXLATE tileSet.TileStoreParameters.SetParameter("TileWidth", value.ToString(CultureInfo.InvariantCulture)); //NOXLATE } /// /// Gets the width of this tile set. Must be using the default tile provider. /// /// /// public static int? GetDefaultTileHeight(this ITileSetDefinition tileSet) { Check.ArgumentNotNull(tileSet, nameof(tileSet)); var p = tileSet.GetParameter("TileHeight"); //NOXLATE if (p != null) return Convert.ToInt32(p.Value); return null; } /// /// Sets the height of this tile set. Must be using the default tile provider. /// /// /// public static void SetDefaultTileHeight(this ITileSetDefinition tileSet, int value) { Check.ArgumentNotNull(tileSet, nameof(tileSet)); if (tileSet.TileStoreParameters.TileProvider == "XYZ") throw new InvalidOperationException(string.Format(Strings.ParameterNotApplicableForTileProvider, "TileHeight", "XYZ")); //NOXLATE tileSet.TileStoreParameters.SetParameter("TileHeight", value.ToString(CultureInfo.InvariantCulture)); //NOXLATE } /// /// Gets the image format of this tile set /// /// /// public static string GetTileFormat(this ITileSetDefinition tileSet) { Check.ArgumentNotNull(tileSet, nameof(tileSet)); var p = tileSet.GetParameter("TileFormat"); //NOXLATE if (p != null) return p.Value; return null; } /// /// Sets the image format of this tile set /// /// /// public static void SetTileFormat(this ITileSetDefinition tileSet, string format) { Check.ArgumentNotNull(tileSet, nameof(tileSet)); Check.ArgumentNotEmpty(format, nameof(format)); tileSet.TileStoreParameters.SetParameter("TileFormat", format); //NOXLATE } /// /// Sets XYZ provider parameters. Any existing parameters are cleared /// /// /// /// public static void SetXYZProviderParameters(this ITileSetDefinition tileSet, string tileFormat = "PNG", string tilePath = "%MG_TILE_CACHE_PATH%") { Check.ArgumentNotNull(tileSet, nameof(tileSet)); var param = tileSet.TileStoreParameters; param.TileProvider = "XYZ"; //NOXLATE param.ClearParameters(); param.AddParameter("TileFormat", tileFormat); //NOXLATE param.AddParameter("TilePath", tilePath); //NOXLATE } /// /// Sets default provider parameters. Any existing parameters are cleared /// /// /// /// /// /// /// /// public static void SetDefaultProviderParameters(this ITileSetDefinition tileSet, int tileWidth, int tileHeight, string coordinateSystem, double [] finiteScaleList, string tileFormat = "PNG", //NOXLATE string tilePath = "%MG_TILE_CACHE_PATH%") //NOXLATE { Check.ArgumentNotNull(tileSet, nameof(tileSet)); var param = tileSet.TileStoreParameters; param.TileProvider = "Default"; //NOXLATE param.ClearParameters(); param.AddParameter("TileWidth", tileWidth.ToString(CultureInfo.InvariantCulture)); //NOXLATE param.AddParameter("TileHeight", tileHeight.ToString(CultureInfo.InvariantCulture)); //NOXLATE param.AddParameter("TileFormat", tileFormat); //NOXLATE param.AddParameter("CoordinateSystem", coordinateSystem); //NOXLATE string str = string.Join(",", finiteScaleList.OrderByDescending(x => x).Select(x => x.ToString(CultureInfo.InvariantCulture)).ToArray()); //NOXLATE param.AddParameter("FiniteScaleList", str); //NOXLATE param.AddParameter("TilePath", tilePath); //NOXLATE } } }