-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayer.cs
717 lines (624 loc) · 22 KB
/
Layer.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/**
Copyright (c) 2012 - 2019, David Skorvaga
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace WmsViewer
{
using CRSIds = SortedDictionary<int, object>;
using LayerCreators = Queue<Layer.LayerCreator>;
[Serializable]
public abstract class Layer : IDisposable, IEnumerable<Layer>
{
public interface IWmsLayerCreator
{
/// <summary>
/// Gets creator of WMS object.
/// </summary>
Wms.WmsCreator WmsCreator { get; }
/// <summary>
/// Gets or sets name of the layer (used for request to the service).
/// </summary>
string Name { get; set; }
/// <summary>
/// Gets or sets title name.
/// </summary>
string Title { get; set; }
/// <summary>
/// Gets or sets short description of the layer.
/// </summary>
string Description { get; set; }
void AddCrs(string crs);
IWmsLayerCreator AddLayer();
bool HasSubLayer();
Layer Create(Wms wms);
}
public class LayerCreator : IWmsLayerCreator
{
public LayerCreator(Wms.WmsCreator wmsCreator)
: this(wmsCreator, null)
{
// Not used
}
public LayerCreator(Wms.WmsCreator wmsCreator, LayerCreator parent)
{
Debug.Assert(wmsCreator != null, "We need WmsCreator to create layers!");
WmsCreator = wmsCreator;
if (parent == null)
{
WmsCreator.Layer = this;
}
Parent = parent;
Name = string.Empty;
Title = string.Empty;
Description = string.Empty;
ImageFormat = ImageFormat.PNG;
}
/// <inheritdoc />
public Wms.WmsCreator WmsCreator { get; private set; }
/// <summary>
/// Gets parent of the layer creator.
/// </summary>
public LayerCreator Parent { get; private set; }
/// <inheritdoc />
public string Name { get; set; }
/// <inheritdoc />
public string Title { get; set; }
/// <inheritdoc />
public string Description { get; set; }
/// <summary>
/// Gets or sets image format.
/// </summary>
public ImageFormat ImageFormat { get; set; }
public void AddCrs(string crs)
{
// crs is readonly.
Debug.Assert(crs != null, "CRS must have any value!");
var crsId = WmsCreator.AddCrs(crs);
// CRS can occur repeatedly in parents (even in layer itself).
for (var layer = this; layer != null; layer = layer.Parent)
{
foreach (var id in layer.crsIds.Keys)
{
if (id == crsId)
{
return;
}
}
}
crsIds.Add(crsId, null);
}
public bool HasCrs()
{
// This object is readonly.
return crsIds.Count > 0;
}
public IWmsLayerCreator AddLayer()
{
var layer = new LayerCreator(WmsCreator, this);
layers.Enqueue(layer);
return layer;
}
public bool HasSubLayer()
{
// This object is readonly.
return layers.Count > 0;
}
public Layer Create(Wms wms)
{
// This object is readonly.
Layer layer;
if (layers.Count > 0)
{
layer = new BranchLayer(wms);
}
else
{
layer = new LeafLayer(Name, wms);
}
try
{
layer.node = new TreeNode(Title);
layer.node.Tag = layer;
layer.node.Checked = true;
layer.node.ToolTipText = Description;
var crsIdsArray = new int[crsIds.Count];
layer.CrsIds = crsIdsArray;
crsIds.Keys.CopyTo(crsIdsArray, 0);
layer.ImageFormat = WmsCreator.GetBestSupportedFormat();
foreach (var creator in layers)
{
var subLayer = creator.Create(wms);
layer.Nodes.Add(subLayer.node);
}
}
catch (Exception)
{
layer.Dispose();
throw;
}
return layer;
}
public void GeneralizeCrsIds()
{
if (HasSubLayer())
{
foreach (var layer in layers)
{
layer.GeneralizeCrsIds();
}
var commonCrsIds = new CRSIds(layers.Peek().crsIds);
var temporaryIds = new CRSIds();
foreach (var layer in layers)
{
foreach (var id in layer.crsIds)
{
object obj;
if (commonCrsIds.TryGetValue(id.Key, out obj))
{
temporaryIds.Add(id.Key, null);
}
}
var temporary = commonCrsIds;
commonCrsIds = temporaryIds;
temporaryIds = temporary;
temporaryIds.Clear();
if (commonCrsIds.Count <= 0)
{
break;
}
}
if (commonCrsIds.Count > 0)
{
foreach (var layer in layers)
{
foreach (var id in commonCrsIds)
{
layer.crsIds.Remove(id.Key);
}
}
foreach (var id in commonCrsIds)
{
crsIds[id.Key] = null;
}
}
}
}
/// <summary>
/// Ids of all supported CRSs of the layer.
/// </summary>
private CRSIds crsIds = new CRSIds();
/// <summary>
/// Creators of Layer object.
/// </summary>
private LayerCreators layers = new LayerCreators();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Erase(WebRequestsPool pool)
{
if (pool != null)
{
EraseImage(pool);
}
else
{
EraseImage();
}
var parent = Parent;
// TODO: The root will never be erased, even if there are no leafs
if (parent != null)
{
// Layer is root
// At first, SubLayer must be removed from list of layers and then can be disposed.
parent.Nodes.Remove(node);
parent.EraseIfEmpty();
}
}
public abstract IEnumerator<Layer> GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public static Layer NodeToLayer(TreeNode node)
{
// Node is readonly.
Debug.Assert(node != null, "The node associated with layer cannot be null!");
var obj = node.Tag;
Debug.Assert(obj != null, "Every TreeNode must have associated Layer!");
var layer = obj as Layer;
Debug.Assert(layer != null, "Object in TreeNode.Tag is not Layer!");
return layer;
}
public BranchLayer Parent
{
get
{
var parentNode = node.Parent;
if (parentNode != null)
{
var layer = NodeToLayer(parentNode);
var branchLayer = layer as BranchLayer;
Debug.Assert(branchLayer != null, "Every parent is branch layer!");
return branchLayer;
}
else
{
return null;
}
}
}
public Layer Root
{
get
{
var parent = Parent;
if (parent != null)
{
return parent.Root;
}
else
{
return this; // Root is layer without parent.
}
}
}
public Wms Wms { get; private set; }
/// <summary>
/// Gets title name.
/// </summary>
public string Title
{
get
{
return node.Text;
}
private set
{
Debug.Assert(value != null, "Layer title must be a text!");
node.Text = value;
}
}
/// <summary>
/// Gets short description of the layer.
/// </summary>
public string Description
{
get
{
return node.ToolTipText;
}
private set
{
Debug.Assert(value != null, "Layer description must be a text!");
node.ToolTipText = value;
}
}
/// <summary>
/// Gets ids of all supported CRSs of the layer.
/// </summary>
public ICollection<int> CrsIds { get; private set; }
/// <summary>
/// Gets or sets a value indicating whether only one request is sent to server for the layer
/// with entire tree of its sublayers (there is only one image).
/// </summary>
public abstract bool DownloadOneImage { get; set; }
/// <summary>
/// Gets or sets a value indicating whether image is updated after every change of viewport.
/// </summary>
public bool AutoUpdate { get; set; }
/// <summary>
/// Gets or sets image format.
/// </summary>
public ImageFormat ImageFormat { get; set; }
/// <summary>
/// Gets or sets a value indicating whether requested image will be transparent
/// if image format supports it.
/// </summary>
public bool IsTransparent { get; set; }
/// <summary>
/// Gets or sets transparent color.
/// </summary>
public Color BgColor { get; set; }
public bool IsRoot()
{
// This object is readonly.
return Parent == null;
}
public abstract bool IsLeaf();
public bool IsVisible
{
get
{
return node.Checked; // If the image is displayed or not (checkbox is checked).
}
}
public virtual Layer FirstWithChildren(int number)
{
var parent = Parent;
if (parent != null)
{
return parent.FirstWithChildren(number);
}
else
{
return null;
}
}
public Uri ImageAddress
{
get
{
var currentImage = image;
if (currentImage != null)
{
return currentImage.GetImageAddress(Wms);
}
else
{
return null;
}
}
}
public MapImage ReleaseImage()
{
return ExchangeImage(null);
}
public void SetImage(MapImage newImage)
{
var oldImage = ExchangeImage(newImage);
if (oldImage != null)
{
oldImage.Dispose();
}
}
public void SetOrErase(MapImage newImage)
{
// It erases image if other one is present.
var oldImage = Interlocked.CompareExchange<MapImage>(ref image, newImage, null);
if (oldImage != null)
{
oldImage.Dispose();
}
}
public MapImage ExchangeImage(MapImage newImage)
{
return Interlocked.Exchange<MapImage>(ref image, newImage);
}
public void EraseImage(WebRequestsPool pool)
{
Debug.Assert(pool != null, "It is needed RequestPool to complete image detetion!");
pool.RemoveRequest(this);
EraseImage();
}
public void EraseImage()
{
var oldImage = ReleaseImage();
if (oldImage != null)
{
oldImage.Dispose();
}
}
public virtual void EraseImagesInTree(WebRequestsPool pool)
{
EraseImage(pool);
}
public bool IsCrsSupportedByParents()
{
// This object is readonly.
return IsCrsSupportedByParents(Wms.CRSId);
}
public bool IsCrsSupportedByParents(int crsId)
{
// This object is readonly.
var parent = Parent;
if (parent != null)
{
foreach (var id in parent.CrsIds)
{
if (id == crsId)
{
return true;
}
}
return parent.IsCrsSupportedByParents(crsId);
}
else
{
return false;
}
}
public bool IsCrsSupportedByLayer()
{
// This object is readonly.
return IsCrsSupportedByLayer(Wms.CRSId);
}
public bool IsCrsSupportedByLayer(int crsId)
{
// This object is readonly.
foreach (var id in CrsIds)
{
if (id == crsId)
{
return true;
}
}
return false;
}
public abstract void GetVisibleLayerNames(StringBuilder names, bool supportedByParents);
public void AddToTreeView(TreeView tree)
{
Debug.Assert(tree != null, "TreeView to append does not exist!");
tree.Nodes.Add(node);
}
public virtual void Draw(IViewport view, Graphics canvas, WebRequestsPool pool)
{
// Viewport is readonly.
Debug.Assert(view != null, "Viewport is missing!");
Debug.Assert(canvas != null, "Canvas is missing!");
Debug.Assert(pool != null, "WebRequestsPool is necessary for image downloading!");
var builder = new StringBuilder();
GetVisibleLayerNames(builder, IsCrsSupportedByParents());
if (builder.Length > 0)
{
var newView = view.Copy();
newView.ReduceViewBox(Wms.MaxWidth, Wms.MaxHeight);
var newImage = new MapImage(builder.ToString(), Wms.CRSId, newView, ImageFormat,
IsTransparent, BgColor);
try
{
// We cannot work with variable directly,
// because another thread could rewrite it during the work.
var oldImage = ReleaseImage();
try
{
if (IsVisible)
{
// Are we downloading?
if (AutoUpdate && Wms.IsDownloadingEnabled)
{
if ((oldImage == null) || (!newImage.Equals(oldImage)))
{
var requested = newImage;
newImage = null; // Do not dispose the object
pool.PutRequest(this, requested, requested.GetImageAddress(Wms));
if (oldImage != null)
{
oldImage.Draw(view, canvas);
}
}
else
{
// If the situation changes and there is requested the same image
// that already exists in the layer, outdated downloading is running
// at this moment and it must be interrupted.
pool.RemoveRequest(this);
oldImage.Draw(view, canvas);
}
}
else
{
// Downloading must be interrupted.
pool.RemoveRequest(this);
if (oldImage != null)
{
// Use an image with different CRS is nonsense.
if (oldImage.CRSEquals(newImage))
{
oldImage.Draw(view, canvas);
}
else
{
oldImage.Dispose();
oldImage = null; // Erase old image
}
}
}
}
else
{
if ((oldImage != null) && (!newImage.Equals(oldImage)))
{
oldImage.Dispose();
oldImage = null; // Erase old image
}
}
}
finally
{
// If another thread save new image, we prefer it.
SetOrErase(oldImage);
}
}
finally
{
if (newImage != null)
{
newImage.Dispose();
}
}
}
else
{
EraseImage(pool);
}
}
public bool IsRequestedIndividually()
{
// This object is readonly.
for (var layer = Parent; layer != null; layer = layer.Parent)
{
if (layer.DownloadOneImage)
{
return false; // It is part of any parent request.
}
}
return true;
}
protected Layer(Wms wms)
{
// This object is readonly.
Debug.Assert(wms != null, "Layer cannot exist without WMS!");
Wms = wms;
CrsIds = null;
AutoUpdate = true;
ImageFormat = ImageFormat.PNG;
IsTransparent = true;
BgColor = Color.White;
node.Checked = true;
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Erase(null);
}
}
protected TreeNodeCollection Nodes // List of all sublayers (Their order matters)
{
get
{
return node.Nodes;
}
}
/// <summary>
/// Object holding information about the image downloaded from the server.
/// </summary>
[NonSerialized]
private MapImage image = null;
/// <summary>
/// Tree node associated with layer (same tree structure).
/// </summary>
private TreeNode node = new TreeNode();
}
}