Skip to content

Commit 35a6de6

Browse files
committed
[Branches/1.0] Added CacheExtent to VectorLayer and LabelLayer
1 parent 5711e07 commit 35a6de6

2 files changed

Lines changed: 60 additions & 27 deletions

File tree

Branches/1.0/SharpMap/Layers/LabelLayer.cs

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public enum MultipartGeometryBehaviourEnum
114114
private GetLabelMethod _getLabelMethod;
115115
private GetPriorityMethod _getPriorityMethod;
116116
private GetLocationMethod _getLocationMethod;
117+
private Envelope _envelope;
117118

118119
/// <summary>
119120
/// Name of the column that holds the value for the label.
@@ -198,7 +199,11 @@ public TextRenderingHint TextRenderingHint
198199
public IProvider DataSource
199200
{
200201
get { return _dataSource; }
201-
set { _dataSource = value; }
202+
set
203+
{
204+
_dataSource = value;
205+
_envelope = null;
206+
}
202207
}
203208

204209
/// <summary>
@@ -334,16 +339,36 @@ public override Envelope Envelope
334339
{
335340
get
336341
{
342+
if (DataSource == null)
343+
throw (new ApplicationException("DataSource property not set on layer '" + LayerName + "'"));
344+
345+
if (_envelope != null && CacheExtent)
346+
return ToTarget(_envelope.Clone());
347+
337348
var wasOpen = DataSource.IsOpen;
338349
if (!wasOpen)
339350
DataSource.Open();
340351
var box = DataSource.GetExtents();
341352
if (!wasOpen) //Restore state
342353
DataSource.Close();
354+
355+
if (CacheExtent)
356+
_envelope = box;
357+
343358
return ToTarget(box);
344359
}
345360
}
346361

362+
/// <summary>
363+
/// Gets or sets a value indicating whether the layer envelope should be treated as static or not.
364+
/// </summary>
365+
/// <remarks>
366+
/// When CacheExtent is enabled the layer Envelope will be calculated only once from DataSource, this
367+
/// helps to speed up the Envelope calculation with some DataProviders. Default is false for backward
368+
/// compatibility.
369+
/// </remarks>
370+
public virtual bool CacheExtent { get; set; }
371+
347372
/// <summary>
348373
/// Gets or sets the SRID of this VectorLayer's data source
349374
/// </summary>
@@ -404,29 +429,19 @@ public override void Render(Graphics g, Map map)
404429
//Initialize label collection
405430
var labels = new List<BaseLabel>();
406431

407-
// Get the compare value for min/max visible. We assume the theme has the same visibility units as the main style
408-
var compareValue = Style.VisibilityUnits == VisibilityUnits.ZoomLevel ? map.Zoom : map.MapScale;
409-
410-
// Set the style to use
411-
var style = Style;
412-
432+
//List<System.Drawing.Rectangle> LabelBoxes; //Used for collision detection
413433
//Render labels
434+
414435
for (int i = 0; i < features.Count; i++)
415436
{
416437
var feature = features[i];
438+
feature.Geometry = ToTarget(feature.Geometry);
417439

418-
// Thematics?
419-
if (Theme != null)
420-
{
421-
//If thematics is enabled, lets override the style
440+
LabelStyle style;
441+
if (Theme != null) //If thematics is enabled, lets override the style
422442
style = Theme.GetStyle(feature) as LabelStyle;
423-
// Check if this style is to be rendered at all
424-
if (style == null) { continue; }
425-
if (!(style.Enabled && style.MaxVisible >= compareValue && Style.MinVisible < compareValue)) continue;
426-
}
427-
428-
// Transform the geometry for the target
429-
feature.Geometry = ToTarget(feature.Geometry);
443+
else
444+
style = Style;
430445

431446
float rotationStyle = style != null ? style.Rotation : 0f;
432447
float rotationColumn = 0f;
@@ -580,7 +595,6 @@ public override void Render(Graphics g, Map map)
580595
lblStyle.GetStringFormat(), lblStyle.IgnoreLength, plbl.Location);
581596
}
582597
}
583-
584598
}
585599
base.Render(g, map);
586600
}
@@ -664,12 +678,9 @@ private static BaseLabel CreateLabel(FeatureDataRow fdr, IGeometry feature, stri
664678
lbl = new Label(text, location, rotation, priority,
665679
new LabelBox(location.X - style.CollisionBuffer.Width,
666680
location.Y - style.CollisionBuffer.Height,
667-
size.Width + 2f * style.CollisionBuffer.Width,
668-
size.Height + 2f * style.CollisionBuffer.Height), style)
669-
{
670-
LabelPoint = position,
671-
Priority = priority,
672-
};
681+
size.Width + 2f*style.CollisionBuffer.Width,
682+
size.Height + 2f*style.CollisionBuffer.Height), style)
683+
{ LabelPoint = position };
673684
}
674685

675686
/*

Branches/1.0/SharpMap/Layers/VectorLayer.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class VectorLayer : Layer, ICanQueryLayer, ICloneable
5050
private IProvider _dataSource;
5151
private SmoothingMode _smoothingMode;
5252
private ITheme _theme;
53+
private Envelope _envelope;
5354

5455
/// <summary>
5556
/// Initializes a new layer
@@ -122,7 +123,11 @@ public SmoothingMode SmoothingMode
122123
public IProvider DataSource
123124
{
124125
get { return _dataSource; }
125-
set { _dataSource = value; }
126+
set
127+
{
128+
_dataSource = value;
129+
_envelope = null;
130+
}
126131
}
127132

128133
/// <summary>
@@ -144,6 +149,10 @@ public override Envelope Envelope
144149
{
145150
if (DataSource == null)
146151
throw (new ApplicationException("DataSource property not set on layer '" + LayerName + "'"));
152+
153+
if (_envelope != null && CacheExtent)
154+
return ToTarget(_envelope.Clone());
155+
147156
Envelope box;
148157
lock (_dataSource)
149158
{
@@ -153,12 +162,25 @@ public override Envelope Envelope
153162
box = DataSource.GetExtents();
154163
if (!wasOpen) //Restore state
155164
DataSource.Close();
165+
166+
if (CacheExtent)
167+
_envelope = box;
156168
}
157169

158170
return ToTarget(box);
159171
}
160172
}
161173

174+
/// <summary>
175+
/// Gets or sets a value indicating whether the layer envelope should be treated as static or not.
176+
/// </summary>
177+
/// <remarks>
178+
/// When CacheExtent is enabled the layer Envelope will be calculated only once from DataSource, this
179+
/// helps to speed up the Envelope calculation with some DataProviders. Default is false for backward
180+
/// compatibility.
181+
/// </remarks>
182+
public virtual bool CacheExtent { get; set; }
183+
162184
/// <summary>
163185
/// Gets or sets the SRID of this VectorLayer's data source
164186
/// </summary>
@@ -415,7 +437,7 @@ protected void RenderInternal(Graphics g, Map map, Envelope envelope)
415437
/// </summary>
416438
/// <param name="style"></param>
417439
/// <returns></returns>
418-
protected static IEnumerable<IStyle> GetStylesToRender(IStyle style)
440+
private static IEnumerable<IStyle> GetStylesToRender(IStyle style)
419441
{
420442
IStyle[] stylesToRender = null;
421443
if (style is GroupStyle)

0 commit comments

Comments
 (0)