-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathVariableLayerCollection.cs
More file actions
194 lines (164 loc) · 6.12 KB
/
VariableLayerCollection.cs
File metadata and controls
194 lines (164 loc) · 6.12 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Timers;
namespace SharpMap.Layers
{
/// <summary>
/// Types of layer collections
/// </summary>
public enum LayerCollectionType
{
/// <summary>
/// Layer collection for layers with datasources that are more or less static (e.g ShapeFiles)
/// </summary>
Static,
/// <summary>
/// Layer collection for layers with datasources that update frequently (e.g. moving vehicle)
/// </summary>
Variable,
/// <summary>
/// Layer collection for layers are completely opaque and serve as Background (e.g. WMS, OSM)
/// </summary>
Background,
}
/// <summary>
/// Signature of function to handle VariableLayerCollectionRequery event
/// </summary>
/// <param name="sender">The sender of the event</param>
/// <param name="e">The arguments, <c>EventArgs.Empty</c> in all cases</param>
public delegate void VariableLayerCollectionRequeryHandler(object sender, EventArgs e);
/// <summary>
/// Layer collection
/// </summary>
/// TODO:REEVALUEATE
[Serializable]
public class VariableLayerCollection : LayerCollection
{
//private readonly LayerCollection _variableLayers;
//[NonSerialized]
//private Timer _timer;
private volatile bool _isQuerying;
//[NonSerialized]
//private EventHandler<ElapsedEventArgs> _handler;
private bool _pause;
/// <summary>
/// Method to restart the internal Timer
/// </summary>
public void TouchTimer()
{
// check for pending re-draw (eg after map pan/zoom completed)
//if (_timer.Enabled) return;
if (_isQuerying) return;
//_timer.Start();
foreach (var lyr in this)
{
if (lyr is ILayerEx lyrEx)
lyrEx.RaiseRenderRequired();
}
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(OnRequery));
}
/// <summary>
/// Method called when deserializing this object.
/// </summary>
/// <param name="context"></param>
[OnDeserializing]
public void OnDeserializing(StreamingContext context)
{
//_timer = new Timer();
}
/// <summary>
/// Event fired every <see cref="Interval"/> to force requery;
/// </summary>
public event VariableLayerCollectionRequeryHandler VariableLayerCollectionRequery;
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="variableLayers">Layer collection that holds layers with data sources updating frequently</param>
public VariableLayerCollection(LayerCollection variableLayers)
{
AddCollection(variableLayers);
//_variableLayers = variableLayers;
//if (_handler == null)
//{
// //_timer = new Timer();
// //_timer.Interval = 500;
// //_timer.Elapsed += new ElapsedEventHandler(TimerElapsed);
//}
}
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
OnRequery(null);
}
/// <inheritdoc/>
protected override void InsertItem(int index, ILayer layer)
{
if (layer == null)
throw new ArgumentNullException("layer", "The passed argument is null or not an ILayer");
lock (((ICollection)this).SyncRoot)
{
TestLayerPresent(this, layer);
base.InsertItem(index, layer);
}
}
/*
protected override void OnAddingNew(System.ComponentModel.AddingNewEventArgs e)
{
ILayer newLayer = (e.NewObject as ILayer);
if (newLayer == null) throw new ArgumentNullException("value", "The passed argument is null or not an ILayer");
TestLayerPresent(_variableLayers, newLayer);
base.OnAddingNew(e);
}
*/
private static void TestLayerPresent(IEnumerable<ILayer> layers, ILayer newLayer)
{
foreach (var layer in layers)
{
var comparison = String.Compare(layer.LayerName,
newLayer.LayerName, StringComparison.CurrentCultureIgnoreCase);
if (comparison == 0) throw new DuplicateLayerException(newLayer.LayerName);
}
}
private void OnRequery(object obj)
{
// if pan/zoom operation in progress then retry on next _timer.Elapsed
if (Pause) return;
// check for race condition when timer has been stopped while event has just been submitted on threadpool.QueueUserWorkItem
//if (!_timer.Enabled) return;
if (_isQuerying) return;
//_timer.Stop();
_isQuerying = true;
VariableLayerCollectionRequery?.Invoke(this, EventArgs.Empty);
_isQuerying = false;
}
/// <summary>
/// Gets/sets the interval in which to update layers
/// </summary>
public double Interval
{
get
{
return 0;//_timer.Interval;
}
set
{
// map sets Interval == 0 when disposing, to prevent race condition
/*
if (value <= 0)
_timer.Stop();
else
_timer.Interval = value;
*/
}
}
/// <summary>
/// Gets/Sets whether this collection should currently be updated or not
/// </summary>
public bool Pause
{
get { return _pause; }
set { _pause = value; }
}
}
}