forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayerGroup.cs
More file actions
377 lines (331 loc) · 13.5 KB
/
LayerGroup.cs
File metadata and controls
377 lines (331 loc) · 13.5 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
// 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.Collections;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using SharpMap.Data;
using GeoAPI.Geometries;
using SharpMap.Styles;
using GeoAPI.CoordinateSystems.Transformations;
namespace SharpMap.Layers
{
/// <summary>
/// Class for holding a group of layers.
/// </summary>
/// <remarks>
/// The Group layer is useful for grouping a set of layers,
/// for instance a set of image tiles, and expose them as a single layer
/// </remarks>
[Serializable]
public partial class LayerGroup : Layer, ICanQueryLayer, ICloneable, ILayersContainer
{
private ObservableCollection<ILayer> _layers;
private bool _isQueryEnabled = true;
/// <summary>
/// Event fired when the Layers collection is replaced.
/// </summary>
public event EventHandler LayersChanged;
/// <summary>
/// Fires the LayersChanged event.
/// </summary>
protected virtual void OnLayersChanged()
{
EventHandler handler = LayersChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
/// <summary>
/// Event fires when the Layers collection is going to be replaced.
/// </summary>
public event EventHandler LayersChanging;
/// <summary>
/// Fires the LayersChanging event.
/// </summary>
protected virtual void OnLayersChanging()
{
EventHandler handler = LayersChanging;
if (handler != null) handler(this, EventArgs.Empty);
}
/// <summary>
/// Initializes a new group layer
/// </summary>
/// <param name="layername">Name of layer</param>
public LayerGroup(string layername)
{
LayerName = layername;
_layers = new ObservableCollection<ILayer>();
}
/// <summary>
/// Whether the layer is queryable when used in a SharpMap.Web.Wms.WmsServer, ExecuteIntersectionQuery() will be possible in all other situations when set to FALSE
/// </summary>
public virtual bool IsQueryEnabled
{
get { return _isQueryEnabled; }
set { _isQueryEnabled = value; }
}
/// <summary>
/// Sublayers in the group
/// </summary>
public virtual ObservableCollection<ILayer> Layers
{
get { return _layers; }
set
{
if (value == null)
throw new ArgumentNullException("value");
if (!Equals(value, _layers))
{
OnLayersChanging();
_layers = value;
OnLayersChanged();
}
}
}
/// <summary>
/// Returns the extent of the layer
/// </summary>
/// <returns>Bounding box corresponding to the extent of the features in the layer</returns>
public override Envelope Envelope
{
get
{
Envelope bbox = null;
var layers = GetSnapshot();
for (int i = 0; i < layers.Length; i++)
{
var layerEnvelope = layers[i].Envelope;
if (layerEnvelope != null)
{
if(bbox == null)
bbox = new Envelope(layerEnvelope);
else
bbox.ExpandToInclude(layerEnvelope);
}
}
return bbox;
}
}
/// <summary>
/// Gets or sets whether coordinate transformations applied to the group should propagate to inner layers.
/// </summary>
/// <remarks>
/// Default is false, transformations are propagated to children layers.
/// </remarks>
public virtual bool SkipTransformationPropagation { get; set; }
/// <summary>
/// Gets or sets the <see cref="GeoAPI.CoordinateSystems.Transformations.ICoordinateTransformation"/> applied
/// to this vectorlayer prior to rendering
/// </summary>
public override ICoordinateTransformation CoordinateTransformation
{
get { return base.CoordinateTransformation; }
set
{
base.CoordinateTransformation = value;
if (!SkipTransformationPropagation)
{
var layers = GetSnapshot();
foreach (var layer in layers.OfType<Layer>())
layer.CoordinateTransformation = value;
}
}
}
/// <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 override ICoordinateTransformation ReverseCoordinateTransformation
{
get { return base.ReverseCoordinateTransformation; }
set
{
base.ReverseCoordinateTransformation = value;
if (!SkipTransformationPropagation)
{
var layers = GetSnapshot();
foreach (var layer in layers.OfType<Layer>())
layer.ReverseCoordinateTransformation = value;
}
}
}
/// <summary>
/// The spatial reference ID (CRS)
/// Propogation to child layers is dependent on <see cref="LayerGroup.SkipTransformationPropagation"/>
/// Changes to SRID with propogation enabled will cause both <see cref="CoordinateTransformation"/> and <see cref="ReverseCoordinateTransformation"/> to be reset
/// </summary>
public override int SRID
{
get { return base.SRID; }
set
{
base.SRID = value;
if (!SkipTransformationPropagation)
{
var layers = GetSnapshot();
foreach (var layer in layers.OfType<Layer>())
layer.SRID = value;
}
}
}
/// <summary>
/// The target spatial reference id
/// Propogation to child layers is dependent on <see cref="LayerGroup.SkipTransformationPropagation"/>
/// Changes to TargetSRID with propogation enabled will cause both <see cref="CoordinateTransformation"/> and <see cref="ReverseCoordinateTransformation"/> to be reset
/// </summary>
public override int TargetSRID
{
get { return base.TargetSRID; }
set
{
base.TargetSRID = value;
if (!SkipTransformationPropagation)
{
var layers = GetSnapshot();
foreach (var layer in layers.OfType<Layer>())
layer.TargetSRID = value;
}
}
}
#region IDisposable Members
/// <summary>
/// Disposes the object
/// </summary>
protected override void ReleaseManagedResources()
{
var layers = GetSnapshot();
foreach (var layer in layers.OfType<IDisposable>().Where(layer => layer != null))
layer.Dispose();
Layers.Clear();
base.ReleaseManagedResources();
}
#endregion
/// <summary>
/// Returns a layer by its name
/// </summary>
/// <param name="name">Name of layer</param>
/// <returns>Layer</returns>
public virtual ILayer GetLayerByName(string name)
{
var layers = GetSnapshot();
return layers.FirstOrDefault(t => String.Equals(t.LayerName, name, StringComparison.InvariantCultureIgnoreCase));
}
/// <summary>
/// Renders the layer
/// </summary>
/// <param name="g">Graphics object reference</param>
/// <param name="map">Map which is rendered</param>
public override void Render(Graphics g, MapViewport map)
{
var layers = GetSnapshot();
var compare = VisibilityUnits == VisibilityUnits.ZoomLevel
? map.Zoom
: map.GetMapScale((int)g.DpiX);
foreach (var layer in layers)
{
if (layer.Enabled && layer.MaxVisible >= compare &&
layer.MinVisible < compare)
layer.Render(g, map);
}
}
#region Implementation of ICanQueryLayer
/// <summary>
/// Returns the data associated with all the geometries that are intersected by 'geom'
/// </summary>
/// <param name="box">Geometry to intersect with</param>
/// <param name="ds">FeatureDataSet to fill data into</param>
public virtual void ExecuteIntersectionQuery(Envelope box, FeatureDataSet ds)
{
var layers = GetSnapshot();
foreach (var layer in layers.OfType<ICanQueryLayer>())
{
layer.ExecuteIntersectionQuery(box, ds);
}
}
/// <summary>
/// Returns the data associated with all the geometries that are intersected by 'geom'
/// </summary>
/// <param name="geometry">Geometry to intersect with</param>
/// <param name="ds">FeatureDataSet to fill data into</param>
public virtual void ExecuteIntersectionQuery(IGeometry geometry, FeatureDataSet ds)
{
var layers = GetSnapshot();
foreach (var layer in layers.OfType<ICanQueryLayer>())
{
layer.ExecuteIntersectionQuery(geometry, ds);
}
}
#endregion
/// <summary>
/// Create an empty new LayerGroup instance.
/// </summary>
/// <remarks>This is used by the Clone() method, inheritors must override this method.</remarks>
/// <returns>Returns an empty LayerGroup.</returns>
protected virtual LayerGroup CreateUninitializedInstance()
{
return new LayerGroup(LayerName);
}
/// <summary>
/// Returns a cloned copy of the group.
/// </summary>
/// <returns></returns>
public virtual object Clone()
{
var clonedGroup = CreateUninitializedInstance();
clonedGroup.Enabled = Enabled;
clonedGroup.IsQueryEnabled = IsQueryEnabled;
clonedGroup.MaxVisible = MaxVisible;
clonedGroup.VisibilityUnits = VisibilityUnits;
clonedGroup.MinVisible = MinVisible;
clonedGroup.Proj4Projection = Proj4Projection;
clonedGroup.Style = Style;
// setting SRIDs resets Transformations
clonedGroup.SRID = SRID;
clonedGroup.TargetSRID = TargetSRID;
// do NOT set NULL CoordinateTransformation, as this will cause SRID, SourceFactory, TargetSRID, and TargetFactory to be reset
if (CoordinateTransformation != null)
{
// restore defined CoordinateTransformation and associated ReverseCoordinateTransformation (causes SRID / TargetSRID to reset appropriately)
clonedGroup.CoordinateTransformation = CoordinateTransformation;
clonedGroup.ReverseCoordinateTransformation = ReverseCoordinateTransformation;
}
var layers = GetSnapshot();
foreach (var layer in layers)
{
var cloneable = layer as ICloneable;
if (cloneable != null)
clonedGroup.Layers.Add((ILayer) cloneable.Clone());
else
clonedGroup.Layers.Add(layer);
}
return clonedGroup;
}
private ILayer[] GetSnapshot()
{
ILayer[] layers;
lock (((ICollection)Layers).SyncRoot)
{
layers = Layers.ToArray();
}
return layers;
}
System.Collections.Generic.IList<ILayer> ILayersContainer.Layers
{
get { return Layers; }
}
}
}