-
Notifications
You must be signed in to change notification settings - Fork 5
/
06-SpatialLiteTilesHandler.ashx.cs
82 lines (71 loc) · 3.24 KB
/
06-SpatialLiteTilesHandler.ashx.cs
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
using System;
using System.Drawing;
using System.Web;
using System.Data.SQLite;
using System.Threading.Tasks;
namespace SpatialTutorial
{
/// <summary>
/// Summary description for DynamicTilesHandler
/// </summary>
public class SpatialLiteTilesHandler : HttpTaskAsyncHandler
{
// going async here to improve scalability
public override async Task ProcessRequestAsync(HttpContext context)
{
//Parse request parameters
if (!uint.TryParse(context.Request.Params["x"], out uint x))
throw (new ArgumentException("Invalid parameter"));
if (!uint.TryParse(context.Request.Params["y"], out uint y))
throw (new ArgumentException("Invalid parameter"));
if (!uint.TryParse(context.Request.Params["z"], out uint z))
throw (new ArgumentException("Invalid parameter"));
// Create a bitmap of size 256x256
using (var bmp = new Bitmap(256, 256))
// get graphics from bitmap
using (var graphics = Graphics.FromImage(bmp))
{
// calc rect from tile key
var qw = TransformTools.TileToWgs(x, y, z);
// build the sql
var query = FormattableString.Invariant($@"
SELECT Id, AsBinary(Geometry) FROM WorldGeom
WHERE ROWID IN
(Select rowid FROM cache_WorldGeom_Geometry
WHERE mbr = FilterMbrIntersects({qw.Left}, {qw.Bottom}, {qw.Right}, {qw.Top}))
");
using (var command = new SQLiteCommand(query, Global.cn))
using (var reader = await command.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
int id = reader.GetInt32(0);
byte[] wkb = reader[1] as byte[];
// create GDI path from wkb
var path = WkbToGdi.Parse(wkb, p => TransformTools.WgsToTile(x, y, z, p));
// degenerated polygon
if (path == null)
continue;
// fill polygon
var fill = new SolidBrush(Color.FromArgb(168, 0, 0, 255));
graphics.FillPath(fill, path);
fill.Dispose();
// draw outline
graphics.DrawPath(Pens.Black, path);
}
reader.Close();
}
//Stream the image to the client
using (var memoryStream = new System.IO.MemoryStream())
{
// Saving a PNG image requires a seekable stream, first save to memory stream
// http://forums.asp.net/p/975883/3646110.aspx#1291641
bmp.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
var buffer = memoryStream.ToArray();
context.Response.ContentType = "image/png";
await context.Response.OutputStream.WriteAsync(buffer, 0, buffer.Length);
}
}
}
}
}