-
Notifications
You must be signed in to change notification settings - Fork 5
/
05-SymbolScalingHandler.ashx.cs
116 lines (95 loc) · 5.63 KB
/
05-SymbolScalingHandler.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
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
using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Web;
namespace SpatialTutorial
{
/// <summary>
/// Summary description for DynamicTilesHandler
/// </summary>
public class SymbolScalingHandler : HttpTaskAsyncHandler
{
// http://msdn.microsoft.com/en-us/library/bb259689.aspx
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))
{
// widen stroke width based on zoom
int strokeSize = (z > 15) ? 4 : (z > 10) ? 3 : (z > 5) ? 2 : 1;
// calculate map scale
var mapSize = 256 * Math.Pow(2, z); // size of the map in pixel
double earthCircumfence = 2.0 * Math.PI * 6378137.0; // circumfence of earth
var scale = mapSize / earthCircumfence; // pixel per mercator unit
// Calculate the symbol sizes with 3 different scaling modes
// 1 - constant scaling - radius is always 16 pixels
int sz1 = (int)(16 * Math.Pow(scale, 0.0));
// 2 - linear scaling - the symbol has a size of 10000 merctor units (=meter at equator, equals sz = 10000 * scale)
int sz2 = (int)(10000 * Math.Pow(scale, 1.0));
// 2 - logarithmic scaling - the size is adapted with a base size (64) and a scaling factor (0.25)
int sz3 = (int)(64 * Math.Pow(scale, 0.25));
// the maximum number of pixels a symbol can "bleed" into a neighbouring tile
var bleedingPixels = (Math.Max(Math.Max(sz1, sz2), sz3) + strokeSize) / 2;
var rect = TransformTools.TileToWgs(x, y, z, bleedingPixels);
int left = (int)Math.Floor(rect.Left);
int right = (int)Math.Floor(rect.Right);
int top = (int)Math.Floor(rect.Top);
int bottom = (int)Math.Floor(rect.Bottom);
var pen = new System.Drawing.Pen(System.Drawing.Color.Black, strokeSize);
var format = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
for (int lon = left; lon <= right; lon++)
{
for (int lat = top; lat <= bottom; lat++)
{
var g1 = new System.Windows.Point(lon, lat);
var g2 = new System.Windows.Point(lon + 1, lat + 1);
var p1 = TransformTools.WgsToTile(x, y, z, g1);
var p2 = TransformTools.WgsToTile(x, y, z, g2);
graphics.DrawLine(pen, new System.Drawing.Point((int)p1.X, (int)p1.Y), new System.Drawing.Point((int)p2.X, (int)p1.Y));
graphics.DrawLine(pen, new System.Drawing.Point((int)p1.X, (int)p1.Y), new System.Drawing.Point((int)p1.X, (int)p2.Y));
if (z < 6) // no symbols for levels < 6
continue;
int sz;
Brush brush;
switch ((lat + lon) % 3) // switch between the 3 size modes
{
case 0: sz = sz1; brush = Brushes.LightGreen; break;
case 1: sz = sz2; brush = Brushes.LightYellow; break;
default: sz = sz3; brush = Brushes.LightBlue; break;
}
graphics.FillEllipse(brush, (int)(p1.X + p2.X) / 2 - sz, (int)p1.Y - sz, sz * 2, sz * 2);
graphics.FillEllipse(brush, (int)(p1.X) - sz, (int)(p1.Y + p2.Y) / 2 - sz, sz * 2, sz * 2);
graphics.DrawEllipse(pen, (int)(p1.X + p2.X) / 2 - sz, (int)p1.Y - sz, sz * 2, sz * 2);
graphics.DrawEllipse(pen, (int)(p1.X) - sz, (int)(p1.Y + p2.Y) / 2 - sz, sz * 2, sz * 2);
if (sz > 4)
{
var font = new Font("Arial", sz - 4);
graphics.DrawString(string.Format("{0}°", lat), font, Brushes.Black, (int)(p1.X + p2.X) / 2, (int)p1.Y, format);
graphics.DrawString(string.Format("{0}°", lon), font, Brushes.Black, (int)(p1.X), (int)(p1.Y + p2.Y) / 2, format);
font.Dispose();
}
}
}
//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);
}
}
}
}
}