-
Notifications
You must be signed in to change notification settings - Fork 1
/
OSMConverter.cs
3863 lines (3461 loc) · 168 KB
/
OSMConverter.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
// Must Set !!! .Net Framework >= 4.5
// Mostly for: NodesXYIndex
/*
<configuration>
<runtime>
<gcAllowVeryLargeObjects enabled="true" />
</runtime>
</configuration>
*/
using System;
using System.Drawing;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
namespace OSM2SHP
{
public class OSMConverter
{
public const string _MainEngine = "dkxce.OSM2SHP.Converter/21.12.10.51-win32";
public const string _MainMode = "NodesXYIndex Arrays Dicts";
public const int MAX_FIELD_LENGTH = 250;
public string _osmFile = null;
public string _dbfFile = null;
public Config _config = null;
public bool IsXML { get { return Path.GetExtension(_osmFile).ToLower() == ".osm"; } }
public bool IsPBF { get { return Path.GetExtension(_osmFile).ToLower() == ".pbf"; } }
public Regex AggrRegex = null;
public Regex hasTextRegex = null;
public Dictionary<long, FLID> linesFLID = new Dictionary<long, FLID>();
private NodesXYIndex nodes_ndi = null;
public long NDILength { get { return nodes_ndi == null ? 0 : nodes_ndi.Length; } }
public string NDIArraysInfo { get { return nodes_ndi == null ? "0" : nodes_ndi.ArraysInfo; } }
public long LFLLength { get { return 48 * linesFLID.Count + 100; } }
public long JNSLength { get { return 16 * relaJJ.Count + 100; } }
private DBFWriter points_dbf = null;
private SHPWriter points_shp = null;
private SHXWriter points_shx = null;
private DBFWriter lines_dbf = null;
private SHPWriter lines_shp = null;
private SHXWriter lines_shx = null;
private DBFWriter areas_dbf = null;
private SHPWriter areas_shp = null;
private SHXWriter areas_shx = null;
private DBFWriter relat_dbf = null;
private DBFWriter relam_dbf = null;
private DBFWriter relaj_dbf = null;
private Dictionary<long, long> relaJJ = new Dictionary<long, long>();
private JSONDict jsd = new JSONDict();
private TYPE_MAP tmap = new TYPE_MAP();
private long points_total = 0;
private long points_procc = 0;
private long points_writd = 0;
private long points_unicl = 0;
private long points_empty = 0;
private long points_from_nodes = 0;
private long points_from_ways = 0;
private long points_form_lines = 0;
private long points_from_areas = 0;
private long points_skb_flt = 0;
private long points_skb_sel = 0;
private long ways_total = 0;
private long ways_procc = 0;
private long ways_writd = 0;
private long ways_as_both = 0;
private long ways_as_lines = 0;
private long ways_as_areas = 0;
private long ways_writd_lines = 0;
private long ways_writd_areas = 0;
private long ways_skipd_lines = 0;
private long ways_skipd_areas = 0;
private long ways_skipd = 0;
private long ways_by_rels_ttl = 0;
private long ways_by_rels_lns = 0;
private long ways_by_rels_ars = 0;
private long relations_total = 0;
private long relations_procc = 0;
private long relations_writd = 0;
private long relations_mwrtd = 0;
private long relations_joins_LNS = 0;
private long relations_joins_TV = 0;
private long relations_joins_FL = 0;
private long relations_joins_ERR = 0;
private long relations_joins_VLD = 0;
private long relations_joins_COPY = 0;
private long nodes_writed = 0;
public long NodesWrited { get { return nodes_writed; } }
private long nodes_fsize = 0;
public long NodesFSize { get { return nodes_fsize; } }
private string nodes_fname = "";
public string NodesFName { get { return nodes_fname; } }
private float nodes_percent = 0;
public float NodesPercentage { get { return nodes_percent; } }
private float nodes_fieldswr = 0;
public float NodesFieldsWrited { get { return nodes_percent; } }
private long lines_tosplit_writed = 0;
public long LinesToSplitWrited { get { return lines_tosplit_writed; } }
private long lines_tosplit_fsize = 0;
public long LinesToSplitFSize { get { return lines_tosplit_fsize; } }
private string lines_tosplit_fname = "";
public string LinesToSplitFName { get { return lines_tosplit_fname; } }
private float lines_tosplit_percent = 0;
public float LinesToSplitPercentage { get { return lines_tosplit_percent; } }
private long files_points_wrtd = 0;
private List<string> files_points_wrts = new List<string>();
private List<string> files_points_wrtf = new List<string>();
private List<long> files_points_wrtc = new List<long>();
private List<long> files_points_sizes = new List<long>();
public long PointsTotal { get { return points_total; } }
public long PointsProcessed { get { return points_procc; } }
public long PointsWrited { get { return points_writd; } }
public long PointsUnical { get { return points_unicl; } }
public long PointsEmpty { get { return points_empty; } }
public long PointsFromNodes { get { return points_from_nodes; } }
public long PointsFromWays { get { return points_from_ways; } }
public long PointsFromLines { get { return points_form_lines; } }
public long PointsFromAreas { get { return points_from_areas; } }
private long files_lines_wrtd = 0;
private List<string> files_lines_wrts = new List<string>();
private List<string> files_lines_wrtf = new List<string>();
private List<long> files_lines_wrtc = new List<long>();
private List<long> files_lines_sizes = new List<long>();
private long files_areas_wrtd = 0;
private List<string> files_areas_wrts = new List<string>();
private List<string> files_areas_wrtf = new List<string>();
private List<long> files_areas_wrtc = new List<long>();
private List<long> files_areas_sizes = new List<long>();
private long files_relations_wrtd = 0;
private List<string> files_relations_wrtt = new List<string>();
private List<string> files_relations_wrtm = new List<string>();
private List<long> files_relations_wrt_rc = new List<long>();
private List<long> files_relations_wrt_mc = new List<long>();
private List<long> files_relations_sizes = new List<long>();
private long files_joins_wrtd = 0;
private List<string> files_joins_wrtf = new List<string>();
private List<long> files_joins_wrtl = new List<long>();
private List<long> files_joins_wrtTV = new List<long>();
private List<long> files_joins_wrtFL = new List<long>();
private List<long> files_joins_wrtER = new List<long>();
private List<long> files_joins_sizes = new List<long>();
public long WaysTotal { get { return ways_total; } }
public long WaysProcessed { get { return ways_procc; } }
public long WaysWrited { get { return ways_writd; } }
public long WaysAsBoth { get { return ways_as_both; } }
public long WaysAsLines { get { return ways_as_lines; } }
public long WaysAsAreas { get { return ways_as_areas; } }
public long WaysWritedAsLines { get { return ways_writd_lines; } }
public long WaysWritedAsAreas { get { return ways_writd_areas; } }
public long WaysSkippedFilterLines { get { return ways_skipd_lines; } }
public long WaysSkippedFilterAreas { get { return ways_skipd_areas; } }
public long WaysSkippedFilter { get { return ways_skipd; } }
public long WaysByRelationsTotal { get { return ways_by_rels_ttl; } }
public long WaysByRelationsLines { get { return ways_by_rels_lns; } }
public long WaysByRelationsAreas { get { return ways_by_rels_ars; } }
public long RelationsTotal { get { return relations_total; } }
public long RelationsProcessed { get { return relations_procc; } }
public long RelationsWrited { get { return relations_writd; } }
public long RelationsMemsWrited { get { return relations_mwrtd; } }
public long NodesSkippedByFilter { get { return points_skb_flt; } }
public long NodesSkippedBySel { get { return points_skb_sel; } }
public long FilesPointsWrited { get { return files_points_wrtd; } }
public string[] FilesPointsWritedShapes { get { return files_points_wrts.ToArray(); } }
public string[] FilesPointsWritedDBFs { get { return files_points_wrtf.ToArray(); } }
public long[] FilesPointsWritedPoints { get { return files_points_wrtc.ToArray(); } }
public long[] FilesPointsWritedSizes { get { return files_points_sizes.ToArray(); } }
public long FilesLinesWrited { get { return files_lines_wrtd; } }
public string[] FilesLinesWritedShapes { get { return files_lines_wrts.ToArray(); } }
public string[] FilesLinesWritedDBFs { get { return files_lines_wrtf.ToArray(); } }
public long[] FilesLinesWritedLines { get { return files_lines_wrtc.ToArray(); } }
public long[] FilesLinesWritedSizes { get { return files_lines_sizes.ToArray(); } }
public long FilesAreasWrited { get { return files_areas_wrtd; } }
public string[] FilesAreasWritedShapes { get { return files_areas_wrts.ToArray(); } }
public string[] FilesAreasWritedDBFs { get { return files_areas_wrtf.ToArray(); } }
public long[] FilesAreasWritedAreas { get { return files_areas_wrtc.ToArray(); } }
public long[] FilesAreasWritedSizes { get { return files_areas_sizes.ToArray(); } }
public long FilesRelationsWrited { get { return files_relations_wrtd; } }
public string[] FilesRelationsWritedMain { get { return files_relations_wrtt.ToArray(); } }
public string[] FilesRelationsWritedMems { get { return files_relations_wrtm.ToArray(); } }
public long[] FilesRelationsWritedRelsCounts { get { return files_relations_wrt_rc.ToArray(); } }
public long[] FilesRelationsWritedMemsCounts { get { return files_relations_wrt_mc.ToArray(); } }
public long[] FilesRelationsWritedSizes { get { return files_relations_sizes.ToArray(); } }
public long FilesJoinsWrited { get { return files_joins_wrtd; } }
public string[] FilesJoinsWritedDBFs { get { return files_joins_wrtf.ToArray(); } }
public long[] FilesJoinsWritedLines { get { return files_joins_wrtl.ToArray(); } }
public long[] FilesJoinsWritedTV { get { return files_joins_wrtTV.ToArray(); } }
public long[] FilesJoinsWritedFL { get { return files_joins_wrtFL.ToArray(); } }
public long[] FilesJoinsWritedER { get { return files_joins_wrtER.ToArray(); } }
public long[] FilesJoinsWritedSizes { get { return files_joins_sizes.ToArray(); } }
public long JoinsRestrictionsLNS { get { return relations_joins_LNS; } }
public long JoinsRestrictionsTV { get { return relations_joins_TV; } }
public long JoinsRestrictionsFL { get { return relations_joins_FL; } }
public long JoinsRestrictionsVLD { get { return relations_joins_VLD; } }
public long JoinsRestrictionsERR { get { return relations_joins_ERR; } }
public long JoinsRestrictionsCOPY { get { return relations_joins_COPY; } }
public Dictionary<string, uint> tags_skipped = new Dictionary<string, uint>();
public int tags_max_count = 0;
public int tags_aggrf_used = 0;
public int tags_aggra_count = 0;
public int tags_string_maxl = 0;
public int MaxFileRecords = 500000; // default = 500000
public delegate void OnProgress(object sender, float percentage, bool processing);
public delegate void OnError(object sender, Exception error);
public event OnProgress onProgress;
public event OnError onError;
public event EventHandler onDone;
public event EventHandler onStart;
private ApplyFilterScript apf = null;
public byte PointsFieldsCount
{
get
{
if (points_dbf == null) return 0;
return points_dbf.FieldsCount;
}
}
public ushort PointsRecordSize
{
get
{
if (points_dbf == null) return 0;
return points_dbf.RecordSize;
}
}
public byte LinesFieldsCount
{
get
{
if (lines_dbf == null) return 0;
return lines_dbf.FieldsCount;
}
}
public byte AreasFieldsCount
{
get
{
if (areas_dbf == null) return 0;
return areas_dbf.FieldsCount;
}
}
public byte RelationsFieldsCount
{
get
{
if (relat_dbf == null) return 0;
return relat_dbf.FieldsCount;
}
}
public byte RelationsMembersFieldsCount
{
get
{
if (relam_dbf == null) return 0;
return relam_dbf.FieldsCount;
}
}
public byte RelationsJoinsFieldsCount
{
get
{
if (relaj_dbf == null) return 0;
return relaj_dbf.FieldsCount;
}
}
public ushort LinesRecordSize
{
get
{
if (lines_dbf == null) return 0;
return lines_dbf.RecordSize;
}
}
public ushort AreasRecordSize
{
get
{
if (areas_dbf == null) return 0;
return areas_dbf.RecordSize;
}
}
public ushort RelationsRecordSize
{
get
{
if (relat_dbf == null) return 0;
return relat_dbf.RecordSize;
}
}
public ushort RelationsMembersRecordSize
{
get
{
if (relam_dbf == null) return 0;
return relam_dbf.RecordSize;
}
}
public ushort RelationsJoinsRecordSize
{
get
{
if (relaj_dbf == null) return 0;
return relaj_dbf.RecordSize;
}
}
private void PrepareForConvert()
{
this.AggrRegex = null;
this.hasTextRegex = null;
if(!String.IsNullOrEmpty(_config.aggrRegex))
try { this.AggrRegex = new Regex(_config.aggrRegex, RegexOptions.IgnoreCase); } catch (Exception ex) { this.AggrRegex = null; };
if (!String.IsNullOrEmpty(_config.hasText))
try { this.hasTextRegex = new Regex(_config.hasText, RegexOptions.IgnoreCase); } catch (Exception ex) { this.hasTextRegex = null; };
_config.PrepareForRead();
points_total = 0;
points_procc = 0;
points_writd = 0;
points_unicl = 0;
points_empty = 0;
points_skb_flt = 0;
points_skb_sel = 0;
files_points_wrtd = 0;
files_points_wrts.Clear();
files_points_wrtf.Clear();
files_points_wrtc.Clear();
files_lines_wrtd = 0;
files_lines_wrts.Clear();
files_lines_wrtf.Clear();
files_lines_wrtc.Clear();
files_areas_wrtd = 0;
files_areas_wrts.Clear();
files_areas_wrtf.Clear();
files_areas_wrtc.Clear();
files_relations_wrtd = 0;
files_relations_wrtt.Clear();
files_relations_wrtm.Clear();
files_relations_wrt_rc.Clear();
files_relations_wrt_mc.Clear();
files_joins_wrtd = 0;
files_joins_wrtf.Clear();
files_joins_wrtl.Clear();
files_joins_wrtTV.Clear();
files_joins_wrtFL.Clear();
files_joins_wrtER.Clear();
tags_skipped.Clear();
tags_aggrf_used = 0;
tags_max_count = 0;
tags_aggra_count = 0;
tags_string_maxl = 0;
nodes_writed = 0;
nodes_fsize = 0;
nodes_fname = "";
nodes_percent = 0;
apf = null;
if(!String.IsNullOrEmpty(_config.scriptFilter))
try
{
string code = ApplyFilterScript.GetCode(_config.scriptFilter);
System.Reflection.Assembly asm = CSScriptLibrary.CSScript.LoadCode(code, null);
CSScriptLibrary.AsmHelper script = new CSScriptLibrary.AsmHelper(asm);
apf = (ApplyFilterScript)script.CreateObject("OSM2SHP.Script");
apf.Converter = this;
apf.args = Program.exeargs;
}
catch { };
}
public OSMConverter(Config config)
{
this._config = config;
this._osmFile = config.inputFileName;
this._dbfFile = config.outputFileName;
}
public bool ProcWaysSure
{
get
{
return (_config.processWays);//(_config.processWays) && ((_config.processLines && (_config.selector != 2)) || (_config.processAreas && (_config.selector != 2)) || (_config.processCentroids > 0));
}
}
public bool ProcLineSure
{
get
{
return _config.processLines && (_config.selector != 2); // (_config.processWays) && _config.processLines && (_config.selector != 2);
}
}
public bool ProcAreaSure
{
get
{
return _config.processAreas && (_config.selector != 2); // (_config.processWays) && _config.processAreas && (_config.selector != 2);
}
}
public bool ProcRelsSure
{
get
{
return _config.processRelations;
}
}
public bool ProcJoins
{
get
{
return _config.processRelations && _config.relationsAsJoins;
}
}
public void Convert()
{
PrepareForConvert();
if (!String.IsNullOrEmpty(_config.sortAggTagsPriority))
{
if (_config.sortAggTagsPriority == "íåò")
{
}
else if (_config.sortAggTagsPriority == "äà")
tagsComparer = new TagsComparer("");
else
tagsComparer = new TagsComparer(_config.sortAggTagsPriority);
};
files_points_wrtd++;
if (this.ProcLineSure) files_lines_wrtd++;
if (this.ProcAreaSure) files_areas_wrtd++;
if (this.ProcRelsSure) files_relations_wrtd++;
if (this.ProcJoins) files_joins_wrtd++;
string _ndsi = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + "[nodes].idx";
string _ndss = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + "[NODES]";
string _ndsp = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + "[L_SPL]";
string _dbfp = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[P{0:0000}]", files_points_wrtd) + ".dbf";
string _shpp = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[P{0:0000}]", files_points_wrtd) + ".shp";
string _shxp = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[P{0:0000}]", files_points_wrtd) + ".shx";
string _prjp = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[P{0:0000}]", files_points_wrtd) + ".prj";
string _dbfl = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[L{0:0000}]", files_lines_wrtd) + ".dbf";
string _shpl = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[L{0:0000}]", files_lines_wrtd) + ".shp";
string _shxl = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[L{0:0000}]", files_lines_wrtd) + ".shx";
string _prjl = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[L{0:0000}]", files_lines_wrtd) + ".prj";
string _dbfa = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[A{0:0000}]", files_areas_wrtd) + ".dbf";
string _shpa = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[A{0:0000}]", files_areas_wrtd) + ".shp";
string _shxa = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[A{0:0000}]", files_areas_wrtd) + ".shx";
string _prja = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[A{0:0000}]", files_areas_wrtd) + ".prj";
string _relat = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[R{0:0000}]", files_relations_wrtd) + ".dbf";
string _relam = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[M{0:0000}]", files_relations_wrtd) + ".dbf";
string _relaj = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[J{0:0000}]", files_joins_wrtd) + ".dbf";
files_points_wrts.Add(Path.GetFileName(_shpp));
files_points_wrtf.Add(Path.GetFileName(_dbfp));
files_points_wrtc.Add(0);
files_points_sizes.Add(0);
if (this.ProcLineSure)
{
files_lines_wrts.Add(Path.GetFileName(_shpl));
files_lines_wrtf.Add(Path.GetFileName(_dbfl));
files_lines_wrtc.Add(0);
files_lines_sizes.Add(0);
};
if (this.ProcAreaSure)
{
files_areas_wrts.Add(Path.GetFileName(_shpa));
files_areas_wrtf.Add(Path.GetFileName(_dbfa));
files_areas_wrtc.Add(0);
files_areas_sizes.Add(0);
};
if (this.ProcRelsSure)
{
files_relations_wrtt.Add(Path.GetFileName(_relat));
files_relations_wrtm.Add(Path.GetFileName(_relam));
files_relations_wrt_rc.Add(0);
files_relations_wrt_mc.Add(0);
files_relations_sizes.Add(0);
}
if(this.ProcJoins)
{
files_joins_wrtf.Add(Path.GetFileName(_relaj));
files_joins_wrtl.Add(0);
files_joins_wrtTV.Add(0);
files_joins_wrtFL.Add(0);
files_joins_wrtER.Add(0);
files_joins_sizes.Add(0);
};
try
{
string dir = Path.GetDirectoryName(_dbfp);
Directory.CreateDirectory(dir);
if (this.ProcWaysSure)
nodes_ndi = _config.useNotInMemoryIndexFile ? new NodesXYIndex(_ndsi) : new NodesXYIndex();
linesFLID.Clear();
points_dbf = new DBFWriter(_dbfp, FileMode.Create, _config.dbfCodePage);
points_dbf.ShortenFieldNameMode = _config.dbfMoreCompatible == 1;
points_shp = SHPWriter.CreatePointsFile(_shpp);
points_shx = SHXWriter.CreatePointsIndex(_shxp);
PRJWriter.CreateProjFile(_prjp);
if (this.ProcLineSure)
{
lines_dbf = new DBFWriter(_dbfl, FileMode.Create, _config.dbfCodePage);
lines_dbf.ShortenFieldNameMode = _config.dbfMoreCompatible == 1;
lines_shp = SHPWriter.CreateLinesFile(_shpl);
lines_shx = SHXWriter.CreateLinesIndex(_shxl);
PRJWriter.CreateProjFile(_prjl);
};
if (this.ProcAreaSure)
{
areas_dbf = new DBFWriter(_dbfa, FileMode.Create, _config.dbfCodePage);
areas_dbf.ShortenFieldNameMode = _config.dbfMoreCompatible == 1;
areas_shp = SHPWriter.CreateAreasFile(_shpa);
areas_shx = SHXWriter.CreateAreasIndex(_shxa);
PRJWriter.CreateProjFile(_prja);
};
if (this.ProcRelsSure)
{
relat_dbf = new DBFWriter(_relat, FileMode.Create, _config.dbfCodePage);
relat_dbf.ShortenFieldNameMode = _config.dbfMoreCompatible == 1;
relam_dbf = new DBFWriter(_relam, FileMode.Create, _config.dbfCodePage);
relam_dbf.ShortenFieldNameMode = _config.dbfMoreCompatible == 1;
};
if(this.ProcJoins)
{
relaj_dbf = new DBFWriter(_relaj, FileMode.Create, _config.dbfCodePage);
relaj_dbf.ShortenFieldNameMode = _config.dbfMoreCompatible == 1;
};
}
catch (Exception ex)
{
if (nodes_ndi != null) nodes_ndi.Close();
linesFLID.Clear();
if (points_dbf != null) points_dbf.Close();
if (points_shp != null) points_shp.Close();
if (points_shx != null) points_shx.Close();
if (this.ProcLineSure)
{
if (lines_dbf != null) lines_dbf.Close();
if (lines_shp != null) lines_shp.Close();
if (lines_shx != null) lines_shx.Close();
};
if (this.ProcAreaSure)
{
if (areas_dbf != null) areas_dbf.Close();
if (areas_shp != null) areas_shp.Close();
if (areas_shx != null) areas_shx.Close();
};
if (this.ProcRelsSure)
{
if (relat_dbf != null) relat_dbf.Close();
if (relam_dbf != null) relam_dbf.Close();
};
if (this.ProcJoins)
{
if (relaj_dbf != null) relaj_dbf.Close();
};
throw ex;
};
WritePointsHeaders(_config.dbfDopFields.ToArray());
files_points_sizes[files_points_wrtc.Count - 1] = points_dbf.Length + points_shx.Length + points_shp.Length + 204;
if (this.ProcLineSure)
{
WriteLinesHeaders(_config.dbfDopFields.ToArray(), _config.addFirstAndLastNodesIdToLines);
files_lines_sizes[files_lines_wrtc.Count - 1] = lines_dbf.Length + lines_shx.Length + lines_shp.Length + 204;
};
if (this.ProcAreaSure)
{
WriteAreasHeaders(_config.dbfDopFields.ToArray());
files_areas_sizes[files_areas_wrtc.Count - 1] = areas_dbf.Length + areas_shx.Length + areas_shp.Length + 204;
};
if (this.ProcRelsSure)
{
WriteRelationsHeaders(_config.dbfDopFields.ToArray());
WriteRelMemsHeaders(new string[0]);
files_relations_sizes[files_relations_wrt_rc.Count - 1] = relat_dbf.Length + relam_dbf.Length;
};
if (this.ProcJoins)
{
WriteJoinsHeaders();
files_joins_sizes[files_joins_wrtl.Count - 1] = relaj_dbf.Length;
};
if ((_config.selector == 0) || (_config.selector == 3))
{
jsd = new JSONDict();
string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + @"\json_mp_points", "*.json");
foreach (string file in files) jsd.AddFile(file);
};
if ((_config.selector == 1) || (_config.selector == 4))
{
jsd = new JSONDict();
string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + @"\json_garmin_points", "*.json");
foreach (string file in files) jsd.AddFile(file);
};
if (_config.selector == 14)
{
tmap = new TYPE_MAP();
string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + @"\xml_selector", "*.xml");
foreach (string file in files) tmap.Import(file);
tmap.Init();
};
if (onStart != null)
onStart(this, new EventArgs());
Exception err = null;
try
{
if (IsPBF) ReadPBF();
if (IsXML) ReadXML();
}
catch (Exception ex)
{
err = ex;
};
if (this.ProcWaysSure)
{
if (_config.saveLineNodesShape)
{
try { WriteNodesShape(_ndss); } catch { };
try { WriteLinesToSplitDbf(_ndsp); } catch { };
};
};
try { Progress(1, false); } catch { }; // 100% DONE STATUS
if (this.ProcWaysSure) nodes_ndi.Close();
linesFLID.Clear();
points_dbf.Close();
points_shp.Close();
points_shx.Close();
if (this.ProcLineSure)
{
lines_dbf.Close();
lines_shp.Close();
lines_shx.Close();
};
if (this.ProcAreaSure)
{
areas_dbf.Close();
areas_shp.Close();
areas_shx.Close();
};
if (this.ProcRelsSure)
{
relat_dbf.Close();
relam_dbf.Close();
};
if (this.ProcJoins)
{
relaj_dbf.Close();
};
if (err == null)
Done();
else
Error(err);
}
private void WriteNodesShape(string fileName)
{
if (nodes_ndi.Length == 0) return;
DBFWriter nodes_dbf = new DBFWriter(fileName + ".dbf", FileMode.Create, _config.dbfCodePage);
nodes_dbf.ShortenFieldNameMode = _config.dbfMoreCompatible == 1;
SHPWriter nodes_shp = SHPWriter.CreatePointsFile(fileName + ".shp");
SHXWriter nodes_shx = SHXWriter.CreatePointsIndex(fileName + ".shx");
PRJWriter.CreateProjFile(fileName + ".prj");
FieldInfos fields = new FieldInfos();
fields.Add("INDEX", 012, 'N');
fields.Add("NODE_ID", 018, 'N');
fields.Add("L_COUNT", 002, 'N');
fields.Add("ML_COUNT", 002, 'N');
nodes_fieldswr = 4;
if (nodes_ndi.InMemory)
{
fields.Add("MLINES", MAX_FIELD_LENGTH, 'C');
nodes_fieldswr++;
};
nodes_dbf.WriteHeader(fields);
long nodes_listed = 0;
float nodes_ttl = (float)nodes_ndi.Count;
nodes_writed = 0;
nodes_fsize = 0;
nodes_percent = 0;
nodes_fname = Path.GetFileNameWithoutExtension(fileName + ".dbf");
if(nodes_ndi.InMemory)
{
foreach (Dictionary<long, NodesXYIndex.LatLonLCo> dict in nodes_ndi.Dicts)
{
foreach (KeyValuePair<long, NodesXYIndex.LatLonLCo> el in dict)
{
nodes_listed++;
nodes_percent = ((float)nodes_listed) / nodes_ttl;
// â lco õðàíèì ÷èñëî ëèíèé, ïåðåñåêàþùèõ òî÷êó
// 00001111 - åñëè òî÷êà ÿâëÿåòñÿ íà÷àëîì èëè êîíöîì ëèíèè (äî 15 ëèíèé)
// 11110000 - åñëè òî÷êà íå ÿâëÿåòñÿ íà÷àëîì èëè êîíöîì ëèíèè (äî 15 ëèíèé)
byte se = (byte)(el.Value.lco & 0x0F);
byte md = (byte)(el.Value.lco & 0xF0);
// åñëè ÷èñëî ïðîõîäÿùèõ òî÷åê ÷åðåç ëèíèþ â ñåðåäèíå > 1
// èëè åñëè îäíà ëèíèÿ ïðîõîäèò ÷åðåç ñåðåäèíó, à äðóãèå íåò
//if ((md > 0x10) || ((md == 0x10) && (se > 0x00)))
if (md > 0x00) // åñëè òîëüêî îäíà ëèíèÿ ÷åðåç ñåðåäèíó - âîçìîæåí çàïðåò ðàçâîðîòà
{
byte lco = (byte)(se + (md >> 4));
byte mlco = (byte)(md >> 4);
Dictionary<string, object> rec = new Dictionary<string, object>();
rec.Add("INDEX", nodes_writed);
rec.Add("NODE_ID", el.Key);
rec.Add("L_COUNT", lco);
rec.Add("ML_COUNT", mlco);
rec.Add("MLINES", el.Value.GetLines());
nodes_dbf.WriteRecord(rec);
nodes_shx.WritePointIndex((int)nodes_shp.Position);
nodes_shp.WritePoint(el.Value.lon, el.Value.lat);
nodes_writed++;
nodes_fsize = nodes_dbf.Length + nodes_shx.Length + nodes_shp.Length + 204;
Progress(0.99f, true);
};
};
};
}
else
{
nodes_ndi.STR.Position = 0;
while (nodes_ndi.STR.Position < nodes_ndi.STR.Length)
{
nodes_listed++;
byte[] buff = new byte[25];
nodes_ndi.STR.Read(buff, 0, buff.Length);
nodes_percent = (float)nodes_listed / nodes_ttl;
// â lco õðàíèì ÷èñëî ëèíèé, ïåðåñåêàþùèõ òî÷êó
// 00001111 - åñëè òî÷êà ÿâëÿåòñÿ íà÷àëîì èëè êîíöîì ëèíèè (äî 15 ëèíèé)
// 11110000 - åñëè òî÷êà íå ÿâëÿåòñÿ íà÷àëîì èëè êîíöîì ëèíèè (äî 15 ëèíèé)
byte se = (byte)(buff[24] & 0x0F);
byte md = (byte)(buff[24] & 0xF0);
//if ((md > 0x10) || ((md == 0x10) && (se > 0x00)))
if (md > 0x00) // åñëè òîëüêî îäíà ëèíèÿ ÷åðåç ñåðåäèíó - âîçìîæåí çàïðåò ðàçâîðîòà
{
byte lco = (byte)(se + (md >> 4));
byte mlco = (byte)(md >> 4);
NodesXYIndex.IdLatLon res = new NodesXYIndex.IdLatLon(BitConverter.ToInt64(buff, 0), BitConverter.ToDouble(buff, 8), BitConverter.ToDouble(buff, 16), buff[24]);
Dictionary<string, object> rec = new Dictionary<string, object>();
rec.Add("INDEX", nodes_writed);
rec.Add("NODE_ID", res.id);
rec.Add("L_COUNT", lco);
rec.Add("ML_COUNT", mlco);
nodes_dbf.WriteRecord(rec, 0, 0, 0, 0);
nodes_shx.WritePointIndex((int)nodes_shp.Position);
nodes_shp.WritePoint(res.lon, res.lat);
nodes_writed++;
nodes_fsize = nodes_dbf.Length + nodes_shx.Length + nodes_shp.Length + 204;
Progress(0.99f, true);
};
};
};
nodes_dbf.Close();
nodes_shp.Close();
nodes_shx.Close();
}
private void WriteLinesToSplitDbf(string fileName)
{
if (nodes_ndi.Length == 0) return;
Dictionary<long, int[]> dal2s = nodes_ndi.DictAsLinesToSplit();
if (dal2s == null) return;
if (dal2s.Count == 0) return;
DBFWriter l2spl_dbf = new DBFWriter(fileName + ".dbf", FileMode.Create, _config.dbfCodePage);
l2spl_dbf.ShortenFieldNameMode = _config.dbfMoreCompatible == 1;
FieldInfos fields = new FieldInfos();
fields.Add("INDEX", 012, 'N');
fields.Add("LINE_ID", 018, 'N');
fields.Add("SPLITCO", 004, 'N');
fields.Add("PNT_NUM", MAX_FIELD_LENGTH, 'C');
l2spl_dbf.WriteHeader(fields);
lines_tosplit_writed = 0;
lines_tosplit_fsize = 0;
lines_tosplit_percent = 0;
lines_tosplit_fname = Path.GetFileNameWithoutExtension(fileName + ".dbf");
long lines_tosplit_listed = 0;
float lines_tosplit_ttl = (float)dal2s.Count;
foreach (KeyValuePair<long, int[]> el in dal2s)
{
lines_tosplit_listed++;
lines_tosplit_percent = ((float)lines_tosplit_listed) / lines_tosplit_ttl;
string nums = "";
foreach(int num in el.Value)
nums += (nums.Length > 0 ? ";" : "") + num.ToString();
Dictionary<string, object> rec = new Dictionary<string, object>();
rec.Add("INDEX", lines_tosplit_writed);
rec.Add("LINE_ID", el.Key);
rec.Add("SPLITCO", el.Value.Length);
rec.Add("PNT_NUM", nums);
l2spl_dbf.WriteRecord(rec);
lines_tosplit_writed++;
lines_tosplit_fsize = l2spl_dbf.Length + 204;
Progress(0.99f, true);
};
l2spl_dbf.Close();
}
private void CheckFilesFull()
{
if ((points_dbf.WritedRecords >= MaxFileRecords) || (points_shp.Position >= (2147483648 - 20 * 1024 * 1024)) || (points_dbf.Position >= (2147483648 - 20 * 1024 * 1024))) // 20 MB
{
points_dbf.Close();
points_shp.Close();
points_shx.Close();
files_points_wrtd++;
string _dbff = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[P{0:0000}]", files_points_wrtd) + ".dbf";
string _shpf = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[P{0:0000}]", files_points_wrtd) + ".shp";
string _prjf = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[P{0:0000}]", files_points_wrtd) + ".prj";
string _shxf = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[P{0:0000}]", files_points_wrtd) + ".shx";
files_points_wrts.Add(Path.GetFileName(_shpf));
files_points_wrtf.Add(Path.GetFileName(_dbff));
files_points_wrtc.Add(0);
files_points_sizes.Add(0);
points_dbf = new DBFWriter(_dbff, FileMode.Create, _config.dbfCodePage);
points_dbf.ShortenFieldNameMode = _config.dbfMoreCompatible == 1;
points_shp = SHPWriter.CreatePointsFile(_shpf);
points_shx = SHXWriter.CreatePointsIndex(_shxf);
PRJWriter.CreateProjFile(_prjf);
WritePointsHeaders(_config.dbfDopFields.ToArray());
files_points_sizes[files_points_wrtc.Count - 1] = points_dbf.Length + points_shx.Length + points_shp.Length + 204;
};
if(lines_dbf != null)
if ((lines_dbf.WritedRecords >= MaxFileRecords) || (lines_shp.Position >= (2147483648 - 20 * 1024 * 1024)) || (lines_dbf.Position >= (2147483648 - 20 * 1024 * 1024))) // 20 MB
{
lines_dbf.Close();
lines_shp.Close();
lines_shx.Close();
files_lines_wrtd++;
string _dbfl = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[L{0:0000}]", files_lines_wrtd) + ".dbf";
string _shpl = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[L{0:0000}]", files_lines_wrtd) + ".shp";
string _prjl = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[L{0:0000}]", files_lines_wrtd) + ".prj";
string _shxl = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[L{0:0000}]", files_lines_wrtd) + ".shx";
files_lines_wrts.Add(Path.GetFileName(_shpl));
files_lines_wrtf.Add(Path.GetFileName(_dbfl));
files_lines_wrtc.Add(0);
files_lines_sizes.Add(0);
lines_dbf = new DBFWriter(_dbfl, FileMode.Create, _config.dbfCodePage);
lines_dbf.ShortenFieldNameMode = _config.dbfMoreCompatible == 1;
lines_shp = SHPWriter.CreateLinesFile(_shpl);
lines_shx = SHXWriter.CreateLinesIndex(_shxl);
PRJWriter.CreateProjFile(_prjl);
WriteLinesHeaders(_config.dbfDopFields.ToArray(), _config.addFirstAndLastNodesIdToLines);
files_lines_sizes[files_lines_wrtc.Count - 1] = lines_dbf.Length + lines_shx.Length + lines_shp.Length + 204;
};
if (areas_dbf != null)
if ((areas_dbf.WritedRecords >= MaxFileRecords) || (areas_shp.Position >= (2147483648 - 20 * 1024 * 1024)) || (areas_dbf.Position >= (2147483648 - 20 * 1024 * 1024))) // 20 MB
{
areas_dbf.Close();
areas_shp.Close();
areas_shx.Close();
files_areas_wrtd++;
string _dbfa = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[A{0:0000}]", files_areas_wrtd) + ".dbf";
string _shpa = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[A{0:0000}]", files_areas_wrtd) + ".shp";
string _prja = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[A{0:0000}]", files_areas_wrtd) + ".prj";
string _shxa = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[A{0:0000}]", files_areas_wrtd) + ".shx";
files_areas_wrts.Add(Path.GetFileName(_shpa));
files_areas_wrtf.Add(Path.GetFileName(_dbfa));
files_areas_wrtc.Add(0);
files_areas_sizes.Add(0);
areas_dbf = new DBFWriter(_dbfa, FileMode.Create, _config.dbfCodePage);
areas_dbf.ShortenFieldNameMode = _config.dbfMoreCompatible == 1;
areas_shp = SHPWriter.CreateAreasFile(_shpa);
areas_shx = SHXWriter.CreateAreasIndex(_shxa);
PRJWriter.CreateProjFile(_prja);
WriteAreasHeaders(_config.dbfDopFields.ToArray());
files_areas_sizes[files_areas_wrtc.Count - 1] = areas_dbf.Length + areas_shx.Length + areas_shp.Length + 204;
};
if (relat_dbf != null)
{
if ((relat_dbf.WritedRecords >= MaxFileRecords) || (relat_dbf.Position >= (2147483648 - 20 * 1024 * 1024)) || (relam_dbf.Position >= (2147483648 - 10 * relam_dbf.RecordSize))) // 20 MB
{
relat_dbf.Close();
relam_dbf.Close();
files_relations_wrtd++;
string _relat = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[R{0:0000}]", files_relations_wrtd) + ".dbf";
string _relam = Path.GetDirectoryName(this._dbfFile) + @"\" + Path.GetFileNameWithoutExtension(this._dbfFile) + String.Format("[M{0:0000}]", files_relations_wrtd) + ".dbf";
files_relations_wrtt.Add(Path.GetFileName(_relat));