forked from chenljian/TrajectorySimilarity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrajectorySimilarityAlgebra.h
656 lines (605 loc) · 15.9 KB
/
TrajectorySimilarityAlgebra.h
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
/******************************************************************************
----
This file is part of SECONDO.
Copyright (C) 2004-2010, University in Hagen, Faculty of Mathematics and
Computer Science, Database Systems for New Applications.
SECONDO is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
SECONDO 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with SECONDO; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
----
***************************************************************************/
#ifndef __TRAJECTORYSIMILARITY_ALGEBRA_H__
#define __TRAJECTORYSIMILARITY_ALGEBRA_H__
#include <iostream>
#include <ctime>
#include <DateTime.h>
#include <sys/types.h>
#include <dirent.h>
#include "NestedList.h"
#include "ListUtils.h"
#include <queue>
#include "../../Tools/Flob/DbArray.h"
#include "SpatialAlgebra.h"
using namespace std;
namespace tsm{
const int PATH_MAX_SIZE = 128;
class MyRecord {
public:
char rstr[1024]; //the string of record;
bool is_defined;
bool str_defined;
int id; //the record id of the car;
int yy, mm, dd, h, m, s; //the date, year, month, day, hour, minute, second;
double X,Y;
MyRecord();
void SetRstr(char *str); //set the record string;
virtual int FreshData(); //fresh the data of record string;
virtual int CheckStr(); //check the string of record whether is right;
int Clear(); //clear all the data of record
bool IsDefined(){ return is_defined;} //return the record whether it is available;
friend ostream & operator <<(ostream &out, const MyRecord &right);
};
MyRecord::MyRecord() {
id = yy = mm = dd = h = m = s = 0;
X = Y = 0.0;
is_defined = false;
str_defined = false;
memset(rstr, 0, sizeof(rstr));
}
bool IsNumber(char c)
{
if(c <= '9' && c >= '0')
return true;
else
return false;
}
int MyRecord::FreshData()
{
if(false == str_defined)
{
Clear();
return -1;
}
if(-1 == CheckStr())
{
is_defined = false;
Clear();
return -1;
}
sscanf(rstr, "%d,%d-%d-%d %d:%d:%d,%lf,%lf", &id, &yy, &mm, &dd, &h, &m, &s, &X, &Y);
#ifdef DEBUG
cout<<"record string: "<<rstr<<endl;
cout<<"id: "<<id<<endl;
cout<<"yy: "<<yy<<endl;
cout<<"mm: "<<mm<<endl;
cout<<"dd: "<<dd<<endl;
cout<<"h: "<<h<<endl;
cout<<"m: "<<m<<endl;
cout<<"s: "<<s<<endl;
cout<<"X: "<<X<<endl;
cout<<"Y: "<<Y<<endl;
#endif
is_defined = true;
return 1;
}
int MyRecord::CheckStr() // check the format of the string whether it is right
{
int pointer = 0, status = 0;
char temp_c;
while('\0' != rstr[pointer])
{
temp_c = rstr[pointer++];
switch(status)
{
case 0:
if(IsNumber(temp_c))
{
status = 1;
}
else
status = 10;
break;
case 1:
if(IsNumber(temp_c))
{
status = 1;
}
else if(temp_c == ',')
{
status = 2;
}
else
status = 10;
break;
case 2:
if(IsNumber(temp_c))
{
status = 2;
}
else if(temp_c == '-')
status = 3;
else if(temp_c == ' ')
status = 4;
else
status = 10;
break;
case 3:
if(IsNumber(temp_c))
status = 2;
else
status = 10;
break;
case 4:
if(IsNumber(temp_c))
status = 4;
else if(':' == temp_c)
status = 5;
else if(',' == temp_c)
status = 6;
else
status = 10;
break;
case 5:
if(IsNumber(temp_c))
status = 4;
else
status = 10;
break;
case 6:
if(IsNumber(temp_c))
status = 6;
else if('.' == temp_c)
status = 7;
else
status = 10;
break;
case 7:
if(IsNumber(temp_c) || temp_c == '\r')
status = 7;
else if(',' == temp_c)
status = 6;
else
status = 10;
break;
default:
return -1;
}
}
if(7 == status)
return 1;
else
return -1;
}
int MyRecord::Clear()
{
id = yy = mm = dd = h = m = s = 0;
X = Y = 0.0;
is_defined = false;
memset(rstr, 0, sizeof(rstr));
return 1;
}
void freeFileList(list<char *> *fl)
{
if(NULL == fl)
return;
while(! fl->empty())
{
delete fl->front();
fl->pop_front();
}
delete fl;
}
char *getAbsolutePath()
{
char tmp[PATH_MAX_SIZE], *result;
int i,cnt;
cnt = readlink("/proc/self/exe", tmp, PATH_MAX_SIZE);
if(cnt < 0 || cnt >= PATH_MAX_SIZE)
{
cout<<"Error in getAbsolutePath: readlink\n"<<endl;
return NULL;
}
i = cnt;
for(i = cnt; i >= 0; i --)
{
if(tmp[i] == '/')
{
tmp[i+1] = '\0';
result = new char[sizeof(char)*(i+2)];
strcpy(result,tmp);
return result;
}
}
return NULL;
}
char *getAbsolutePath(char *basePath)
{
char tmp[PATH_MAX_SIZE], *result;
if(basePath == NULL)
{
return getAbsolutePath();
}
//cout<<"test in get absolute path function!"<<endl;
if(basePath[0] != '/'){
if(NULL == realpath(basePath, tmp)){
cout<<"Error in getAbsolutePath: realpath\n"<<endl;
return NULL;
}
strcat(tmp, "/");
}else{
strcpy(tmp, basePath);
if(tmp[strlen(tmp)-1] != '/'){
strcat(tmp, "/");
}
}
result = new char[sizeof(char)*(strlen(tmp)+1)];
strcpy(result, tmp);
//cout<<"test2 in get absolute path function!"<<endl;
return result;
}
bool IsDir(char *filename)
{
DIR *dir;
if(filename == NULL){
return false;
}
if((dir = opendir(filename)) != NULL){
closedir(dir);
return true;
}
return false;
}
bool IsFile(char *filename)
{
if(filename == NULL){
return false;
}
if(access(filename, F_OK) == 0){
return true;
}
return false;
}
bool strContain(const char *source, const char *dest)
{
int sptr=0, dptr=0;
if(source == NULL){
return false;
}
if(dest == NULL){
return true;
}
while('\0' != source[sptr] && '\r' != source[sptr]){
if(source[sptr++] == dest[0]){
dptr = 1;
while('\0' != dest[dptr] && '\r' != dest[dptr] && source[sptr] == dest[dptr]){
sptr++;
dptr++;
}
if('\0' == dest[dptr] || '\r' == dest[dptr]){
//cout<<"sptr: "<<sptr<<endl;
//cout<<"dptr: "<<dptr<<endl;
return true;
}
}
}
return false;
}
list<char*> *getAllFileList(char *file_dir = NULL, char *subname = NULL)
{
DIR *dir;
struct dirent *ptr;
char *cur_dir, tmp[PATH_MAX_SIZE], *basepath;
queue<char*> dirs;
list<char*> *result = NULL;
//cur_dir = getAbsolutePath();
//cout<<"test in get all files function!"<<endl;
if(file_dir == NULL)
{
cout<<"test1 in get all files function!"<<endl;
if((cur_dir = getAbsolutePath()) == NULL)
{
cout<<"Error in get the current path!"<<endl;
return NULL;
}
}else if(IsDir(file_dir)){
cout<<"test2 in get all files function!"<<endl;
if((cur_dir = getAbsolutePath(file_dir)) == NULL){
cout<<"Error in get the directory : "<<file_dir<<endl;
//return NULL;
}
//cur_dir = new char[(strlen(file_dir)+1)*sizeof(char)];
//strcpy(cur_dir, file_dir);
}else if(IsFile(file_dir)){ //file_dir is only a file
cout<<"test3 in get all files function!"<<endl;
result = new list<char*>();
basepath = new char[(strlen(file_dir)+1)*sizeof(char)];
strcpy(basepath, file_dir);
result->push_back(basepath);
return result;
}else{
cout<<"Error: "<<file_dir <<" is not a file or directory!"<<endl;
return NULL;
}
//
//basepath = new char[sizeof(char)*(strlen(cur_dir+1))];
//strcpy(basepath, cur_dir);
dirs.push(cur_dir);
//cout<<"test!"<<endl;
result = new list<char*>();
while(! dirs.empty())
{
cur_dir = dirs.front();
dirs.pop();
if((dir = opendir(cur_dir)) == NULL)
{
cout<<"Error in open the current directory!"<<endl;
//return NULL;
cout<<cur_dir<<endl;
continue;
}
//free(cur_dir);
//cur_dir = NULL;
//
while((ptr = readdir(dir)) != NULL)
{
if(ptr->d_type == 10 || strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)
{
continue;
}
else if(ptr->d_type == 8) // file
{
sprintf(tmp, "%s%s", cur_dir, ptr->d_name);
if(strContain(tmp, subname)){
basepath = new char[(strlen(tmp)+1)*sizeof(char)];
strcpy(basepath, tmp);
result->push_back(basepath);
}
}
else if(ptr->d_type == 4)
{
sprintf(tmp, "%s%s", cur_dir, ptr->d_name);
strcat(tmp, "/");
basepath = new char[(strlen(tmp)+1)*sizeof(char)];
strcpy(basepath, tmp);
dirs.push(basepath);
}
else
{
continue;
}
}
closedir(dir);
}
return result;
}
class GPSRecord
{
public:
int Oid; //object id
int yy,mm,dd,h,m,s; //date and time
double lo, la; //longitude and latitude
};
class RecordManager
{
private:
bool isDef; //if the recordmanager is av
bool isDir; //if the input is directory, it is true, or false;
char *name; // the name of input;
char *extension;
MyRecord temprecord;
list<char *> *files; //if input is a directory, and store all the files in this directory;
list<char *>::iterator fileit;
ifstream curfile;
public:
RecordManager()
{
isDef = false;
isDir = false;
extension = NULL;
name = NULL;
files = NULL;
}
~RecordManager()
{
if(name){
delete name;
}
if(extension){
delete extension;
}
if(curfile.is_open()){
curfile.close();
}
freeFileList(files);
}
bool setName(const char *str)
{
if(name){
delete name;
}
name = new char[strlen(str)+1];
if(name == NULL){
return false;
}
strcpy(name, str);
return true;
}
bool setExtension(const char *str)
{
if(extension){
delete extension;
}
extension = new char[strlen(str)+1];
if(extension == NULL){
return false;
}
strcpy(extension, str);
return true;
}
char *getName(){ return name;}
bool isDirectory(){ return isDir;}
bool isDefined(){ return isDef;}
void resetDefined(){ isDef = false;}
bool Init()
{
files = getAllFileList(name, extension);
if(NULL == files){
cout<<"ERROR: There is not files in "<<name<<"!"<<endl;
return false;
}
if(files->size() <= 0){
cout<<"ERROR: There is not record in "<<name<<"!"<<endl;
return false;
}
fileit = files->begin();
if(! openNextFile()){
// ERROR: can not open the files!
return false;
}
return true;
}
bool openNextFile()
{
if(fileit == files->end()){
cout<<"Warning: There is no file to open!"<<endl;
return false;
}
if(curfile.is_open()){
curfile.close();
}
cout<<"Warning: Trying to open file "<<*fileit<<endl;
curfile.open(*fileit);
fileit++;
while((!curfile.is_open()) && fileit != files->end()){
cout<<"Warning: Trying to open file "<<*fileit<<endl;
curfile.open(*fileit);
fileit++;
}
if((!curfile.is_open()) && (fileit == files->end())){
cout<<"Warning: There is no file to open!"<<endl;
return false;
}
return true;
}
bool getNextRecord(GPSRecord &gpsr)
{
char buf[1024];
if(! curfile.is_open() || curfile.eof()){
if(! openNextFile()){
return false;
}
}
curfile.getline(buf, 1024);
// check the string format
while(1 != CheckStringFormat(buf)){
//check whether the file is to end~
if(curfile.eof()){
if(! openNextFile()){
return false;
}
}
curfile.getline(buf, 1024);
}
// get data from string
sscanf(buf, "%d,%d-%d-%d %d:%d:%d,%lf,%lf", &gpsr.Oid, &gpsr.yy, &gpsr.mm, &gpsr.dd, &gpsr.h, &gpsr.m, &gpsr.s, &gpsr.lo, &gpsr.la);
return true;
}
int CheckStringFormat(char *rstr) // check the format of the string whether it is right
{
int pointer = 0, status = 0;
char temp_c;
while('\0' != rstr[pointer])
{
temp_c = rstr[pointer++];
switch(status)
{
case 0:
if(IsNumber(temp_c))
{
status = 1;
}
else
status = 10;
break;
case 1:
if(IsNumber(temp_c))
{
status = 1;
}
else if(temp_c == ',')
{
status = 2;
}
else
status = 10;
break;
case 2:
if(IsNumber(temp_c))
{
status = 2;
}
else if(temp_c == '-')
status = 3;
else if(temp_c == ' ')
status = 4;
else
status = 10;
break;
case 3:
if(IsNumber(temp_c))
status = 2;
else
status = 10;
break;
case 4:
if(IsNumber(temp_c))
status = 4;
else if(':' == temp_c)
status = 5;
else if(',' == temp_c)
status = 6;
else
status = 10;
break;
case 5:
if(IsNumber(temp_c))
status = 4;
else
status = 10;
break;
case 6:
if(IsNumber(temp_c))
status = 6;
else if('.' == temp_c)
status = 7;
else
status = 10;
break;
case 7:
if(IsNumber(temp_c) || temp_c == '\r')
status = 7;
else if(',' == temp_c)
status = 6;
else
status = 10;
break;
default:
return -1;
}
}
if(7 == status)
return 1;
else
return -1;
}
};
}
#endif