-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathTileAsyncLayer.cs
More file actions
410 lines (349 loc) · 17.4 KB
/
TileAsyncLayer.cs
File metadata and controls
410 lines (349 loc) · 17.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Threading;
using BruTile;
using BruTile.Cache;
using System.IO;
using System.Net;
using GeoAPI.Geometries;
using System.Diagnostics;
using System.Threading.Tasks;
using Common.Logging;
namespace SharpMap.Layers
{
/// <summary>
/// Tile layer class that gets and serves tiles asynchronously
/// </summary>
[Serializable]
public class TileAsyncLayer : TileLayer, ITileAsyncLayer
{
static readonly ILog Logger = LogManager.GetLogger(typeof(TileAsyncLayer));
[NonSerialized]
private CancellationTokenSource _cancellationTokenSource;
//[NonSerialized]
//private readonly List<Task> _currentDownloads = new List<Task>();
int _numPendingDownloads;
bool _onlyRedrawWhenComplete = false;
/// <summary>
/// Gets or Sets a value indicating if to redraw the map only when all tiles are downloaded
/// </summary>
public bool OnlyRedrawWhenComplete
{
get { return _onlyRedrawWhenComplete; }
set { _onlyRedrawWhenComplete = value; }
}
/// <summary>
/// Returns the number of tiles that are in queue for download
/// </summary>
public int NumPendingDownloads { get { return _numPendingDownloads; } }
/// <summary>
/// Event raised when tiles are downloaded
/// </summary>
public event DownloadProgressHandler DownloadProgressChanged;
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="tileSource">The tile source</param>
/// <param name="layerName">The layers name</param>
public TileAsyncLayer(ITileSource tileSource, string layerName)
: base(tileSource, layerName, new Color(), true, null)
{
}
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="tileSource">The tile source</param>
/// <param name="layerName">The layers name</param>
/// <param name="transparentColor">The color that should be treated as <see cref="Color.Transparent"/></param>
/// <param name="showErrorInTile">Value indicating that an error tile should be generated for non-existent tiles</param>
public TileAsyncLayer(ITileSource tileSource, string layerName, Color transparentColor, bool showErrorInTile)
: base(tileSource, layerName, transparentColor, showErrorInTile, null)
{
}
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="tileSource">The tile source</param>
/// <param name="layerName">The layers name</param>
/// <param name="transparentColor">The color that should be treated as <see cref="Color.Transparent"/></param>
/// <param name="showErrorInTile">Value indicating that an error tile should be generated for non-existent tiles</param>
/// <param name="fileCacheDir">The directories where tiles should be stored</param>
public TileAsyncLayer(ITileSource tileSource, string layerName, Color transparentColor, bool showErrorInTile, string fileCacheDir)
: base(tileSource, layerName, transparentColor, showErrorInTile, fileCacheDir)
{
}
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="tileSource">The tile source</param>
/// <param name="layerName">The layers name</param>
/// <param name="transparentColor">The color that should be treated as <see cref="Color.Transparent"/></param>
/// <param name="showErrorInTile">Value indicating that an error tile should be generated for non-existent tiles</param>
/// <param name="fileCache">If the layer should use a file-cache so store tiles, set this to a fileCacheProvider. Set to null to avoid filecache</param>
/// <param name="imgFormat">Set the format of the tiles to be used</param>
public TileAsyncLayer(ITileSource tileSource, string layerName, Color transparentColor, bool showErrorInTile, FileCache fileCache, ImageFormat imgFormat)
: base(tileSource, layerName, transparentColor, showErrorInTile, fileCache,imgFormat)
{
}
/// <summary>
/// EventHandler for event fired when a new Tile is available for rendering
/// </summary>
public event MapNewTileAvaliabledHandler MapNewTileAvaliable;
/// <summary>
/// Method to cancel the async layer
/// </summary>
public void Cancel()
{
//lock (_currentDownloads)
//{
var cts = _cancellationTokenSource;
cts?.Cancel();
//_currentDownloads.Clear();
_numPendingDownloads = 0;
DownloadProgressChanged?.Invoke(_numPendingDownloads);
//}
}
/// <summary>
/// Renders the layer
/// </summary>
/// <param name="graphics">Graphics object reference</param>
/// <param name="map">Map which is rendered</param>
public override void Render(Graphics graphics, MapViewport map)
{
var bbox = map.Envelope;
var extent = new Extent(bbox.MinX, bbox.MinY, bbox.MaxX, bbox.MaxY);
string level = BruTile.Utilities.GetNearestLevel(_source.Schema.Resolutions, Math.Max(map.PixelWidth, map.PixelHeight));
var tiles = new List<TileInfo>(_source.Schema.GetTileInfos(extent, level));
tiles.Sort(new DistanceComparison(map.CenterOfInterest));
//Abort previous running Threads
Cancel();
_cancellationTokenSource = new CancellationTokenSource();
var token = _cancellationTokenSource.Token;
var taskFactory = new TaskFactory(token);
using (var ia = new ImageAttributes())
{
if (!_transparentColor.IsEmpty)
ia.SetColorKey(_transparentColor, _transparentColor);
#if !PocketPC
ia.SetWrapMode(WrapMode.TileFlipXY);
#endif
int tileWidth = _source.Schema.GetTileWidth(level);
int tileHeight = _source.Schema.GetTileHeight(level);
foreach (var info in tiles)
{
var bitmap = _bitmaps.Find(info.Index);
if (bitmap != null)
{
//draws directly the bitmap
var bb = new Envelope(new Coordinate(info.Extent.MinX, info.Extent.MinY),
new Coordinate(info.Extent.MaxX, info.Extent.MaxY));
HandleMapNewTileAvailable(map, graphics, bb, bitmap, tileWidth, tileHeight, ia);
}
else if (_fileCache?.Exists(info.Index) ?? false)
{
var img = GetImageFromFileCache(info) as Bitmap;
_bitmaps.Add(info.Index, img);
//draws directly the bitmap
var btExtent = info.Extent;
var bb = new Envelope(new Coordinate(btExtent.MinX, btExtent.MinY),
new Coordinate(btExtent.MaxX, btExtent.MaxY));
HandleMapNewTileAvailable(map, graphics, bb, img, tileWidth, tileHeight, ia);
}
else
{
var localInfo = info;
if (Logger.IsDebugEnabled)
Logger.DebugFormat("Starting new Task to download tile {0},{1},{2}", info.Index.Level, info.Index.Col, info.Index.Row);
// Increase num pending downloads
Interlocked.Increment(ref _numPendingDownloads);
DownloadProgressChanged?.Invoke(_numPendingDownloads);
taskFactory.StartNew(() =>
{
if (token.IsCancellationRequested)
{
Debug.Print($"Cancelling {Task.CurrentId}");
token.ThrowIfCancellationRequested();
}
if (Logger.IsDebugEnabled)
Logger.DebugFormat("Task started for download of tile {0},{1},{2}", info.Index.Level, info.Index.Col, info.Index.Row);
bool res = GetTileOnThread(token, _source, localInfo, _bitmaps, true);
if (res)
{
Interlocked.Decrement(ref _numPendingDownloads);
DownloadProgressChanged?.Invoke(_numPendingDownloads);
}
},
_cancellationTokenSource.Token);
//var t = new System.Threading.Tasks.Task(delegate
//{
// if (token.IsCancellationRequested)
// token.ThrowIfCancellationRequested();
// if (Logger.IsDebugEnabled)
// Logger.DebugFormat("Task started for download of tile {0},{1},{2}", info.Index.Level, info.Index.Col, info.Index.Row);
// var res = GetTileOnThread(token, _source, l_info, _bitmaps, true);
// if (res)
// {
// Interlocked.Decrement(ref _numPendingDownloads);
// var e = DownloadProgressChanged;
// if (e != null)
// e(_numPendingDownloads);
// }
//}, token);
//var dt = new DownloadTask() { CancellationToken = cancelToken, Task = t };
//lock (_currentTasks)
//{
// _currentTasks.Add(dt);
// _numPendingDownloads++;
//}
//t.Start();
}
}
}
}
private class DistanceComparison : IComparer<TileInfo>
{
private readonly Coordinate _mapCenter;
public DistanceComparison(Coordinate mapCenter)
{
_mapCenter = mapCenter;
}
public int Compare(TileInfo x, TileInfo y)
{
double distanceX = _mapCenter.Distance(new Coordinate(x.Extent.CenterX, x.Extent.CenterY));
double distanceY = _mapCenter.Distance(new Coordinate(y.Extent.CenterX, y.Extent.CenterY));
return distanceX.CompareTo(distanceY);
}
}
static void HandleMapNewTileAvailable(MapViewport map, Graphics g, Envelope box, Image bm, int sourceWidth, int sourceHeight, ImageAttributes imageAttributes)
{
try
{
var min = map.WorldToImage(box.Min());
var max = map.WorldToImage(box.Max());
min = new PointF((float)Math.Round(min.X), (float)Math.Round(min.Y));
max = new PointF((float)Math.Round(max.X), (float)Math.Round(max.Y));
using (var ia = (ImageAttributes) imageAttributes.Clone())
{
g.DrawImage(bm,
new Rectangle((int) min.X, (int) max.Y, (int) (max.X - min.X), (int) (min.Y - max.Y)),
0, 0,
sourceWidth, sourceHeight,
GraphicsUnit.Pixel,
ia);
}
// g.Dispose();
}
catch (Exception ex)
{
Logger.Warn(ex.Message, ex);
//this can be a GDI+ Hell Exception...
}
}
/// <summary>
///
/// </summary>
/// <param name="cancelToken"></param>
/// <param name="tileProvider"></param>
/// <param name="tileInfo"></param>
/// <param name="bitmaps"></param>
/// <param name="retry"></param>
/// <returns><c>true</c> if task finished without getting a cancellation signal, otherwise <c>false</c></returns>
private bool GetTileOnThread(CancellationToken cancelToken, ITileProvider tileProvider, TileInfo tileInfo, ITileCache<Bitmap> bitmaps, bool retry)
{
try
{
if (cancelToken.IsCancellationRequested)
cancelToken.ThrowIfCancellationRequested();
//We may have gotten the tile from another thread now.
if (bitmaps.Find(tileInfo.Index) != null)
return true;
if (Logger.IsDebugEnabled)
Logger.DebugFormat("Calling ITileProvider.GetTile for tile {0},{1},{2}", tileInfo.Index.Level, tileInfo.Index.Col, tileInfo.Index.Row);
byte[] bytes = tileProvider.GetTile(tileInfo);
if (bytes == null)
return true;
if (cancelToken.IsCancellationRequested)
cancelToken.ThrowIfCancellationRequested();
using (var ms = new MemoryStream(bytes))
{
var bitmap = new Bitmap(ms);
bitmaps.Add(tileInfo.Index, bitmap);
if (_fileCache != null && !_fileCache.Exists(tileInfo.Index))
{
AddImageToFileCache(tileInfo, bitmap);
}
if (cancelToken.IsCancellationRequested)
cancelToken.ThrowIfCancellationRequested();
OnMapNewTileAvailable(tileInfo, bitmap);
}
return true;
}
catch (WebException ex)
{
if (Logger.IsDebugEnabled)
Logger.DebugFormat("Exception downloading tile {0},{1},{2} {3}", tileInfo.Index.Level, tileInfo.Index.Col, tileInfo.Index.Row, ex.Message);
if (retry)
{
return GetTileOnThread(cancelToken, tileProvider, tileInfo, bitmaps, false);
}
if (!_showErrorInTile)
{
var index = tileInfo.Index;
Logger.Debug(fmh => fmh("Failed to get tile ({0},{1},{2}), returning true anyway", index.Level, index.Col, index.Row));
return true;
}
// create the timeout tile
int tileWidth = _source.Schema.GetTileWidth(tileInfo.Index.Level);
int tileHeight = _source.Schema.GetTileHeight(tileInfo.Index.Level);
var bitmap = new Bitmap(tileWidth, tileHeight);
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.DrawString(ex.Message, new Font(FontFamily.GenericSansSerif, 12), new SolidBrush(Color.Black),
new RectangleF(0, 0, tileWidth, tileHeight));
}
//Draw the Timeout Tile
OnMapNewTileAvailable(tileInfo, bitmap);
return true;
}
catch (OperationCanceledException tex)
{
if (Logger.IsInfoEnabled)
{
Logger.InfoFormat("TileAsyncLayer - Thread aborting: {0}", Thread.CurrentThread.Name);
Logger.InfoFormat(tex.Message);
}
return false;
}
catch (Exception ex)
{
Logger.Warn("TileAsyncLayer - GetTileOnThread Exception", ex);
//This is not due to cancellation so return true
return true;
}
}
private void OnMapNewTileAvailable(TileInfo tileInfo, Bitmap bitmap)
{
if (_onlyRedrawWhenComplete)
return;
var mapNewTileAvailable = MapNewTileAvaliable;
if (mapNewTileAvailable != null)
{
var bb = new Envelope(new Coordinate(tileInfo.Extent.MinX, tileInfo.Extent.MinY), new Coordinate(tileInfo.Extent.MaxX, tileInfo.Extent.MaxY));
using (var ia = new ImageAttributes())
{
if (!_transparentColor.IsEmpty)
ia.SetColorKey(_transparentColor, _transparentColor);
#if !PocketPC
ia.SetWrapMode(WrapMode.TileFlipXY);
#endif
int tileWidth = _source.Schema.GetTileWidth(tileInfo.Index.Level);
int tileHeight = _source.Schema.GetTileHeight(tileInfo.Index.Level);
mapNewTileAvailable(this, bb, bitmap, tileWidth, tileHeight, ia);
}
}
}
}
}