/
trajcomp_geolife.hpp
executable file
·135 lines (111 loc) · 3.41 KB
/
trajcomp_geolife.hpp
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
#ifndef TRAJCOMP_GEOLIFE_HPP
#define TRAJCOMP_GEOLIFE_HPP
#include "trajcomp.hpp"
#include<fstream>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include<algorithm>
namespace trajcomp{
// Dataset reader with time
/*
* Line 1...6 are useless in this dataset, and can be ignored. Points are described in following lines, one for each line.
Field 1: Latitude in decimal degrees.
Field 2: Longitude in decimal degrees.
Field 3: All set to 0 for this dataset.
Field 4: Altitude in feet (-777 if not valid).
Field 5: Date - number of days (with fractional part) that have passed since 12/30/1899.
Field 6: Date as a string.
Field 7: Time as a string.
Note that field 5 and field 6&7 represent the same date/time in this dataset. You may use either of them.
*
* */
template<class TrajectoryType, size_t Lat=0,size_t Lon=1,size_t Alt=2, size_t Time=3>
class geolife_reader
{
public:
std::string geolife_base;
size_t MAX_DIR;
geolife_reader(std::string base, size_t maxdir):geolife_base(base),MAX_DIR(maxdir)
{};
geolife_reader(std::string base):geolife_base(base),MAX_DIR(200)
{}
TrajectoryType handle_file (const char *dir, const char *fname)
{
if (fname[0] == '.') return 0; // ignore ., .. and hidden files
char buffer[1024];
strncpy(buffer,dir,1024);
strncat(buffer,fname,1024);
std::ifstream infile;
infile.open(std::string(buffer));
if (!infile)
{
throw(std::runtime_error("File not found."));
}
TrajectoryType traj(4); // 4-dimensional here
std::string line;
int skip=6; // The first 6 lines are useless (see top)
while (std::getline(infile, line))
{
if (skip-->0) continue;
typename TrajectoryType::ElementType element;
element.resize(4);
std::replace( line.begin(), line.end(), ',', ' ');
//std::cout << line << std::endl;
std::stringstream iss(line);
typename TrajectoryType::ElementType::value_type value;
if (! (iss >> element[ Lat ]))
throw(std::runtime_error("Parsing Error at Lat"));
if (! (iss >> element[Lon]))
throw(std::runtime_error("Parsing Error at Lon"));
size_t tmp;
if (! (iss >> tmp))
throw(std::runtime_error("Parsing Error at Reserved"));
if (! (iss >> element[Alt]))
throw(std::runtime_error("Parsing Error at Alt"));
if (! (iss >> element[Time]))
throw(std::runtime_error("Parsing Error at Time"));
//std::cout << tools::make_string(element) << std::endl;
traj.push_back(element);
}
//traj.summary();
return traj;
}
int load(std::vector<TrajectoryType> &db) //std::vector<TimeType> time
{
std::cout << "Loading from Base " << geolife_base << std::endl;
size_t i;
for (i=0; i < MAX_DIR ; i++)
{
char buffer[1024];
snprintf(buffer,1024,"%s/Data/%03d/Trajectory/",geolife_base.c_str(),i);
#ifdef VERBOSE_GEOLIFE_READER
std::cout <<"Going to enumerate directory" << buffer << std::endl;
#endif
DIR *dp;
struct dirent *ep;
dp = opendir (buffer);
if (dp != NULL)
{
TrajectoryType t;
while ( (ep = readdir (dp)) )
{
t = handle_file (buffer,ep->d_name);
if (t.size() != 0)
{
db.push_back(t);
}else{
std::cout << "Warning: " << buffer << "led to empty trajectory." << std::endl;
}
}
(void) closedir (dp);
}
else
return i;
//throw(std::runtime_error("geolife: Couldn't open the directory"));
}
return i;
}
};
}
#endif