-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
1385 lines (1139 loc) · 57.7 KB
/
Program.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
/*
* This file is part of ISO_GML_Converter
*
* Copyright 2022 Juha Backman / Natural Resources Institute Finland
*
* ISO_GML_Converter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* ISO_GML_Converter is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with ISO_GML_Converter.
* If not, see <http:https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace ISO_GML_Converter
{
public class DPD_Reference
{
public string element;
public string DDI;
public DPD_Reference(string ref_element = "", string ref_DDI = "")
{
element = ref_element;
DDI = ref_DDI;
}
public DPD_Reference(DPD_Reference DPD)
{
element = DPD.element;
DDI = DPD.DDI;
}
}
public class LogElement
{
public string name;
public Type type;
public DPD_Reference DPD;
public virtual string getValueString(int index)
{
return "";
}
public virtual int getSize()
{
return 0;
}
}
public class LogElementType<T> : LogElement
{
public List<T> values;
public LogElementType(string description)
{
name = description;
values = new List<T>();
type = typeof(T);
}
public override string getValueString(int index)
{
return values.ElementAt(index).ToString();
}
public override int getSize()
{
return values.Count;
}
}
public class Point
{
public double x;
public double y;
public double z;
public DPD_Reference DPD_x = null;
public DPD_Reference DPD_y = null;
public DPD_Reference DPD_z = null;
public Point(double point_x = 0, double point_y = 0, double point_z = 0)
{
x = point_x;
y = point_y;
z = point_z;
}
public Point(Point p)
{
x = p.x;
y = p.y;
z = p.z;
DPD_x = p.DPD_x;
DPD_y = p.DPD_y;
DPD_z = p.DPD_z;
}
public double DotProduct(Point p)
{
return x * p.x + y * p.y + z * p.z;
}
public Point RotateZ(double angle)
{
return new Point(Math.Cos(angle) * x - Math.Sin(angle) * y, Math.Sin(angle) * x + Math.Cos(angle) * y);
}
public static Point operator + (Point p1, Point p2)
{
return new Point(p1.x + p2.x, p1.y + p2.y, p1.z + p2.z);
}
public static Point operator - (Point p1, Point p2)
{
return new Point(p1.x - p2.x, p1.y - p2.y, p1.z - p2.z);
}
public override string ToString()
{
string output = "";
output += "x: ";
if (DPD_x != null)
output += "<" + DPD_x.element + " (" + DPD_x.DDI + ")>";
else
output += x;
output += " y: ";
if (DPD_y != null)
output += "<" + DPD_y.element + " (" + DPD_y.DDI + ")>";
else
output += y;
output += " z: ";
if (DPD_z != null)
output += "<" + DPD_z.element + " (" + DPD_z.DDI + ")>";
else
output += z;
return output;
}
}
public class Connection
{
/*
* Connector Type; DDI=157
*
* -1 = Not available ==> Mounted
* 0 = unknown(default) ==> Mounted
* 1 = ISO 6489-3 Tractor drawbar ==> Towed
* 2 = ISO 730 Three-point-hitch semi-mounted ==> Mounted
* 3 = ISO 730 Three-point-hitch mounted ==> Mounted
* 4 = ISO 6489-1 Hitch-hook ==> Towed
* 5 = ISO 6489-2 Clevis coupling 40 ==> Towed
* 6 = ISO 6489-4 Piton type coupling ==> Towed
* 7 = ISO 6489-5 CUNA hitch ==> Towed
* 8 = ISO 24347 Ball type hitch ==> Towed
* 9 = Chassis Mounted-Self-Propelled ==> Mounted
* 10 = ISO 5692-2 Pivot wagon hitch ==> Towed ???
*/
public enum ConnectionType
{
Mounted,
Towed
}
public ConnectionType type = ConnectionType.Mounted;
public DPD_Reference DPD_type = null;
public Connection(ConnectionType type = ConnectionType.Mounted)
{
this.type = type;
}
static public Connection.ConnectionType DDIValueToConnection(int value)
{
switch (value)
{
case 1:
case 4:
case 5:
case 6:
case 7:
case 8:
case 10:
return Connection.ConnectionType.Towed;
default:
return Connection.ConnectionType.Mounted;
}
}
public override string ToString()
{
string output = "";
output += " ";
if (DPD_type != null)
output += "<" + DPD_type.element + " (" + DPD_type.DDI + ")>";
else
output += type;
return output;
}
}
public class TractorImplementGeometry
{
public Connection connection;
public string element; // reference to DeviceElementReferencePoint
public string description;
public Point TractorDRP; // DeficeReferencePoint
public Point TractorNRP; // NavigationReferencePoint
public Point TractorCRP; // ConnectorReferencePoint
public Point ImplementDRP; // DeficeReferencePoint
public Point ImplementCRP; // ConnectorReferencePoint
public Point ImplementERP; // DeviceElementReferencePoint
public DPD_Reference yaw; // tractor yaw angle
public DPD_Reference connectionangle; // angle between the tractor and the implement
public List<LogElement> datalogheader = new List<LogElement>();
public List<LogElement> datalogdata = new List<LogElement>();
public Connection.ConnectionType getConnection(int index)
{
if (connection.DPD_type != null)
{
var DPD = datalogdata.Where(data => data.DPD.DDI == connection.DPD_type.DDI && data.DPD.element == connection.DPD_type.element);
if (DPD.Count() == 1)
{
connection.type = Connection.DDIValueToConnection(((LogElementType<System.Int32>)DPD.Single()).values[index]);
}
else
{
// TODO: this should throw exception
}
}
return connection.type;
}
public Point getValues(Point point, int index)
{
if (point.DPD_x != null)
{
var DPD = datalogdata.Where(data => data.DPD.DDI == point.DPD_x.DDI && data.DPD.element == point.DPD_x.element);
if(DPD.Count() == 1)
{
point.x = (double)((LogElementType<System.Int32>)DPD.Single()).values[index] * 0.001;
}
else
{
// TODO: this should throw exception
}
}
if (point.DPD_y != null)
{
var DPD = datalogdata.Where(data => data.DPD.DDI == point.DPD_y.DDI && data.DPD.element == point.DPD_y.element);
if (DPD.Count() == 1)
{
point.y = -(double)((LogElementType<System.Int32>)DPD.Single()).values[index] * 0.001;
}
else
{
// TODO: this should throw exception
}
}
if (point.DPD_z != null)
{
var DPD = datalogdata.Where(data => data.DPD.DDI == point.DPD_z.DDI && data.DPD.element == point.DPD_z.element);
if (DPD.Count() == 1)
{
point.z = -(double)((LogElementType<System.Int32>)DPD.Single()).values[index] * 0.001;
}
else
{
// TODO: this should throw exception
}
}
return point;
}
}
public class TimeLogData
{
public string taskname;
public string field;
public string farm;
public List<string> devices;
public List<TractorImplementGeometry> geometry = new List<TractorImplementGeometry>();
public List<LogElement> datalogheader = new List<LogElement>();
public List<LogElement> datalogdata = new List<LogElement>();
}
class Program
{
public TimeLogData TLGdata = new TimeLogData();
enum OutputType
{
GML,
CSV
};
OutputType outputType = OutputType.GML;
static void Main(string[] args)
{
Program program = new Program();
program.run(args);
}
void run(string[] args)
{
string taskfilename = Directory.GetCurrentDirectory() + "\\TASKDATA.XML";
foreach (var arg in args)
{
if (arg.StartsWith("-"))
{
string[] options = arg.Split('=');
switch(options[0])
{
case "-o":
case "-output":
switch(options[1])
{
case "GML":
case "gml":
outputType = OutputType.GML;
break;
case "CSV":
case "csv":
outputType = OutputType.CSV;
break;
default:
Console.WriteLine("Unknown output type.");
Console.WriteLine("Possible values are [GML, CSV]");
return;
}
break;
default:
Console.WriteLine("Unknown parameter");
Console.WriteLine("Possible values are:");
Console.WriteLine("-o/-output=[GML, CSV]");
return;
}
}
else
{
taskfilename = args[0];
}
}
Console.WriteLine("Open file " + taskfilename);
Console.WriteLine("==============================================");
if (!convertTaskFile(taskfilename))
{
Console.WriteLine("ERROR IN CONVERTING FILE");
return;
}
}
string generateName(string description)
{
string name = TLGdata.farm + " " + TLGdata.field + " " + TLGdata.taskname + " " + description;
LogElementType<System.String> TimeStartDATE = (LogElementType<System.String>)TLGdata.datalogheader.Where(val => val.name == "TimeStartDATE").Single();
name += " " + TimeStartDATE.values.First();
LogElementType<System.String> TimeStartTOFD = (LogElementType<System.String>)TLGdata.datalogheader.Where(val => val.name == "TimeStartTOFD").Single();
name += " " + TimeStartTOFD.values.First().Replace(":", ".");
char[] invalids = System.IO.Path.GetInvalidFileNameChars();
name = String.Join("", name.Split(invalids, StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.');
return name;
}
bool hasGeometryOffset(XElement element)
{
var DOR = element.Descendants("DOR").Attributes("A").Select(atr => atr.Value).ToList();
var DPD_offsetX = element.Ancestors("DVC").Single().Descendants("DPD").Where(dpd => DOR.Contains(dpd.Attribute("A").Value) && Convert.ToInt32(dpd.Attribute("B").Value, 16) == 134);
if (DPD_offsetX.Any())
return true;
var DPD_offsetY = element.Ancestors("DVC").Single().Descendants("DPD").Where(dpd => DOR.Contains(dpd.Attribute("A").Value) && Convert.ToInt32(dpd.Attribute("B").Value, 16) == 135);
if (DPD_offsetY.Any())
return true;
var DPD_offsetZ = element.Ancestors("DVC").Single().Descendants("DPD").Where(dpd => DOR.Contains(dpd.Attribute("A").Value) && Convert.ToInt32(dpd.Attribute("B").Value, 16) == 136);
if (DPD_offsetZ.Any())
return true;
var DPT_offsetX = element.Ancestors("DVC").Single().Descendants("DPT").Where(dpt => DOR.Contains(dpt.Attribute("A").Value) && Convert.ToInt32(dpt.Attribute("B").Value, 16) == 134);
if (DPT_offsetX.Any())
return true;
var DPT_offsetY = element.Ancestors("DVC").Single().Descendants("DPT").Where(dpt => DOR.Contains(dpt.Attribute("A").Value) && Convert.ToInt32(dpt.Attribute("B").Value, 16) == 135);
if (DPT_offsetY.Any())
return true;
var DPT_offsetZ = element.Ancestors("DVC").Single().Descendants("DPT").Where(dpt => DOR.Contains(dpt.Attribute("A").Value) && Convert.ToInt32(dpt.Attribute("B").Value, 16) == 136);
if (DPT_offsetZ.Any())
return true;
return false;
}
Point extractGeometryOffset(XElement element)
{
Point point = new Point();
var DOR = element.Descendants("DOR").Attributes("A").Select(atr => atr.Value).ToList();
var DPD_offsetX = element.Ancestors("DVC").Single().Descendants("DPD").Where(dpd => DOR.Contains(dpd.Attribute("A").Value) && Convert.ToInt32(dpd.Attribute("B").Value, 16) == 134);
if (DPD_offsetX.Any())
point.DPD_x = new DPD_Reference(element.Attribute("A").Value, DPD_offsetX.Single().Attribute("B").Value);
var DPD_offsetY = element.Ancestors("DVC").Single().Descendants("DPD").Where(dpd => DOR.Contains(dpd.Attribute("A").Value) && Convert.ToInt32(dpd.Attribute("B").Value, 16) == 135);
if (DPD_offsetY.Any())
point.DPD_y = new DPD_Reference(element.Attribute("A").Value, DPD_offsetY.Single().Attribute("B").Value);
var DPD_offsetZ = element.Ancestors("DVC").Single().Descendants("DPD").Where(dpd => DOR.Contains(dpd.Attribute("A").Value) && Convert.ToInt32(dpd.Attribute("B").Value, 16) == 136);
if (DPD_offsetZ.Any())
point.DPD_z = new DPD_Reference(element.Attribute("A").Value, DPD_offsetZ.Single().Attribute("B").Value);
var DPT_offsetX = element.Ancestors("DVC").Single().Descendants("DPT").Where(dpt => DOR.Contains(dpt.Attribute("A").Value) && Convert.ToInt32(dpt.Attribute("B").Value, 16) == 134);
if (DPT_offsetX.Any())
point.x = (double)Int32.Parse(DPT_offsetX.Single().Attribute("C").Value) * 0.001;
var DPT_offsetY = element.Ancestors("DVC").Single().Descendants("DPT").Where(dpt => DOR.Contains(dpt.Attribute("A").Value) && Convert.ToInt32(dpt.Attribute("B").Value, 16) == 135);
if (DPT_offsetY.Any())
point.y = -(double)Int32.Parse(DPT_offsetY.Single().Attribute("C").Value) * 0.001;
var DPT_offsetZ = element.Ancestors("DVC").Single().Descendants("DPT").Where(dpt => DOR.Contains(dpt.Attribute("A").Value) && Convert.ToInt32(dpt.Attribute("B").Value, 16) == 136);
if (DPT_offsetZ.Any())
point.z = -(double)Int32.Parse(DPT_offsetZ.Single().Attribute("C").Value) * 0.001;
// Note: ISO 11783-10 defines coordinates:
// positive x: driving direction
// positive y: to the right relative to the driving direction
// positive z: down
// In here we are using ENU coordinates (x: East, y: Noth, z: Up)
return point;
}
bool extractGeometryInformation(XElement TSK)
{
bool retvalue = true;
TLGdata.geometry.Clear();
// TODO: read True Rotation Point; DDI=306 and 304 (Does anyone use it?)
// TODO: read Actual relative connection angle; DDI=466 (Does anyone use it?)
try
{
// include devices that are listed in Task and are connected together
foreach (var CNN in TSK.Elements("CNN"))
{
var DVC_0 = TSK.Ancestors().Descendants("DVC").Where(dvc => dvc.Attribute("A").Value == CNN.Attribute("A").Value).Single();
var CRP_0_DET = DVC_0.Descendants("DET").Where(det => det.Attribute("A").Value == CNN.Attribute("B").Value).Single();
Point CRP_0 = extractGeometryOffset(CRP_0_DET);
var DVC_1 = TSK.Ancestors().Descendants("DVC").Where(dvc => dvc.Attribute("A").Value == CNN.Attribute("C").Value).Single();
var CRP_1_DET = DVC_1.Descendants("DET").Where(det => det.Attribute("A").Value == CNN.Attribute("D").Value).Single();
Point CRP_1 = extractGeometryOffset(CRP_1_DET);
var NRP = DVC_0.Descendants("DET").Where(det => det.Attribute("C").Value == "7");
if (!NRP.Any())
{
// assumption: The tractor has GNSS mounted on; DVC_0=tractor, DVC_1=implement. Swap if GNSS is not found from DVC_0
var DVC_temp = DVC_0;
DVC_0 = DVC_1;
DVC_1 = DVC_temp;
var CRP_DET = CRP_0_DET;
CRP_0_DET = CRP_1_DET;
CRP_1_DET = CRP_DET;
var CRP_temp = CRP_0;
CRP_0 = CRP_1;
CRP_1 = CRP_temp;
}
NRP = DVC_0.Descendants("DET").Where(det => det.Attribute("C").Value == "7");
if (!NRP.Any())
continue; // No GNSS position found, cannot do anything
Point NRP_0 = extractGeometryOffset(NRP.Single());
DPD_Reference YAW_reference = null;
var YAW_DPD = DVC_0.Descendants("DPD").Where(dpd => Convert.ToInt32(dpd.Attribute("B").Value, 16) == 144);
if (YAW_DPD.Any())
{
var DET = DVC_0.Descendants("DOR").Where(dor => dor.Attribute("A").Value == YAW_DPD.Single().Attribute("A").Value).First().Parent;
YAW_reference = new DPD_Reference(DET.Attribute("A").Value, YAW_DPD.Single().Attribute("B").Value);
}
var DOR = CRP_1_DET.Descendants("DOR").Attributes("A").Select(atr => atr.Value).ToList();
var ConnectionTypeDPT = DVC_1.Descendants("DPT").Where(dpt => DOR.Contains(dpt.Attribute("A").Value) && Convert.ToInt32(dpt.Attribute("B").Value, 16) == 157);
var ConnectionTypeDPD = DVC_1.Descendants("DPD").Where(dpd => DOR.Contains(dpd.Attribute("A").Value) && Convert.ToInt32(dpd.Attribute("B").Value, 16) == 157);
Connection connection = new Connection();
if (ConnectionTypeDPD.Any())
connection.DPD_type = new DPD_Reference(CRP_1_DET.Attribute("A").Value, ConnectionTypeDPD.Single().Attribute("B").Value);
if (ConnectionTypeDPT.Any())
connection.type = Connection.DDIValueToConnection(Int32.Parse(ConnectionTypeDPT.Single().Attribute("C").Value));
var DRP_1 = DVC_1.Descendants("DET").Where(det => det.Attribute("F").Value == "0").Single();
if (!hasGeometryOffset(DRP_1))
{
TLGdata.geometry.Add(new TractorImplementGeometry()
{
connection = connection,
TractorCRP = CRP_0,
TractorNRP = NRP_0,
ImplementCRP = CRP_1,
ImplementERP = new Point(),
yaw = YAW_reference,
element = DRP_1.Attribute("A").Value
});
}
foreach (var ERP in DVC_1.Descendants("DET").Where(det => hasGeometryOffset(det)))
{
TLGdata.geometry.Add(new TractorImplementGeometry()
{
connection = connection,
TractorCRP = CRP_0,
TractorNRP = NRP_0,
ImplementCRP = CRP_1,
ImplementERP = extractGeometryOffset(ERP),
yaw = YAW_reference,
element = ERP.Attribute("A").Value
});
}
}
// include also all other devices that has GNSS mounted on
foreach (var NRP in TSK.Ancestors().Descendants("DET").Where(det => det.Attribute("C").Value == "7"))
{
var DVC_0 = NRP.Ancestors("DVC");
var DRP_0 = DVC_0.Descendants("DET").Where(det => det.Attribute("F").Value == "0").Single();
if (!TLGdata.geometry.Where(geometry => geometry.element == DRP_0.Attribute("A").Value).Any())
{
Point NRP_0 = extractGeometryOffset(NRP);
DPD_Reference YAW_reference = null;
var YAW_DPD = DVC_0.Descendants("DPD").Where(dpd => Convert.ToInt32(dpd.Attribute("B").Value, 16) == 144);
if (YAW_DPD.Any())
{
var DET = DVC_0.Descendants("DOR").Where(dor => dor.Attribute("A").Value == YAW_DPD.Single().Attribute("A").Value).First().Parent;
YAW_reference = new DPD_Reference(DET.Attribute("A").Value, YAW_DPD.Single().Attribute("B").Value);
}
TLGdata.geometry.Add(new TractorImplementGeometry()
{
connection = new Connection(Connection.ConnectionType.Mounted),
TractorCRP = new Point(),
TractorNRP = NRP_0,
ImplementCRP = new Point(),
ImplementERP = new Point(),
yaw = YAW_reference,
element = DRP_0.Attribute("A").Value
});
foreach (var ERP in DVC_0.Descendants("DET").Where(det => hasGeometryOffset(det)))
{
TLGdata.geometry.Add(new TractorImplementGeometry()
{
connection = new Connection(Connection.ConnectionType.Mounted),
TractorCRP = new Point(),
TractorNRP = NRP_0,
ImplementCRP = new Point(),
ImplementERP = extractGeometryOffset(ERP),
yaw = YAW_reference,
element = ERP.Attribute("A").Value
});
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR IN EXTRACTING GEOMETRY INFORMATION");
Console.WriteLine(ex.ToString());
retvalue = false;
}
TLGdata.geometry.Add(new TractorImplementGeometry()
{
connection = new Connection(Connection.ConnectionType.Mounted),
TractorCRP = new Point(),
TractorNRP = new Point(),
ImplementCRP = new Point(),
ImplementERP = new Point(),
element = "original"
});
Console.WriteLine("******* Geometry: **********");
foreach (var geometry in TLGdata.geometry)
{
geometry.description = geometry.element;
var DET = TSK.Ancestors().Descendants("DET").Where(det => det.Attribute("A").Value == geometry.element);
if (DET.Any() && DET.Single().Attributes("D").Any())
{
var DVC = DET.Ancestors("DVC").Single();
if (DVC.Attributes("B").Any())
geometry.description = DVC.Attribute("B").Value;
else
geometry.description = DVC.Attribute("A").Value;
geometry.description += "/ " + DET.Single().Attribute("D").Value;
}
Console.WriteLine("Geometry: " + geometry.description);
Console.WriteLine("DeviceElement: " + geometry.element);
Console.WriteLine("Connection type: " + geometry.connection);
Console.WriteLine("TractorCRP: " + geometry.TractorCRP);
Console.WriteLine("TractorNRP: " + geometry.TractorNRP);
Console.WriteLine("ImplementCRP: " + geometry.ImplementCRP);
Console.WriteLine("ImplementERP: " + geometry.ImplementERP);
if(geometry.yaw != null)
Console.WriteLine("Yaw angle: <" + geometry.yaw.element + " (" + geometry.yaw.DDI + ")>");
Console.WriteLine("----------------------------------------------");
}
return retvalue;
}
bool extractTimelog(XElement TLG, string directory)
{
// read header
string header_file = directory + TLG.Attribute("A").Value + ".xml";
XDocument TLGFile;
try
{
TLGFile = XDocument.Load(header_file);
}
catch (System.IO.FileNotFoundException e)
{
Console.WriteLine(e.Message);
return false;
}
if (TLGFile.Element("TIM").Attribute("A").Value == "")
{
TLGdata.datalogheader.Add(new LogElementType<System.String>("TimeStartTOFD"));
TLGdata.datalogheader.Add(new LogElementType<System.String>("TimeStartDATE"));
}
// Attribute B and C are not valid for TIM in TLG
foreach (var PTN in TLGFile.Element("TIM").Descendants("PTN"))
{
if (PTN.Attribute("A") != null && PTN.Attribute("A").Value == "")
TLGdata.datalogheader.Add(new LogElementType<System.Int32>("PositionNorth"));
if (PTN.Attribute("B") != null && PTN.Attribute("B").Value == "")
TLGdata.datalogheader.Add(new LogElementType<System.Int32>("PositionEast"));
if (PTN.Attribute("C") != null && PTN.Attribute("C").Value == "")
TLGdata.datalogheader.Add(new LogElementType<System.Int32>("PositionUp"));
if (PTN.Attribute("D") != null && PTN.Attribute("D").Value == "")
TLGdata.datalogheader.Add(new LogElementType<System.Byte>("PositionStatus"));
if (PTN.Attribute("E") != null && PTN.Attribute("E").Value == "")
TLGdata.datalogheader.Add(new LogElementType<System.UInt16>("PDOP"));
if (PTN.Attribute("F") != null && PTN.Attribute("F").Value == "")
TLGdata.datalogheader.Add(new LogElementType<System.UInt16>("HDOP"));
if (PTN.Attribute("G") != null && PTN.Attribute("G").Value == "")
TLGdata.datalogheader.Add(new LogElementType<System.Byte>("NumberOfSatellites"));
if (PTN.Attribute("H") != null && PTN.Attribute("H").Value == "")
TLGdata.datalogheader.Add(new LogElementType<System.String>("GpsUtcTime"));
if (PTN.Attribute("I") != null && PTN.Attribute("I").Value == "")
TLGdata.datalogheader.Add(new LogElementType<System.String>("GpsUtcDate"));
}
foreach (var DLV in TLGFile.Element("TIM").Descendants("DLV"))
{
string ProcessDataDDI = DLV.Attribute("A").Value;
string DeviceElementIdRef = DLV.Attribute("C").Value;
var DET = TLG.Ancestors().Descendants("DET").Where(det => det.Attribute("A").Value == DeviceElementIdRef).Single();
LogElementType<System.Int32> logelement;
try
{
var DOR = DET.Descendants("DOR").Attributes("A").Select(atr => atr.Value).ToList();
var DPD = DET.Parent.Descendants("DPD").Where(dpd => DOR.Contains(dpd.Attribute("A").Value) && dpd.Attribute("B").Value == ProcessDataDDI).Single();
string DVCdesignator = DET.Parent.Attribute("B").Value; // Note! optional attribute
string DETdesignator = DET.Attribute("D").Value; // Note! optional attribute
string DPDdesignator = DPD.Attribute("E").Value; // Note! optional attribute --> Use DDI description instead
logelement = new LogElementType<System.Int32>(DVCdesignator + "_" + DETdesignator + "_" + DPDdesignator) { DPD = new DPD_Reference(DeviceElementIdRef, ProcessDataDDI) };
try
{
logelement.values.Add(System.Int32.Parse(DLV.Attribute("B").Value));
}
catch (System.FormatException)
{
logelement.values.Add(0);
}
TLGdata.datalogdata.Add(logelement);
}
catch (System.InvalidOperationException)
{
Console.WriteLine("Process data description not found!");
logelement = new LogElementType<System.Int32>(DeviceElementIdRef + "_" + ProcessDataDDI) { DPD = new DPD_Reference(DeviceElementIdRef, ProcessDataDDI) };
try
{
logelement.values.Add(System.Int32.Parse(DLV.Attribute("B").Value));
}
catch (System.FormatException)
{
logelement.values.Add(0);
}
TLGdata.datalogdata.Add(logelement);
}
// Find the geometry information for current logelement
var DVC_DET_list = DET.Ancestors("DVC").Descendants("DET");
var DET_geometry = DET;
var geometry = TLGdata.geometry.Where(geo => geo.element == DET_geometry.Attribute("A").Value);
while (!geometry.Any() && DET_geometry.Attribute("B").Value != "0")
{
var parent = DVC_DET_list.Where(det => det.Attribute("B").Value == DET_geometry.Attribute("F").Value);
if (parent.Count() == 1)
DET_geometry = parent.Single();
else
break;
geometry = TLGdata.geometry.Where(geo => geo.element == DET_geometry.Attribute("A").Value);
}
if (geometry.Any())
geometry.Single().datalogdata.Add(logelement);
else
TLGdata.geometry.Where(geo => geo.element == "original").Single().datalogdata.Add(logelement);
}
Console.WriteLine(TLG.Attribute("A").Value);
Console.WriteLine("******* Header: **********");
foreach (var element in TLGdata.datalogheader)
{
Console.WriteLine(element.name);
}
Console.WriteLine("******* Data: **********");
foreach (var geometry in TLGdata.geometry)
{
if (geometry.datalogdata.Any())
{
Console.WriteLine("Data measured in geometry position: " + geometry.description);
foreach (var element in geometry.datalogdata)
{
Console.WriteLine(element.name);
}
Console.WriteLine("----------------------------------------------");
}
}
Console.WriteLine("==============================================");
// ready binary
string binary_file = directory + TLG.Attribute("A").Value + ".bin";
Console.WriteLine("** Read binary file **");
Console.WriteLine(binary_file);
BinaryReader reader = new BinaryReader(new FileStream(binary_file, FileMode.Open));
List<LogElement>.Enumerator header = TLGdata.datalogheader.GetEnumerator();
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
if (!header.MoveNext())
{
header = TLGdata.datalogheader.GetEnumerator();
if (!header.MoveNext())
{
Console.WriteLine("NO HEADER FOR DATA");
return false;
}
System.Int32[] lastdata = new System.Int32[TLGdata.datalogdata.Count];
for (int i = 0; i < TLGdata.datalogdata.Count; i++)
lastdata[i] = ((LogElementType<System.Int32>)TLGdata.datalogdata.ElementAt(i)).values.Last();
System.Byte DLVs = reader.ReadByte();
for (int n = 0; n < DLVs; n++)
{
System.Byte DLVn = reader.ReadByte();
lastdata[DLVn] = reader.ReadInt32();
}
for (int i = 0; i < TLGdata.datalogdata.Count; i++)
((LogElementType<System.Int32>)TLGdata.datalogdata.ElementAt(i)).values.Add(lastdata[i]);
if (reader.BaseStream.Position >= reader.BaseStream.Length)
break;
}
if (header.Current.name == "TimeStartDATE" || header.Current.name == "GpsUtcDate")
{
LogElementType<System.String> element = (LogElementType<System.String>)header.Current;
DateTime date = new DateTime(1980, 1, 1);
date = date.AddDays(reader.ReadUInt16());
element.values.Add(date.ToShortDateString());
}
else if (header.Current.name == "TimeStartTOFD" || header.Current.name == "GpsUtcTime")
{
LogElementType<System.String> element = (LogElementType<System.String>)header.Current;
DateTime date = new DateTime();
date = date.AddMilliseconds(reader.ReadUInt32());
element.values.Add(date.TimeOfDay.ToString());
}
else if (header.Current.type == typeof(System.Byte))
{
LogElementType<System.Byte> element = (LogElementType<System.Byte>)header.Current;
element.values.Add(reader.ReadByte());
}
else if (header.Current.type == typeof(System.Int16))
{
LogElementType<System.Int16> element = (LogElementType<System.Int16>)header.Current;
element.values.Add(reader.ReadInt16());
}
else if (header.Current.type == typeof(System.Int32))
{
LogElementType<System.Int32> element = (LogElementType<System.Int32>)header.Current;
element.values.Add(reader.ReadInt32());
}
else if (header.Current.type == typeof(System.UInt16))
{
LogElementType<System.UInt16> element = (LogElementType<System.UInt16>)header.Current;
element.values.Add(reader.ReadUInt16());
}
else if (header.Current.type == typeof(System.UInt32))
{
LogElementType<System.UInt32> element = (LogElementType<System.UInt32>)header.Current;
element.values.Add(reader.ReadUInt32());
}
else if (header.Current.type == typeof(System.UInt64))
{
LogElementType<System.UInt64> element = (LogElementType<System.UInt64>)header.Current;
element.values.Add(reader.ReadUInt64());
}
}
foreach (var element in TLGdata.datalogdata)
{
LogElementType<System.Int32> data = (LogElementType<System.Int32>)element;
data.values.RemoveAt(0);
}
return true;
}
// Return difference betweeen angle1 and angle2 wrapped on interval [-pi, pi]
static double angleDiff(double angle1, double angle2)
{
double angle = angle1 - angle2;
while (angle > Math.PI)
angle -= 2*Math.PI;
while (angle < -Math.PI)
angle += 2*Math.PI;
return angle;
}
bool simulateGeometry()
{
// Convert WGS-84 coordinates to ENU coordinates
List<System.Int32> latitude = ((LogElementType<System.Int32>)TLGdata.datalogheader.Where(header => header.name == "PositionNorth").Single()).values;
List<System.Int32> longitude = ((LogElementType<System.Int32>)TLGdata.datalogheader.Where(header => header.name == "PositionEast").Single()).values;
List<System.Int32> height;
// If data is missing height use 0
var heightData = TLGdata.datalogheader.Where(header => header.name == "PositionUp").ToList();
if (heightData.Count > 0)
height = ((LogElementType<System.Int32>)heightData.First()).values;
else
height = new List<System.Int32>(new System.Int32[latitude.Count]);
List<System.Double> xGNSS = new List<System.Double>();
List<System.Double> yGNSS = new List<System.Double>();
List<System.Double> zGNSS = new List<System.Double>();
List<System.Double> yawGNSS = new List<System.Double>();
double lat0 = latitude[0] * 0.0000001;
double lon0 = longitude[0] * 0.0000001;
double h0 = height[0] * 0.001;
xGNSS.Add(0.0);
yGNSS.Add(0.0);
zGNSS.Add(0.0);
yawGNSS.Add(0.0);
bool fillFirsts = true;
bool reverse = false;
int reversecount = 0;
int forwardcount = 0;
double dx = 0;
double dy = 0;
for (int i=1; i<longitude.Count; i++)
{
GpsUtils.GeodeticToEnu(latitude[i] * 0.0000001, longitude[i] * 0.0000001, height[i] * 0.001,
lat0, lon0, h0,
out double x, out double y, out double z);
xGNSS.Add(x);
yGNSS.Add(y);
zGNSS.Add(z);
dx += x - xGNSS[i - 1];
dy += y - yGNSS[i - 1];
// Estimate Yaw if there is no measurement
// have to move enough before heading is estimated
if ((dx * dx + dy * dy) > 0.01) // 0.01 --> 0.1m
{
yawGNSS.Add(Math.Atan2(dy, dx));
dx = 0;
dy = 0;
if (fillFirsts)
{
for (var j = 0; j < i; j++)
yawGNSS[j] = yawGNSS[i];
fillFirsts = false;
}
else
{
if (reverse)
{
yawGNSS[i] += Math.PI;
reversecount++;
}
else
forwardcount++;
// yaw is changed to opposite direction? --> tractor is reversing