/
MIFDigest.cpp
728 lines (718 loc) · 22.3 KB
/
MIFDigest.cpp
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
//
// This program code is a part of The Master Scheduler (TMS)
// and is Copyright (C) 1991-2004 Schedule Masters, Inc.
// All rights reserved.
//
#include "stdafx.h"
extern "C" {
#include "TMSHeader.h"
void EN2LL(int, double, double, char*, double *, double *);
} // extern "C"
int SetupMIFLINES(MIFPASSEDDATADef *, MIFLINESDef *);
extern "C" {
int MIFDigest(MIFPASSEDDATADef *pMIFPASSEDDATA, MIFLINESDef *pMIFLINES)
{
return SetupMIFLINES(pMIFPASSEDDATA, pMIFLINES);
}
} // extern "C"
#include "TMS.h"
static const char *wSpace = " \t\r\n";
int SetupMIFLINES(MIFPASSEDDATADef *pMIFPASSEDDATA, MIFLINESDef *pMIFLINES)
{
MIFLINESDef MIFLINES[TMSRPT61_MAXMIFLINES];
CString s;
double dValue1, dValue2, dValue3, dValue4;
double Lat, Long;
double distance;
double leastDistance;
BOOL bDATAFound;
BOOL bCoordSysNonEarthFound;
BOOL bBreak;
BOOL bFound;
char *ptr;
long firstNODESrecordID;
long firstStopNumber;
long firstStopNODESrecordID;
long toNODESrecordID;
long toStopNumber;
long toStopNODESrecordID;
long NODESrecordID;
long previousNODESrecordID;
long travelTime;
int nI, nJ, nK;
int numMultiples;
int numSegments;
int numMIFLINES;
int rcode2;
int leastDistanceIndex;
int MIFPointer;
//
// Establish and open the .mif file
//
CString MIFFileName;
CStdioFile MIFFile;
MIFFileName = pMIFPASSEDDATA->szMIFFileName;
//
// Look for this pattern name attached to the szMIFFileName
// to see if we have a spcialized trace file
//
PATTERNNAMESKey0.recordID = pMIFPASSEDDATA->PATTERNNAMESrecordID;
rcode2 = btrieve(B_GETEQUAL, TMS_PATTERNNAMES, &PATTERNNAMES, &PATTERNNAMESKey0, 0);
if(rcode2 != 0)
{
sprintf(tempString, "***ERR Pattern not found in PATTERNNAMES : RecordID = %ld\r\n", pMIFPASSEDDATA->PATTERNNAMESrecordID);
_lwrite(pMIFPASSEDDATA->hfErrorLog, tempString, strlen(tempString));
return(NO_RECORD);
}
else
{
strncpy(tempString, PATTERNNAMES.name, PATTERNNAMES_NAME_LENGTH);
trim(tempString, PATTERNNAMES_NAME_LENGTH);
MIFFileName += tempString;
MIFFileName += ".mif";
sprintf(tempString, "***INF Opening Trace File : \"%s\"\r\n", MIFFileName);
_lwrite(pMIFPASSEDDATA->hfErrorLog, tempString, strlen(tempString));
//
// The specialized trace file isn't there - look for the regular one
//
if(MIFFile.Open(MIFFileName, CFile::modeRead | CFile::typeText | CFile::shareDenyNone) == 0)
{
sprintf(tempString, "***INF Could not open Trace File : \"%s\"\r\n", MIFFileName);
_lwrite(pMIFPASSEDDATA->hfErrorLog, tempString, strlen(tempString));
MIFFileName = pMIFPASSEDDATA->szMIFFileName;
MIFFileName += ".mif";
sprintf(tempString, "***INF Opening Trace File : \"%s\"\r\n", MIFFileName);
_lwrite(pMIFPASSEDDATA->hfErrorLog, tempString, strlen(tempString));
if(MIFFile.Open(MIFFileName, CFile::modeRead | CFile::typeText | CFile::shareDenyNone) == 0)
{
s.LoadString(ERROR_349);
sprintf(szarString, s, MIFFileName);
MessageBeep(MB_ICONINFORMATION);
MessageBox(NULL, szarString, TMS, MB_OK | MB_ICONINFORMATION);
sprintf(tempString, "***ERR Could not open Trace File : \"%s\"\r\n", MIFFileName);
_lwrite(pMIFPASSEDDATA->hfErrorLog, tempString, strlen(tempString));
return(NO_RECORD);
}
}
}
//
// Cycle through the .MIF file
//
bDATAFound = FALSE;
bCoordSysNonEarthFound = FALSE;
numMIFLINES = 0;
MIFFile.SeekToBegin();
while(MIFFile.ReadString(s))
{
s.MakeUpper();
//
// Scan through until we hit "DATA" and/or "CoordSys"
//
if(!bDATAFound)
{
//
// Is "CoordSys NonEarth" present?
//
if(!bCoordSysNonEarthFound)
{
nI = s.Find("COORDSYS NONEARTH", 0);
if(nI != -1)
{
bCoordSysNonEarthFound = TRUE;
continue;
}
}
//
// Check for "DATA"
//
nI = s.Find("DATA", 0);
if(nI != -1)
{
bDATAFound = TRUE;
}
continue;
}
//
// "DATA" Encountered. Now look to see what lines are involved.
//
// "PLINE"
//
nI = s.Find("PLINE", 0);
if(nI != -1)
{
//
// There are two possibilities in a PLINE statement:
// 1) It's followed (on the same line) by the number of points to follow
// 2) It's followed (on the same line) by the keyword "MULTIPLE" and then the number of sections,
// with each section starting with the number of points to follow
//
// Look for "MULTIPLE"
//
strcpy(tempString, s);
nI = s.Find("MULTIPLE", 0);
if(nI != -1)
{
if((ptr = strtok(tempString, wSpace)) == NULL) // Get rid of "PLINE"
{
continue;
}
if((ptr = strtok(NULL, wSpace)) == NULL) // Get rid of "MULTIPLE"
{
continue;
}
if((ptr = strtok(NULL, wSpace)) == NULL) // Get the number of multiples
{
continue;
}
numMultiples = atol(ptr);
if(!MIFFile.ReadString(s))
{
break;
}
numSegments = atol(s);
}
//
// No "MULTIPLE" - get the number of segments
//
else
{
numMultiples = 1;
if((ptr = strtok(tempString, wSpace)) == NULL) // Get rid of "PLINE"
{
continue;
}
if((ptr = strtok(NULL, wSpace)) == NULL) // Get the number of segments
{
continue;
}
numSegments = atol(ptr);
}
//
// Loop through the segments and multiples
//
bBreak = FALSE;
for(nI = 0; nI < numMultiples; nI++)
{
if(bBreak)
{
break;
}
for(nJ = 0; nJ < numSegments; nJ++)
{
if(!MIFFile.ReadString(s))
{
bBreak = TRUE;
break;
}
strcpy(tempString, s);
if((ptr = strtok(tempString, wSpace)) == NULL) // Take the first numeric
{
continue;
}
dValue1 = (double)atof(ptr);
if((ptr = strtok(NULL, wSpace)) == NULL) // Get the second numeric
{
continue;
}
dValue2 = (double)atof(ptr);
//
// Got two doubles - do we have to convert them to Lat/Long?
//
if(bCoordSysNonEarthFound)
{
EN2LL(23, dValue1, dValue2, szUTMZone, &Lat, &Long);
}
else
{
Long = dValue1;
Lat = dValue2;
// Long = dValue2;
// Lat = dValue1;
}
//
// Save Lat and Long to the data structure
//
MIFLINES[numMIFLINES].flags = nJ == 0 ? MIFLINES_FLAG_START : 0;
if(nJ != numSegments - 1)
{
MIFLINES[numMIFLINES].from.longitude = Long;
MIFLINES[numMIFLINES].from.latitude = Lat;
}
if(nJ > 0)
{
MIFLINES[numMIFLINES - 1].to.longitude = Long;
MIFLINES[numMIFLINES - 1].to.latitude = Lat;
}
if(nJ != numSegments - 1)
{
numMIFLINES++;
if(numMIFLINES >= TMSRPT61_MAXMIFLINES)
{
sprintf(tempString, "***ERR TMSRPT61_MAXMIFLINES too small (1)\r\n");
_lwrite(pMIFPASSEDDATA->hfErrorLog, tempString, strlen(tempString));
return(NO_RECORD);
}
}
}
if(numMultiples > 1)
{
if(!MIFFile.ReadString(s))
{
bBreak = TRUE;
}
else
{
numSegments = atol(s);
}
}
}
}
//
// "LINE"
//
else
{
nI = s.Find("LINE", 0);
if(nI != -1)
{
strcpy(tempString, s);
if((ptr = strtok(tempString, wSpace)) == NULL) // Get rid of "LINE"
{
continue;
}
if((ptr = strtok(NULL, wSpace)) == NULL) // Take the first numeric
{
continue;
}
dValue1 = (double)atof(ptr);
if((ptr = strtok(NULL, wSpace)) == NULL) // Get the second numeric
{
continue;
}
dValue2 = (double)atof(ptr);
if((ptr = strtok(NULL, wSpace)) == NULL) // Get the third numeric
{
continue;
}
dValue3 = (double)atof(ptr);
if((ptr = strtok(NULL, wSpace)) == NULL) // Get the fourth numeric
{
continue;
}
dValue4 = (double)atof(ptr);
//
// Do we convert?
//
if(bCoordSysNonEarthFound)
{
EN2LL(23, dValue1, dValue2, szUTMZone, &Lat, &Long);
}
else
{
Long = dValue1;
Lat = dValue2;
}
MIFLINES[numMIFLINES].flags = MIFLINES_FLAG_START;
MIFLINES[numMIFLINES].from.longitude = Long;
MIFLINES[numMIFLINES].from.latitude = Lat;
if(bCoordSysNonEarthFound)
{
EN2LL(23, dValue3, dValue4, szUTMZone, &Lat, &Long);
}
else
{
Long = dValue3;
Lat = dValue4;
}
MIFLINES[numMIFLINES].to.longitude = Long;
MIFLINES[numMIFLINES].to.latitude = Lat;
numMIFLINES++;
if(numMIFLINES >= TMSRPT61_MAXMIFLINES)
{
sprintf(tempString, "***ERR TMSRPT61_MAXMIFLINES too small (2)\r\n");
_lwrite(pMIFPASSEDDATA->hfErrorLog, tempString, strlen(tempString));
return(NO_RECORD);
}
}
}
}
//
// Establish the output lines
//
// Get the first node on the pattern
//
PATTERNSKey2.ROUTESrecordID = pMIFPASSEDDATA->ROUTESrecordID;
PATTERNSKey2.SERVICESrecordID = pMIFPASSEDDATA->SERVICESrecordID;
PATTERNSKey2.directionIndex = pMIFPASSEDDATA->directionIndex;
PATTERNSKey2.PATTERNNAMESrecordID = pMIFPASSEDDATA->PATTERNNAMESrecordID;
PATTERNSKey2.nodeSequence = NO_RECORD;
rcode2 = btrieve(B_GETGREATER, TMS_PATTERNS, &PATTERNS, &PATTERNSKey2, 2);
if(rcode2 == 0 &&
PATTERNS.ROUTESrecordID == pMIFPASSEDDATA->ROUTESrecordID &&
PATTERNS.SERVICESrecordID == pMIFPASSEDDATA->SERVICESrecordID &&
PATTERNS.directionIndex == pMIFPASSEDDATA->directionIndex &&
PATTERNS.PATTERNNAMESrecordID == pMIFPASSEDDATA->PATTERNNAMESrecordID)
{
NODESrecordID = NodeEquivalenced(PATTERNS.NODESrecordID);
NODESKey0.recordID = NODESrecordID == NO_RECORD ? PATTERNS.NODESrecordID : NODESrecordID;
btrieve(B_GETEQUAL, TMS_NODES, &NODES, &NODESKey0, 0);
firstNODESrecordID = NODES.recordID;
firstStopNumber = (pMIFPASSEDDATA->directionIndex == 0 ? NODES.OBStopNumber : NODES.IBStopNumber);
if(firstStopNumber <= 0)
{
firstStopNumber = NODES.number;
}
if(firstStopNumber > 0)
{
bFound = FALSE;
rcode2 = btrieve(B_GETFIRST, TMS_NODES, &NODES, &NODESKey0, 0);
while(rcode2 == 0)
{
if((NODES.flags & NODES_FLAG_STOP))
{
if(NODES.number == firstStopNumber)
{
firstStopNODESrecordID = NODES.recordID;
bFound = TRUE;
break;
}
}
rcode2 = btrieve(B_GETNEXT, TMS_NODES, &NODES, &NODESKey0, 0);
}
if(!bFound)
{
NODESKey0.recordID = PATTERNS.NODESrecordID;
btrieve(B_GETEQUAL, TMS_NODES, &NODES, &NODESKey0, 0);
}
//
// Change to stop number (as derived from long name) : Bob Antonisse, 2-Aug-07
//
else
{
strncpy(tempString, NODES.longName, NODES_LONGNAME_LENGTH);
trim(tempString, NODES_LONGNAME_LENGTH);
firstStopNumber = atol(&tempString[4]);
}
}
Long = NODES.longitude;
Lat = NODES.latitude;
}
else
{
sprintf(tempString, "***ERR Pattern without nodes\r\n");
_lwrite(pMIFPASSEDDATA->hfErrorLog, tempString, strlen(tempString));
return(NO_RECORD);
}
//
// Locate the closest MIFLINE to the starting node
// and string the rest of the lines together in order
//
for(nI = 0; nI < numMIFLINES; nI++)
{
leastDistance = 10000;
leastDistanceIndex = NO_RECORD;
for(nJ = 0; nJ < numMIFLINES; nJ++)
{
if(MIFLINES[nJ].flags & MIFLINES_FLAG_USED)
{
continue;
}
distance = GreatCircleDistance(Long, Lat, MIFLINES[nJ].from.longitude, MIFLINES[nJ].from.latitude);
if(distance < leastDistance)
{
leastDistance = distance;
leastDistanceIndex = nJ;
}
}
if(leastDistanceIndex == NO_RECORD)
{
sprintf(tempString, "***ERR Least distance index remained at NO_RECORD in search for starting node\r\n");
_lwrite(pMIFPASSEDDATA->hfErrorLog, tempString, strlen(tempString));
return(NO_RECORD);
}
pMIFLINES[nI] = MIFLINES[leastDistanceIndex];
Long = MIFLINES[leastDistanceIndex].to.longitude;
Lat = MIFLINES[leastDistanceIndex].to.latitude;
MIFLINES[leastDistanceIndex].flags = MIFLINES_FLAG_USED;
}
if(pMIFPASSEDDATA->ROUTESrecordID == 16)
{
nI = 1;
}
//
// Set all the lines to be contiguous (no "pen up")
// and set all the nodes to NO_RECORD
//
for(nI = 1; nI < numMIFLINES; nI++)
{
pMIFLINES[nI].from.latitude = pMIFLINES[nI - 1].to.latitude;
pMIFLINES[nI].from.longitude = pMIFLINES[nI - 1].to.longitude;
}
//
// Nodes and associated stops refer to the "from" element of the
// first line kept in MIFLINES, and to the "to" element of each
// subsequent line.
//
// We got the first node in the pattern earlier
//
pMIFLINES[0].from.latitude = NODES.latitude;
pMIFLINES[0].from.longitude = NODES.longitude;
pMIFLINES[0].from.flags = 0;
pMIFLINES[0].from.NODESrecordID = firstNODESrecordID;
pMIFLINES[0].from.associatedStopNODESrecordID = firstStopNODESrecordID;
pMIFLINES[0].from.associatedStopNumber = firstStopNumber;
pMIFLINES[0].to.flags = 0;
pMIFLINES[0].to.NODESrecordID = NO_RECORD;
pMIFLINES[0].to.associatedStopNODESrecordID = NO_RECORD;
pMIFLINES[0].to.associatedStopNumber = NO_RECORD;
//
// Clear out the (yet) unused portions of the structure
//
for(nI = 1; nI < numMIFLINES; nI++)
{
pMIFLINES[nI].from.flags = 0;
pMIFLINES[nI].from.NODESrecordID = NO_RECORD;
pMIFLINES[nI].from.associatedStopNODESrecordID = NO_RECORD;
pMIFLINES[nI].from.associatedStopNumber = NO_RECORD;
pMIFLINES[nI].to.flags = 0;
pMIFLINES[nI].to.NODESrecordID = NO_RECORD;
pMIFLINES[nI].to.associatedStopNODESrecordID = NO_RECORD;
pMIFLINES[nI].to.associatedStopNumber = NO_RECORD;
}
//
// And record the nodes/stops as encountered
//
pMIFPASSEDDATA->NODESrecordIDs[0] = PATTERNS.NODESrecordID;
pMIFPASSEDDATA->numNodes = 1;
//
// Put the rest of the pattern nodes/stops into the MIFLINES structure
//
MIFPointer = 0;
previousNODESrecordID = PATTERNS.NODESrecordID;
rcode2 = btrieve(B_GETNEXT, TMS_PATTERNS, &PATTERNS, &PATTERNSKey2, 2);
while(rcode2 == 0 &&
PATTERNS.ROUTESrecordID == pMIFPASSEDDATA->ROUTESrecordID &&
PATTERNS.SERVICESrecordID == pMIFPASSEDDATA->SERVICESrecordID &&
PATTERNS.directionIndex == pMIFPASSEDDATA->directionIndex &&
PATTERNS.PATTERNNAMESrecordID == pMIFPASSEDDATA->PATTERNNAMESrecordID)
{
pMIFPASSEDDATA->NODESrecordIDs[pMIFPASSEDDATA->numNodes] = PATTERNS.NODESrecordID;
pMIFPASSEDDATA->numNodes++;
//
// If we encounter a mid-trip layover, it gets a special record.
//
// From Orbital:
// "To declare a mid-trip layover, the pattern in the VTP File must contain exactly one record where the
// 'from timepoint name' and 'to timepoint name' fields are set to exactly the same non-null value.
// In the example case, they should both be set to 'LTCA'. It wouldn't work to set one to 'LTCA' and
// the other to 'LTCD' because our software would interpret this as two distinct timepoints. The following
// field pairs must also match: 'from timepoint id' = 'to timepoint id', 'from stop name' = 'to stop name',
// 'from stop id' = 'to stop id', 'from longitude' = 'to longitude', 'from latitude' = 'to latitude'.
//
if(NodesEquivalent(previousNODESrecordID, PATTERNS.NODESrecordID, &travelTime))
{
pMIFLINES[MIFPointer - 1].flags |= MIFLINES_FLAG_NEXTISSAMELOCATION; // MIFPointer can't be 0
}
//
// Not a mid-trip layover
//
else
{
//
// Isolate the node
//
if(PATTERNS.flags & PATTERNS_FLAG_BUSSTOP)
{
NODESKey0.recordID = PATTERNS.NODESrecordID;
btrieve(B_GETEQUAL, TMS_NODES, &NODES, &NODESKey0, 0);
toNODESrecordID = NO_RECORD;
toStopNODESrecordID = NODES.recordID;
toStopNumber = (pMIFPASSEDDATA->directionIndex == 0 ? NODES.OBStopNumber : NODES.IBStopNumber);
if(toStopNumber <= 0)
{
// toStopNumber = NODES.number;
strncpy(tempString, NODES.longName, NODES_LONGNAME_LENGTH);
trim(tempString, NODES_LONGNAME_LENGTH);
toStopNumber = atol(&tempString[4]);
}
}
else
{
NODESrecordID = NodeEquivalenced(PATTERNS.NODESrecordID);
NODESKey0.recordID = NODESrecordID == NO_RECORD ? PATTERNS.NODESrecordID : NODESrecordID;
btrieve(B_GETEQUAL, TMS_NODES, &NODES, &NODESKey0, 0);
toNODESrecordID = NODES.recordID;
toStopNumber = (pMIFPASSEDDATA->directionIndex == 0 ? NODES.OBStopNumber : NODES.IBStopNumber);
toStopNODESrecordID = NO_RECORD;
if(toStopNumber <= 0)
{
toStopNumber = NODES.number;
}
if(toStopNumber > 0)
{
bFound = FALSE;
rcode2 = btrieve(B_GETFIRST, TMS_NODES, &NODES, &NODESKey0, 0);
while(rcode2 == 0)
{
if((NODES.flags & NODES_FLAG_STOP) && NODES.number == toStopNumber)
{
toStopNODESrecordID = NODES.recordID;
bFound = TRUE;
break;
}
rcode2 = btrieve(B_GETNEXT, TMS_NODES, &NODES, &NODESKey0, 0);
}
if(!bFound)
{
NODESrecordID = NodeEquivalenced(PATTERNS.NODESrecordID);
NODESKey0.recordID = NODESrecordID == NO_RECORD ? PATTERNS.NODESrecordID : NODESrecordID;
btrieve(B_GETEQUAL, TMS_NODES, &NODES, &NODESKey0, 0);
}
else
{
strncpy(tempString, NODES.longName, NODES_LONGNAME_LENGTH);
trim(tempString, NODES_LONGNAME_LENGTH);
toStopNumber = atol(&tempString[4]);
}
}
}
//
// Find the closest endpoint to the node/stop
//
if(pMIFPASSEDDATA->ROUTESrecordID == 21)
{
int nx = 1;
}
leastDistance = 10000;
leastDistanceIndex = NO_RECORD;
for(nI = MIFPointer; nI < numMIFLINES; nI++)
{
distance = GreatCircleDistance(NODES.longitude, NODES.latitude, pMIFLINES[nI].to.longitude, pMIFLINES[nI].to.latitude);
if(distance < leastDistance)
{
leastDistance = distance;
leastDistanceIndex = nI;
}
}
if(leastDistanceIndex == NO_RECORD)
{
sprintf(tempString, "***ERR Least distance index remained at NO_RECORD in search for subsequent nodes\r\n");
_lwrite(pMIFPASSEDDATA->hfErrorLog, tempString, strlen(tempString));
strncpy(szarString, NODES.abbrName, NODES_ABBRNAME_LENGTH);
trim(szarString, NODES_ABBRNAME_LENGTH);
sprintf(tempString, "***INF This function failed at or before the search for %s on the pattern\r\n", szarString);
_lwrite(pMIFPASSEDDATA->hfErrorLog, tempString, strlen(tempString));
return(NO_RECORD);
}
//
// Set the "to" portion of the line to the located node's coordinates
//
pMIFLINES[leastDistanceIndex].to.flags = PATTERNS.flags;
pMIFLINES[leastDistanceIndex].to.latitude = NODES.latitude;
pMIFLINES[leastDistanceIndex].to.longitude = NODES.longitude;
pMIFLINES[leastDistanceIndex].to.NODESrecordID = toNODESrecordID;
pMIFLINES[leastDistanceIndex].to.associatedStopNumber = toStopNumber;
pMIFLINES[leastDistanceIndex].to.associatedStopNODESrecordID = toStopNODESrecordID;
//
// If we're not on the last one, set the "from" portion of
// the next line to the located node's coordinates
//
if(leastDistanceIndex < numMIFLINES - 1)
{
pMIFLINES[leastDistanceIndex + 1].from = pMIFLINES[leastDistanceIndex].to;
}
//
// Reset the MIFPointer to the next line and cycle back
//
MIFPointer = leastDistanceIndex + 1;
}
previousNODESrecordID = PATTERNS.NODESrecordID;
rcode2 = btrieve(B_GETNEXT, TMS_PATTERNS, &PATTERNS, &PATTERNSKey2, 2);
}
//
// Go through MIFLINES and fill in the "blanks" where lines connect to just other lines
//
// Do "from" first
//
long setIndex = 0;
nI = -1;
while(nI < numMIFLINES - 1)
{
nI++;
if(nI >= numMIFLINES)
{
break;
}
if(pMIFLINES[nI].from.NODESrecordID == NO_RECORD &&
pMIFLINES[nI].from.associatedStopNODESrecordID == NO_RECORD)
{
for(bFound = FALSE, nJ = nI; nJ < numMIFLINES; nJ++)
{
if(pMIFLINES[nJ].from.NODESrecordID == NO_RECORD &&
pMIFLINES[nJ].from.associatedStopNODESrecordID == NO_RECORD)
{
continue;
}
bFound = TRUE;
break;
}
if(!bFound)
{
nJ = numMIFLINES;
}
for(nK = nI; nK < nJ; nK++)
{
pMIFLINES[nK].from.NODESrecordID = pMIFLINES[setIndex].from.NODESrecordID;
pMIFLINES[nK].from.associatedStopNODESrecordID = pMIFLINES[setIndex].from.associatedStopNODESrecordID;
pMIFLINES[nK].from.associatedStopNumber = pMIFLINES[setIndex].from.associatedStopNumber;
pMIFLINES[nK].from.flags = pMIFLINES[setIndex].from.flags;
}
nI = nK - 1;
}
setIndex = nI;
}
//
// Now do "to"
//
nI = -1;
while(nI < numMIFLINES - 1)
{
nI++;
if(nI >= numMIFLINES)
{
break;
}
if(pMIFLINES[nI].to.NODESrecordID == NO_RECORD &&
pMIFLINES[nI].to.associatedStopNODESrecordID == NO_RECORD)
{
for(nJ = nI; nI < numMIFLINES - 1; nJ++)
{
if(pMIFLINES[nJ].to.NODESrecordID == NO_RECORD &&
pMIFLINES[nJ].to.associatedStopNODESrecordID == NO_RECORD)
{
continue;
}
for(nK = nI; nK < nJ; nK++)
{
pMIFLINES[nK].to.NODESrecordID = pMIFLINES[nJ].to.NODESrecordID;
pMIFLINES[nK].to.associatedStopNODESrecordID = pMIFLINES[nJ].to.associatedStopNODESrecordID;
pMIFLINES[nK].to.associatedStopNumber = pMIFLINES[nJ].to.associatedStopNumber;
pMIFLINES[nK].to.flags = pMIFLINES[nJ].to.flags;
}
nI = nK - 1;
break;
}
}
}
//
//
// Close the file
//
MIFFile.Close();
//
// All done
//
// Return MIFPointer instead of numMIFLINES in case we short turned
//
return(MIFPointer);
}