-
Notifications
You must be signed in to change notification settings - Fork 409
/
StaticImage.cs
467 lines (401 loc) · 18.6 KB
/
StaticImage.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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using GMap.NET;
using GMap.NET.WindowsForms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using GMap.NET.MapProviders;
using System.IO.Compression;
using System.Text;
using System.Globalization;
namespace Demo.WindowsForms
{
public partial class StaticImage : Form
{
MainForm Main;
BackgroundWorker bg = new BackgroundWorker();
readonly List<GPoint> tileArea = new List<GPoint>();
public StaticImage(MainForm main)
{
InitializeComponent();
Main = main;
numericUpDown1.Maximum = Main.MainMap.MaxZoom;
numericUpDown1.Minimum = Main.MainMap.MinZoom;
numericUpDown1.Value = new decimal(Main.MainMap.Zoom);
bg.WorkerReportsProgress = true;
bg.WorkerSupportsCancellation = true;
bg.DoWork += new DoWorkEventHandler(bg_DoWork);
bg.ProgressChanged += new ProgressChangedEventHandler(bg_ProgressChanged);
bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
}
void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if(!e.Cancelled)
{
if(e.Error != null)
{
MessageBox.Show("Error:" + e.Error.ToString(), "GMap.NET", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if(e.Result != null)
{
try
{
Process.Start(e.Result as string);
}
catch
{
}
}
}
this.Text = "Static Map maker";
progressBar1.Value = 0;
button1.Enabled = true;
numericUpDown1.Enabled = true;
Main.MainMap.Refresh();
}
void bg_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
GPoint p = (GPoint)e.UserState;
this.Text = "Static Map maker: Downloading[" + p + "]: " + tileArea.IndexOf(p) + " of " + tileArea.Count;
}
void bg_DoWork(object sender, DoWorkEventArgs e)
{
MapInfo info = (MapInfo)e.Argument;
if(!info.Area.IsEmpty)
{
string bigImage = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + Path.DirectorySeparatorChar + "GMap at zoom " + info.Zoom + " - " + info.Type + "-" + DateTime.Now.Ticks + ".jpg";
e.Result = bigImage;
// current area
GPoint topLeftPx = info.Type.Projection.FromLatLngToPixel(info.Area.LocationTopLeft, info.Zoom);
GPoint rightButtomPx = info.Type.Projection.FromLatLngToPixel(info.Area.Bottom, info.Area.Right, info.Zoom);
GPoint pxDelta = new GPoint(rightButtomPx.X - topLeftPx.X, rightButtomPx.Y - topLeftPx.Y);
GMap.NET.GSize maxOfTiles = info.Type.Projection.GetTileMatrixMaxXY(info.Zoom);
int padding = info.MakeWorldFile || info.MakeKmz ? 0 : 22;
{
using(Bitmap bmpDestination = new Bitmap((int)(pxDelta.X + padding * 2), (int)(pxDelta.Y + padding * 2)))
{
using(Graphics gfx = Graphics.FromImage(bmpDestination))
{
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.SmoothingMode = SmoothingMode.HighQuality;
int i = 0;
// get tiles & combine into one
lock(tileArea)
{
foreach(var p in tileArea)
{
if(bg.CancellationPending)
{
e.Cancel = true;
return;
}
int pc = (int)(((double)++i / tileArea.Count) * 100);
bg.ReportProgress(pc, p);
foreach(var tp in info.Type.Overlays)
{
Exception ex;
GMapImage tile;
// tile number inversion(BottomLeft -> TopLeft) for pergo maps
if(tp.InvertedAxisY)
{
tile = GMaps.Instance.GetImageFrom(tp, new GPoint(p.X, maxOfTiles.Height - p.Y), info.Zoom, out ex) as GMapImage;
}
else // ok
{
tile = GMaps.Instance.GetImageFrom(tp, p, info.Zoom, out ex) as GMapImage;
}
if(tile != null)
{
using(tile)
{
long x = p.X * info.Type.Projection.TileSize.Width - topLeftPx.X + padding;
long y = p.Y * info.Type.Projection.TileSize.Width - topLeftPx.Y + padding;
{
gfx.DrawImage(tile.Img, x, y, info.Type.Projection.TileSize.Width, info.Type.Projection.TileSize.Height);
}
}
}
}
}
}
// draw routes
{
foreach(GMapRoute r in Main.routes.Routes)
{
if(r.IsVisible)
{
using(GraphicsPath rp = new GraphicsPath())
{
for(int j = 0; j < r.Points.Count; j++)
{
var pr = r.Points[j];
GPoint px = info.Type.Projection.FromLatLngToPixel(pr.Lat, pr.Lng, info.Zoom);
px.Offset(padding, padding);
px.Offset(-topLeftPx.X, -topLeftPx.Y);
GPoint p2 = px;
if(j == 0)
{
rp.AddLine(p2.X, p2.Y, p2.X, p2.Y);
}
else
{
System.Drawing.PointF p = rp.GetLastPoint();
rp.AddLine(p.X, p.Y, p2.X, p2.Y);
}
}
if(rp.PointCount > 0)
{
gfx.DrawPath(r.Stroke, rp);
}
}
}
}
}
// draw polygons
{
foreach(GMapPolygon r in Main.polygons.Polygons)
{
if(r.IsVisible)
{
using(GraphicsPath rp = new GraphicsPath())
{
for(int j = 0; j < r.Points.Count; j++)
{
var pr = r.Points[j];
GPoint px = info.Type.Projection.FromLatLngToPixel(pr.Lat, pr.Lng, info.Zoom);
px.Offset(padding, padding);
px.Offset(-topLeftPx.X, -topLeftPx.Y);
GPoint p2 = px;
if(j == 0)
{
rp.AddLine(p2.X, p2.Y, p2.X, p2.Y);
}
else
{
System.Drawing.PointF p = rp.GetLastPoint();
rp.AddLine(p.X, p.Y, p2.X, p2.Y);
}
}
if(rp.PointCount > 0)
{
rp.CloseFigure();
gfx.FillPath(r.Fill, rp);
gfx.DrawPath(r.Stroke, rp);
}
}
}
}
}
// draw markers
{
foreach(GMapMarker r in Main.objects.Markers)
{
if(r.IsVisible)
{
var pr = r.Position;
GPoint px = info.Type.Projection.FromLatLngToPixel(pr.Lat, pr.Lng, info.Zoom);
px.Offset(padding, padding);
px.Offset(-topLeftPx.X, -topLeftPx.Y);
px.Offset(r.Offset.X, r.Offset.Y);
gfx.ResetTransform();
gfx.TranslateTransform(-r.LocalPosition.X, -r.LocalPosition.Y);
gfx.TranslateTransform((int)px.X, (int)px.Y);
r.OnRender(gfx);
}
}
// tooltips above
foreach(GMapMarker m in Main.objects.Markers)
{
if(m.IsVisible && m.ToolTip != null && m.IsVisible)
{
if(!string.IsNullOrEmpty(m.ToolTipText))
{
var pr = m.Position;
GPoint px = info.Type.Projection.FromLatLngToPixel(pr.Lat, pr.Lng, info.Zoom);
px.Offset(padding, padding);
px.Offset(-topLeftPx.X, -topLeftPx.Y);
px.Offset(m.Offset.X, m.Offset.Y);
gfx.ResetTransform();
gfx.TranslateTransform(-m.LocalPosition.X, -m.LocalPosition.Y);
gfx.TranslateTransform((int)px.X, (int)px.Y);
m.ToolTip.OnRender(gfx);
}
}
}
gfx.ResetTransform();
}
// draw info
if(!info.MakeWorldFile)
{
System.Drawing.Rectangle rect = new System.Drawing.Rectangle();
{
rect.Location = new System.Drawing.Point(padding, padding);
rect.Size = new System.Drawing.Size((int)pxDelta.X, (int)pxDelta.Y);
}
using(Font f = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Bold))
{
// draw bounds & coordinates
using(Pen p = new Pen(Brushes.DimGray, 3))
{
p.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
gfx.DrawRectangle(p, rect);
string topleft = info.Area.LocationTopLeft.ToString();
SizeF s = gfx.MeasureString(topleft, f);
gfx.DrawString(topleft, f, p.Brush, rect.X + s.Height / 2, rect.Y + s.Height / 2);
string rightBottom = new PointLatLng(info.Area.Bottom, info.Area.Right).ToString();
SizeF s2 = gfx.MeasureString(rightBottom, f);
gfx.DrawString(rightBottom, f, p.Brush, rect.Right - s2.Width - s2.Height / 2, rect.Bottom - s2.Height - s2.Height / 2);
}
// draw scale
using(Pen p = new Pen(Brushes.Blue, 1))
{
double rez = info.Type.Projection.GetGroundResolution(info.Zoom, info.Area.Bottom);
int px100 = (int)(100.0 / rez); // 100 meters
int px1000 = (int)(1000.0 / rez); // 1km
gfx.DrawRectangle(p, rect.X + 10, rect.Bottom - 20, px1000, 10);
gfx.DrawRectangle(p, rect.X + 10, rect.Bottom - 20, px100, 10);
string leftBottom = "scale: 100m | 1Km";
SizeF s = gfx.MeasureString(leftBottom, f);
gfx.DrawString(leftBottom, f, p.Brush, rect.X + 10, rect.Bottom - s.Height - 20);
}
}
}
}
bmpDestination.Save(bigImage, ImageFormat.Jpeg);
}
}
//The worldfile for the original image is:
//0.000067897543 // the horizontal size of a pixel in coordinate units (longitude degrees in this case);
//0.0000000
//0.0000000
//-0.0000554613012 // the comparable vertical pixel size in latitude degrees, negative because latitude decreases as you go from top to bottom in the image.
//-111.743323868834 // longitude of the pixel in the upper-left-hand corner.
//35.1254392635083 // latitude of the pixel in the upper-left-hand corner.
// generate world file
if(info.MakeWorldFile)
{
string wf = bigImage + "w";
using(StreamWriter world = File.CreateText(wf))
{
world.WriteLine("{0:0.000000000000}", (info.Area.WidthLng / pxDelta.X));
world.WriteLine("0.0000000");
world.WriteLine("0.0000000");
world.WriteLine("{0:0.000000000000}", (-info.Area.HeightLat / pxDelta.Y));
world.WriteLine("{0:0.000000000000}", info.Area.Left);
world.WriteLine("{0:0.000000000000}", info.Area.Top);
world.Close();
}
}
if(info.MakeKmz)
{
var kmzFile = Path.GetDirectoryName(bigImage) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(bigImage) + ".kmz";
e.Result = kmzFile;
using(ZipStorer zip = ZipStorer.Create(kmzFile, "GMap.NET"))
{
zip.AddFile(ZipStorer.Compression.Store, bigImage, "files/map.jpg", "map");
using(var readme = new MemoryStream(
Encoding.UTF8.GetBytes(
string.Format(CultureInfo.InvariantCulture, @"<?xml version=""1.0"" encoding=""UTF-8""?>
<kml xmlns=""http://www.opengis.net/kml/2.2"" xmlns:gx=""http://www.google.com/kml/ext/2.2"" xmlns:kml=""http://www.opengis.net/kml/2.2"" xmlns:atom=""http://www.w3.org/2005/Atom"">
<GroundOverlay>
<name>{8}</name>
<LookAt>
<longitude>{6}</longitude>
<latitude>{7}</latitude>
<altitude>0</altitude>
<heading>0</heading>
<tilt>0</tilt>
<range>69327.55500845652</range>
</LookAt>
<color>91ffffff</color>
<Icon>
<href>files/map.jpg</href>
</Icon>
<gx:LatLonQuad>
<coordinates>
{0},{1},0 {2},{3},0 {4},{5},0 {6},{7},0
</coordinates>
</gx:LatLonQuad>
</GroundOverlay>
</kml>", info.Area.Left, info.Area.Bottom,
info.Area.Right, info.Area.Bottom,
info.Area.Right, info.Area.Top,
info.Area.Left, info.Area.Top,
kmzFile))))
{
zip.AddStream(ZipStorer.Compression.Store, "doc.kml", readme, DateTime.Now, "kml");
zip.Close();
}
}
}
}
}
readonly List<PointLatLng> GpxRoute = new List<PointLatLng>();
RectLatLng AreaGpx = RectLatLng.Empty;
private void button1_Click(object sender, EventArgs e)
{
RectLatLng? area = null;
if(checkBoxRoutes.Checked)
{
area = Main.MainMap.GetRectOfAllRoutes(null);
if(!area.HasValue)
{
MessageBox.Show("No routes in map", "GMap.NET", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
}
else
{
area = Main.MainMap.SelectedArea;
if(area.Value.IsEmpty)
{
MessageBox.Show("Select map area holding ALT", "GMap.NET", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
}
if(!bg.IsBusy)
{
lock(tileArea)
{
tileArea.Clear();
tileArea.AddRange(Main.MainMap.MapProvider.Projection.GetAreaTileList(area.Value, (int)numericUpDown1.Value, 1));
tileArea.TrimExcess();
}
numericUpDown1.Enabled = false;
progressBar1.Value = 0;
button1.Enabled = false;
Main.MainMap.HoldInvalidation = true;
bg.RunWorkerAsync(new MapInfo(area.Value, (int)numericUpDown1.Value, Main.MainMap.MapProvider, checkBoxWorldFile.Checked, checkBoxKMZ.Checked));
}
}
private void button2_Click(object sender, EventArgs e)
{
if(bg.IsBusy)
{
bg.CancelAsync();
}
}
}
public struct MapInfo
{
public RectLatLng Area;
public int Zoom;
public GMapProvider Type;
public bool MakeWorldFile;
public bool MakeKmz;
public MapInfo(RectLatLng Area, int Zoom, GMapProvider Type, bool makeWorldFile, bool MakeKmz)
{
this.Area = Area;
this.Zoom = Zoom;
this.Type = Type;
this.MakeWorldFile = makeWorldFile;
this.MakeKmz = MakeKmz;
}
}
}