#region Disclaimer / License
// Copyright (C) 2010, 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
using System;
using System.Collections.Generic;
using System.Text;
using OSGeo.MapGuide.MaestroAPI.Resource;
using OSGeo.MapGuide.ObjectModels.Common;
using System.Drawing;
using System.ComponentModel;
using OSGeo.MapGuide.MaestroAPI;
using OSGeo.MapGuide.ObjectModels.WatermarkDefinition;
namespace OSGeo.MapGuide.ObjectModels.MapDefinition
{
///
/// Represents the base interface of map definitions and their runtime forms
///
public interface IMapDefinitionBase
{
///
/// Gets the name.
///
/// The name.
string Name { get; }
///
/// Gets the coordinate system. Layers whose coordinate system does
/// not match will be re-projecte to this coordinate system when rendering
///
/// The coordinate system.
string CoordinateSystem { get; }
///
/// Gets or sets the color of the background.
///
/// The color of the background.
Color BackgroundColor { get; set; }
}
///
/// Represents a Map Definition
///
public interface IMapDefinition : IResource, IMapDefinitionBase, INotifyPropertyChanged
{
///
/// If true, the first layer added to this map definition will automatically set the extents
/// based on that layer's added extents. Default is false
///
bool SetExtentsFromFirstAddedLayer { get; set; }
///
/// Gets or sets the name.
///
/// The name.
string Name { get; set; }
///
/// Gets or sets the coordinate system. Layers whose coordinate system does
/// not match will be re-projected to this coordinate system when rendering
///
/// The coordinate system.
string CoordinateSystem { get; set; }
///
/// Gets or sets the extents.
///
/// The extents.
IEnvelope Extents { get; set; }
///
/// Sets the extents.
///
/// The minx.
/// The miny.
/// The maxx.
/// The maxy.
void SetExtents(double minx, double miny, double maxx, double maxy);
///
/// Gets or sets the metadata.
///
/// The metadata.
string Metadata { get; set; }
///
/// Returns the base map section of this map definition. Ensure
/// is called first before accessing this property
///
IBaseMapDefinition BaseMap { get; }
///
/// Initializes the base map section of this map definition. Subsequent calls
/// do nothing, unless you have cleared the section via
///
void InitBaseMap();
///
/// Clears the base map section of this map definition. If you want to rebuild
/// this section, ensure is called
///
void RemoveBaseMap();
///
/// Gets the map layers.
///
/// The map layers.
IEnumerable MapLayer { get; }
///
/// Adds a layer to this map. If this is the first layer to be added, the coordinate system
/// of this map and its extents will be set to the coordinate system and extents of this layer
/// if this has not been set already.
///
///
///
///
///
IMapLayer AddLayer(string groupName, string layerName, string resourceId);
///
/// Adds a layer to this map. If this is the first layer to be added, the coordinate system
/// of this map and its extents will be set to the coordinate system and extents of this layer
/// if this has not been set already.
///
/// The layer to insert above in the draw order
/// The name of the group this layer belongs to. If null or empty, this layer will not belong to any group
/// The name of this layer. This must be unique
/// The layer definition id
/// The added layer
IMapLayer AddLayer(IMapLayer layerToInsertAbove, string groupName, string layerName, string resourceId);
///
/// Removes the layer.
///
/// The layer.
void RemoveLayer(IMapLayer layer);
///
/// Gets the index of the specified layer
///
/// The layer.
///
int GetIndex(IMapLayer layer);
///
/// Moves the layer up the draw order
///
/// The layer.
///
int MoveUp(IMapLayer layer);
///
/// Moves the layer down the draw order.
///
/// The layer.
///
int MoveDown(IMapLayer layer);
///
/// Gets the map layer groups.
///
/// The map layer groups.
IEnumerable MapLayerGroup { get; }
///
/// Adds the group.
///
/// Name of the group.
///
IMapLayerGroup AddGroup(string groupName);
///
/// Removes the group
///
///
void RemoveGroup(IMapLayerGroup group);
///
/// Gets the index of the specified group
///
///
///
int GetIndex(IMapLayerGroup group);
///
/// Moves the specified layer to the top of the draw order
///
///
void SetTopDrawOrder(IMapLayer layer);
///
/// Moves the specified layer to the bottom of the draw order
///
///
void SetBottomDrawOrder(IMapLayer layer);
///
/// Inserts the layer at the specified index
///
///
///
void InsertLayer(int idx, IMapLayer layer);
///
/// Moves a Map Group down the presentation order
///
///
int MoveDownGroup(IMapLayerGroup group);
///
/// Moves a Map Group up the presentation order
///
///
int MoveUpGroup(IMapLayerGroup group);
}
///
/// Represents a Map Definition with support for watermarks. Corresponds to schema version 2.3.0
///
public interface IMapDefinition2 : IMapDefinition, IWatermarkCollection
{
}
///
/// Extension methdo class
///
public static class BaseMapDefinitionExtensions
{
///
/// Gets the minimum finite display scale
///
///
///
public static double GetMinScale(this IBaseMapDefinition map)
{
Check.NotNull(map, "map");
if (map.ScaleCount == 0)
return 0.0;
var scales = new List(map.FiniteDisplayScale);
scales.Sort();
return scales[0];
}
///
/// Gets the maximum finite display scale
///
///
///
public static double GetMaxScale(this IBaseMapDefinition map)
{
Check.NotNull(map, "map");
if (map.ScaleCount == 0)
return 0.0;
var scales = new List(map.FiniteDisplayScale);
scales.Sort();
return scales[scales.Count - 1];
}
///
/// Gets the parent group for the specified layer
///
///
///
///
public static IBaseMapGroup GetGroupForLayer(this IBaseMapDefinition map, IBaseMapLayer layer)
{
Check.NotNull(map, "map");
foreach (var group in map.BaseMapLayerGroup)
{
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.NotNull(grp, "grp");
return new List(grp.BaseMapLayer).Count > 0;
}
///
/// Gets whether this base map has tiled layers
///
///
///
public static bool HasLayers(this IBaseMapDefinition map)
{
Check.NotNull(map, "map");
if (!map.HasGroups())
return false;
foreach (var group in map.BaseMapLayerGroup)
{
if (group.HasLayers())
return true;
}
return false;
}
///
/// Gets whether this base map has groups
///
///
///
public static bool HasGroups(this IBaseMapDefinition map)
{
Check.NotNull(map, "map");
return new List(map.BaseMapLayerGroup).Count > 0;
}
///
/// Gets the first base map group
///
///
///
public static IBaseMapGroup GetFirstGroup(this IBaseMapDefinition map)
{
Check.NotNull(map, "map");
var list = new List(map.BaseMapLayerGroup);
if (list.Count > 0)
return list[0];
return null;
}
///
/// Gets whether a tiled layer of the specified name exists.
///
///
///
///
public static bool LayerExists(this IBaseMapDefinition map, string layerName)
{
Check.NotNull(map, "map");
Check.NotEmpty(layerName, "layerName");
foreach (var group in map.BaseMapLayerGroup)
{
foreach (var layer in group.BaseMapLayer)
{
if (layerName.Equals(layer.Name))
return true;
}
}
return false;
}
///
/// Gets the base map group of the specified name
///
///
///
///
public static IBaseMapGroup GetGroup(this IBaseMapDefinition map, string groupName)
{
Check.NotNull(map, "map");
Check.NotEmpty(groupName, "groupName");
foreach (var group in map.BaseMapLayerGroup)
{
if (groupName.Equals(group.Name))
return group;
}
return null;
}
///
/// Gets whether the specified base map group exists
///
///
///
///
public static bool GroupExists(this IBaseMapDefinition map, string groupName)
{
Check.NotNull(map, "map");
Check.NotEmpty(groupName, "groupName");
foreach (var group in map.BaseMapLayerGroup)
{
if (groupName.Equals(group.Name))
return true;
}
return false;
}
///
/// Gets the tiled layers for the specified base map group
///
///
///
///
public static IEnumerable GetLayersForGroup(this IBaseMapDefinition map, string groupName)
{
Check.NotNull(map, "map");
Check.NotEmpty(groupName, "groupName");
foreach (var group in map.BaseMapLayerGroup)
{
if (groupName.Equals(group.Name))
{
return group.BaseMapLayer;
}
}
return new IBaseMapLayer[0];
}
}
///
/// Extension method class
///
public static class MapDefinitionExtensions
{
///
/// Updates the group name references of all layers belonging to a particular group
///
/// The map.
/// Old name of the group.
/// New name of the group.
public static void UpdateDynamicGroupName(this IMapDefinition map, string oldGroupName, string newGroupName)
{
Check.NotNull(map, "map");
Check.NotEmpty(oldGroupName, "oldGroupName");
Check.NotEmpty(newGroupName, "newGroupName");
var layers = map.GetLayersForGroup(oldGroupName);
var groups = map.GetGroupsForGroup(oldGroupName);
foreach (var l in layers)
{
l.Group = newGroupName;
}
foreach (var g in groups)
{
g.Group = newGroupName;
}
}
///
/// Removes a layer group and all layers associated with this group
///
///
///
/// The number of layers removed. Returns 0 if the group is empty or does not exist
public static int RemoveLayerGroupAndChildLayers(this IMapDefinition map, string groupName)
{
Check.NotNull(map, "map");
Check.NotEmpty(groupName, "groupName");
var affectedParentGroups = new Dictionary>();
IMapLayerGroup group = null;
foreach (var grp in map.MapLayerGroup)
{
if (grp.Name == groupName)
group = grp;
string parentGroupName = grp.Group;
if (!string.IsNullOrEmpty(parentGroupName))
{
if (!affectedParentGroups.ContainsKey(parentGroupName))
affectedParentGroups[parentGroupName] = new List();
affectedParentGroups[parentGroupName].Add(grp);
}
}
if (group != null)
{
List layers = new List(map.GetLayersForGroup(groupName));
int removed = 0;
//Remove layers first
foreach (var l in layers)
{
map.RemoveLayer(l);
removed++;
}
//Then the group
map.RemoveGroup(group);
//Then see if any child groups are under this group and remove them too
if (affectedParentGroups.ContainsKey(group.Name))
{
for (int i = 0; i < affectedParentGroups[group.Name].Count; i++)
{
var removeMe = affectedParentGroups[group.Name][i];
removed += map.RemoveLayerGroupAndChildLayers(removeMe.Name);
}
}
return removed;
}
return 0;
}
///
/// Get a layer by its name
///
///
///
///
public static IMapLayer GetLayerByName(this IMapDefinition map, string name)
{
Check.NotNull(map, "map");
Check.NotEmpty(name, "name");
foreach (var layer in map.MapLayer)
{
if (name.Equals(layer.Name))
return layer;
}
return null;
}
///
/// Gets a group by its name
///
///
///
///
public static IMapLayerGroup GetGroupByName(this IMapDefinition map, string name)
{
Check.NotNull(map, "map");
foreach (var group in map.MapLayerGroup)
{
if (name.Equals(group.Name))
return group;
}
return null;
}
///
/// Gets the number of layers (non-tiled) on this map
///
///
///
public static int GetLayerCount(this IMapDefinition map)
{
Check.NotNull(map, "map");
return new List(map.MapLayer).Count;
}
///
/// Gets the number of groups (non-tiled) on this map
///
///
///
public static int GetGroupCount(this IMapDefinition map)
{
Check.NotNull(map, "map");
return new List(map.MapLayerGroup).Count;
}
///
/// Gets all the layers that belong to the specified group
///
///
///
///
public static IEnumerable GetLayersForGroup(this IMapDefinition map, string name)
{
Check.NotNull(map, "map");
foreach (var layer in map.MapLayer)
{
if (name.Equals(layer.Group))
yield return layer;
}
}
///
/// Gets all the groups that belong to the specified group
///
///
///
///
public static IEnumerable GetGroupsForGroup(this IMapDefinition map, string name)
{
Check.NotNull(map, "map"); //NOXLATE
foreach (var group in map.MapLayerGroup)
{
if (name.Equals(group.Group))
yield return group;
}
}
///
/// Gets all that layers that do not belong to a group
///
///
///
public static IEnumerable GetLayersWithoutGroups(this IMapDefinition map)
{
Check.NotNull(map, "map");
foreach (var layer in map.MapLayer)
{
if (string.IsNullOrEmpty(layer.Group))
yield return layer;
}
}
}
///
/// Represents the tiled map portion of the Map Definition
///
public interface IBaseMapDefinition
{
///
/// Gets the finite display scales
///
IEnumerable FiniteDisplayScale { get; }
///
/// Adds the finite display scale.
///
/// 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);
///
/// Gets the base map layer groups.
///
/// The base map layer groups.
IEnumerable BaseMapLayerGroup { get; }
///
/// 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);
///
/// Removes all scales.
///
void RemoveAllScales();
}
///
/// Base legend element
///
public interface IMapLegendElementBase : INotifyPropertyChanged
{
///
/// Gets or sets the name.
///
/// The name.
string Name { get; set; }
///
/// Gets or sets a value indicating whether [show in legend].
///
/// true if [show in legend]; otherwise, false.
bool ShowInLegend { get; set; }
///
/// Gets or sets the legend label.
///
/// The legend label.
string LegendLabel { get; set; }
///
/// Gets or sets a value indicating whether [expand in legend].
///
/// true if [expand in legend]; otherwise, false.
bool ExpandInLegend { get; set; }
}
///
/// Base layer interface
///
public interface IBaseMapLayer : IMapLegendElementBase
{
///
/// Gets or sets the resource id.
///
/// The resource id.
string ResourceId { get; set; }
///
/// Gets or sets a value indicating whether this is selectable.
///
/// true if selectable; otherwise, false.
bool Selectable { get; set; }
}
///
/// Tiled map group
///
public interface IBaseMapGroup : IMapLegendElementBase
{
///
/// Gets or sets a value indicating whether this is visible.
///
/// true if visible; otherwise, false.
bool Visible { get; set; }
///
/// Gets the base map layers.
///
/// The base map layers.
IEnumerable BaseMapLayer { get; }
///
/// Adds the layer.
///
/// Name of the layer.
/// The resource id.
///
IBaseMapLayer AddLayer(string layerName, string resourceId);
///
/// Removes the base map layer.
///
/// The layer.
void RemoveBaseMapLayer(IBaseMapLayer layer);
///
/// Insert the base map layer at the specified index
///
///
///
void InsertLayer(int index, IBaseMapLayer layer);
///
/// Gets the index of the specified layer
///
///
///
int GetIndex(IBaseMapLayer layer);
///
/// Moves the specified layer up.
///
/// The layer.
///
int MoveUp(IBaseMapLayer layer);
///
/// Moves the specified layer down.
///
/// The layer.
///
int MoveDown(IBaseMapLayer layer);
}
///
/// A dynamic map layer
///
public interface IMapLayer : IBaseMapLayer
{
///
/// Gets or sets a value indicating whether this is visible.
///
/// true if visible; otherwise, false.
bool Visible { get; set; }
///
/// Gets or sets the group.
///
/// The group.
string Group { get; set; }
}
///
/// A dynamic map layer group
///
public interface IMapLayerGroup : IMapLegendElementBase
{
///
/// Gets or sets a value indicating whether this is visible.
///
/// true if visible; otherwise, false.
bool Visible { get; set; }
///
/// Gets or sets the group name. If null, it means this layer doesn't belong to any group
///
/// The group.
string Group { get; set; }
///
/// Gets the parent map definition
///
/// The parent map definition.
IMapDefinition Parent { get; }
}
}