-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathVectorLayer.cs
More file actions
692 lines (587 loc) · 26.4 KB
/
VectorLayer.cs
File metadata and controls
692 lines (587 loc) · 26.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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// 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.ObjectModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using SharpMap.Data;
using SharpMap.Data.Providers;
using GeoAPI.Geometries;
using SharpMap.Rendering;
using SharpMap.Rendering.Thematics;
using SharpMap.Styles;
using System.Collections.Generic;
using Common.Logging;
namespace SharpMap.Layers
{
/// <summary>
/// Class for vector layer properties
/// </summary>
[Serializable]
public class VectorLayer : Layer, ICanQueryLayer, ICloneable
{
static readonly ILog _logger = LogManager.GetLogger(typeof(VectorLayer));
private bool _clippingEnabled;
private bool _isQueryEnabled = true;
private IBaseProvider _dataSource;
private SmoothingMode _smoothingMode;
private ITheme _theme;
private Envelope _envelope;
/// <summary>
/// Initializes a new layer
/// </summary>
/// <param name="layername">Name of layer</param>
public VectorLayer(string layername)
: base(new VectorStyle())
{
LayerName = layername;
SmoothingMode = SmoothingMode.AntiAlias;
}
/// <summary>
/// Initializes a new layer with a specified datasource
/// </summary>
/// <param name="layername">Name of layer</param>
/// <param name="dataSource">Data source</param>
public VectorLayer(string layername, IBaseProvider dataSource) : this(layername)
{
_dataSource = dataSource;
}
/// <summary>
/// Gets or sets a Dictionary with themes suitable for this layer. A theme in the dictionary can be used for rendering be setting the Theme Property using a delegate function
/// </summary>
public Dictionary<string, ITheme> Themes
{
get;
set;
}
/// <summary>
/// Gets or sets thematic settings for the layer. Set to null to ignore thematics
/// </summary>
public ITheme Theme
{
get { return _theme; }
set { _theme = value; }
}
/// <summary>
/// Specifies whether polygons should be clipped prior to rendering
/// </summary>
/// <remarks>
/// <para>Clipping will clip <see cref="GeoAPI.Geometries.IPolygon"/> and
/// <see cref="GeoAPI.Geometries.IMultiPolygon"/> to the current view prior
/// to rendering the object.</para>
/// <para>Enabling clipping might improve rendering speed if you are rendering
/// only small portions of very large objects.</para>
/// </remarks>
public bool ClippingEnabled
{
get { return _clippingEnabled; }
set { _clippingEnabled = value; }
}
/// <summary>
/// Render whether smoothing (antialiasing) is applied to lines and curves and the edges of filled areas
/// </summary>
public SmoothingMode SmoothingMode
{
get { return _smoothingMode; }
set { _smoothingMode = value; }
}
/// <summary>
/// Gets or sets the datasource
/// </summary>
public IBaseProvider DataSource
{
get { return _dataSource; }
set
{
_dataSource = value;
_envelope = null;
}
}
/// <summary>
/// Gets or sets the rendering style of the vector layer.
/// </summary>
public new VectorStyle Style
{
get { return base.Style as VectorStyle; }
set { base.Style = value; }
}
/// <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
{
if (DataSource == null)
throw (new ApplicationException("DataSource property not set on layer '" + LayerName + "'"));
if (_envelope != null && CacheExtent)
return ToTarget(_envelope.Copy());
Envelope box;
lock (_dataSource)
{
// Is datasource already open?
bool wasOpen = DataSource.IsOpen;
if (!wasOpen) { DataSource.Open(); }
box = DataSource.GetExtents();
if (!wasOpen) { DataSource.Close(); }
}
if (CacheExtent)
_envelope = box;
return ToTarget(box);
}
}
/// <summary>
/// Gets or sets a value indicating whether the layer envelope should be treated as static or not.
/// </summary>
/// <remarks>
/// When CacheExtent is enabled the layer Envelope will be calculated only once from DataSource, this
/// helps to speed up the Envelope calculation with some DataProviders. Default is false for backward
/// compatibility.
/// </remarks>
public virtual bool CacheExtent { get; set; }
/// <summary>
/// Gets or sets the SRID of this VectorLayer's data source
/// </summary>
public override int SRID
{
get
{
if (DataSource == null)
throw (new ApplicationException("DataSource property not set on layer '" + LayerName + "'"));
return DataSource.SRID;
}
set
{
DataSource.SRID = value;
base.SRID = value;
}
}
#region IDisposable Members
/// <summary>
/// Disposes the object
/// </summary>
protected override void ReleaseManagedResources()
{
if (DataSource != null)
DataSource.Dispose();
base.ReleaseManagedResources();
}
#endregion
/// <summary>
/// Renders the layer to a graphics object, using the given map viewport
/// </summary>
/// <param name="g">Graphics object reference</param>
/// <param name="mvp">Map view port</param>
public override void Render(Graphics g, MapViewport mvp)
{
if (mvp.Center == null)
throw (new ApplicationException("Cannot render map. View center not specified"));
g.SmoothingMode = SmoothingMode;
var envelope = ToSource(mvp.Envelope); //View to render
if (DataSource == null)
throw (new ApplicationException("DataSource property not set on layer '" + LayerName + "'"));
//If thematics is enabled, we use a slighty different rendering approach
if (Theme != null)
RenderInternal(g, mvp, envelope, Theme);
else
RenderInternal(g, mvp, envelope);
//OnLayerRendered(g);
// Obsolete (and will cause infinite loop)
base.Render(g, mvp);
}
/// <summary>
/// Method to render this layer to the map, applying <paramref name="theme"/>.
/// </summary>
/// <param name="g">The graphics object</param>
/// <param name="map">The map object</param>
/// <param name="envelope">The envelope to render</param>
/// <param name="theme">The theme to apply</param>
protected void RenderInternal(Graphics g, MapViewport map, Envelope envelope, ITheme theme)
{
var canvasArea = RectangleF.Empty;
var combinedArea = RectangleF.Empty;
var ds = new FeatureDataSet();
lock (_dataSource)
{
// Is datasource already open?
bool wasOpen = DataSource.IsOpen;
if (!wasOpen) { DataSource.Open(); }
DataSource.ExecuteIntersectionQuery(envelope, ds);
if (!wasOpen) { DataSource.Close(); }
}
double scale = map.GetMapScale((int)g.DpiX);
double zoom = map.Zoom;
Func<MapViewport, FeatureDataRow, IStyle> evalStyle;
if (theme is IThemeEx)
evalStyle = new ThemeExEvaluator((IThemeEx)theme).GetStyle;
else
evalStyle = new ThemeEvaluator(theme).GetStyle;
foreach (FeatureDataTable features in ds.Tables)
{
// Transform geometries if necessary
if (CoordinateTransformation != null)
{
for (int i = 0; i < features.Count; i++)
{
features[i].Geometry = ToTarget(features[i].Geometry);
}
}
//Linestring outlines is drawn by drawing the layer once with a thicker line
//before drawing the "inline" on top.
if (Style.EnableOutline)
{
for (int i = 0; i < features.Count; i++)
{
var feature = features[i];
var outlineStyle = evalStyle(map, feature) as VectorStyle;
if (outlineStyle == null) continue;
if (!(outlineStyle.Enabled && outlineStyle.EnableOutline)) continue;
var compare = outlineStyle.VisibilityUnits == VisibilityUnits.ZoomLevel ? zoom : scale;
if (!(outlineStyle.MinVisible <= compare && compare <= outlineStyle.MaxVisible)) continue;
using (outlineStyle = outlineStyle.Clone())
{
if (outlineStyle != null)
{
//Draw background of all line-outlines first
if (feature.Geometry is ILineString)
{
canvasArea = VectorRenderer.DrawLineStringEx(g, feature.Geometry as ILineString, outlineStyle.Outline,
map, outlineStyle.LineOffset);
}
else if (feature.Geometry is IMultiLineString)
{
canvasArea = VectorRenderer.DrawMultiLineStringEx(g, feature.Geometry as IMultiLineString,
outlineStyle.Outline, map, outlineStyle.LineOffset);
}
combinedArea = canvasArea.ExpandToInclude(combinedArea);
}
}
}
}
for (int i = 0; i < features.Count; i++)
{
var feature = features[i];
var style = evalStyle(map, feature);
if (style == null) continue;
if (!style.Enabled) continue;
double compare = style.VisibilityUnits == VisibilityUnits.ZoomLevel ? zoom : scale;
if (!(style.MinVisible <= compare && compare <= style.MaxVisible)) continue;
IEnumerable<IStyle> stylesToRender = GetStylesToRender(style);
if (stylesToRender == null)
return;
foreach (var vstyle in stylesToRender)
{
if (!(vstyle is VectorStyle) || !vstyle.Enabled)
continue;
using (var clone = (vstyle as VectorStyle).Clone())
{
if (clone != null)
{
canvasArea = RenderGeometryEx(g, map, feature.Geometry, clone);
combinedArea = canvasArea.ExpandToInclude(combinedArea);
}
}
}
}
}
CanvasArea = combinedArea;
}
/// <summary>
/// Method to render this layer to the map, applying <see cref="Style"/>.
/// </summary>
/// <param name="g">The graphics object</param>
/// <param name="map">The map object</param>
/// <param name="envelope">The envelope to render</param>
protected void RenderInternal(Graphics g, MapViewport map, Envelope envelope)
{
//if style is not enabled, we don't need to render anything
if (!Style.Enabled) return;
IEnumerable<IStyle> stylesToRender = GetStylesToRender(Style);
if (stylesToRender == null)
return;
var canvasArea = RectangleF.Empty;
var combinedArea = RectangleF.Empty;
Collection<IGeometry> geoms = null;
foreach (var style in stylesToRender)
{
if (!(style is VectorStyle) || !style.Enabled)
continue;
using (var vStyle = (style as VectorStyle).Clone())
{
if (vStyle != null)
{
if (geoms == null)
{
lock (_dataSource)
{
// Is datasource already open?
bool wasOpen = DataSource.IsOpen;
if (!wasOpen) { DataSource.Open(); }
// Read data
geoms = DataSource.GetGeometriesInView(envelope);
if (!wasOpen) { DataSource.Close(); }
}
if (_logger.IsDebugEnabled)
{
_logger.DebugFormat("Layer {0}, NumGeometries {1}", LayerName, geoms.Count);
}
// Transform geometries if necessary
if (CoordinateTransformation != null)
{
for (int i = 0; i < geoms.Count; i++)
{
geoms[i] = ToTarget(geoms[i]);
}
}
}
if (vStyle.LineSymbolizer != null)
{
vStyle.LineSymbolizer.Begin(g, map, geoms.Count);
}
else
{
//Linestring outlines is drawn by drawing the layer once with a thicker line
//before drawing the "inline" on top.
if (vStyle.EnableOutline)
{
foreach (var geom in geoms)
{
if (geom != null)
{
//Draw background of all line-outlines first
if (geom is ILineString)
canvasArea = VectorRenderer.DrawLineStringEx(g, geom as ILineString, vStyle.Outline, map, vStyle.LineOffset);
else if (geom is IMultiLineString)
canvasArea = VectorRenderer.DrawMultiLineStringEx(g, geom as IMultiLineString, vStyle.Outline, map, vStyle.LineOffset);
combinedArea = canvasArea.ExpandToInclude(combinedArea);
}
}
}
}
foreach (IGeometry geom in geoms)
{
if (geom != null)
{
canvasArea = RenderGeometryEx(g, map, geom, vStyle);
combinedArea = canvasArea.ExpandToInclude(combinedArea);
}
}
if (vStyle.LineSymbolizer != null)
{
vStyle.LineSymbolizer.Symbolize(g, map);
vStyle.LineSymbolizer.End(g, map);
}
}
}
}
CanvasArea = combinedArea;
}
/// <summary>
/// Unpacks styles to render (can be nested group-styles)
/// </summary>
/// <param name="style"></param>
/// <returns></returns>
public static IEnumerable<IStyle> GetStylesToRender(IStyle style)
{
IStyle[] stylesToRender = null;
if (style is GroupStyle)
{
var gs = style as GroupStyle;
var styles = new List<IStyle>();
for (var i = 0; i < gs.Count; i++)
{
styles.AddRange(GetStylesToRender(gs[i]));
}
stylesToRender = styles.ToArray();
}
else if (style is VectorStyle)
{
stylesToRender = new[] { style };
}
return stylesToRender;
}
/// <summary>
/// Method to render <paramref name="feature"/> using <paramref name="style"/>
/// </summary>
/// <param name="g">The graphics object</param>
/// <param name="map">The map</param>
/// <param name="feature">The feature's geometry</param>
/// <param name="style">The style to apply</param>
protected void RenderGeometry(Graphics g, MapViewport map, IGeometry feature, VectorStyle style)
{
RenderGeometryEx(g, map, feature, style);
}
/// <summary>
/// Function to render <paramref name="feature"/> using <paramref name="style"/> and returning the area covered.
/// </summary>
/// <param name="g">The graphics object</param>
/// <param name="map">The map</param>
/// <param name="feature">The feature's geometry</param>
/// <param name="style">The style to apply</param>
protected RectangleF RenderGeometryEx(Graphics g, MapViewport map, IGeometry feature, VectorStyle style)
{
if (feature == null) return RectangleF.Empty;
var geometryType = feature.OgcGeometryType;
switch (geometryType)
{
case OgcGeometryType.Polygon:
if (style.EnableOutline)
return VectorRenderer.DrawPolygonEx(g, (IPolygon)feature, style.Fill, style.Outline, _clippingEnabled,
map);
else
return VectorRenderer.DrawPolygonEx(g, (IPolygon)feature, style.Fill, null, _clippingEnabled, map);
case OgcGeometryType.MultiPolygon:
if (style.EnableOutline)
return VectorRenderer.DrawMultiPolygonEx(g, (IMultiPolygon)feature, style.Fill, style.Outline, _clippingEnabled, map);
return VectorRenderer.DrawMultiPolygonEx(g, (IMultiPolygon)feature, style.Fill, null, _clippingEnabled, map);
case OgcGeometryType.LineString:
if (style.LineSymbolizer != null)
{
style.LineSymbolizer.Render(map, (ILineString)feature, g);
return RectangleF.Empty;
}
return VectorRenderer.DrawLineStringEx(g, (ILineString)feature, style.Line, map, style.LineOffset);
case OgcGeometryType.MultiLineString:
if (style.LineSymbolizer != null)
{
style.LineSymbolizer.Render(map, (IMultiLineString)feature, g);
return RectangleF.Empty;
}
return VectorRenderer.DrawMultiLineStringEx(g, (IMultiLineString)feature, style.Line, map, style.LineOffset);
case OgcGeometryType.Point:
if (style.PointSymbolizer != null)
return VectorRenderer.DrawPointEx(style.PointSymbolizer, g, (IPoint)feature, map);
if (style.Symbol != null || style.PointColor == null)
return VectorRenderer.DrawPointEx(g, (IPoint)feature, style.Symbol, style.SymbolScale, style.SymbolOffset,
style.SymbolRotation, map);
return VectorRenderer.DrawPointEx(g, (IPoint)feature, style.PointColor, style.PointSize, style.SymbolOffset, map);
case OgcGeometryType.MultiPoint:
if (style.PointSymbolizer != null)
return VectorRenderer.DrawMultiPointEx(style.PointSymbolizer, g, (IMultiPoint)feature, map);
if (style.Symbol != null || style.PointColor == null)
return VectorRenderer.DrawMultiPointEx(g, (IMultiPoint)feature, style.Symbol, style.SymbolScale, style.SymbolOffset, style.SymbolRotation, map);
return VectorRenderer.DrawMultiPointEx(g, (IMultiPoint)feature, style.PointColor, style.PointSize, style.SymbolOffset, map);
case OgcGeometryType.GeometryCollection:
var coll = (IGeometryCollection)feature;
var combinedArea = RectangleF.Empty;
for (var i = 0; i < coll.NumGeometries; i++)
{
IGeometry geom = coll[i];
var canvasArea = RenderGeometryEx(g, map, geom, style);
combinedArea = canvasArea.ExpandToInclude(combinedArea);
}
return combinedArea;
}
throw new NotSupportedException();
}
#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 void ExecuteIntersectionQuery(Envelope box, FeatureDataSet ds)
{
box = ToSource(box);
int tableCount = ds.Tables.Count;
lock (_dataSource)
{
// Is datasource already open?
bool wasOpen = _dataSource.IsOpen;
if (!wasOpen) { _dataSource.Open(); }
_dataSource.ExecuteIntersectionQuery(box, ds);
if (!wasOpen) { DataSource.Close(); }
}
if (ds.Tables.Count > tableCount)
{
//We added a table, name it according to layer
ds.Tables[ds.Tables.Count - 1].TableName = LayerName;
}
}
/// <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 void ExecuteIntersectionQuery(IGeometry geometry, FeatureDataSet ds)
{
geometry = ToSource(geometry);
int tableCount = ds.Tables.Count;
lock (_dataSource)
{
// Is datasource already open?
bool wasOpen = DataSource.IsOpen;
if (!wasOpen) { DataSource.Open(); }
_dataSource.ExecuteIntersectionQuery(geometry, ds);
if (!wasOpen) { DataSource.Close(); }
}
if (ds.Tables.Count > tableCount)
{
//We added a table, name it according to layer
ds.Tables[ds.Tables.Count - 1].TableName = LayerName;
}
}
/// <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 bool IsQueryEnabled
{
get { return _isQueryEnabled; }
set { _isQueryEnabled = value; }
}
#endregion
/// <inheritdoc cref="ICloneable.Clone()"/>>
public object Clone()
{
var res = (VectorLayer)MemberwiseClone();
res.Style = Style.Clone();
if (Theme is ICloneable)
res.Theme = (ITheme)((ICloneable)Theme).Clone();
return res;
}
#region Theme evaluators
private abstract class ThemeEvaluatorBase
{
public abstract IStyle GetStyle(MapViewport mvp, FeatureDataRow feature);
}
private class ThemeEvaluator : ThemeEvaluatorBase
{
private readonly ITheme _theme;
public ThemeEvaluator(ITheme theme)
{
_theme = theme;
}
public sealed override IStyle GetStyle(MapViewport mvp, FeatureDataRow feature)
{
return _theme.GetStyle(feature);
}
}
private class ThemeExEvaluator : ThemeEvaluatorBase
{
private readonly IThemeEx _theme;
public ThemeExEvaluator(IThemeEx theme)
{
_theme = theme;
}
public sealed override IStyle GetStyle(MapViewport mvp, FeatureDataRow feature)
{
return _theme.GetStyle(mvp, feature);
}
}
#endregion
}
}