forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncLayerProxyLayer.cs
More file actions
424 lines (373 loc) · 15.1 KB
/
AsyncLayerProxyLayer.cs
File metadata and controls
424 lines (373 loc) · 15.1 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
// Copyright 2014 - Felix Obermaier (www.ivv-aachen.de)
//
// 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.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.Serialization;
using GeoAPI.Geometries;
using SharpMap.Data;
using SharpMap.Styles;
namespace SharpMap.Layers
{
/// <summary>
/// A proxy class to allow async tile rendering for static layers
/// </summary>
/// <example lang="C#">
/// var map = new SharpMap.Map(new System.Drawing.Size(1024, 786));
/// var provider = new SharpMap.Data.Providers.Shapefile("<Path to shapefile>", true);
/// var layer = new SharpMap.Layers.VectorLayer("LAYER1", provider);
/// map.BackgroundLayer.Add(AsyncLayerProxyLayer.Create(layer));
/// </example>
[Serializable]
public class AsyncLayerProxyLayer : ITileAsyncLayer, IDisposable, ICanQueryLayer
{
private readonly ILayer _baseLayer;
private bool _onlyRedrawWhenComplete;
[NonSerialized]
private int _numPendingDownloads;
private readonly Size _cellSize;
private readonly Size _cellBuffer = new Size(5, 5);
private ImageAttributes _imageAttributes = new ImageAttributes();
[NonSerialized]
private object _renderLock = new object();
[NonSerialized]
private Bitmap _bitmap;
[NonSerialized]
private Envelope _lastViewport = new Envelope();
private class RenderTask
{
/// <summary>
/// The token to cancel the task
/// </summary>
internal System.Threading.CancellationTokenSource CancellationToken;
/// <summary>
/// The task
/// </summary>
internal System.Threading.Tasks.Task Task;
}
[NonSerialized]
private List<RenderTask> _currentTasks = new List<RenderTask>();
/// <summary>
/// Method to warp a usual layer in an async layer
/// </summary>
/// <param name="layer">The layer to wrap</param>
/// <param name="tileSize">The size of the tile</param>
/// <returns>A async tile layer</returns>
public static ILayer Create(ILayer layer, Size? tileSize = null)
{
if (layer == null)
throw new ArgumentNullException("layer");
if (layer is ITileAsyncLayer)
return layer;
tileSize = tileSize ?? new Size(256, 256);
return new AsyncLayerProxyLayer(layer, tileSize.Value);
}
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="baseLayer">The layer to proxy</param>
/// <param name="cellSize">The size of the tile</param>
private AsyncLayerProxyLayer(ILayer baseLayer, Size cellSize)
{
_baseLayer = baseLayer;
_cellSize = cellSize;
((ILayer)this).VisibilityUnits = baseLayer.VisibilityUnits;
}
double ILayer.MinVisible
{
get { return _baseLayer.MinVisible; }
set { _baseLayer.MinVisible = value; }
}
double ILayer.MaxVisible
{
get { return _baseLayer.MaxVisible; }
set { _baseLayer.MaxVisible = value; }
}
/// <summary>
/// Gets or Sets what level-reference the Min/Max values are defined in
/// </summary>
VisibilityUnits ILayer.VisibilityUnits { get; set; }
/// <summary>
/// Specifies whether this layer should be rendered or not
/// </summary>
bool ILayer.Enabled
{
get { return _baseLayer.Enabled; }
set { _baseLayer.Enabled = value; }
}
/// <summary>
/// Optional title of the layer. It will be used for services like WMS where both Name and Title are supported.
/// </summary>
public string LayerTitle
{
get { return _baseLayer.LayerTitle; }
set { _baseLayer.LayerTitle = value; }
}
/// <summary>
/// Name of layer
/// </summary>
string ILayer.LayerName
{
get { return _baseLayer.LayerName; }
set { _baseLayer.LayerName = value; }
}
/// <summary>
/// Gets the boundingbox of the entire layer
/// </summary>
Envelope ILayer.Envelope
{
get { return _baseLayer.Envelope; }
}
/// <summary>
/// The spatial reference ID (CRS)
/// </summary>
int ILayer.SRID
{
get { return _baseLayer.SRID; }
set { _baseLayer.SRID = value; }
}
/// <summary>
/// The spatial reference ID (CRS) that can be exposed externally.
/// </summary>
/// <remarks>
/// TODO: explain better why I need this property
/// </remarks>
int ILayer.TargetSRID
{
get { return _baseLayer.TargetSRID; }
}
/// <summary>
/// Proj4 String Projection
/// </summary>
string ILayer.Proj4Projection
{
get { return _baseLayer.Proj4Projection; }
set { _baseLayer.Proj4Projection = value; }
}
/// <summary>
/// Renders the layer
/// </summary>
/// <param name="g">Graphics object reference</param>
/// <param name="map">Map which is rendered</param>
void ILayer.Render(Graphics g, Map map)
{
((ILayer)this).Render(g, (MapViewport)map);
}
void ILayer.Render(Graphics g, MapViewport map)
{
// We don't need to regenerate the tiles
if (map.Envelope.Equals(_lastViewport) && _numPendingDownloads == 0)
{
g.DrawImage(_bitmap, Point.Empty);
return;
}
// Create a backbuffer
lock (_renderLock)
{
if (_bitmap == null || _bitmap.Size != map.Size)
{
_bitmap = new Bitmap(map.Size.Width, map.Size.Height, PixelFormat.Format32bppArgb);
using (var tmpGraphics = Graphics.FromImage(_bitmap))
tmpGraphics.Clear(Color.Transparent);
}
}
// Save the last viewport
_lastViewport = map.Envelope;
// Cancel old rendercycle
((ITileAsyncLayer)this).Cancel();
var mapViewport = map.Envelope;
var mapSize = map.Size;
var mapColumnWidth = _cellSize.Width+_cellBuffer.Width;
var mapColumnHeight = _cellSize.Height + _cellBuffer.Width;
var columns = (int)Math.Ceiling((double) mapSize.Width/mapColumnWidth);
var rows = (int) Math.Ceiling((double) mapSize.Height/mapColumnHeight);
var renderMapSize = new Size(columns * _cellSize.Width + _cellBuffer.Width,
rows * _cellSize.Height + _cellBuffer.Height);
var horizontalFactor = (double) renderMapSize.Width/mapSize.Width;
var verticalFactor = (double) renderMapSize.Height/mapSize.Height;
var diffX = 0.5d*(horizontalFactor*mapViewport.Width - mapViewport.Width);
var diffY = 0.5d*(verticalFactor*mapViewport.Height-mapViewport.Height);
var totalRenderMapViewport = mapViewport.Grow(diffX, diffY);
var columnWidth = totalRenderMapViewport.Width/columns;
var rowHeight = totalRenderMapViewport.Height/rows;
var rmdx = (int)((mapSize.Width-renderMapSize.Width) * 0.5f);
var rmdy = (int)((mapSize.Height - renderMapSize.Height) * 0.5f);
var tileSize = Size.Add(_cellSize, Size.Add(_cellBuffer, _cellBuffer));
var miny = totalRenderMapViewport.MinY;
var pty = rmdy + renderMapSize.Height - tileSize.Height;
for (var i = 0; i < rows; i ++)
{
var minx = totalRenderMapViewport.MinX;
var ptx = rmdx;
for (var j = 0; j < columns; j++)
{
var tmpMap = new Map(_cellSize);
tmpMap.Layers.Add(_baseLayer);
tmpMap.DisposeLayersOnDispose = false;
tmpMap.ZoomToBox(new Envelope(minx, minx + columnWidth, miny, miny + rowHeight));
var cancelToken = new System.Threading.CancellationTokenSource();
var token = cancelToken.Token;
var pt = new Point(ptx, pty);
var t = new System.Threading.Tasks.Task(delegate
{
if (token.IsCancellationRequested)
token.ThrowIfCancellationRequested();
var res = RenderCellOnThread(token, pt, tmpMap);
if (res)
{
System.Threading.Interlocked.Decrement(ref _numPendingDownloads);
var e = DownloadProgressChanged;
if (e != null)
e(_numPendingDownloads);
}
}, token);
var dt = new RenderTask {CancellationToken = cancelToken, Task = t};
lock (_currentTasks)
{
_currentTasks.Add(dt);
_numPendingDownloads++;
}
t.Start();
minx += columnWidth;
ptx += _cellSize.Width;
}
miny += rowHeight;
pty -= _cellSize.Height;
}
}
/// <summary>
/// Event raised when a new tile has been rendered an is now avalable
/// </summary>
public event MapNewTileAvaliabledHandler MapNewTileAvaliable;
/// <summary>
/// Event raised when the rendering of tiles has made progress
/// </summary>
public event DownloadProgressHandler DownloadProgressChanged;
bool ITileAsyncLayer.OnlyRedrawWhenComplete
{
get { return _onlyRedrawWhenComplete; }
set { _onlyRedrawWhenComplete = value; }
}
void ITileAsyncLayer.Cancel()
{
lock (_currentTasks)
{
foreach (var t in _currentTasks)
{
if (!t.Task.IsCompleted)
t.CancellationToken.Cancel();
}
_currentTasks.Clear();
_numPendingDownloads = 0;
}
}
int ITileAsyncLayer.NumPendingDownloads
{
get { return _numPendingDownloads; }
}
private bool RenderCellOnThread(System.Threading.CancellationToken token, Point ptInsert, Map map)
{
var tile = new Bitmap(map.Size.Width, map.Size.Height, PixelFormat.Format32bppArgb);
var mvp = new MapViewport(map);
using (var g = Graphics.FromImage(tile))
{
g.Clear(Color.Transparent);
_baseLayer.Render(g, mvp);
map.Layers.Clear();
}
if (!token.IsCancellationRequested)
{
OnTileRendered(ptInsert, map.Envelope, tile);
return true;
}
return false;
}
/// <summary>
/// Method to raise the map tile available event
/// </summary>
/// <param name="ptInsert"></param>
/// <param name="env"></param>
/// <param name="bmp"></param>
protected virtual void OnTileRendered(Point ptInsert, Envelope env, Bitmap bmp)
{
if (!env.Equals(_lastViewport))
{
System.Threading.Monitor.Enter(_renderLock);
using (var g = Graphics.FromImage(_bitmap))
g.DrawImageUnscaled(bmp, ptInsert);
System.Threading.Monitor.Exit(_renderLock);
}
var h1 = MapNewTileAvaliable;
if (h1 != null)
{
h1(null, env, bmp, bmp.Width, bmp.Height, _imageAttributes);
}
var h2 = DownloadProgressChanged;
if (h2 != null)
{
System.Threading.Interlocked.Decrement(ref _numPendingDownloads);
h2(_numPendingDownloads);
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <filterpriority>2</filterpriority>
public void Dispose()
{
((ITileAsyncLayer)this).Cancel();
_imageAttributes.Dispose();
}
void ICanQueryLayer.ExecuteIntersectionQuery(Envelope box, FeatureDataSet ds)
{
if (!((ICanQueryLayer)this).IsQueryEnabled)
return;
((ICanQueryLayer)_baseLayer).ExecuteIntersectionQuery(box, ds);
}
void ICanQueryLayer.ExecuteIntersectionQuery(IGeometry geometry, FeatureDataSet ds)
{
if (!((ICanQueryLayer)this).IsQueryEnabled)
return;
((ICanQueryLayer)_baseLayer).ExecuteIntersectionQuery(geometry, ds);
}
bool ICanQueryLayer.IsQueryEnabled
{
get
{
var cql = _baseLayer as ICanQueryLayer;
if (cql != null && cql.IsQueryEnabled)
return true;
return false;
}
set
{
var cql = _baseLayer as ICanQueryLayer;
if (cql != null )
cql.IsQueryEnabled = value;
}
}
[OnDeserialized]
private void OnDeserialized(StreamingContext context)
{
_imageAttributes = new ImageAttributes();
_currentTasks = new List<RenderTask>();
_renderLock = new object();
_lastViewport = new Envelope();
}
}
}