Index: Core/Components/src/Core.Components.DotSpatial.Forms/MapControl.cs
===================================================================
diff -u -r4dbfc20ef0200e34db43efeb8899d72e4046cc5b -r7660d43cae4eda23b77c027f50dde28c23715c1b
--- Core/Components/src/Core.Components.DotSpatial.Forms/MapControl.cs (.../MapControl.cs) (revision 4dbfc20ef0200e34db43efeb8899d72e4046cc5b)
+++ Core/Components/src/Core.Components.DotSpatial.Forms/MapControl.cs (.../MapControl.cs) (revision 7660d43cae4eda23b77c027f50dde28c23715c1b)
@@ -92,10 +92,15 @@
public void ZoomToAllVisibleLayers()
{
- IEnvelope envelope = CreateEnvelopeForAllVisibleLayers();
+ ZoomToAllVisibleLayers(Data);
+ }
+
+ public void ZoomToAllVisibleLayers(MapData layerData)
+ {
+ IEnvelope envelope = CreateEnvelopeForAllVisibleLayers(layerData);
if (!envelope.IsNull)
{
- var extent = envelope.ToExtent();
+ Extent extent = envelope.ToExtent();
AddPadding(extent);
map.ViewExtents = extent;
}
@@ -151,16 +156,58 @@
}
}
- private IEnvelope CreateEnvelopeForAllVisibleLayers()
+ ///
+ /// Defines the area taken up by the visible map-data based on the provided map-data.
+ ///
+ /// The data to determine the visible extent for.
+ /// The area definition.
+ /// Thrown when is
+ /// not part of the drawn map features.
+ private IEnvelope CreateEnvelopeForAllVisibleLayers(MapData mapData)
{
+ var collection = mapData as MapDataCollection;
+ if (collection != null)
+ {
+ return CreateEnvelopeForAllVisibleLayers(collection);
+ }
+
+ DrawnMapData drawnMapData = drawnMapDataList.FirstOrDefault(dmd => dmd.FeatureBasedMapData.Equals(mapData));
+ if (drawnMapData == null)
+ {
+ throw new ArgumentException($"Can only zoom to {typeof(MapData).Name} that is part of this {typeof(MapControl).Name}s drawn mapdata.",
+ nameof(mapData));
+ }
+
IEnvelope envelope = new Envelope();
- foreach (IMapLayer layer in map.Layers.Where(layer => layer.IsVisible && !layer.Extent.IsEmpty()))
+ if (LayerHasVisibleExtent(drawnMapData.FeatureBasedMapDataLayer))
{
- envelope.ExpandToInclude(layer.Extent.ToEnvelope());
+ envelope.ExpandToInclude(drawnMapData.FeatureBasedMapDataLayer.Extent.ToEnvelope());
}
return envelope;
}
+ ///
+ /// Defines the area taken up by the visible map-data based on the provided map-data.
+ ///
+ /// The data to determine the visible extent for.
+ /// The area definition.
+ /// Thrown when or
+ /// any of it's children is not part of the drawn map features.
+ private IEnvelope CreateEnvelopeForAllVisibleLayers(MapDataCollection mapData)
+ {
+ IEnvelope envelope = new Envelope();
+ foreach (MapData childMapData in mapData.Collection)
+ {
+ envelope.ExpandToInclude(CreateEnvelopeForAllVisibleLayers(childMapData));
+ }
+ return envelope;
+ }
+
+ private static bool LayerHasVisibleExtent(IMapLayer layer)
+ {
+ return layer.IsVisible && !layer.Extent.IsEmpty();
+ }
+
private void ResetDefaultInteraction()
{
IsPanningEnabled = false;
@@ -199,7 +246,7 @@
private void DrawInitialMapData()
{
- foreach (var featureBasedMapData in GetFeatureBasedMapDataRecursively(Data))
+ foreach (FeatureBasedMapData featureBasedMapData in GetFeatureBasedMapDataRecursively(Data))
{
DrawMapData(featureBasedMapData);
}
Index: Core/Components/src/Core.Components.Gis.Forms/IMapControl.cs
===================================================================
diff -u -r675771641656abfb56ef89be86bad7bd21684016 -r7660d43cae4eda23b77c027f50dde28c23715c1b
--- Core/Components/src/Core.Components.Gis.Forms/IMapControl.cs (.../IMapControl.cs) (revision 675771641656abfb56ef89be86bad7bd21684016)
+++ Core/Components/src/Core.Components.Gis.Forms/IMapControl.cs (.../IMapControl.cs) (revision 7660d43cae4eda23b77c027f50dde28c23715c1b)
@@ -19,6 +19,7 @@
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
+using System;
using Core.Components.Gis.Data;
namespace Core.Components.Gis.Forms
@@ -54,6 +55,14 @@
void ZoomToAllVisibleLayers();
///
+ /// Zooms to a level such that the given map data is in view.
+ ///
+ /// The data to zoom to.
+ /// Thrown when
+ /// is not part of .
+ void ZoomToAllVisibleLayers(MapData layerData);
+
+ ///
/// Toggles panning of the . Panning is invoked by clicking the left mouse-button.
///
void TogglePanning();
Index: Core/Components/test/Core.Components.DotSpatial.Forms.Test/Core.Components.DotSpatial.Forms.Test.csproj
===================================================================
diff -u -r23d1e296e2da4364fbfe346e68d582dfcf966bb0 -r7660d43cae4eda23b77c027f50dde28c23715c1b
--- Core/Components/test/Core.Components.DotSpatial.Forms.Test/Core.Components.DotSpatial.Forms.Test.csproj (.../Core.Components.DotSpatial.Forms.Test.csproj) (revision 23d1e296e2da4364fbfe346e68d582dfcf966bb0)
+++ Core/Components/test/Core.Components.DotSpatial.Forms.Test/Core.Components.DotSpatial.Forms.Test.csproj (.../Core.Components.DotSpatial.Forms.Test.csproj) (revision 7660d43cae4eda23b77c027f50dde28c23715c1b)
@@ -80,6 +80,10 @@
{3BBFD65B-B277-4E50-AE6D-BD24C3434609}
Core.Common.Base
+
+ {d749ee4c-ce50-4c17-bf01-9a953028c126}
+ Core.Common.TestUtil
+
{5a91174a-fb95-4c9d-9ca5-81c0b8d4361a}
Core.Components.DotSpatial.Forms
Index: Core/Components/test/Core.Components.DotSpatial.Forms.Test/MapControlTest.cs
===================================================================
diff -u -r2cafb330e0b90d1103bc9329eafbcba24836787f -r7660d43cae4eda23b77c027f50dde28c23715c1b
--- Core/Components/test/Core.Components.DotSpatial.Forms.Test/MapControlTest.cs (.../MapControlTest.cs) (revision 2cafb330e0b90d1103bc9329eafbcba24836787f)
+++ Core/Components/test/Core.Components.DotSpatial.Forms.Test/MapControlTest.cs (.../MapControlTest.cs) (revision 7660d43cae4eda23b77c027f50dde28c23715c1b)
@@ -19,9 +19,12 @@
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
+using System;
+using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Core.Common.Base.Geometry;
+using Core.Common.TestUtil;
using Core.Components.DotSpatial.MapFunctions;
using Core.Components.Gis.Data;
using Core.Components.Gis.Features;
@@ -374,10 +377,10 @@
// Assert
Assert.AreEqual(2, invalidated);
+
Extent expectedExtent = mapView.GetMaxExtent();
+ ExtendWithExpectedMargin(expectedExtent);
- var smallest = expectedExtent.Height < expectedExtent.Width ? expectedExtent.Height : expectedExtent.Width;
- expectedExtent.ExpandBy(smallest*padding);
Assert.AreEqual(expectedExtent, mapView.ViewExtents);
}
}
@@ -393,8 +396,7 @@
map.Data = GetTestData();
var expectedExtent = new Extent(0.0, 0.5, 1.6, 2.1);
- var smallest = expectedExtent.Height < expectedExtent.Width ? expectedExtent.Height : expectedExtent.Width;
- expectedExtent.ExpandBy(smallest*padding);
+ ExtendWithExpectedMargin(expectedExtent);
// Precondition
Assert.AreEqual(3, mapView.Layers.Count, "Precondition failed: mapView.Layers != 3");
@@ -444,8 +446,7 @@
map.Data = mapDataCollection;
var expectedExtent = new Extent(0.0, 0.0, xMax, yMax);
- var smallest = expectedExtent.Height < expectedExtent.Width ? expectedExtent.Height : expectedExtent.Width;
- expectedExtent.ExpandBy(smallest*padding);
+ ExtendWithExpectedMargin(expectedExtent);
// Call
map.ZoomToAllVisibleLayers();
@@ -465,7 +466,226 @@
}
[Test]
+ public void ZoomToAllVisibleLayers_WithNonChildMapData_ThrowArgumentException()
+ {
+ // Setup
+ var mapDataCollection = new MapDataCollection("Collection");
+ using (var map = new MapControl
+ {
+ Data = mapDataCollection
+ })
+ {
+ var mapData = new MapPointData("Test data");
+
+ // Call
+ TestDelegate call = () => map.ZoomToAllVisibleLayers(mapData);
+
+ // Assert
+ string message = "Can only zoom to MapData that is part of this MapControls drawn mapdata.";
+ string paramName = TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, message).ParamName;
+ Assert.AreEqual("mapData", paramName);
+ }
+ }
+
+ [Test]
[RequiresSTA]
+ public void ZoomToAllVisibleLayers_MapInFormWithEmptyDataSetAndForChildMapData_ViewInvalidatedLayersSame()
+ {
+ // Setup
+ using (var map = new MapControl())
+ {
+ var mapDataCollection = new MapDataCollection("Collection");
+ var mapData = new MapPointData("Test data");
+ var invalidated = 0;
+ var mapView = map.Controls.OfType