-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathLayer.cs
More file actions
678 lines (593 loc) · 23.4 KB
/
Layer.cs
File metadata and controls
678 lines (593 loc) · 23.4 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
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
// Copyright 2005, 2006 - Morten Nielsen (www.iter.dk)
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Drawing;
using GeoAPI.CoordinateSystems.Transformations;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries.Utilities;
using SharpMap.Base;
using SharpMap.Rendering;
using SharpMap.Styles;
using SharpMap.Utilities;
namespace SharpMap.Layers
{
/// <summary>
/// Abstract class for common layer properties
/// Implement this class instead of the ILayer interface to save a lot of common code.
/// </summary>
[Serializable]
public abstract partial class Layer : DisposableObject, ILayerEx
{
#region Events
#region Delegates
/// <summary>
/// EventHandler for event fired when the layer has been rendered
/// </summary>
/// <param name="layer">Layer rendered</param>
/// <param name="g">Reference to graphics object used for rendering</param>
public delegate void LayerRenderedEventHandler(Layer layer, Graphics g);
#endregion
/// <summary>
/// Event fired when the layer has been rendered
/// </summary>
public event LayerRenderedEventHandler LayerRendered;
/// <summary>
/// Event raised when the layer's <see cref="SRID"/> property has changed
/// </summary>
public event EventHandler SRIDChanged;
/// <summary>
/// Method called when <see cref="SRID"/> has changed, to invoke <see cref="E:SharpMap.Layers.Layer.SRIDChanged"/>
/// </summary>
/// <param name="eventArgs">The arguments associated with the event</param>
protected virtual void OnSridChanged(EventArgs eventArgs)
{
var handler = SRIDChanged;
if (handler != null) handler(this, eventArgs);
}
/// <summary>
/// Event raised when the layer's <see cref="Style"/> property has changed
/// </summary>
public event EventHandler StyleChanged;
/// <summary>
/// Method called when <see cref="Style"/> has changed, to invoke <see cref="E:SharpMap.Layers.Layer.StyleChanged"/>
/// </summary>
/// <param name="eventArgs">The arguments associated with the event</param>
protected virtual void OnStyleChanged(EventArgs eventArgs)
{
var handler = StyleChanged;
if (handler != null) handler(this, eventArgs);
}
/// <summary>
/// Event raised when the layers's <see cref="LayerName"/> property has changed
/// </summary>
public event EventHandler LayerNameChanged;
/// <summary>
/// Method called when <see cref="LayerName"/> has changed, to invoke <see cref="E:SharpMap.Layers.Layer.LayerNameChanged"/>
/// </summary>
/// <param name="eventArgs">The arguments associated with the event</param>
protected virtual void OnLayerNameChanged(EventArgs eventArgs)
{
var handler = LayerNameChanged;
if (handler != null) handler(this, eventArgs);
}
#endregion
private ICoordinateTransformation _coordinateTransform;
private ICoordinateTransformation _reverseCoordinateTransform;
private IGeometryFactory _sourceFactory;
private IGeometryFactory _targetFactory;
private string _layerName;
private string _layerTitle;
private IStyle _style;
private int _srid = -1;
private int? _targetSrid;
[field: NonSerialized]
private bool _shouldNotResetCt;
/// <summary>
/// The area of the map that was covered by this layer
/// </summary>
[field: NonSerialized]
protected RectangleF CanvasArea = RectangleF.Empty;
// ReSharper disable PublicConstructorInAbstractClass
///<summary>
/// Creates an instance of this class using the given Style
///</summary>
///<param name="style"></param>
public Layer(Style style)
// ReSharper restore PublicConstructorInAbstractClass
{
_style = style;
}
/// <summary>
/// Creates an instance of this class
/// </summary>
protected Layer() //Style style)
{
_style = new Style();
}
/// <summary>
/// Releases managed resources
/// </summary>
protected override void ReleaseManagedResources()
{
_coordinateTransform = null;
_reverseCoordinateTransform = null;
_style = null;
base.ReleaseManagedResources();
}
/// <summary>
/// Gets or sets the <see cref="GeoAPI.CoordinateSystems.Transformations.ICoordinateTransformation"/> applied
/// to this vectorlayer prior to rendering
/// </summary>
public virtual ICoordinateTransformation CoordinateTransformation
{
get
{
if (_coordinateTransform == null && NeedsTransformation)
{
var css = Session.Instance.CoordinateSystemServices;
_coordinateTransform = css.CreateTransformation(
css.GetCoordinateSystem(SRID), css.GetCoordinateSystem(TargetSRID));
}
return _coordinateTransform;
}
set
{
if (value == _coordinateTransform && value != null)
return;
_coordinateTransform = value;
try
{
// we don't want that by setting SRID we get the CoordinateTransformation resetted
_shouldNotResetCt = true;
if (_coordinateTransform != null)
{
// causes sourceFactory/targetFactory to reset to new SRID/TargetSRID
SRID = Convert.ToInt32(CoordinateTransformation.SourceCS.AuthorityCode);
TargetSRID = Convert.ToInt32(CoordinateTransformation.TargetCS.AuthorityCode);
}
else
{
_sourceFactory = GeoAPI.GeometryServiceProvider.Instance.CreateGeometryFactory(SRID);
// causes targetFactory to be cleared
TargetSRID = 0;
}
}
finally
{
_shouldNotResetCt = false;
}
// check if ReverseTransform is required
if (_coordinateTransform == null || !NeedsTransformation)
_reverseCoordinateTransform = null;
// check if existing ReverseTransform is compatible with CoordinateTransform
if (_reverseCoordinateTransform != null)
{
//clear if not compatible with CoordinateTransformation
if (_coordinateTransform.SourceCS.AuthorityCode != _coordinateTransform.TargetCS.AuthorityCode ||
_coordinateTransform.TargetCS.AuthorityCode != _coordinateTransform.SourceCS.AuthorityCode)
_reverseCoordinateTransform = null;
}
OnCoordinateTransformationChanged(EventArgs.Empty);
}
}
/// <summary>
/// Event raised when the <see cref="CoordinateTransformation"/> has changed
/// </summary>
public event EventHandler CoordinateTransformationChanged;
/// <summary>
/// Event invoker for the <see cref="CoordinateTransformationChanged"/> event
/// </summary>
/// <param name="e">The event's arguments</param>
protected virtual void OnCoordinateTransformationChanged(EventArgs e)
{
if (CoordinateTransformationChanged != null)
CoordinateTransformationChanged(this, e);
}
/// <summary>
/// Gets the geometry factory to create source geometries
/// </summary>
protected internal IGeometryFactory SourceFactory { get { return _sourceFactory ?? (_sourceFactory = GeoAPI.GeometryServiceProvider.Instance.CreateGeometryFactory(SRID)); } }
/// <summary>
/// Gets the geometry factory to create target geometries
/// </summary>
protected internal IGeometryFactory TargetFactory { get { return _targetFactory ?? _sourceFactory; } }
/// <summary>
/// Certain Transformations cannot be inverted in ProjNet, in those cases use this property to set the reverse <see cref="GeoAPI.CoordinateSystems.Transformations.ICoordinateTransformation"/> (of CoordinateTransformation) to fetch data from Datasource
///
/// If your CoordinateTransformation can be inverted you can leave this property to null
/// </summary>
public virtual ICoordinateTransformation ReverseCoordinateTransformation
{
get
{
if (_reverseCoordinateTransform == null && NeedsTransformation)
{
var css = Session.Instance.CoordinateSystemServices;
_reverseCoordinateTransform = css.CreateTransformation(
css.GetCoordinateSystem(TargetSRID), css.GetCoordinateSystem(SRID));
}
return _reverseCoordinateTransform;
}
set
{
if (value == _reverseCoordinateTransform)
return;
_reverseCoordinateTransform = value;
}
}
/// <summary>
/// Gets a flag indicating if the layer needs coordinate transformation
/// </summary>
protected bool NeedsTransformation
{
get { return SRID != 0 && TargetSRID != 0 && SRID != TargetSRID; }
}
#region ILayer Members
/// <summary>
/// Gets or sets the name of the layer
/// </summary>
public string LayerName
{
get { return _layerName; }
set { _layerName = value; }
}
/// <summary>
/// Gets or sets the title of the layer
/// </summary>
public string LayerTitle
{
get { return _layerTitle; }
set { _layerTitle = value; }
}
/// <summary>
/// The spatial reference ID (CRS)
/// </summary>
public virtual int SRID
{
get { return _srid; }
set
{
if (value != _srid)
{
_srid = value;
_sourceFactory = GeoAPI.GeometryServiceProvider.Instance.CreateGeometryFactory(value);
if (!_shouldNotResetCt)
_coordinateTransform = _reverseCoordinateTransform = null;
OnSridChanged(EventArgs.Empty);
}
}
}
/// <summary>
/// Gets or sets a value indicating the target spatial reference id
/// </summary>
public virtual int TargetSRID
{
get { return _targetSrid.HasValue ? _targetSrid.Value : SRID; }
set
{
if (value == SRID || value == 0)
{
_targetSrid = null;
_targetFactory = null;
}
else if (_targetSrid != value)
{
_targetSrid = value;
_targetFactory = GeoAPI.GeometryServiceProvider.Instance.CreateGeometryFactory(value);
}
if (!_shouldNotResetCt)
_coordinateTransform = _reverseCoordinateTransform = null;
}
}
//public abstract SharpMap.CoordinateSystems.CoordinateSystem CoordinateSystem { get; set; }
/// <summary>
/// Renders the layer
/// </summary>
/// <param name="g">Graphics object reference</param>
/// <param name="map">Map which is rendered</param>
[Obsolete("Use Render(Graphics, MapViewport, out Envelope affectedArea)")]
public virtual void Render(Graphics g, Map map)
{
Render(g, (MapViewport)map, out _);
}
/// <summary>
/// Renders the layer using the current viewport
/// </summary>
/// <param name="g">Graphics object reference</param>
/// <param name="map">Map which is rendered</param>
public virtual void Render(Graphics g, MapViewport map)
{
Render(g, map, out _);
}
/// <summary>
/// Renders the layer using the current viewport
/// </summary>
/// <param name="g">Graphics object reference</param>
/// <param name="map">Map which is rendered</param>
/// <returns>Rectangle enclosing the actual area rendered on the graphics canvas</returns>
Rectangle ILayerEx.Render(Graphics g, MapViewport map)
{
Render(g, map, out var canvasArea);
return canvasArea;
}
private bool _renderCalled;
/// <summary>
/// Renders the layer using the given graphics object and viewport. The <paramref name="affectedArea"/> is an additional result.
/// </summary>
/// <param name="g">A graphics object</param>
/// <param name="mvp">A map viewport</param>
/// <param name="affectedArea">The affected area.</param>
protected virtual void Render(Graphics g, MapViewport mvp, out Rectangle affectedArea)
{
if (_renderCalled)
return;
_renderCalled = true;
Render(g, mvp);
_renderCalled = false;
var mapRect = new Rectangle(new Point(0, 0), mvp.Size);
if (CanvasArea.IsEmpty)
{
affectedArea = mapRect;
}
else
{
affectedArea = ToGraphicsCanvas(CanvasArea, g.Transform);
// clip to graphics canvas
affectedArea.Intersect(mapRect);
CanvasArea = RectangleF.Empty;
}
OnLayerRendered(g);
}
/// <summary>
/// Transforms cavas area to untransformed graphics canvas
/// </summary>
/// <param name="area"></param>
/// <param name="matrix"></param>
/// <returns></returns>
protected static Rectangle ToGraphicsCanvas(RectangleF area, System.Drawing.Drawing2D.Matrix matrix)
{
if (!matrix.IsIdentity)
{
var pts = area.ToPointArray();
matrix.TransformPoints(pts);
// Enclosing rectangle aligned with graphics canvas and inflated to nearest integer values.
area = pts.ToRectangleF();
}
// This is the area of the graphics canvas that needs to be refreshed when invalidating the image.
var affectedArea = area.ToRectangle();
// // proof of concept: draw affected area to screen aligned with graphics canvas
// using (var orig = g.Transform.Clone())
// {
// var areaToBeRendered = affectedArea;
// areaToBeRendered.Intersect(mapRect);
// g.ResetTransform();
// g.DrawRectangle(new Pen(Color.Red, 3f) {Alignment = System.Drawing.Drawing2D.PenAlignment.Inset},
// areaToBeRendered);
// g.Transform = orig;
// }
// allow for bleed and/or minor labelling misdemeanours
affectedArea.Inflate(1, 1);
return affectedArea;
}
/// <summary>
/// Event invoker for the <see cref="LayerRendered"/> event.
/// </summary>
/// <param name="g">The graphics object</param>
protected virtual void OnLayerRendered(Graphics g)
{
if (LayerRendered != null)
LayerRendered(this, g);
}
/// <summary>
/// Returns the extent of the layer
/// </summary>
/// <returns>Bounding box corresponding to the extent of the features in the layer</returns>
public abstract Envelope Envelope { get; }
#endregion
#region Properties
/// <summary>
/// Proj4 projection definition string
/// </summary>
public string Proj4Projection { get; set; }
/*
private bool _Enabled = true;
private double _MaxVisible = double.MaxValue;
private double _MinVisible = 0;
*/
/// <summary>
/// Minimum visibility zoom, including this value
/// </summary>
public double MinVisible
{
get
{
return _style.MinVisible; // return _MinVisible;
}
set
{
_style.MinVisible = value; // _MinVisible = value;
}
}
/// <summary>
/// Maximum visibility zoom, excluding this value
/// </summary>
public double MaxVisible
{
get
{
//return _MaxVisible;
return _style.MaxVisible;
}
set
{
//_MaxVisible = value;
_style.MaxVisible = value;
}
}
/// <summary>
/// Gets or Sets what kind of units the Min/Max visible properties are defined in
/// </summary>
public VisibilityUnits VisibilityUnits
{
get
{
return _style.VisibilityUnits;
}
set
{
_style.VisibilityUnits = value;
}
}
/// <summary>
/// Specified whether the layer is rendered or not
/// </summary>
public bool Enabled
{
get
{
//return _Enabled;
return _style.Enabled;
}
set
{
if (value == _style.Enabled)
return;
_style.Enabled = value;
RaiseRenderRequired();
}
}
/// <summary>
/// Gets or sets the Style for this Layer
/// </summary>
public virtual IStyle Style
{
get { return _style; }
set
{
if (value != _style && !_style.Equals(value))
{
_style = value;
OnStyleChanged(EventArgs.Empty);
RaiseRenderRequired();
}
}
}
#endregion
/// <summary>
/// Returns the name of the layer.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return LayerName;
}
/// <summary>
/// Invokes <see cref="RenderRequired"/> event on this layer
/// </summary>
public void RaiseRenderRequired()
{
RenderRequired?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Event raised when the layer needs to be rendered.
/// </summary>
public event EventHandler RenderRequired;
#region Reprojection utility functions
/// <summary>
/// Utility function to transform given envelope using a specific transformation
/// </summary>
/// <param name="envelope">The source envelope</param>
/// <param name="coordinateTransformation">The <see cref="GeoAPI.CoordinateSystems.Transformations.ICoordinateTransformation"/> to use.</param>
/// <returns>The target envelope</returns>
protected virtual Envelope ToTarget(Envelope envelope, ICoordinateTransformation coordinateTransformation)
{
if (coordinateTransformation == null)
return envelope;
return GeometryTransform.TransformBox(envelope, coordinateTransformation.MathTransform);
}
/// <summary>
/// Utility function to transform given envelope to the target envelope
/// </summary>
/// <param name="envelope">The source envelope</param>
/// <returns>The target envelope</returns>
protected Envelope ToTarget(Envelope envelope)
{
return ToTarget(envelope, CoordinateTransformation);
}
/// <summary>
/// Utility function to transform given envelope to the source envelope
/// </summary>
/// <param name="envelope">The target envelope</param>
/// <returns>The source envelope</returns>
protected virtual Envelope ToSource(Envelope envelope)
{
if (ReverseCoordinateTransformation != null)
{
return GeometryTransform.TransformBox(envelope, ReverseCoordinateTransformation.MathTransform);
}
if (CoordinateTransformation != null)
{
var mt = CoordinateTransformation.MathTransform;
mt.Invert();
var res = GeometryTransform.TransformBox(envelope, mt);
mt.Invert();
return res;
}
// no transformation
return envelope;
}
/// <summary>
/// Function to transform <paramref name="geometry"/> to the coordinate system defined by <see cref="TargetSRID"/>
/// </summary>
/// <param name="geometry">A geometry</param>
/// <returns>The transformed geometry</returns>
protected virtual IGeometry ToTarget(IGeometry geometry)
{
if (geometry.SRID == TargetSRID)
return geometry;
if (CoordinateTransformation != null)
{
return GeometryTransform.TransformGeometry(geometry, CoordinateTransformation.MathTransform, TargetFactory);
}
return geometry;
}
/// <summary>
/// Function to transform <paramref name="geometry"/> to the coordinate system defined by <see cref="SRID"/>
/// </summary>
/// <param name="geometry">A geometry</param>
/// <returns>The transformed geometry</returns>
protected virtual IGeometry ToSource(IGeometry geometry)
{
if (geometry.SRID == SRID)
return geometry;
if (ReverseCoordinateTransformation != null)
{
return GeometryTransform.TransformGeometry(geometry,
ReverseCoordinateTransformation.MathTransform, SourceFactory);
}
if (CoordinateTransformation != null)
{
var mt = CoordinateTransformation.MathTransform;
mt.Invert();
var res = GeometryTransform.TransformGeometry(geometry, mt, SourceFactory);
mt.Invert();
return res;
}
return geometry;
}
#endregion
}
}