Index: Core/Components/src/Core.Components.DotSpatial.Forms/MapControl.cs =================================================================== diff -u -r6049b52f0552bb346a429c06d7395d19a36ef26a -rb593eae6f256eba2b3881fb7e00f0a6ca78ac11e --- Core/Components/src/Core.Components.DotSpatial.Forms/MapControl.cs (.../MapControl.cs) (revision 6049b52f0552bb346a429c06d7395d19a36ef26a) +++ Core/Components/src/Core.Components.DotSpatial.Forms/MapControl.cs (.../MapControl.cs) (revision b593eae6f256eba2b3881fb7e00f0a6ca78ac11e) @@ -65,7 +65,6 @@ private MapDataCollection data; private ImageBasedMapData backgroundMapData; private Timer updateTimer; - private Font myFont; /// /// Creates a new instance of . @@ -195,12 +194,11 @@ AddFontMemResourceEx(fontPtr, (uint) Resources.Deltares_Riskeer_Symbols.Length, IntPtr.Zero, ref dummy); Marshal.FreeCoTaskMem(fontPtr); - myFont = new Font(fonts.Families[0], 14.0F); - - panToolStripButton.Font = myFont; - zoomToRectangleToolStripButton.Font = myFont; - zoomToAllVisibleLayersToolStripButton.Font = myFont; - showCoordinatesToolStripButton.Font = myFont; + var font = new Font(fonts.Families[0], 14.0F); + panToolStripButton.Font = font; + zoomToRectangleToolStripButton.Font = font; + zoomToAllVisibleLayersToolStripButton.Font = font; + showCoordinatesToolStripButton.Font = font; } private void InitializeMap() @@ -497,17 +495,21 @@ public void ZoomToAllVisibleLayers(MapData layerData) { Envelope envelope = CreateEnvelopeForAllVisibleLayers(layerData); + if (!envelope.IsNull) { - var extent = envelope.ToExtent(); + Extent extent = envelope.ToExtent(); + AddPadding(extent); + map.ViewExtents = extent; } } private static void AddPadding(Extent extent) { double padding = Math.Min(extent.Height, extent.Width) * 0.05; + if (Math.Max(extent.Height, extent.Width) + padding <= double.MaxValue) { extent.ExpandBy(padding); @@ -529,13 +531,15 @@ } DrawnMapData drawnMapData = drawnMapDataList.FirstOrDefault(dmd => dmd.FeatureBasedMapData.Equals(mapData)); + if (drawnMapData == null) { throw new ArgumentException($@"Can only zoom to {nameof(MapData)} that is part of this {nameof(MapControl)}s drawn {nameof(mapData)}.", nameof(mapData)); } - Envelope envelope = new Envelope(); + var envelope = new Envelope(); + if (LayerHasVisibleExtent(drawnMapData.FeatureBasedMapDataLayer)) { envelope.ExpandToInclude(drawnMapData.FeatureBasedMapDataLayer.Extent.ToEnvelope()); @@ -553,7 +557,8 @@ /// any of its children is not part of the drawn map features. private Envelope CreateEnvelopeForAllVisibleLayers(MapDataCollection mapData) { - Envelope envelope = new Envelope(); + var envelope = new Envelope(); + foreach (MapData childMapData in mapData.Collection) { envelope.ExpandToInclude(CreateEnvelopeForAllVisibleLayers(childMapData)); Index: Core/Components/test/Core.Components.DotSpatial.Forms.Test/MapControlTest.cs =================================================================== diff -u -r12505b776d1c5e455b996dee4fadc9df625a1c3f -rb593eae6f256eba2b3881fb7e00f0a6ca78ac11e --- Core/Components/test/Core.Components.DotSpatial.Forms.Test/MapControlTest.cs (.../MapControlTest.cs) (revision 12505b776d1c5e455b996dee4fadc9df625a1c3f) +++ Core/Components/test/Core.Components.DotSpatial.Forms.Test/MapControlTest.cs (.../MapControlTest.cs) (revision b593eae6f256eba2b3881fb7e00f0a6ca78ac11e) @@ -63,13 +63,13 @@ private string settingsDirectory; [Test] - public void DefaultConstructor_DefaultValues() + public void Constructor_DefaultValues() { // Call using (var map = new MapControl()) { // Assert - Assert.IsInstanceOf(map); + Assert.IsInstanceOf(map); Assert.IsInstanceOf(map); Assert.IsNull(map.Data); Assert.IsNull(map.BackgroundMapData); @@ -78,7 +78,7 @@ [Test] [Apartment(ApartmentState.STA)] - public void DefaultConstructor_MapCorrectlyInitialized() + public void Constructor_MapCorrectlyInitialized() { using (var form = new Form()) { @@ -440,10 +440,10 @@ private static Extent GetExpectedExtent(FeatureBasedMapData visibleMapData) { - double minX = double.MaxValue; - double maxX = double.MinValue; - double minY = double.MaxValue; - double maxY = double.MinValue; + var minX = double.MaxValue; + var maxX = double.MinValue; + var minY = double.MaxValue; + var maxY = double.MinValue; foreach (MapFeature feature in visibleMapData.Features) { @@ -668,12 +668,12 @@ ProjectionInfo originalProjection = mapView.Projection; // When - Action call = () => map.BackgroundMapData = backgroundMapData; + void Call() => map.BackgroundMapData = backgroundMapData; // Then const string expectedMessage = "Verbinden met WMTS is mislukt waardoor geen kaartgegevens ingeladen kunnen worden. " + "De achtergrondkaart kan nu niet getoond worden."; - TestHelper.AssertLogMessageWithLevelIsGenerated(call, Tuple.Create(expectedMessage, LogLevelConstant.Error)); + TestHelper.AssertLogMessageWithLevelIsGenerated(Call, Tuple.Create(expectedMessage, LogLevelConstant.Error)); Assert.AreEqual(0, mapView.Layers.Count); @@ -702,12 +702,12 @@ disposeHelper.LockDirectory(FileSystemRights.Write); // When - Action call = () => map.BackgroundMapData = backgroundMapData; + void Call() => map.BackgroundMapData = backgroundMapData; // Then const string expectedMessage = "Configuratie van kaartgegevens hulpbestanden is mislukt. " + "De achtergrondkaart kan nu niet getoond worden."; - TestHelper.AssertLogMessageWithLevelIsGenerated(call, Tuple.Create(expectedMessage, LogLevelConstant.Error)); + TestHelper.AssertLogMessageWithLevelIsGenerated(Call, Tuple.Create(expectedMessage, LogLevelConstant.Error)); Assert.AreEqual(0, mapView.Layers.Count); @@ -730,10 +730,10 @@ using (var map = new MapControl()) { // Precondition - Action setAndCauseFailingInitialization = () => map.BackgroundMapData = backgroundMapData; + void SetAndCauseFailingInitialization() => map.BackgroundMapData = backgroundMapData; const string expectedMessage = "Verbinden met WMTS is mislukt waardoor geen kaartgegevens ingeladen kunnen worden. " + "De achtergrondkaart kan nu niet getoond worden."; - TestHelper.AssertLogMessageWithLevelIsGenerated(setAndCauseFailingInitialization, Tuple.Create(expectedMessage, LogLevelConstant.Error)); + TestHelper.AssertLogMessageWithLevelIsGenerated(SetAndCauseFailingInitialization, Tuple.Create(expectedMessage, LogLevelConstant.Error)); using (new UseCustomTileSourceFactoryConfig(backgroundMapData)) { @@ -769,10 +769,10 @@ helper.LockDirectory(FileSystemRights.Write); // Precondition - Action setAndCauseCacheInitializationFailure = () => map.BackgroundMapData = backgroundMapData; + void SetAndCauseCacheInitializationFailure() => map.BackgroundMapData = backgroundMapData; const string expectedMessage = "Configuratie van kaartgegevens hulpbestanden is mislukt. " + "De achtergrondkaart kan nu niet getoond worden."; - TestHelper.AssertLogMessageIsGenerated(setAndCauseCacheInitializationFailure, expectedMessage, 1); + TestHelper.AssertLogMessageIsGenerated(SetAndCauseCacheInitializationFailure, expectedMessage, 1); helper.UnlockDirectory(); @@ -807,16 +807,16 @@ ProjectionInfo originalProjection = mapView.Projection; // Precondition - Action setAndCauseFailToInitializeLayer = () => map.BackgroundMapData = backgroundMapData; + void SetAndCauseFailToInitializeLayer() => map.BackgroundMapData = backgroundMapData; const string expectedMessage = "Verbinden met WMTS is mislukt waardoor geen kaartgegevens ingeladen kunnen worden. " + "De achtergrondkaart kan nu niet getoond worden."; - TestHelper.AssertLogMessageIsGenerated(setAndCauseFailToInitializeLayer, expectedMessage, 1); + TestHelper.AssertLogMessageIsGenerated(SetAndCauseFailToInitializeLayer, expectedMessage, 1); // When - Action call = () => backgroundMapData.NotifyObservers(); + void Call() => backgroundMapData.NotifyObservers(); // Then - TestHelper.AssertLogMessagesCount(call, 0); + TestHelper.AssertLogMessagesCount(Call, 0); Assert.AreEqual(0, mapView.Layers.Count); @@ -825,7 +825,7 @@ } [Test] - public void GivenMapControlWithFailedCacheforBackgroundMapData_WhenBackgroundNotifiesObservers_ThenFailedInitializationShouldNotGenerateLogMessage() + public void GivenMapControlWithFailedCacheForBackgroundMapData_WhenBackgroundNotifiesObservers_ThenFailedInitializationShouldNotGenerateLogMessage() { // Given string folderWithoutPermission = Path.GetRandomFileName(); @@ -846,16 +846,16 @@ disposeHelper.LockDirectory(FileSystemRights.Write); // Precondition - Action setAndCauseCacheInitializationFailure = () => map.BackgroundMapData = backgroundMapData; + void SetAndCauseCacheInitializationFailure() => map.BackgroundMapData = backgroundMapData; const string expectedMessage = "Configuratie van kaartgegevens hulpbestanden is mislukt. " + "De achtergrondkaart kan nu niet getoond worden."; - TestHelper.AssertLogMessageIsGenerated(setAndCauseCacheInitializationFailure, expectedMessage, 1); + TestHelper.AssertLogMessageIsGenerated(SetAndCauseCacheInitializationFailure, expectedMessage, 1); // When - Action call = () => backgroundMapData.NotifyObservers(); + void Call() => backgroundMapData.NotifyObservers(); // Then - TestHelper.AssertLogMessagesCount(call, 0); + TestHelper.AssertLogMessagesCount(Call, 0); Assert.AreEqual(0, mapView.Layers.Count); @@ -1427,15 +1427,15 @@ mapDataCollection.Add(mapPointData); // When - Action call = () => + void Call() { map.BackgroundMapData = backgroundMapData; map.Data = mapDataCollection; - }; + } // Then const string expectedMessage = "Verbinden met WMTS is mislukt waardoor geen kaartgegevens ingeladen kunnen worden. De achtergrondkaart kan nu niet getoond worden."; - TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); // Message should only the generated once! + TestHelper.AssertLogMessageIsGenerated(Call, expectedMessage, 1); // Message should only the generated once! Assert.AreEqual(1, mapView.Layers.Count); var pointsLayer = (FeatureLayer) mapView.Layers[0]; @@ -1487,16 +1487,16 @@ mapDataCollection.Add(mapPointData); // When - Action call = () => + void Call() { map.BackgroundMapData = backgroundMapData; map.Data = mapDataCollection; - }; + } // Then const string expectedMessage = "Configuratie van kaartgegevens hulpbestanden is mislukt. " + "De achtergrondkaart kan nu niet getoond worden."; - TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); + TestHelper.AssertLogMessageIsGenerated(Call, expectedMessage, 1); Assert.AreEqual(1, mapView.Layers.Count); var pointsLayer = (FeatureLayer) mapView.Layers[0]; @@ -1552,7 +1552,7 @@ Assert.AreEqual(5313545.4037142638, pointFeatureLayer.FeatureSet.Features[0].Geometry.Coordinates[0].Y, 1e-6, "Coordinate does not match (Estimate of expected value can be calculated from https://epsg.io/transform#s_srs=28992&t_srs=25831&x=1.1000000&y=2.2000000)."); - Action callAction = () => + void Call() { // When mapPointData.Features = new[] @@ -1569,19 +1569,18 @@ }) }; mapPointData.NotifyObservers(); - }; - Action assertAction = () => + } + + void AssertAction() { // Then CollectionAssert.AreEqual(layersBeforeUpdate, mapView.Layers); Assert.IsTrue(mapView.Projection.Equals(pointFeatureLayer.Projection)); - Assert.AreEqual(535405.97207404568, pointFeatureLayer.FeatureSet.Features[0].Geometry.Coordinates[0].X, 1e-6, - "Coordinate does not match. (Ball park expected value can be calculated from https://epsg.io/transform#s_srs=28992&t_srs=25831&x=12345.6789000&y=9876.5432100)."); - Assert.AreEqual(5323789.7111355662, pointFeatureLayer.FeatureSet.Features[0].Geometry.Coordinates[0].Y, 1e-6, - "Coordinate does not match (Estimate of expected value can be calculated from https://epsg.io/transform#s_srs=28992&t_srs=25831&x=12345.6789000&y=9876.5432100)."); - }; + Assert.AreEqual(535405.97207404568, pointFeatureLayer.FeatureSet.Features[0].Geometry.Coordinates[0].X, 1e-6, "Coordinate does not match. (Ball park expected value can be calculated from https://epsg.io/transform#s_srs=28992&t_srs=25831&x=12345.6789000&y=9876.5432100)."); + Assert.AreEqual(5323789.7111355662, pointFeatureLayer.FeatureSet.Features[0].Geometry.Coordinates[0].Y, 1e-6, "Coordinate does not match (Estimate of expected value can be calculated from https://epsg.io/transform#s_srs=28992&t_srs=25831&x=12345.6789000&y=9876.5432100)."); + } - TestHelper.PerformActionWithDelayedAssert(callAction, assertAction, 20); + TestHelper.PerformActionWithDelayedAssert(Call, AssertAction, 20); } } @@ -1592,7 +1591,7 @@ /// The data for the test cases. /// Some test runners, like TeamCity, cannot properly deal with reuse of /// sources where the source defines a name of the test, - /// as these testrunners to not display tests in hierarchical form. + /// as these test runners to not display tests in hierarchical form. private static IEnumerable GetProblematicTileSourceFactoryTestCaseData(string prefix) { var factoryWithoutRequiredTileSource = MockRepository.GenerateStub(); @@ -1664,13 +1663,13 @@ ProjectionInfo originalProjection = mapView.Projection; // When - Action call = () => map.BackgroundMapData = backgroundMapData; + void Call() => map.BackgroundMapData = backgroundMapData; // Then string tileDisplayName = TypeUtils.GetDisplayName(backgroundMapData.TileSource); string expectedMessage = $"Verbinden met '{tileDisplayName}' is mislukt waardoor geen kaartgegevens ingeladen kunnen worden. " + "De achtergrondkaart kan nu niet getoond worden."; - TestHelper.AssertLogMessageWithLevelIsGenerated(call, Tuple.Create(expectedMessage, LogLevelConstant.Error)); + TestHelper.AssertLogMessageWithLevelIsGenerated(Call, Tuple.Create(expectedMessage, LogLevelConstant.Error)); Assert.AreEqual(0, mapView.Layers.Count); @@ -1699,12 +1698,12 @@ disposeHelper.LockDirectory(FileSystemRights.Write); // When - Action call = () => map.BackgroundMapData = backgroundMapData; + void Call() => map.BackgroundMapData = backgroundMapData; // Then const string expectedMessage = "Configuratie van kaartgegevens hulpbestanden is mislukt. " + "De achtergrondkaart kan nu niet getoond worden."; - TestHelper.AssertLogMessageWithLevelIsGenerated(call, Tuple.Create(expectedMessage, LogLevelConstant.Error)); + TestHelper.AssertLogMessageWithLevelIsGenerated(Call, Tuple.Create(expectedMessage, LogLevelConstant.Error)); Assert.AreEqual(0, mapView.Layers.Count); @@ -1728,10 +1727,10 @@ { // Precondition string tileDisplayName = TypeUtils.GetDisplayName(backgroundMapData.TileSource); - Action setAndCauseFailingInitialization = () => map.BackgroundMapData = backgroundMapData; + void SetAndCauseFailingInitialization() => map.BackgroundMapData = backgroundMapData; string expectedMessage = $"Verbinden met '{tileDisplayName}' is mislukt waardoor geen kaartgegevens ingeladen kunnen worden. " + "De achtergrondkaart kan nu niet getoond worden."; - TestHelper.AssertLogMessageWithLevelIsGenerated(setAndCauseFailingInitialization, Tuple.Create(expectedMessage, LogLevelConstant.Error)); + TestHelper.AssertLogMessageWithLevelIsGenerated(SetAndCauseFailingInitialization, Tuple.Create(expectedMessage, LogLevelConstant.Error)); using (new UseCustomTileSourceFactoryConfig(backgroundMapData)) { @@ -1765,11 +1764,12 @@ using (var map = new MapControl()) { helper.LockDirectory(FileSystemRights.Write); + // Precondition - Action setAndCauseCacheInitializationFailure = () => map.BackgroundMapData = backgroundMapData; + void SetAndCauseCacheInitializationFailure() => map.BackgroundMapData = backgroundMapData; const string expectedMessage = "Configuratie van kaartgegevens hulpbestanden is mislukt. " + "De achtergrondkaart kan nu niet getoond worden."; - TestHelper.AssertLogMessageIsGenerated(setAndCauseCacheInitializationFailure, expectedMessage, 1); + TestHelper.AssertLogMessageIsGenerated(SetAndCauseCacheInitializationFailure, expectedMessage, 1); helper.UnlockDirectory(); // When @@ -1804,23 +1804,23 @@ // Precondition string tileDisplayName = TypeUtils.GetDisplayName(backgroundMapData.TileSource); - Action setAndCauseFailToInitializeLayer = () => map.BackgroundMapData = backgroundMapData; + void SetAndCauseFailToInitializeLayer() => map.BackgroundMapData = backgroundMapData; string expectedMessage = $"Verbinden met '{tileDisplayName}' is mislukt waardoor geen kaartgegevens ingeladen kunnen worden. " + "De achtergrondkaart kan nu niet getoond worden."; - TestHelper.AssertLogMessageWithLevelIsGenerated(setAndCauseFailToInitializeLayer, Tuple.Create(expectedMessage, LogLevelConstant.Error)); + TestHelper.AssertLogMessageWithLevelIsGenerated(SetAndCauseFailToInitializeLayer, Tuple.Create(expectedMessage, LogLevelConstant.Error)); // When - Action call = () => backgroundMapData.NotifyObservers(); + void Call() => backgroundMapData.NotifyObservers(); // Then - TestHelper.AssertLogMessagesCount(call, 0); + TestHelper.AssertLogMessagesCount(Call, 0); Assert.AreEqual(0, mapView.Layers.Count); Assert.AreSame(originalProjection, mapView.Projection); } } [Test] - public void GivenMapControlWithFailedCacheforBackgroundMapData_WhenWellKnownBackgroundNotifiesObservers_ThenFailedInitializationShouldNotGenerateLogMessage() + public void GivenMapControlWithFailedCacheForBackgroundMapData_WhenWellKnownBackgroundNotifiesObservers_ThenFailedInitializationShouldNotGenerateLogMessage() { // Given string folderWithoutPermission = Path.GetRandomFileName(); @@ -1840,16 +1840,16 @@ disposeHelper.LockDirectory(FileSystemRights.Write); // Precondition - Action setAndCauseCacheInitializationFailure = () => map.BackgroundMapData = backgroundMapData; + void SetAndCauseCacheInitializationFailure() => map.BackgroundMapData = backgroundMapData; const string expectedMessage = "Configuratie van kaartgegevens hulpbestanden is mislukt. " + "De achtergrondkaart kan nu niet getoond worden."; - TestHelper.AssertLogMessageIsGenerated(setAndCauseCacheInitializationFailure, expectedMessage, 1); + TestHelper.AssertLogMessageIsGenerated(SetAndCauseCacheInitializationFailure, expectedMessage, 1); // When - Action call = () => backgroundMapData.NotifyObservers(); + void Call() => backgroundMapData.NotifyObservers(); // Then - TestHelper.AssertLogMessagesCount(call, 0); + TestHelper.AssertLogMessagesCount(Call, 0); Assert.AreEqual(0, mapView.Layers.Count); Assert.AreSame(originalProjection, mapView.Projection); } @@ -1983,17 +1983,17 @@ mapDataCollection.Add(mapPointData); // When - Action call = () => + void Call() { map.BackgroundMapData = backgroundMapData; map.Data = mapDataCollection; - }; + } // Then string expectedMessage = $"Verbinden met '{TypeUtils.GetDisplayName(backgroundMapData.TileSource)}' " + "is mislukt waardoor geen kaartgegevens ingeladen kunnen worden. " + "De achtergrondkaart kan nu niet getoond worden."; - TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); // Message should only the generated once! + TestHelper.AssertLogMessageIsGenerated(Call, expectedMessage, 1); // Message should only the generated once! Assert.AreEqual(1, mapView.Layers.Count); var pointsLayer = (FeatureLayer) mapView.Layers[0]; @@ -2044,16 +2044,16 @@ mapDataCollection.Add(mapPointData); // When - Action call = () => + void Call() { map.BackgroundMapData = backgroundMapData; map.Data = mapDataCollection; - }; + } // Then const string expectedMessage = "Configuratie van kaartgegevens hulpbestanden is mislukt. " + "De achtergrondkaart kan nu niet getoond worden."; - TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); + TestHelper.AssertLogMessageIsGenerated(Call, expectedMessage, 1); Assert.AreEqual(1, mapView.Layers.Count); var pointsLayer = (FeatureLayer) mapView.Layers[0]; @@ -2371,11 +2371,11 @@ var mapData = new MapPointData("Test data"); // Call - TestDelegate call = () => map.ZoomToAllVisibleLayers(mapData); + void Call() => map.ZoomToAllVisibleLayers(mapData); // Assert const string message = "Can only zoom to MapData that is part of this MapControls drawn mapData."; - string paramName = TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, message).ParamName; + string paramName = TestHelper.AssertThrowsArgumentExceptionAndTestMessage(Call, message).ParamName; Assert.AreEqual("mapData", paramName); } }