/
Map.cs
357 lines (301 loc) · 10.4 KB
/
Map.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright (c) The Mapsui authors.
// The Mapsui authors licensed this file under the MIT license.
// See the LICENSE file in the project root for full license information.
// This file was originally created by Morten Nielsen (www.iter.dk) as part of SharpMap
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Mapsui.Extensions;
using Mapsui.Fetcher;
using Mapsui.Layers;
using Mapsui.Styles;
using Mapsui.Widgets;
namespace Mapsui;
/// <summary>
/// Map class
/// </summary>
/// <remarks>
/// Map holds all map related infos like the target CRS, layers, widgets and so on.
/// </remarks>
public class Map : INotifyPropertyChanged, IDisposable
{
private LayerCollection _layers = new();
private Color _backColor = Color.White;
/// <summary>
/// Initializes a new map
/// </summary>
public Map()
{
BackColor = Color.White;
Layers = new LayerCollection();
Navigator.RefreshDataRequest += Navigator_RefreshDataRequest;
Navigator.ViewportChanged += Navigator_ViewportChanged;
}
private void Navigator_ViewportChanged(object? sender, PropertyChangedEventArgs e)
{
RefreshGraphics();
}
/// <summary>
/// To register if the initial Home call has been done.
/// </summary>
public bool HomeIsCalledOnce { get; set; }
/// <summary>
/// List of Widgets belonging to map
/// </summary>
public ConcurrentQueue<IWidget> Widgets { get; } = new();
/// <summary>
/// Coordinate reference system (projection type of map).
/// Default: "EPSG:3857" (SphericalMercator).
/// </summary>
public string? CRS { get; set; } = "EPSG:3857";
/// <summary>
/// A collection of layers. The first layer in the list is drawn first, the last one on top.
/// </summary>
public LayerCollection Layers
{
get => _layers;
private set
{
var tempLayers = _layers;
if (tempLayers != null)
_layers.Changed -= LayersCollectionChanged;
_layers = value;
_layers.Changed += LayersCollectionChanged;
}
}
/// <summary>
/// Map background color (defaults to transparent)
/// </summary>
public Color BackColor
{
get => _backColor;
set
{
if (_backColor == value) return;
_backColor = value;
OnPropertyChanged(nameof(BackColor));
}
}
/// <summary>
/// Gets the extent of the map based on the extent of all the layers in the layers collection
/// </summary>
/// <returns>Full map extent</returns>
public MRect? Extent
{
get
{
if (_layers.Count == 0) return null;
MRect? extent = null;
foreach (var layer in _layers)
{
extent = extent == null ? layer.Extent : extent.Join(layer.Extent);
}
return extent;
}
}
/// <summary>
/// Called whenever a property changed
/// </summary>
public event PropertyChangedEventHandler? PropertyChanged;
/// <summary>
/// DataChanged should be triggered by any data changes of any of the child layers
/// </summary>
public event DataChangedEventHandler? DataChanged;
public event EventHandler? RefreshGraphicsRequest;
/// <summary>
/// Called whenever the map is clicked. The MapInfoEventArgs contain the features that were hit in
/// the layers that have IsMapInfoLayer set to true.
/// </summary>
public event EventHandler<MapInfoEventArgs>? Info;
/// <summary>
/// Handles all manipulations of the map viewport
/// </summary>
public Navigator Navigator { get; private set; } = new Navigator();
private void Navigator_RefreshDataRequest(object? sender, EventArgs e)
{
RefreshData(ChangeType.Discrete);
}
/// <summary>
/// Refresh data of the map and than repaint it
/// </summary>
public void Refresh(ChangeType changeType = ChangeType.Discrete)
{
RefreshData(changeType);
RefreshGraphics();
}
/// <summary>
/// Refresh data of Map, but don't paint it
/// </summary>
public void RefreshData(ChangeType changeType = ChangeType.Discrete)
{
if (Navigator.Viewport.ToExtent() is null)
return;
if (Navigator.Viewport.ToExtent().GetArea() <= 0)
return;
var fetchInfo = new FetchInfo(Navigator.Viewport.ToSection(), CRS, changeType);
RefreshData(fetchInfo);
}
public void RefreshGraphics()
{
RefreshGraphicsRequest?.Invoke(this, EventArgs.Empty);
}
public void OnViewportSizeInitialized()
{
ViewportInitialized?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Called when the viewport is initialized
/// </summary>
public event EventHandler? ViewportInitialized; //todo: Consider to use the Viewport PropertyChanged
/// <summary>
/// Abort fetching of all layers
/// </summary>
public void AbortFetch()
{
foreach (var layer in _layers.ToList())
{
if (layer is IAsyncDataFetcher asyncLayer) asyncLayer.AbortFetch();
}
}
/// <summary>
/// Clear cache of all layers
/// </summary>
public void ClearCache()
{
foreach (var layer in _layers)
{
if (layer is IAsyncDataFetcher asyncLayer) asyncLayer.ClearCache();
}
}
public void RefreshData(FetchInfo fetchInfo)
{
foreach (var layer in _layers.ToList())
{
if (layer is IAsyncDataFetcher asyncDataFetcher)
asyncDataFetcher.RefreshData(fetchInfo);
}
}
private void LayersCollectionChanged(object sender, LayerCollectionChangedEventArgs args)
{
foreach (var layer in args.RemovedLayers ?? Enumerable.Empty<ILayer>())
LayerRemoved(layer);
foreach (var layer in args.AddedLayers ?? Enumerable.Empty<ILayer>())
LayerAdded(layer);
LayersChanged();
}
private void LayerAdded(ILayer layer)
{
layer.DataChanged += LayerDataChanged;
layer.PropertyChanged += LayerPropertyChanged;
}
private void LayerRemoved(ILayer layer)
{
if (layer is IAsyncDataFetcher asyncLayer)
asyncLayer.AbortFetch();
layer.DataChanged -= LayerDataChanged;
layer.PropertyChanged -= LayerPropertyChanged;
}
private void LayersChanged()
{
Navigator.DefaultResolutions = DetermineResolutions(Layers);
Navigator.DefaultZoomBounds = GetMinMaxResolution(Navigator.Resolutions);
Navigator.DefaultPanBounds = Extent?.Copy();
OnPropertyChanged(nameof(Layers));
}
private MMinMax? GetMinMaxResolution(IEnumerable<double>? resolutions)
{
if (resolutions == null || resolutions.Count() == 0) return null;
resolutions = resolutions.OrderByDescending(r => r).ToList();
var mostZoomedOut = resolutions.First();
var mostZoomedIn = resolutions.Last() * 0.5; // Divide by two to allow one extra level to zoom-in
return new MMinMax(mostZoomedOut, mostZoomedIn);
}
private static IReadOnlyList<double> DetermineResolutions(IEnumerable<ILayer> layers)
{
var items = new Dictionary<double, double>();
const float normalizedDistanceThreshold = 0.75f;
foreach (var layer in layers)
{
if (!layer.Enabled || layer.Resolutions == null) continue;
foreach (var resolution in layer.Resolutions)
{
// About normalization:
// Resolutions don't have equal distances because they
// are multiplied by two at every step. Distances on the
// lower zoom levels have very different meaning than on the
// higher zoom levels. So we work with a normalized resolution
// to determine if another resolution adds value. If a resolution
// is a factor of 2 of another resolution. The normalized distance
// is one.
var normalized = Math.Log(resolution, 2);
if (items.Count == 0)
{
items[normalized] = resolution;
}
else
{
var normalizedDistance = items.Keys.Min(k => Math.Abs(k - normalized));
if (normalizedDistance > normalizedDistanceThreshold) items[normalized] = resolution;
}
}
}
return items.Select(i => i.Value).OrderByDescending(i => i).ToList();
}
private void LayerPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
OnPropertyChanged(sender, e.PropertyName);
}
private void OnPropertyChanged(object? sender, string? propertyName)
{
PropertyChanged?.Invoke(sender, new PropertyChangedEventArgs(propertyName));
}
private void OnPropertyChanged(string name)
{
OnPropertyChanged(this, name);
}
private void LayerDataChanged(object sender, DataChangedEventArgs e)
{
OnDataChanged(sender, e);
}
private void OnDataChanged(object sender, DataChangedEventArgs e)
{
DataChanged?.Invoke(sender, e);
}
public Action<Navigator> Home { get; set; } = n => n.ZoomToPanBounds();
public IEnumerable<IWidget> GetWidgetsOfMapAndLayers()
{
return Widgets.Concat(Layers.Where(l => l.Enabled).Select(l => l.Attribution))
.Where(a => a != null && a.Enabled).ToList();
}
/// <summary>
/// This method is to invoke the Info event from the Map. This method is called
/// by the MapControl/MapView and should usually not be called from user code.
/// </summary>
public void OnInfo(MapInfoEventArgs? mapInfoEventArgs)
{
if (mapInfoEventArgs == null) return;
Info?.Invoke(this, mapInfoEventArgs);
}
public virtual void Dispose()
{
foreach (var layer in Layers)
{
// remove Event so that no memory leaks occour
LayerRemoved(layer);
}
// clear the layers
Layers.Clear();
}
public bool UpdateAnimations()
{
var areAnimationsRunning = false;
foreach (var layer in Layers)
{
if (layer.UpdateAnimations())
areAnimationsRunning = true;
}
return areAnimationsRunning;
}
}