-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTileRendering.cs
4176 lines (3733 loc) · 164 KB
/
TileRendering.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -- read shape 1st record
// çàìåíèòü tmpArea.points = new PointF[se-si-1];
// íà tmpArea.points = new PointF[se-si];
// CHECK: int se = (i + 1) == this.Area.parts.Length ? this.Area.points.Length : this.Area.parts[i + 1];
// CEHCK: int se = (i+1) == this.Area.parts.Length ? this.Area.points.Length : this.Area.parts[i+1];
#define TileRenderer
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.InteropServices;
using BOOL = System.Boolean;
using DWORD = System.UInt32;
using LPWSTR = System.String;
using NET_API_STATUS = System.UInt32;
namespace TileRendering
{
/// <summary>
/// Ôîðìàò õðàíåíèÿ òàéëîâ â ïàïêàõ
/// </summary>
public enum TileRenderPathScheme
{
[Description(@"Õðàíèòü â ôîðìàòå Slippy map \Zoom\X\Y")]
/// <summary>
/// Õðàíèòü â ôîðìàòå Slippy map \Zoom\X\Y
/// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
/// </summary>
fsSlippyMap = 0x01,
[Description(@"Õðàíèòü â ôîðìàòå ArcGIS conf.xml (\Zoom\Y\X)")]
/// <summary>
/// Õðàíèòü â ôîðìàòå ArcGIS conf.xml (\Zoom\Y\X)
/// </summary>
fsArcGIS = 0x02
}
/// <summary>
/// Ðàçìåð èçîáðàæåíèÿ äëÿ ðàçäåëåíèÿ ó÷àñòêà íà òàéëû
/// </summary>
public enum TileRenderZoneSize
{
/// <summary>
/// 256x256 (1 òàéë)
/// </summary>
tileZone_1x1 = 0x01,
/// <summary>
/// 512x512 (4 òàéëà)
/// </summary>
tileZone_2x2 = 0x02,
tileZone_3x3 = 0x03,
tileZone_4x4 = 0x04,
tileZone_5x5 = 0x05,
tileZone_6x6 = 0x06,
tileZone_7x7 = 0x07,
tileZone_8x8 = 0x08,
tileZone_9x9 = 0x09,
tileZone_10x10 = 0x0A,
tileZone_11x11 = 0x0B,
tileZone_12x12 = 0x0C,
tileZone_13x13 = 0x0D,
tileZone_14x14 = 0x0E,
tileZone_15x15 = 0x0F
}
/// <summary>
/// Ñîõðàíÿòü ëè ïóñòûå è íåîáðàáîòàííûå òàéëû
/// </summary>
public enum TileRenderEmptyData
{
/// <summary>
/// Ñîõðàíÿòü âñå òàéëû
/// </summary>
saveAnyTiles = 0x01,
/// <summary>
/// Ñîõðàíÿòü âñå òàéëû â íàðåçàåìîì äèàïàçîíå (â òîì ÷èñëå îäíîöâåòíûå è íåîòðèñîâàííûå)
/// </summary>
saveOneColorTiles = 0x02,
/// <summary>
/// Íå ñîõðàíÿòü îäíîöâåòíûå òàéëû (ñîõðàíÿþòñÿ òîëüêî íåñóùóþ ïîëåçíóþ èíôîðìàöèþ)
/// </summary>
saveActualTiles = 0x03,
/// <summary>
/// Íå ñîõðàíÿòü îäíîöâåòíûå òàéëû (ñîõðàíÿþòñÿ òîëüêî íåñóùóþ ïîëåçíóþ èíôîðìàöèþ)
/// </summary>
saveAnyAreaTiles = 0x04
}
/// <summary>
/// Ñîõðàíÿòü çàïðàøèâàåìûé òàéë èëè âñå òàéëû çîíû
/// </summary>
public enum TileRenderZoneMode
{
/// <summary>
/// Ñîõðàíÿòü âñå òàéëû çîíû
/// </summary>
keepFullZone = 0x01,
/// <summary>
/// Ñîõðàíÿòü òîëüêî çàïðàøèâàåìûé òàéë
/// </summary>
keepOnlyCurrent = 0x02
}
/// <summary>
/// Îïòèìèçèðîâàòü ëè òàéëû
/// </summary>
public enum TileOptimizeMethod
{
none = 0x01,
fastest = 0x02, // èñïîëüçóåòñÿ FreeImage.DLL/áûñòðîå è Standard, ñîõðàíÿåòñÿ ôàéë ñ ìåíüøèì ðàçìåðîì
fastmax = 0x03, // èñïîëüçóåòñÿ FreeImage.DLL/ìàêñèìàëüíîå è Standard, ñîõðàíÿåòñÿ ôàéë ñ ìåíüøèì ðàçìåðîì
imageMagic = 0x04, // èñïîëüçóåòñÿ imageMagic è Standard, ñîõðàíÿåòñÿ ôàéë ñ ìåíüøèì ðàçìåðîì
optimize = 0x05, // èñïîëüçóåòñÿ optimize.dll è Standard, ñîõðàíÿåòñÿ ôàéë ñ ìåíüøèì ðàçìåðîì
fastMagic = 0x06, //èñïîëüçóåòñÿ imageMagic, FreeImage.DLL/ìàêñèìàëüíîå è Standard, ñîõðàíÿåòñÿ ôàéë ñ ìåíüøèì ðàçìåðîì
maximim = 0x07 // èñïîëüçóåòñÿ imageMagic, optimize.dll è Standard, ñîõðàíÿåòñÿ ôàéë ñ ìåíüøèì ðàçìåðîì
}
/// <summary>
/// Åñëè òàéë ñ òàêèì èìåíåì óæå ñóùåñòâóåò
/// </summary>
public enum TileFinalMerging
{
[Description("Íå ïåðåçàïèñûâàòü ñóùåñòâóþùèé ôàéë")]
/// <summary>
/// Íå ïåðåçàïèñûâàòü ñóùåñòâóþùèé ôàéë
/// </summary>
keepExistingFile = 0x01,
[Description("Ïåðåçàïèñûâàòü ñóùåñòâóþùèé ôàéë")]
/// <summary>
/// Ïåðåçàïèñûâàòü ñóùåñòâóþùèé ôàéë
/// </summary>
overwriteExistingFile = 0x02,
[Description("Îáúåäèíÿòü ñ ñóùåñòâóþùèì òàéëîì")]
/// <summary>
/// Îáúåäèíÿòü ñ ñóùåñòâóþùèì òàéëîì
/// </summary>
mergeWithExistingFile = 0x03
}
/// <summary>
/// Êîíôèãóðàöèÿ
/// </summary>
[Serializable]
public class TileRendererConfig
{
/// <summary>
/// ×èñëî ïîòîêîâ
/// </summary>
public int multithreadCount = 1;
/// <summary>
/// Ïðè îøèáêàõ îòðèñîâêè ïðîáûâàòü îòðèçñîâàòü çîíó çàíîâî, ïîëîæèâ ñíîâà â î÷åðåäü
/// </summary>
public bool addToQueueAgainIfError = true;
/// <summary>
/// Ïåðåçàãðóæàòüôàéë êàðòû ïðè êàæäîé íàðåçêå çîíû
/// </summary>
public bool reloadMapEachZone = false;
// <summary>
/// Ðàçìåð ïåðåêðûòèÿ çîíû çà ãðàíèöåé èçîáðàæåíèÿ
/// </summary>
public ushort tileZoneOverSize = 128;
/// <summary>
/// Äîáàâëÿòü ëè êîïèðàéò?
/// </summary>
public string addCopyrightsFile = null;
/// <summary>
/// Îáðàáàòûâàòü òàéëû òîëüêî ïîïàäàþùèå â ïîëèãîí
/// </summary>
public string renderOnlyInPolygon = null;
/// <summary>
/// Ñòðóêòóðà ïàïîê õðàíåíèÿ òàéëîâ
/// </summary>
public TileRenderPathScheme pathStructure = TileRenderPathScheme.fsSlippyMap;
/// <summary>
/// Ðàçìåð êàðòû ïðè îòðèñîâêè
/// </summary>
public TileRenderZoneSize tileZoneSize = TileRenderZoneSize.tileZone_8x8;
/// <summary>
/// Ñîõðàíåíèå òàéëîâ ïðè îòðèñîâêè
/// </summary>
public TileRenderZoneMode tileZoneMode = TileRenderZoneMode.keepFullZone;
/// <summary>
/// Îáðàáàòûâàòü ïóñòûå òàéëû
/// </summary>
public TileRenderEmptyData processEmptyTiles = TileRenderEmptyData.saveActualTiles;
/// <summary>
/// Îïòèìèçèðîâàòü ëè òàéëû
/// </summary>
public TileOptimizeMethod tileOptimizeMethod = TileOptimizeMethod.none;
/// <summary>
/// Ôîðìàò PNG
/// </summary>
public byte tileOptimizeFormat = 0;
/// <summary>
/// Ïåðåçàïèñûâàòü ëè ñóùåñòâóþùèé òàéë
/// </summary>
public TileFinalMerging tileFinalMerging = TileFinalMerging.overwriteExistingFile;
}
public class TileRenderingErrors
{
public class ErrorInfo
{
public DateTime dt;
public uint ThreadID;
public int x;
public int y;
public int z;
public Exception ex;
public string comment;
public string parameter;
public ErrorInfo(int x, int y, int z, string comment, Exception ex, string parameter)
{
this.dt = DateTime.Now;
this.ThreadID = MultithreadTileRenderer.GetCurrentThreadId();
this.x = x;
this.y = y;
this.z = z;
this.comment = comment;
this.ex = ex;
this.parameter = parameter;
}
}
public static Mutex ErrorQueueMutex = new Mutex();
public static ulong ErrorQueueTotalCount = 0;
public static byte PauseThreadsIfErrorsCountIs = 0;
public static List<uint> PausedThreads = new List<uint>();
public static Mutex PausedThreadsMutex = new Mutex();
public static Queue<ErrorInfo> ErrorQueue = new Queue<ErrorInfo>();
public static bool PauseNormal = false;
public static ulong PausedNormal = 0;
public static bool WithDump = true;
//public static string
}
/// <summary>
/// Spherical Mercator Projection
/// http://spatialreference.org/ref/sr-org/7483/
/// EPSG:3857 / SR-ORG:7483 / EPSG:900913 / WGS84 Web Mercator (Auxiliary Sphere, Spherical Mercator)
/// Proj4: +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +k=1.0 +x_0=0.0 +y_0=0.0 +units=m +no_defs +nadgrids=@null +over
/// [[ORIGINAL]]
/// </summary>
public class TilesProjection
{
public class PointD
{
private double x;
private double y;
public double X {get {return x;} set {x = value;}}
public double Y {get {return y;} set {y = value;}}
public double Lat {get {return y;} set {y = value;}}
public double Lon{get {return x;} set {x = value;}}
public PointD(){}
public PointD(double X, double Y) {this.X = X; this.Y = Y;}
public PointD(float X, float Y) { this.X = X; this.Y = Y; }
public PointD(Point p) {this.X = p.X; this.Y = p.Y;}
public PointD(PointF p) {this.X = p.X; this.Y = p.Y;}
public static PointD FromXY(int X, int Y) { return new PointD(X, Y); }
public static PointD FromXY(int[] xy) { return new PointD(xy[0], xy[1]); }
public static PointD FromXY(double X, double Y) { return new PointD(X,Y);}
public static PointD FromXY(double[] xy) { return new PointD(xy[0], xy[1]); }
public static PointD FromXY(float X, float Y) { return new PointD(X, Y); }
public static PointD FromXY(Point p) { return new PointD(p);}
public static PointD FromXY(PointF p) { return new PointD(p);}
public static PointD FromLL(double Lat, double Lon) { return new PointD(Lon, Lat); }
public Point Point { get { return new Point((int)x, (int)y); } set { X = value.X; Y = value.Y; } }
public PointF PointF { get { return new PointF((float)x,(float)y); } set { X = value.X; Y = value.Y; }}
public override string ToString() { return "{" + x.ToString() + ";" + y.ToString() + "}"; }
public static explicit operator PointD(Point p) { return new PointD(p.X, p.Y); }
public static explicit operator PointD(PointF p) { return new PointD(p.X, p.Y); }
public static explicit operator PointD(double[] xy) { return new PointD(xy[0], xy[1]); }
public static explicit operator PointD(int[] xy) { return new PointD(xy[0], xy[1]); }
public static explicit operator Point(PointD p) { return new Point((int)p.X, (int)p.Y); }
public static explicit operator PointF(PointD p) { return new PointF((float)p.X, (float)p.Y); }
public static explicit operator double[](PointD p) { return new double[] { p.X, p.Y }; }
public static explicit operator int[](PointD p) { return new int[] { (int)p.X, (int)p.Y }; }
}
#region private static methods and constants
private const double DEG_TO_RAD = Math.PI / 180;
private const double RAD_TO_DEG = 180 / Math.PI;
private const double earth_radius = 6378137;
private const double max_error = 1e-6;
private static double[] Ac;
private static double[] Bc;
private static double[] Cc;
private static double[][] zc;
private static double c = 256;
static TilesProjection()
{
Ac = new double[22];
Bc = new double[22];
Cc = new double[22];
zc = new double[22][];
for (int d = 0; d < 22; d++)
{
double e = c / 2;
Bc[d] = c / 360;
Cc[d] = c / (2 * Math.PI);
zc[d] = new double[] { e, e };
Ac[d] = c;
c *= 2;
};
}
private static double minmax(double a, double b, double c)
{
a = Math.Max(a, b);
a = Math.Min(a, c);
return a;
}
private static double deg2rad(double d) { return ((d) * Math.PI) / 180; }
private static double rad2deg(double d) { return ((d) * 180) / Math.PI; }
#endregion
#region fromLLtoPixel
public static double[] fromLLtoPixel(double[] ll, int zoom)
{
double[] d = zc[zoom];
double e = Math.Round(d[0] + ll[0] * Bc[zoom]);
double f = minmax(Math.Sin(DEG_TO_RAD * ll[1]), -0.9999, 0.9999);
double g = Math.Round(d[1] + 0.5 * Math.Log((1 + f) / (1 - f)) * (-1 * Cc[zoom]));
return new double[] { e, g };
}
public static Point fromLLtoPixel(double lat, double lon, int zoom)
{
double[] xy = fromLLtoPixel(new double[] { lon, lat }, zoom);
return new Point((int)xy[0], (int)xy[1]);
}
public static Point fromLLtoPixel(PointF p, int zoom)
{
double[] xy = fromLLtoPixel(new double[] { p.X, p.Y }, zoom);
return new Point((int)xy[0], (int)xy[1]);
}
public static PointD fromLLtoPixel(PointD p, int zoom)
{
double[] xy = fromLLtoPixel(new double[] { p.X, p.Y }, zoom);
return new PointD(xy[0], xy[1]);
}
#endregion
#region fromPixelToLL
public static double[] fromPixelToLL(double[] px, int zoom)
{
double[] e = zc[zoom];
double f = (px[0] - e[0]) / Bc[zoom];
double g = (px[1] - e[1]) / (-1 * Cc[zoom]);
double h = RAD_TO_DEG * (2 * Math.Atan(Math.Exp(g)) - 0.5 * Math.PI);
return new double[] { f, h };
}
public static PointF fromPixelToLL(int x, int y, int zoom)
{
double[] ll = fromPixelToLL(new double[] { x, y }, zoom);
return new PointF((float)ll[0], (float)ll[1]);
}
public static PointF fromPixelToLL(Point p, int zoom)
{
double[] ll = fromPixelToLL(new double[] { p.X, p.Y }, zoom);
return new PointF((float)ll[0], (float)ll[1]);
}
public static PointF fromPixelToLL(PointF p, int zoom)
{
double[] ll = fromPixelToLL(new double[] { p.X, p.Y }, zoom);
return new PointF((float)ll[0], (float)ll[1]);
}
public static PointD fromPixelToLL(PointD p, int zoom)
{
double[] ll = fromPixelToLL(new double[] { p.X, p.Y }, zoom);
return new PointD(ll[0], ll[1]);
}
#endregion
#region fromLLToTile
public static void fromLLToTile(double lat, double lon, int zoom, out int x, out int y)
{
x = (int)((lon + 180.0) / 360.0 * Math.Pow(2.0, zoom));
y = (int)((1.0 - Math.Log(Math.Tan(lat * Math.PI / 180.0) +
1.0 / Math.Cos(lat * Math.PI / 180.0)) / Math.PI) / 2.0 * Math.Pow(2.0, zoom));
}
public static void fromLLToTile(double lat, double lon, int zoom, out double x, out double y)
{
double sin_phi = System.Math.Sin(lat * System.Math.PI / 180);
double norm_x = lon / 180;
double norm_y = (0.5 * System.Math.Log((1 + sin_phi) / (1 - sin_phi))) / System.Math.PI;
x = (System.Math.Pow(2, zoom) * ((norm_x + 1) / 2));
y = (System.Math.Pow(2, zoom) * ((1 - norm_y) / 2));
}
public static Point fromLLToTile(double lat, double lon, int zoom)
{
int x, y;
fromLLToTile(lat, lon, zoom, out x, out y);
return new Point(x, y);
}
public static Point fromLLToTile(PointF p, int zoom)
{
int x, y;
fromLLToTile(p.Y, p.X, zoom, out x, out y);
return new Point(x, y);
}
public static PointD fromLLToTile(PointD p, int zoom)
{
double x, y;
fromLLToTile(p.Y, p.X, zoom, out x, out y);
return new PointD(x, y);
}
#endregion
#region fromTileToLL
public static void fromTileToLL(int x, int y, int zoom, out double lat, out double lon)
{
lon = ((x / Math.Pow(2.0, zoom) * 360.0) - 180.0);
double n = Math.PI - ((2.0 * Math.PI * y) / Math.Pow(2.0, zoom));
lat = (180.0 / Math.PI * Math.Atan(Math.Sinh(n)));
}
public static void fromTileToLL(double x, double y, int zoom, out double Lat, out double Lng)
{
Lng = ((x * 256) - (256 * Math.Pow(2, zoom - 1))) / ((256 * Math.Pow(2, zoom)) / 360);
while (Lng > 180) Lng -= 360;
while (Lng < -180) Lng += 360;
double exp = ((y * 256) - (256 * Math.Pow(2, zoom - 1))) / ((-256 * Math.Pow(2, zoom)) / (2 * Math.PI));
Lat = ((2 * Math.Atan(Math.Exp(exp))) - (Math.PI / 2)) / (Math.PI / 180);
if (Lat < -90) Lat = -90;
if (Lat > 90) Lat = 90;
}
public static PointF fromTileToLL(double x, double y, int zoom)
{
double Lng = ((x * 256) - (256 * Math.Pow(2, zoom - 1))) / ((256 * Math.Pow(2, zoom)) / 360);
while (Lng > 180) Lng -= 360;
while (Lng < -180) Lng += 360;
double exp = ((y * 256) - (256 * Math.Pow(2, zoom - 1))) / ((-256 * Math.Pow(2, zoom)) / (2 * Math.PI));
double Lat = ((2 * Math.Atan(Math.Exp(exp))) - (Math.PI / 2)) / (Math.PI / 180);
if (Lat < -90) Lat = -90;
if (Lat > 90) Lat = 90;
return new PointF((float)Lng, (float)Lat);
}
public static PointF fromTileToLL(int x, int y, int zoom)
{
double lat, lon;
fromTileToLL(x, y, zoom, out lat, out lon);
return new PointF((float)lon, (float)lat);
}
public static PointF fromTileToLL(Point p, int zoom)
{
return fromTileToLL(p.X, p.Y, zoom);
}
public static PointF fromTileToLL(PointF p, int zoom)
{
double x, y;
fromTileToLL(p.X, p.Y, zoom, out y, out x);
return new PointF((float)x, (float)y);
}
public static PointD fromTileToLL(PointD p, int zoom)
{
PointD res = new PointD();
res.PointF = fromTileToLL(p.Point, zoom);
return res;
}
#endregion
#region fromLLtoMeter
public static PointF fromLLtoMeter(double lat, double lon)
{
return new PointF((float)lon2x_m(lon), (float)lat2y_m(lat));
}
public static PointF fromLLtoMeter(PointF p)
{
return new PointF((float)lon2x_m(p.X), (float)lat2y_m(p.Y));
}
public static PointD fromLLtoMeter(PointD p)
{
return new PointD(lon2x_m(p.X), lat2y_m(p.Y));
}
#endregion
#region fromMeterToLL
public static PointF fromMeterToLL(double x, double y)
{
return new PointF((float)x2lon_m(x), (float)y2lat_m(y));
}
public static PointF fromMeterToLL(PointF p)
{
return new PointF((float)x2lon_m(p.X), (float)y2lat_m(p.Y));
}
public static PointD fromMeterToLL(PointD p)
{
return new PointD(x2lon_m(p.X), y2lat_m(p.Y));
}
#endregion
#region fromPixelToMeter
public static PointF fromPixelToMeter(int x, int y, int zoom)
{
return fromLLtoMeter(fromPixelToLL(x, y, zoom));
}
public static PointF fromPixelToMeter(Point p, int zoom)
{
return fromLLtoMeter(fromPixelToLL(p, zoom));
}
public static PointF fromPixelToMeter(PointF p, int zoom)
{
return fromLLtoMeter(fromPixelToLL(p, zoom));
}
public static PointD fromPixelToMeter(PointD p, int zoom)
{
return fromLLtoMeter(fromPixelToLL(p, zoom));
}
#endregion
#region fromMeterToPixel
public static Point fromMeterToPixel(double x, double y, int zoom)
{
return fromLLtoPixel(fromMeterToLL(x, y), zoom);
}
public static Point fromMeterToPixel(PointF p, int zoom)
{
return fromLLtoPixel(fromMeterToLL(p), zoom);
}
public static PointD fromMeterToPixel(PointD p, int zoom)
{
return fromLLtoPixel(fromMeterToLL(p), zoom);
}
#endregion
#region fromTileToMeter
public static PointF fromTileToMeter(int x, int y, int zoom)
{
return fromLLtoMeter(fromTileToLL(x, y, zoom));
}
public static PointF fromTileToMeter(double x, double y, int zoom)
{
return fromLLtoMeter(fromTileToLL(new PointD(x, y), zoom)).PointF;
}
public static PointF fromTileToMeter(Point p, int zoom)
{
return fromLLtoMeter(fromTileToLL(p, zoom));
}
public static PointF fromTileToMeter(PointF p, int zoom)
{
return fromLLtoMeter(fromTileToLL(p, zoom));
}
public static PointD fromTileToMeter(PointD p, int zoom)
{
return fromLLtoMeter(fromTileToLL(p, zoom));
}
#endregion
#region fromMeterToTile
public static Point fromMeterToTile(double x, double y, int zoom)
{
return fromLLToTile(fromMeterToLL(x, y), zoom);
}
public static Point fromMeterToTile(PointF p, int zoom)
{
return fromLLToTile(p, zoom);
}
public static PointD fromMeterToTile(PointD p, int zoom)
{
return fromLLToTile(p, zoom);
}
#endregion
#region fromPixelToTile
public static Point fromPixelToTile(int x, int y)
{
return new Point((int)((double)x / 256.0), (int)((double)y / 256.0));
}
public static Point fromPixelToTile(Point p)
{
return new Point((int)((double)p.X / 256.0), (int)((double)p.Y / 256.0));
}
public static PointF fromPixelToTile(PointF p)
{
return new PointF(p.X / 256, p.Y / 256);
}
public static PointD fromPixelToTile(PointD p)
{
return new PointD(p.X / 256.0, p.Y / 256.0);
}
#endregion
#region fromTileToPixel
public static Point fromTileToPixel(int x, int y)
{
return new Point((int)((double)x * 256.0), (int)((double)y * 256.0));
}
public static Point fromTileToPixel(Point p)
{
return new Point((int)((double)p.X * 256.0), (int)((double)p.Y * 256.0));
}
public static PointF fromTileToPixel(PointF p)
{
return new PointF(p.X * 256, p.Y * 256);
}
public static PointD fromTileToPixel(PointD p)
{
return new PointD(p.X * 256.0, p.Y * 256.0);
}
#endregion
#region GetLengthMeters
public static uint GetLengthMeters(PointF a, PointF b)
{
return GetLengthMeters(a.Y, a.X, b.Y, b.X, false);
}
public static uint GetLengthMeters(PointD a, PointD b)
{
return GetLengthMeters(a.Lat, a.Lon, b.Lat, b.Lon, false);
}
public static uint GetLengthMeters(double StartLat, double StartLong, double EndLat, double EndLong, bool radians)
{
return GetLengthMetersC(StartLat, StartLong, EndLat, EndLong, radians);
}
public static uint GetLengthMetersA(double StartLat, double StartLong, double EndLat, double EndLong, bool radians)
{
double D2R = Math.PI / 180; // Ïðåîáðàçîâàíèå ãðàäóñîâ â ðàäèàíû
double a = 6378137.0000; // WGS-84 Equatorial Radius (a)
double f = 1 / 298.257223563; // WGS-84 Flattening (f)
double b = (1 - f) * a; // WGS-84 Polar Radius
double e2 = (2 - f) * f; // WGS-84 Êâàäðàò ýêñöåíòðè÷íîñòè ýëëèïñîèäà // 1-(b/a)^2
// Ïåðåìåííûå, èñïîëüçóåìûå äëÿ âû÷èñëåíèÿ ñìåùåíèÿ è ðàññòîÿíèÿ
double fPhimean; // Ñðåäíÿÿ øèðîòà
double fdLambda; // Ðàçíèöà ìåæäó äâóìÿ çíà÷åíèÿìè äîëãîòû
double fdPhi; // Ðàçíèöà ìåæäó äâóìÿ çíà÷åíèÿìè øèðîòû
double fAlpha; // Ñìåùåíèå
double fRho; // Ìåðèäèàíñêèé ðàäèóñ êðèâèçíû
double fNu; // Ïîïåðå÷íûé ðàäèóñ êðèâèçíû
double fR; // Ðàäèóñ ñôåðû Çåìëè
double fz; // Óãëîâîå ðàññòîÿíèå îò öåíòðà ñôåðîèäà
double fTemp; // Âðåìåííàÿ ïåðåìåííàÿ, èñïîëüçóþùàÿñÿ â âû÷èñëåíèÿõ
// Âû÷èñëÿåì ðàçíèöó ìåæäó äâóìÿ äîëãîòàìè è øèðîòàìè è ïîëó÷àåì ñðåäíþþ øèðîòó
// ïðåäïîëîæèòåëüíî ÷òî ðàññòîÿíèå ìåæäó òî÷êàìè << ðàäèóñà çåìëè
if (!radians)
{
fdLambda = (StartLong - EndLong) * D2R;
fdPhi = (StartLat - EndLat) * D2R;
fPhimean = ((StartLat + EndLat) / 2) * D2R;
}
else
{
fdLambda = StartLong - EndLong;
fdPhi = StartLat - EndLat;
fPhimean = (StartLat + EndLat) / 2;
};
// Âû÷èñëÿåì ìåðèäèàííûå è ïîïåðå÷íûå ðàäèóñû êðèâèçíû ñðåäíåé øèðîòû
fTemp = 1 - e2 * (sqr(Math.Sin(fPhimean)));
fRho = (a * (1 - e2)) / Math.Pow(fTemp, 1.5);
fNu = a / (Math.Sqrt(1 - e2 * (Math.Sin(fPhimean) * Math.Sin(fPhimean))));
// Âû÷èñëÿåì óãëîâîå ðàññòîÿíèå
if (!radians)
{
fz = Math.Sqrt(sqr(Math.Sin(fdPhi / 2.0)) + Math.Cos(EndLat * D2R) * Math.Cos(StartLat * D2R) * sqr(Math.Sin(fdLambda / 2.0)));
}
else
{
fz = Math.Sqrt(sqr(Math.Sin(fdPhi / 2.0)) + Math.Cos(EndLat) * Math.Cos(StartLat) * sqr(Math.Sin(fdLambda / 2.0)));
};
fz = 2 * Math.Asin(fz);
// Âû÷èñëÿåì ñìåùåíèå
if (!radians)
{
fAlpha = Math.Cos(EndLat * D2R) * Math.Sin(fdLambda) * 1 / Math.Sin(fz);
}
else
{
fAlpha = Math.Cos(EndLat) * Math.Sin(fdLambda) * 1 / Math.Sin(fz);
};
fAlpha = Math.Asin(fAlpha);
// Âû÷èñëÿåì ðàäèóñ Çåìëè
fR = (fRho * fNu) / (fRho * sqr(Math.Sin(fAlpha)) + fNu * sqr(Math.Cos(fAlpha)));
// Ïîëó÷àåì ðàññòîÿíèå
return (uint)Math.Round(Math.Abs(fz * fR));
}
public static uint GetLengthMetersB(double StartLat, double StartLong, double EndLat, double EndLong, bool radians)
{
double fPhimean, fdLambda, fdPhi, fAlpha, fRho, fNu, fR, fz, fTemp, Distance,
D2R = Math.PI / 180,
a = 6378137.0,
e2 = 0.006739496742337;
if (radians) D2R = 1;
fdLambda = (StartLong - EndLong) * D2R;
fdPhi = (StartLat - EndLat) * D2R;
fPhimean = (StartLat + EndLat) / 2.0 * D2R;
fTemp = 1 - e2 * Math.Pow(Math.Sin(fPhimean), 2);
fRho = a * (1 - e2) / Math.Pow(fTemp, 1.5);
fNu = a / Math.Sqrt(1 - e2 * Math.Sin(fPhimean) * Math.Sin(fPhimean));
fz = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(fdPhi / 2.0), 2) +
Math.Cos(EndLat * D2R) * Math.Cos(StartLat * D2R) * Math.Pow(Math.Sin(fdLambda / 2.0), 2)));
fAlpha = Math.Asin(Math.Cos(EndLat * D2R) * Math.Sin(fdLambda) / Math.Sin(fz));
fR = fRho * fNu / (fRho * Math.Pow(Math.Sin(fAlpha), 2) + fNu * Math.Pow(Math.Cos(fAlpha), 2));
Distance = fz * fR;
return (uint)Math.Round(Distance);
}
public static uint GetLengthMetersC(double StartLat, double StartLong, double EndLat, double EndLong, bool radians)
{
double D2R = Math.PI / 180;
if (radians) D2R = 1;
double dDistance = Double.MinValue;
double dLat1InRad = StartLat * D2R;
double dLong1InRad = StartLong * D2R;
double dLat2InRad = EndLat * D2R;
double dLong2InRad = EndLong * D2R;
double dLongitude = dLong2InRad - dLong1InRad;
double dLatitude = dLat2InRad - dLat1InRad;
// Intermediate result a.
double a = Math.Pow(Math.Sin(dLatitude / 2.0), 2.0) +
Math.Cos(dLat1InRad) * Math.Cos(dLat2InRad) *
Math.Pow(Math.Sin(dLongitude / 2.0), 2.0);
// Intermediate result c (great circle distance in Radians).
double c = 2.0 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1.0 - a));
const double kEarthRadiusKms = 6378137.0000;
dDistance = kEarthRadiusKms * c;
return (uint)Math.Round(dDistance);
}
public static double GetLengthMetersD(double sLat, double sLon, double eLat, double eLon, bool radians)
{
double EarthRadius = 6378137.0;
double lon1 = radians ? sLon : deg2rad(sLon);
double lon2 = radians ? eLon : deg2rad(eLon);
double lat1 = radians ? sLat : deg2rad(sLat);
double lat2 = radians ? eLat : deg2rad(eLat);
return EarthRadius * (Math.Acos(Math.Sin(lat1) * Math.Sin(lat2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(lon1 - lon2)));
}
public static double GetLengthMetersE(double sLat, double sLon, double eLat, double eLon, bool radians)
{
double EarthRadius = 6378137.0;
double lon1 = radians ? sLon : deg2rad(sLon);
double lon2 = radians ? eLon : deg2rad(eLon);
double lat1 = radians ? sLat : deg2rad(sLat);
double lat2 = radians ? eLat : deg2rad(eLat);
/* This algorithm is called Sinnott's Formula */
double dlon = (lon2) - (lon1);
double dlat = (lat2) - (lat1);
double a = Math.Pow(Math.Sin(dlat / 2), 2.0) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow(Math.Sin(dlon / 2), 2.0);
double c = 2 * Math.Asin(Math.Sqrt(a));
return EarthRadius * c;
}
private static double sqr(double val)
{
return val * val;
}
#endregion
// /* The following functions take or return there results in degrees */
//private static double y2lat_d(double y) { return rad2deg(2 * Math.Atan(Math.Exp(deg2rad(y))) - Math.PI / 2); }
//private static double x2lon_d(double x) { return x; }
//private static double lat2y_d(double lat) { return rad2deg(Math.Log(Math.Tan(Math.PI / 4 + deg2rad(lat) / 2))); }
//private static double lon2x_d(double lon) { return lon; }
/* The following functions take or return there results in something close to meters, along the equator */
public static double y2lat_m(double y) { return rad2deg(2 * Math.Atan(Math.Exp((y / earth_radius))) - Math.PI / 2); }
public static double x2lon_m(double x) { return rad2deg(x / earth_radius); }
public static double lat2y_m(double lat) { return earth_radius * Math.Log(Math.Tan(Math.PI / 4 + deg2rad(lat) / 2)); }
public static double lon2x_m(double lon) { return deg2rad(lon) * earth_radius; }
}
/// <summary>
/// Íàðåçêà òàéëîâ â íåñêîëüêî ïîòîêîâ
/// </summary>
public class MultithreadTileRenderer // : MarshalByRefObject // for Lifetime Services
{
/// <summary>
/// ×èñëî ïîòîêîâ
/// </summary>
public int multithreadCount = 8;
/// <summary>
/// Ïðè îøèáêàõ îòðèñîâêè ïðîáûâàòü îòðèñîâàòü çîíó ïîâòîðíî
/// </summary>
public bool addToQueueAgainIfError = true;
/// <summary>
/// Ïåðåçàãðóæàòüôàéë êàðòû ïðè êàæäîé íàðåçêå çîíû
/// </summary>
public bool reloadMapEachZone = false;
/// <summary>
/// Ðàçìåð ïåðåêðûòèÿ çîíû çà ãðàíèöåé èçîáðàæåíèÿ
/// </summary>
public ushort tileZoneOverSize = 128;
/// <summary>
/// Äîáàâëÿòü ëè êîïèðàéò?
/// </summary>
public string addCopyrightsFile = null;
/// <summary>
/// Îáðàáàòûâàòü òàéëû òîëüêî ïîïàäàþùèå â ïîëèãîí
/// </summary>
public string renderOnlyInPolygon = null;
/// <summary>
/// Ñòðóêòóðà ïàïîê õðàíåíèÿ òàéëîâ
/// </summary>
public TileRenderPathScheme pathStructure = TileRenderPathScheme.fsSlippyMap;
/// <summary>
/// Ðàçìåð êàðòû ïðè îòðèñîâêå
/// </summary>
public TileRenderZoneSize tileZoneSize = TileRenderZoneSize.tileZone_8x8;
private int tileZoneSizeDiv = 8;
private int tileZoneSizeCount = 8 * 8;
/// <summary>
/// Ñîõðàíåíèå òàéëîâ ïðè îòðèñîâêå
/// </summary>
public TileRenderZoneMode tileZoneMode = TileRenderZoneMode.keepFullZone;
/// <summary>
/// Îáðàáàòûâàòü ïóñòûå òàéëû
/// </summary>
public TileRenderEmptyData processEmptyTiles = TileRenderEmptyData.saveActualTiles;
/// <summary>
/// Îïòèìèçèðîâàòü ëè òàéëû
/// </summary>
public TileOptimizeMethod tileOptimizeMethod = TileOptimizeMethod.none;
/// <summary>
/// Ôîðìàò PNG
/// </summary>
public byte tileOptimizeFormat = 0;
/// <summary>
/// Ïåðåçàïèñûâàòü ëè ñóùåñòâóþùèé òàéë
/// </summary>
public TileFinalMerging tileFinalMerging = TileFinalMerging.overwriteExistingFile;
/// <summary>
/// ×èñëî òàéëîâ äëÿ íàðåçêè
/// </summary>
public ulong status_total_to_render = 0;
/// <summary>
/// ×èñëî íàðåçàííûõ è ñîõðàíåííûõ òàéëîâ
/// </summary>
public ulong status_total_created = 0;
/// <summary>
/// Ðàçìåð íàðåçàííûõ òàéëîâ
/// </summary>
public double status_total_size = 0;
/// <summary>
/// ×èñëî íàðåçàííûõ è íåñîõðàíåííûõ òàéëîâ
/// </summary>
public ulong status_total_skipped = 0;
/// <summary>
/// ×èñëî òàéëîâ, êîòîðûå íå óäàëîñü ñîõðàíèòü
/// </summary>
public ulong status_total_witherr = 0;
/// <summary>
/// ×èñëî çîí, â êîòîðûõ åñòü äûðû
/// </summary>
public ulong status_total_zoneerr = 0;
/// <summary>
/// ×èñëî íàðåçííàûõ òàéëîâ (â ò.÷. ïóñòûõ)
/// </summary>
public ulong status_total_passed = 0;
/// <summary>
/// X Tile Started to Render
/// </summary>
public int status_x_start = 0;
/// <summary>
/// Y Tile Started to Render
/// </summary>
public int status_x_current = 0;
/// <summary>
/// X Tile Current to Render
/// </summary>
public int status_x_end = 0;
/// <summary>
/// Y Tile Current to Render
/// </summary>
public int status_y_start = 0;
/// <summary>
/// X Tile Finish to Render
/// </summary>
public int status_y_current = 0;
/// <summary>
/// Y Tile Finish to Render
/// </summary>
public int status_y_end = 0;
public int[] last_enqueued = null;
public int[] last_dequeued = null;
[XmlIgnore]
public RenderView VIEW = null;
public static string holes_fileName = "";
/// <summary>
/// Îáúåêòû ïîòîêîâ
/// </summary>
private ThreadRenderObject[] rdata = null;
/// <summary>
/// Ïîòîêè
/// </summary>
private System.Threading.Thread[] threads = null;
[DllImport("kernel32.dll")]
public static extern uint GetCurrentThreadId();
[DllImport("kernel32.dll")]
public static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
[DllImport("kernel32.dll")]
public static extern bool TerminateThread(IntPtr hThread, uint dwExitCode);
/// <summary>
/// Îñòàíîâèòü âñå ïîòîêè ðåíäåðèíãà
/// </summary>
public void BreakThreads()
{
BreakThreads(false, 0);
}
/// <summary>
/// Îñòàíîâèòü âñå ïîòîêè ðåíäåðèíãà
/// </summary>
/// <param name="terminate">Óáèâàòü ïîòîê ïðèíóäèòåëüíî?</param>
public void BreakThreads(bool terminate, int millisecondsTimeout)
{
queueBreaked = terminate ? (byte)2 : (byte)1;
}
/// <summary>
/// Ïîëó÷àåì ñïèñîê ñèñòåìíûõ ThreadID
/// äëÿ âñåõ ïîòîêîâ íàðåçêè, êðîìå òîãî, êîòîðûé äîáàâëÿåò â î÷åðåäü
/// </summary>
/// <returns></returns>
public uint[] GetMultitreadThreadIDs()
{
if (threads == null) return new uint[0];
if (threads.Length == 0) return new uint[0];
List<uint> threadIDs = new List<uint>();
for (int i = 0; i < threads.Length; i++)