Index: Core/Components/src/Core.Components.DotSpatial.Forms/MapControl.cs =================================================================== diff -u -r9649e55312348f856051d40881b4e902b1a15a3b -rd15513df1bcbfee9f9edbe42da62e63c0b131574 --- Core/Components/src/Core.Components.DotSpatial.Forms/MapControl.cs (.../MapControl.cs) (revision 9649e55312348f856051d40881b4e902b1a15a3b) +++ Core/Components/src/Core.Components.DotSpatial.Forms/MapControl.cs (.../MapControl.cs) (revision d15513df1bcbfee9f9edbe42da62e63c0b131574) @@ -37,6 +37,7 @@ using DotSpatial.Topology; using log4net; using ILog = log4net.ILog; +using Timer = System.Timers.Timer; namespace Core.Components.DotSpatial.Forms { @@ -45,12 +46,14 @@ /// public class MapControl : Control, IMapControl { + private const int updateTimerInterval = 10; private readonly ILog log = LogManager.GetLogger(typeof(MapControl)); private readonly Cursor defaultCursor = Cursors.Default; private readonly RecursiveObserver mapDataCollectionObserver; private readonly Observer backGroundMapDataObserver; private readonly List drawnMapDataList = new List(); private readonly MapControlBackgroundLayerStatus backgroundLayerStatus = new MapControlBackgroundLayerStatus(); + private readonly List mapDataLayersToUpdate = new List(); private Map map; private bool removing; @@ -60,6 +63,8 @@ private MapDataCollection data; private ImageBasedMapData backgroundMapData; + private Timer updateTimer; + /// /// Creates a new instance of . /// @@ -70,6 +75,8 @@ mapDataCollectionObserver = new RecursiveObserver(HandleMapDataCollectionChange, mdc => mdc.Collection); backGroundMapDataObserver = new Observer(HandleBackgroundMapDataChange); + + InitializeUpdateTimer(); } public MapDataCollection Data @@ -413,8 +420,12 @@ FeatureBasedMapDataLayer = featureBasedMapDataLayer }; - drawnMapData.Observer = new Observer(() => drawnMapData.FeatureBasedMapDataLayer.Update()) + drawnMapData.Observer = new Observer(() => { + mapDataLayersToUpdate.Add(drawnMapData.FeatureBasedMapDataLayer); + StartUpdateTimer(); + }) + { Observable = featureBasedMapData }; @@ -424,6 +435,7 @@ { featureBasedMapDataLayer.Reproject(Projection); } + map.Layers.Add(featureBasedMapDataLayer); } @@ -570,6 +582,7 @@ { envelope.ExpandToInclude(drawnMapData.FeatureBasedMapDataLayer.Extent.ToEnvelope()); } + return envelope; } @@ -587,6 +600,7 @@ { envelope.ExpandToInclude(CreateEnvelopeForAllVisibleLayers(childMapData)); } + return envelope; } @@ -604,5 +618,44 @@ } #endregion + + #region Update timer + + private void InitializeUpdateTimer() + { + updateTimer = new Timer + { + Interval = updateTimerInterval, + SynchronizingObject = this + }; + + updateTimer.Elapsed += (sender, args) => + { + updateTimer.Stop(); + UpdateMapDataLayers(); + }; + } + + private void StartUpdateTimer() + { + if (updateTimer.Enabled) + { + updateTimer.Stop(); + } + + updateTimer.Start(); + } + + private void UpdateMapDataLayers() + { + foreach (IFeatureBasedMapDataLayer mapDataLayerToUpdate in mapDataLayersToUpdate.Distinct()) + { + mapDataLayerToUpdate.Update(); + } + + mapDataLayersToUpdate.Clear(); + } + + #endregion } } \ No newline at end of file Index: Core/Components/test/Core.Components.DotSpatial.Forms.Test/MapControlTest.cs =================================================================== diff -u -r1fd2033b78aa540d01635bcf993080e5fe2437bd -rd15513df1bcbfee9f9edbe42da62e63c0b131574 --- Core/Components/test/Core.Components.DotSpatial.Forms.Test/MapControlTest.cs (.../MapControlTest.cs) (revision 1fd2033b78aa540d01635bcf993080e5fe2437bd) +++ Core/Components/test/Core.Components.DotSpatial.Forms.Test/MapControlTest.cs (.../MapControlTest.cs) (revision d15513df1bcbfee9f9edbe42da62e63c0b131574) @@ -1537,29 +1537,36 @@ Assert.AreEqual(5313600.4932731427, pointFeatureLayer.FeatureSet.Features[0].BasicGeometry.Coordinates[0].Y, "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)."); - // When - mapPointData.Features = new[] + Action callAction = () => { - new MapFeature(new[] + // When + mapPointData.Features = new[] { - new MapGeometry(new[] + new MapFeature(new[] { - new[] + new MapGeometry(new[] { - new Point2D(12345.6789, 9876.54321) - } + new[] + { + new Point2D(12345.6789, 9876.54321) + } + }) }) - }) + }; + mapPointData.NotifyObservers(); }; - mapPointData.NotifyObservers(); + Action assertAction = () => + { + // Then + CollectionAssert.AreEqual(layersBeforeUpdate, mapView.Layers); + Assert.IsTrue(mapView.Projection.Equals(pointFeatureLayer.Projection)); + Assert.AreEqual(535419.87415209203, pointFeatureLayer.FeatureSet.Features[0].BasicGeometry.Coordinates[0].X, + "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(5323846.0863087801, pointFeatureLayer.FeatureSet.Features[0].BasicGeometry.Coordinates[0].Y, + "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)."); + }; - // Then - CollectionAssert.AreEqual(layersBeforeUpdate, mapView.Layers); - Assert.IsTrue(mapView.Projection.Equals(pointFeatureLayer.Projection)); - Assert.AreEqual(535419.87415209203, pointFeatureLayer.FeatureSet.Features[0].BasicGeometry.Coordinates[0].X, - "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(5323846.0863087801, pointFeatureLayer.FeatureSet.Features[0].BasicGeometry.Coordinates[0].Y, - "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); } }