-
Notifications
You must be signed in to change notification settings - Fork 3
/
BenzinPriceAnalyzerForm.cs
1054 lines (936 loc) · 45.5 KB
/
BenzinPriceAnalyzerForm.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
namespace BenzinPriceAnalizer
{
public partial class BenzinPriceAnalizerForm : Form
{
public List<YPlacemark> LoadedMarks = new List<YPlacemark>();
public List<PointF> LoadedRoute = new List<PointF>();
public BenzinPriceAnalizerForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
marksFilter.SelectedIndex = 0;
bgFiles.AllowDrop = true;
bgFiles.DragDrop += bpFiles_DragDrop;
bgFiles.DragEnter += bpFiles_DragEnter;
}
private void bpFiles_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
}
private void bpFiles_DragDrop(object sender, DragEventArgs e)
{
string[] droppedfiles = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in droppedfiles)
{
bool skip = false;
foreach (object oj in bgFiles.Items)
{
LBFile f = (LBFile)oj;
if (f.filename == file) skip = true;
};
if (skip)
continue;
else
{
LBFile f = new LBFile(file);
bgFiles.Items.Add(f);
LoadMarks(f);
};
};
AddStatus("Found " + LoadedMarks.Count.ToString() + " points in " + bgFiles.Items.Count.ToString() + " file(s)");
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
delone.Enabled = bgFiles.SelectedItems.Count > 0;
delall.Enabled = bgFiles.Items.Count > 0;
}
private void delall_click(object sender, EventArgs e)
{
bgFiles.Items.Clear();
LoadedMarks.Clear();
AddStatus("Found " + LoadedMarks.Count.ToString() + " points in " + bgFiles.Items.Count.ToString() + " file(s)");
}
private void delmnu_click(object sender, EventArgs e)
{
if (bgFiles.SelectedItems.Count == 0) return;
for (int i = bgFiles.SelectedIndices.Count - 1; i >= 0; i--)
{
LBFile f = (LBFile)bgFiles.Items[bgFiles.SelectedIndices[i]];
if (LoadedMarks.Count > 0)
for (int x = LoadedMarks.Count - 1; x >= 0; x--)
if (LoadedMarks[x].fromfile == f.filename)
LoadedMarks.RemoveAt(x);
bgFiles.Items.RemoveAt(bgFiles.SelectedIndices[i]);
};
AddStatus("Found " + LoadedMarks.Count.ToString() + " points in " + bgFiles.Items.Count.ToString() + " file(s)");
}
private void addmnu_click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
foreach(string file in ofd.FileNames)
{
bool skip = false;
foreach (object oj in bgFiles.Items)
{
LBFile f = (LBFile)oj;
if (f.filename == file) skip = true;
};
if (skip) continue;
{
LBFile f = new LBFile(file);
bgFiles.Items.Add(f);
LoadMarks(f);
};
};
AddStatus("Found " + LoadedMarks.Count.ToString() + " points in " + bgFiles.Items.Count.ToString() + " file(s)");
};
ofd.Dispose();
}
private void LoadMarks(LBFile f)
{
AddStatus("Loading points from file: " + f.name);
YPlacemark[] m = ReadFile(f).ToArray();
AddStatus("Loaded " + m.Length.ToString() + " points from file: " + f.name);
LoadedMarks.AddRange(m);
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.DefaultExt = ".kml,.gpx";
ofd.Filter = "KML, GPX (*.kml;*.gpx)|*.kml;*.gpx";
if (ofd.ShowDialog() == DialogResult.OK)
loadroute(ofd.FileName);
ofd.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
dosort();
}
private void loadroute(string filename)
{
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.InstalledUICulture;
System.Globalization.NumberFormatInfo ni = (System.Globalization.NumberFormatInfo)ci.NumberFormat.Clone();
ni.NumberDecimalSeparator = ".";
AddStatus("Loading route (path) from file: " + System.IO.Path.GetFileName(filename));
routeFileBox.Text = filename;
LoadedRoute.Clear();
System.IO.FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
System.IO.StreamReader sr = new StreamReader(fs);
if (System.IO.Path.GetExtension(filename).ToLower() == ".kml")
{
string file = sr.ReadToEnd();
int si = file.IndexOf("<coordinates>");
int ei = file.IndexOf("</coordinates>");
string co = file.Substring(si + 13, ei - si - 13).Trim().Replace("\r", " ").Replace("\n", " ").Replace("\t", " ");
string[] arr = co.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
if ((arr != null) && (arr.Length > 0))
for (int i = 0; i < arr.Length; i++)
{
string[] xyz = arr[i].Split(new string[] { "," }, StringSplitOptions.None);
LoadedRoute.Add(new PointF(float.Parse(xyz[0], ni), float.Parse(xyz[1], ni)));
};
};
if (System.IO.Path.GetExtension(filename).ToLower() == ".gpx")
{
string file = sr.ReadToEnd();
int si = 0;
int ei = 0;
si = file.IndexOf("<rtept", ei);
ei = file.IndexOf(">", si);
while (si > 0)
{
string rtept = file.Substring(si + 7, ei - si - 7).Replace("\"", "").Replace("/", "").Trim();
int ssi = rtept.IndexOf("lat=");
int sse = rtept.IndexOf(" ", ssi);
if (sse < 0) sse = rtept.Length;
string lat = rtept.Substring(ssi + 4, sse - ssi - 4);
ssi = rtept.IndexOf("lon=");
sse = rtept.IndexOf(" ", ssi);
if (sse < 0) sse = rtept.Length;
string lon = rtept.Substring(ssi + 4, sse - ssi - 4);
LoadedRoute.Add(new PointF(float.Parse(lon, ni), float.Parse(lat, ni)));
si = file.IndexOf("<rtept", ei);
if (si > 0)
ei = file.IndexOf(">", si);
};
};
sr.Close();
fs.Close();
label3.Text = "Route (path) has " + LoadedRoute.Count.ToString() + " points";
marksFilter.Enabled = LoadedRoute.Count > 1;
AddStatus("Route (path) with " + LoadedRoute.Count.ToString() + " points loaded");
}
private void dosort()
{
if (LoadedMarks.Count == 0) return;
if (marksFilter.SelectedIndex == 0)
{
foreach (YPlacemark m in LoadedMarks) m.skip = false;
AddStatus("Filtered " + LoadedMarks.Count.ToString() + " points of " + LoadedMarks.Count.ToString() + " in " + bgFiles.Items.Count.ToString() + " file(s)");
return;
};
if (LoadedRoute.Count < 2) return;
double rad = (double)inrad.Value;
if (marksFilter.SelectedIndex == 1) AddStatus("Select points in " + rad.ToString() + "m at buffer zone of the path");
if (marksFilter.SelectedIndex == 2) AddStatus("Select points in " + inrad.Value.ToString() + "m at right of the path");
if (marksFilter.SelectedIndex == 3) AddStatus("Select points in " + inrad.Value.ToString() + "m at left of the path");
int notskipped = 0;
foreach (YPlacemark m in LoadedMarks)
{
m.skip = true;
double length2 = double.MaxValue;
int side2 = 0;
for (int i = 1; i < LoadedRoute.Count; i++)
{
PointF op;
int side;
double d = DistanceFromPointToLine(new PointF((float)m.x, (float)m.y), LoadedRoute[i - 1], LoadedRoute[i], out op, out side);
if (d < length2)
{
length2 = d;
side2 = side;
};
};
if (length2 <= rad)
{
if (marksFilter.SelectedIndex == 1)
m.skip = false;
else
if ((marksFilter.SelectedIndex == 2) && (side2 <= 0))
m.skip = false;
else
if ((marksFilter.SelectedIndex == 3) && (side2 > 0)) m.skip = false;
};
if (!m.skip) notskipped++;
};
AddStatus("Filtered "+notskipped.ToString()+" points of " + LoadedMarks.Count.ToString() + " in " + bgFiles.Items.Count.ToString() + " file(s)");
}
private void doSaveAsIs()
{
if (LoadedMarks.Count == 0) return;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "KMZ (*.kmz)|*.kmz";
sfd.DefaultExt = ".kmz";
if (sfd.ShowDialog() == DialogResult.OK)
{
AddStatus("Saving filtered points: " + sfd.FileName);
string title = "benzine-price analyzer data";
if (marksFilter.SelectedIndex > 0) title = "AZS " + marksFilter.Text;
InputBox("Saving", "Layer name:", ref title);
File.Delete(sfd.FileName);
System.IO.Directory.CreateDirectory(sfd.FileName);
System.IO.Directory.CreateDirectory(sfd.FileName + @"\images\");
System.IO.FileStream fs = new System.IO.FileStream(sfd.FileName + @"\doc.kml", System.IO.FileMode.Create, System.IO.FileAccess.Write);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.UTF8);
sw.WriteLine("<?xml version='1.0' encoding='UTF-8'?>");
sw.WriteLine("<kml xmlns='http://www.opengis.net/kml/2.2'><Document>");
sw.WriteLine("<name>" + title + "</name><Folder><name>" + title + "</name>");
int saved = 0;
List<string> MarksIcons = new List<string>();
foreach (YPlacemark m in LoadedMarks)
{
if (m.skip) continue;
sw.WriteLine("<Placemark><name>" + m.hintContent.Replace("<p class=price>", "").Replace("<br />", "").Replace("&","&") + "</name>");
sw.WriteLine("<description><![CDATA[" + m.balloonContent.Replace("<p class=price>", "") + "]]></description>");
if (m.iconImageHref != String.Empty)
{
string ic = m.iconImageHref.Replace("/", "-").Replace(".", "-");
if (MarksIcons.IndexOf(ic) < 0)
{
MarksIcons.Add(ic);
GrabImage(m.iconImageHref, sfd.FileName + @"\images\" + ic + ".png");
};
sw.WriteLine("<styleUrl>#" + ic + "</styleUrl>");
};
sw.WriteLine("<Point><coordinates>" + m.x.ToString().Replace(",", ".") + "," + m.y.ToString().Replace(",", ".") + ",0.0</coordinates></Point></Placemark>");
saved++;
};
sw.WriteLine("</Folder>");
if (MarksIcons.Count > 0)
foreach (string icon in MarksIcons)
{
sw.WriteLine("<Style id='" + icon + "'><IconStyle><scale>0.6</scale><Icon>");
sw.WriteLine("<href>images/" + icon + ".png</href></Icon></IconStyle></Style>");
};
sw.WriteLine("</Document></kml>");
sw.Close();
fs.Close();
CreateZip(sfd.FileName);
AddStatus("Saved " + saved.ToString() + " points of " + LoadedMarks.Count.ToString() + " and " + MarksIcons.Count.ToString() + " icons ");
};
sfd.Dispose();
}
private void doSaveGroupped()
{
if (LoadedMarks.Count == 0) return;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "KMZ (*.kmz)|*.kmz";
sfd.DefaultExt = ".kmz";
if (sfd.ShowDialog() == DialogResult.OK)
{
AddStatus("Saving points to file: " + sfd.FileName);
string title = "benzine-price analyzer data";
if (marksFilter.SelectedIndex > 0) title = marksFilter.Text;
File.Delete(sfd.FileName);
System.IO.Directory.CreateDirectory(sfd.FileName);
System.IO.Directory.CreateDirectory(sfd.FileName + @"\images\");
System.IO.FileStream fs = new System.IO.FileStream(sfd.FileName + @"\doc.kml", System.IO.FileMode.Create, System.IO.FileAccess.Write);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.UTF8);
sw.WriteLine("<?xml version='1.0' encoding='UTF-8'?>");
sw.WriteLine("<kml xmlns='http://www.opengis.net/kml/2.2'>");
sw.WriteLine("\t<Document>");
sw.WriteLine("\t\t<name>" + title + "</name>");
int saved = 0;
//////////
List<Renm> pictures = new List<Renm>();
foreach (YPlacemark m in LoadedMarks)
{
if (m.skip) continue;
string href = m.iconImageHref;
string ic = href.Replace("/", "-").Replace(".", "-");
string hint = m.hintContent.Replace("<p class=price>", "").Replace("<br />", "");
int ind = -1;
if (pictures.Count > 0) for (int i = 0; i < pictures.Count; i++) if (pictures[i].ic == ic) ind = i;
if (ind < 0)
{
pictures.Add(new Renm(ic, "Noname " + pictures.Count.ToString(), hint));
if (ic != "")
{
GrabImage(m.iconImageHref, sfd.FileName + @"\images\" + ic + ".png");
};
}
else
pictures[ind].names.Add(hint);
};
//////////
if (pictures.Count > 0)
{
KMZRebuilder.LayersRenamerForm sl = new KMZRebuilder.LayersRenamerForm();
for (int i = 0; i < pictures.Count; i++)
{
string od = pictures[i].names[0];
if (pictures[i].names.Count > 1)
for (int x = 1; x < pictures[i].names.Count; x++)
od = LCS(od, pictures[i].names[x]);
od = od.Trim();
if (od != String.Empty) pictures[i].title = od;
if (pictures[i].title.ToUpper().IndexOf("ÀÇÑ") < 0) pictures[i].title = "ÀÇÑ " + pictures[i].title;
pictures[i].title += " [" + pictures[i].names.Count.ToString()+"]";
if (marksFilter.SelectedIndex == 1) pictures[i].title += " at route";
if (marksFilter.SelectedIndex == 2) pictures[i].title += " by right";
if (marksFilter.SelectedIndex == 3) pictures[i].title += " by left";
Image im = Image.FromFile(sfd.FileName + @"\images\" + pictures[i].ic + ".png");
sl.images.Images.Add((Image)new Bitmap(im));
im.Dispose();
sl.layers.Items.Add(pictures[i].title, i);
};
if (sl.ShowDialog() == DialogResult.OK)
{
for (int i = 0; i < pictures.Count; i++)
pictures[i].title = sl.layers.Items[i].Text;
};
sl.Dispose();
};
//////////
if (pictures.Count > 0)
for (int i = 0; i < pictures.Count; i++)
{
sw.WriteLine("\t\t<Folder>");
sw.WriteLine("\t\t\t<name>" + pictures[i].title + "</name>");
foreach (YPlacemark m in LoadedMarks)
{
if (m.skip) continue;
string href = m.iconImageHref;
string ic = m.iconImageHref.Replace("/", "-").Replace(".", "-");
if (ic != pictures[i].ic) continue;
string hint = m.hintContent.Replace("<p class=price>", "").Replace("<br />", "");
string desc = m.balloonContent.Replace("<p class=price>", "");
sw.WriteLine("<Placemark><name>" + hint.Replace("&", "&") + "</name>");
sw.WriteLine("<description><![CDATA[" + desc + "]]></description>");
if (hint != String.Empty)
sw.WriteLine("<styleUrl>#" + ic + "</styleUrl>");
sw.WriteLine("<Point><coordinates>" + m.x.ToString().Replace(",", ".") + "," + m.y.ToString().Replace(",", ".") + ",0.0</coordinates></Point></Placemark>");
saved++;
};
sw.WriteLine("\t\t</Folder>");
};
if (pictures.Count > 0)
for (int i = 0; i < pictures.Count; i++)
{
sw.WriteLine("<Style id='" + pictures[i].ic + "'><IconStyle><scale>0.6</scale><Icon>");
sw.WriteLine("<href>images/" + pictures[i].ic + ".png</href></Icon></IconStyle></Style>");
};
sw.WriteLine("</Document></kml>");
sw.Close();
fs.Close();
CreateZip(sfd.FileName);
AddStatus("Saved " + saved.ToString() + " points of " + LoadedMarks.Count.ToString() + " and " + pictures.Count.ToString() + " icons ");
};
sfd.Dispose();
}
public static string LCS(string s1, string s2)
{
int[,] a = new int[s1.Length + 1, s2.Length + 1];
int u = 0, v = 0;
for (int i = 0; i < s1.Length; i++)
for (int j = 0; j < s2.Length; j++)
if (s1[i] == s2[j])
{
a[i + 1, j + 1] = a[i, j] + 1;
if (a[i + 1, j + 1] > a[u, v])
{
u = i + 1;
v = j + 1;
}
}
return s1.Substring(u - a[u, v], a[u, v]);
}
/// <summary>
/// Ðàñ÷åò ðàññòîÿíèÿ îò òî÷êè äî ëèíèè
/// </summary>
/// <param name="pt">Èñêîìàÿ òî÷êà</param>
/// <param name="lineStart">Íà÷ òî÷êà ëèíèè</param>
/// <param name="lineEnd">Êîí òî÷êà ëèíèè</param>
/// <param name="pointOnLine">òî÷êà íà ëèíèè áëèæàéøàÿ ê èñêîìîé</param>
/// <param name="side">Ñ êàêîé ñòîðîíû ëèíèè íàõîäèòñÿ èñêîìàÿ òî÷êà (+ ñëåâà, - ñïðàâà)</param>
/// <returns>ìåòðû</returns>
private static float DistanceFromPointToLine(PointF pt, PointF lineStart, PointF lineEnd, out PointF pointOnLine, out int side)
{
float dx = lineEnd.X - lineStart.X;
float dy = lineEnd.Y - lineStart.Y;
if ((dx == 0) && (dy == 0))
{
// line is a point
// ëèíèÿ ìîæåò áûòü ñ íóëåâîé äëèíîé ïîñëå àíàëèçà TRA
pointOnLine = lineStart;
side = 0;
//dx = pt.X - lineStart.X;
//dy = pt.Y - lineStart.Y;
//return Math.Sqrt(dx * dx + dy * dy);
return Utils.GetLengthMeters(pt.Y, pt.X, pointOnLine.Y, pointOnLine.X, false);
};
side = Math.Sign((lineEnd.X - lineStart.X) * (pt.Y - lineStart.Y) - (lineEnd.Y - lineStart.Y) * (pt.X - lineStart.X));
// Calculate the t that minimizes the distance.
float t = ((pt.X - lineStart.X) * dx + (pt.Y - lineStart.Y) * dy) / (dx * dx + dy * dy);
// See if this represents one of the segment's
// end points or a point in the middle.
if (t < 0)
{
pointOnLine = new PointF(lineStart.X, lineStart.Y);
dx = pt.X - lineStart.X;
dy = pt.Y - lineStart.Y;
}
else if (t > 1)
{
pointOnLine = new PointF(lineEnd.X, lineEnd.Y);
dx = pt.X - lineEnd.X;
dy = pt.Y - lineEnd.Y;
}
else
{
pointOnLine = new PointF(lineStart.X + t * dx, lineStart.Y + t * dy);
dx = pt.X - pointOnLine.X;
dy = pt.Y - pointOnLine.Y;
};
//return Math.Sqrt(dx * dx + dy * dy);
return Utils.GetLengthMeters(pt.Y, pt.X, pointOnLine.Y, pointOnLine.X, false);
}
private void GrabImage(string url, string file)
{
try
{
System.Net.HttpWebRequest wr = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://benzin-price.ru/"+url);
System.Net.WebResponse wres = wr.GetResponse();
System.IO.FileStream fs = new System.IO.FileStream(file, System.IO.FileMode.Create, System.IO.FileAccess.Write);
System.IO.Stream rs = wres.GetResponseStream();
byte[] ba = new byte[4096];
int read = -1;
while ((read = rs.Read(ba, 0, ba.Length)) > 0)
fs.Write(ba, 0, read);
wres.Close();
fs.Close();
}
catch {};
}
private List<YPlacemark> ReadFile(LBFile f)
{
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.InstalledUICulture;
System.Globalization.NumberFormatInfo ni = (System.Globalization.NumberFormatInfo)ci.NumberFormat.Clone();
ni.NumberDecimalSeparator = ".";
Regex rvar = new Regex(@"var\s([0-9a-zA-Z]{33})\s=\s([.0-9]+);");
Regex rtwo = new Regex(@"var\s([0-9a-zA-Z]{33})\s=\s([0-9a-zA-Z]{33})(.)([0-9a-zA-Z]{33});");
Regex rxy = new Regex(@"([0-9a-zA-Z]{33})(.)([0-9a-zA-Z]{33}),([0-9a-zA-Z]{33})(.)([0-9a-zA-Z]{33})");
Hashtable ht = new Hashtable();
string placemark = "";
List<YPlacemark> marks = new List<YPlacemark>();
System.IO.FileStream fs = new System.IO.FileStream(f.filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.StreamReader sr = new System.IO.StreamReader(fs);
byte fType = 0;
while (!sr.EndOfStream)
{
string ln = sr.ReadLine();
if (rvar.IsMatch(ln))
{
MatchCollection mc = rvar.Matches(ln);
string var = mc[0].Groups[1].Value;
double val = double.Parse(mc[0].Groups[2].Value,ni);
ht.Add(var, val);
};
if(rtwo.IsMatch(ln))
{
MatchCollection mc = rtwo.Matches(ln);
string var = mc[0].Groups[1].Value;
string varA = mc[0].Groups[2].Value;
string todo = mc[0].Groups[3].Value;
string varB = mc[0].Groups[4].Value;
if (todo == "*")
ht.Add(var, (double)ht[varA] * (double)ht[varB]);
if (todo == "/")
ht.Add(var, (double)ht[varA] / (double)ht[varB]);
};
// PLACEMARK
if (ln.IndexOf("ymaps.Placemark") > 0)
{
placemark += " ";
};
if ((ln.IndexOf("myCollection.add") > 0) && ((fType == 0) || (fType == 2)))
{
fType = 2;
YPlacemark jp = new YPlacemark(f.filename);
int si = placemark.IndexOf("new ymaps.Placemark([");
int ei = placemark.IndexOf("]", si);
{
string xy = placemark.Substring(si + 21, ei - si - 21);
string[] xys = xy.Split(new char[] { ',' }, 2);
double X = 0; double.TryParse(xys[1],System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out X);
double Y = 0; double.TryParse(xys[0], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out Y);
jp.x = X;
jp.y = Y;
};
{
si = placemark.IndexOf("hintContent: \"<p class=price>");
ei = placemark.IndexOf("<br />", si);
jp.hintContent = placemark.Substring(si + 29, ei - si - 29);
si = placemark.IndexOf("balloonContentBody: \"<p class=price>");
ei = placemark.IndexOf("\",\t\t\t\t\ticonContent:", si);
jp.balloonContent = placemark.Substring(si + 36, ei - si - 36);
};
{
si = placemark.IndexOf("iconImageHref: '");
ei = placemark.IndexOf("',", si);
jp.iconImageHref = placemark.Substring(si + 16, ei - si - 16);
};
placemark = "";
marks.Add(jp);
};
if ((ln.IndexOf("geoObjects.add") > 0) && ((fType == 0) || (fType == 1)))
{
fType = 1;
YPlacemark jp = new YPlacemark(f.filename);
int si = placemark.IndexOf("new ymaps.Placemark([");
int ei = placemark.IndexOf("]", si);
{
string xy = placemark.Substring(si + 21, ei - si - 21);
MatchCollection mc = rxy.Matches(xy);
string varYA = mc[0].Groups[1].Value;
string todoY = mc[0].Groups[2].Value;
string varYB = mc[0].Groups[3].Value;
string varXA = mc[0].Groups[4].Value;
string todoX = mc[0].Groups[5].Value;
string varXB = mc[0].Groups[6].Value;
double X = (double)ht[varXA] + (double)ht[varXB];
double Y = (double)ht[varYA] + (double)ht[varYB];
jp.x = X;
jp.y = Y;
};
{
si = placemark.IndexOf("hintContent: \"<p class=price>");
ei = placemark.IndexOf("<br />", si);
jp.hintContent = placemark.Substring(si + 29, ei - si - 29);
si = placemark.IndexOf("balloonContent: \"<p class=price>");
ei = placemark.IndexOf(",\t\t\t\ticonContent:", si);
jp.balloonContent = placemark.Substring(si + 32, ei - si - 32);
};
{
si = placemark.IndexOf("iconImageHref: '");
ei = placemark.IndexOf("',", si);
jp.iconImageHref = placemark.Substring(si + 16, ei - si - 16);
};
placemark = "";
marks.Add(jp);
//AddStatus(String.Format("Adding mark {3}: {0} {1} {2}", jp.Y, jp.X, jp.hintContent, marks.Count));
};
if (placemark.Length > 0)
placemark += ln;
};
sr.Close();
fs.Close();
return marks;
}
// https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples#Create_a_Zip_fromto_a_memory_stream_or_byte_array_1
public void CreateZip(string filename)
{
FileStream fsOut = File.Create(filename+".tmp");
ZipOutputStream zipStream = new ZipOutputStream(fsOut);
zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
// zipStream.Password = password; // optional. Null is the same as not setting. Required if using AES.
CompressFolder(filename + @"\", zipStream, filename.Length);
zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream
zipStream.Close();
System.IO.DirectoryInfo di = new DirectoryInfo(filename);
di.Delete(true);
File.Move(filename + ".tmp", filename);
}
// Recurses down the folder structure
//
private void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset)
{
string[] files = Directory.GetFiles(path);
foreach (string filename in files)
{
FileInfo fi = new FileInfo(filename);
string entryName = filename.Substring(folderOffset); // Makes the name in zip based on the folder
entryName = ZipEntry.CleanName(entryName); // Removes drive from name and fixes slash direction
ZipEntry newEntry = new ZipEntry(entryName);
newEntry.DateTime = fi.LastWriteTime; // Note the zip format stores 2 second granularity
// Specifying the AESKeySize triggers AES encryption. Allowable values are 0 (off), 128 or 256.
// A password on the ZipOutputStream is required if using AES.
// newEntry.AESKeySize = 256;
// To permit the zip to be unpacked by built-in extractor in WinXP and Server2003, WinZip 8, Java, and other older code,
// you need to do one of the following: Specify UseZip64.Off, or set the Size.
// If the file may be bigger than 4GB, or you do not need WinXP built-in compatibility, you do not need either,
// but the zip will be in Zip64 format which not all utilities can understand.
// zipStream.UseZip64 = UseZip64.Off;
newEntry.Size = fi.Length;
zipStream.PutNextEntry(newEntry);
// Zip the file in buffered chunks
// the "using" will close the stream even if an exception occurs
byte[] buffer = new byte[4096];
using (FileStream streamReader = File.OpenRead(filename))
{
StreamUtils.Copy(streamReader, zipStream, buffer);
}
zipStream.CloseEntry();
}
string[] folders = Directory.GetDirectories(path);
foreach (string folder in folders)
{
CompressFolder(folder, zipStream, folderOffset);
}
}
private void AddStatus(string txt)
{
status.Text += txt + "\r\n";
status.Refresh();
status.SelectionStart = status.Text.Length;
status.ScrollToCaret();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
inrad.Enabled = (marksFilter.SelectedIndex > 0);
}
private void button3_Click(object sender, EventArgs e)
{
if (!byImgs.Checked)
doSaveAsIs();
else
doSaveGroupped();
}
public static DialogResult InputBox(string title, string promptText, ref string value)
{
Form form = new Form();
Label label = new Label();
TextBox textBox = new TextBox();
Button buttonOk = new Button();
Button buttonCancel = new Button();
form.Text = title;
label.Text = promptText;
textBox.Text = value;
buttonOk.Text = "OK";
buttonCancel.Text = "Cancel";
buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = DialogResult.Cancel;
label.SetBounds(9, 20, 372, 13);
textBox.SetBounds(12, 36, 372, 20);
buttonOk.SetBounds(228, 72, 75, 23);
buttonCancel.SetBounds(309, 72, 75, 23);
label.AutoSize = true;
textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
form.ClientSize = new Size(396, 107);
form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = buttonOk;
form.CancelButton = buttonCancel;
DialogResult dialogResult = form.ShowDialog();
value = textBox.Text;
return dialogResult;
}
public static DialogResult InputBox(string title, string promptText, ref string value, Bitmap icon)
{
Form form = new Form();
Label label = new Label();
TextBox textBox = new TextBox();
Button buttonOk = new Button();
Button buttonCancel = new Button();
PictureBox picture = new PictureBox();
form.Text = title;
label.Text = promptText;
textBox.Text = value;
if (icon != null) picture.Image = icon;
picture.SizeMode = PictureBoxSizeMode.StretchImage;
buttonOk.Text = "OK";
buttonCancel.Text = "Cancel";
buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = DialogResult.Cancel;
label.SetBounds(9, 20, 372, 13);
textBox.SetBounds(12, 36, 372, 20);
buttonOk.SetBounds(228, 72, 75, 23);
buttonCancel.SetBounds(309, 72, 75, 23);
picture.SetBounds(12, 72, 22, 22);
label.AutoSize = true;
textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
form.ClientSize = new Size(396, 107);
form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel, picture });
form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = buttonOk;
form.CancelButton = buttonCancel;
DialogResult dialogResult = form.ShowDialog();
if (picture.Image != null) picture.Image.Dispose();
form.Dispose();
value = textBox.Text;
return dialogResult;
}
}
public class YPlacemark
{
public double x = 0;
public double y = 0;
public string hintContent = "";
public string balloonContent = "";
public string iconImageHref = "";
public bool skip = false;
public string fromfile = "";
public YPlacemark(string filename)
{
this.fromfile = filename;
}
}
public class Renm
{
public string ic;
public string title;
public List<string> names = new List<string>();
public Renm(string image, string title, string name)
{
this.ic = image;
this.title = title;
this.names.Add(name);
}
}
public class LBFile
{
public string filename = "";
public string name {
get
{
return System.IO.Path.GetFileName(filename);
}
}
public LBFile(string filename)
{
this.filename = filename;
}
public override string ToString()
{
return name;
}
}
public class Utils
{
// Ðàññ÷åò ðàññòîÿíèÿ
#region LENGTH
public static float GetLengthMeters(double StartLat, double StartLong, double EndLat, double EndLong, bool radians)
{
// use fastest
float result = (float)GetLengthMetersD(StartLat, StartLong, EndLat, EndLong, radians);
if (float.IsNaN(result))
{
result = (float)GetLengthMetersC(StartLat, StartLong, EndLat, EndLong, radians);
if (float.IsNaN(result))
{
result = (float)GetLengthMetersE(StartLat, StartLong, EndLat, EndLong, radians);
if (float.IsNaN(result))
result = 0;
};
};
return result;
}
// Slower
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));
}
// Slowest
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);
}
// Average
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.