previous version: ThinkGeo.MapSuite.WpfDesktopEdition & ThinkGeo.MapSuite.Core 7.0.0.0
newer version: ThinkGeo.Core & ThinkGeo.UI.Wpf 14.1.2
previous sample code:
/// <summary>
/// Initializes the background map.
/// </summary>
private void InitBackgroundMap()
{
if (string.IsNullOrWhiteSpace(MapData.Url))
{
OpenStreetMapOverlay background = new OpenStreetMapOverlay
{
IsVisible = true,
DrawingExceptionMode = DrawingExceptionMode.DrawException,
// var cache = new FileBitmapTileCache(System.IO.Path.GetTempPath(), "MY-OSM");
TileCache = new InMemoryBitmapTileCache()
};
MapControl.Overlays.Add("background", background);
MapControl.Refresh(background);
}
else
{
var background = new LayerOverlay
{
TileHeight = 256,
TileWidth = 256,
LockLayerMode = LockLayerMode.DoNotLock,
TransitionEffect = TransitionEffect.Stretch,
IsVisible = true,
DrawingExceptionMode = DrawingExceptionMode.DrawException
};
var cache = new InMemoryBitmapTileCache();
background.TileCache = cache;
WmsRasterLayer firstWmsLayer = new WmsRasterLayer(new Uri(MapData.Url))
{
Crs = string.Format("EPSG:{0}", MapData.SrsEPSG),
OutputFormat = "image/png"
};
foreach (string layer in MapData.Layers)
{
firstWmsLayer.ActiveLayerNames.Add(layer);
}
background.Layers.Add("backlayer", firstWmsLayer);
if (MapData.InitialExtend == null)
{
try
{
// try to get define initial extend from server.
MapData.InitialExtend = firstWmsLayer.GetBoundingBox().ToMapRectangle();
}
catch (Exception error)
{
logger.Warn(error, "Fail to get BoudingBox");
}
}
MapControl.Overlays.Add("background", background);
MapControl.Refresh(background);
}
}
current sample code (I tried different things so the code got a bit complicated)
private async Task _initBackgroundMap()
{
// It is important to set the map unit first to either feet, meters or decimal degrees.
map.MapUnit = GeographyUnit.Meter;
map.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.FromHtml("#6996AD"));
// Create and Add the overlay to the map.
GenerateBackgroundOverlay();
// Set the current extent to a local area.
SetMapCurrentExtent();
// Refresh the map.
await map.RefreshAsync();
}
private void GenerateBackgroundOverlay()
{
// Clear out the overlays so we start fresh
map.Overlays.Clear();
// Create an overlay that we will add the layer to.
var staticOverlay = new LayerOverlay
{
DrawingExceptionMode = DrawingExceptionMode.ThrowException,
/*IsVisible = true,
TileCache = new InMemoryRasterTileCache(),
TileHeight = 256,
TileWidth = 256,*/
};
staticOverlay.ThrowingException += (sender, e) =>
{
logger.Warn(e.Exception, "ThinkGeo exception : ");
e.Handled = true;
};
map.Overlays.Add(staticOverlay);
CustomWmsLayer custom = new CustomWmsLayer(new Uri(MapData.Url), $"EPSG:{MapData.SrsEPSG}");
custom.ActiveLayerNames.Add("LAYERS", string.Join(",", MapData.Layers));
custom.ActiveStyleNames.Add("");
staticOverlay.Layers.Add("background", custom);
/*
var wmsOverlay = new WmsOverlay();
wmsOverlay.AxisOrder = WmsAxisOrder.XY;
wmsOverlay.Uri = new Uri(MapData.Url);
wmsOverlay.Crs = $"EPSG:{MapData.SrsEPSG}";
wmsOverlay.Parameters.Add("LAYERS", string.Join(",", MapData.Layers));
wmsOverlay.Parameters.Add("VERSION", "1.3.0");
map.Overlays.Add("background", wmsOverlay);*/
}
private void SetMapCurrentExtent()
{
if (MapData.InitialExtend == null)
SetExtentDefault();
map.CurrentExtent = MapData.InitialExtend.ToShape();
void SetExtentDefault()
{
//Hard coded lyon extent
RectangleShape lyonRectangleShape = new RectangleShape(4.7649178, 45.7943508, 4.916865, 45.7527233);
MapData.InitialExtend = lyonRectangleShape.ToMapRectangle();
}
}
public class CustomWmsLayer : WmsLayer
{
private WmsLayer firstLayer;
private readonly Logger logger = LogManager.GetCurrentClassLogger();
public CustomWmsLayer(Uri uri, string crs)
: base(uri)
{
DrawingExceptionMode = DrawingExceptionMode.ThrowException;
Crs = crs;
}
protected override void DrawExceptionCore(GeoCanvas canvas, Exception e)
{
// customize the drawing exception. Here below we draw the error in red on orange canvas.
logger.Error(e);
}
}