forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharpMapTileSource.cs
More file actions
155 lines (123 loc) · 5.14 KB
/
SharpMapTileSource.cs
File metadata and controls
155 lines (123 loc) · 5.14 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
namespace WinFormSamples
{
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using BruTile;
using BruTile.Cache;
using GeoAPI.Geometries;
using SharpMap;
using SharpMap.Layers;
internal class SharpMapTileSource : ITileSource
{
private readonly SharpMapTileProvider provider;
public SharpMapTileSource(ITileSchema schema, params ILayer[] layers) : this(schema, null, layers) { }
public SharpMapTileSource(ITileSchema schema, ITileCache<byte[]> cache, params ILayer[] layers)
{
Title = "SharpMap.Map";
if (schema == null)
throw new ArgumentNullException("schema");
if (layers == null)
throw new ArgumentNullException("layers");
this.provider = new SharpMapTileProvider(schema, cache, layers);
}
public string Title { get; set; }
public ITileProvider Provider
{
get { return this.provider; }
}
public ITileSchema Schema
{
get { return this.provider.Schema; }
}
public string Name { get; private set; }
public Extent Extent
{
get { throw new NotImplementedException(); }
}
public Attribution Attribution => throw new NotImplementedException();
public byte[] GetTile(TileInfo tileInfo)
{
return provider.GetTile(tileInfo);
}
}
internal class SharpMapTileProvider : ITileProvider
{
private class NullCache : ITileCache<byte[]>
{
public void Add(TileIndex index, byte[] image) { }
public void Remove(TileIndex index) { throw new NotImplementedException(); }
public byte[] Find(TileIndex index) { return null; }
}
private readonly ITileCache<byte[]> cache;
public ITileSchema Schema { get; private set; }
private readonly LayerCollection layers;
private ImageFormat format;
public SharpMapTileProvider(ITileSchema schema, ITileCache<byte[]> cache, params ILayer[] layers)
{
if (schema == null)
throw new ArgumentNullException("schema");
if (layers == null)
throw new ArgumentNullException("layers");
this.Schema = schema;
this.cache = cache ?? new NullCache();
LayerCollection collection = new LayerCollection();
foreach (ILayer layer in layers)
collection.Add(layer);
this.layers = collection;
}
public byte[] GetTile(TileInfo info)
{
if (info == null)
throw new ArgumentNullException("info");
if (this.format == null)
this.format = GetFormat(this.Schema);
TileIndex index = info.Index;
byte[] bytes = this.cache.Find(index);
if (bytes != null)
return bytes;
using (Image tile = this.CreateTile(info))
using (MemoryStream ms = new MemoryStream())
{
tile.Save(ms, this.format);
bytes = ms.ToArray();
this.cache.Add(index, bytes);
return ms.ToArray();
}
}
private static ImageFormat GetFormat(ITileSchema schema)
{
string format = schema.Format;
if (String.Equals("JPEG", format, StringComparison.InvariantCultureIgnoreCase))
return ImageFormat.Jpeg;
if (String.Equals("JPG", format, StringComparison.InvariantCultureIgnoreCase))
return ImageFormat.Jpeg;
if (String.Equals("GIF", format, StringComparison.InvariantCultureIgnoreCase))
return ImageFormat.Gif;
if (String.Equals("BMP", format, StringComparison.InvariantCultureIgnoreCase))
return ImageFormat.Bmp;
if (String.Equals("TIFF", format, StringComparison.InvariantCultureIgnoreCase))
return ImageFormat.Tiff;
if (String.Equals("TIF", format, StringComparison.InvariantCultureIgnoreCase))
return ImageFormat.Tiff;
return ImageFormat.Png;
}
private readonly object synclock = new object();
private Image CreateTile(TileInfo info)
{
lock (synclock)
{
var tileWidth = this.Schema.GetTileWidth(info.Index.Level);
var tileHeight = this.Schema.GetTileHeight(info.Index.Level);
Size size = new Size(tileWidth, tileHeight);
Map map = new Map(size) { BackColor = Color.Transparent };
map.Layers.AddCollection(this.layers);
Extent ext = info.Extent;
Envelope bbox = new Envelope(ext.MinX, ext.MaxX, ext.MinY, ext.MaxY);
map.ZoomToBox(bbox);
return map.GetMap();
}
}
}
}