-
Notifications
You must be signed in to change notification settings - Fork 2
/
LDMIOTrack.java
270 lines (242 loc) · 6.84 KB
/
LDMIOTrack.java
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
/* Copyright (C) 2014,2015 Maximilian Diedrich
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>
*/
package de.hu_berlin.informatik.spws2014.ImagePositionLocator;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
/**
* Main backend for LDM.
* Does not provide realtime saving.
* Saves only when save is called.
*/
public class LDMIOTrack implements ILDMIOHandler {
String filename;
//Denotes the highest supported .track version
int protVersionNumber = 3;
ArrayList<GpsPoint> gpspath;
ArrayList<Marker> markers;
long time;
/**
* Reads track format.
*/
@SuppressWarnings("unchecked")
private boolean readTrackFile(ObjectInputStream ois) throws IOException {
protVersionNumber = ois.readInt();
try {
switch (protVersionNumber) {
case 3:
gpspath = (ArrayList<GpsPoint>) ois.readObject();
markers = (ArrayList<Marker>) ois.readObject();
break;
default:
System.err.println("Unknown file version number: "
+ protVersionNumber
+ " encountered while reading DataManager.");
return false;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
return false;
}
return true;
}
private void readBinary(String pathToFile) throws IOException {
File dbgTrackfile = new File(pathToFile);
FileInputStream fis = new FileInputStream(dbgTrackfile);
ObjectInputStream ois = new ObjectInputStream(fis);
readTrackFile(ois);
ois.close();
fis.close();
}
public LDMIOTrack(String pathToFile) throws IOException {
filename = pathToFile;
File dbgTrackfile = new File(pathToFile);
boolean isFileValid = dbgTrackfile.exists();
if (isFileValid) {
BufferedReader br = new BufferedReader(new FileReader(dbgTrackfile));
br.mark(1);
int first_byte = br.read();
br.reset();
if (first_byte < '0' || first_byte > '9') {
readBinary(pathToFile);
} else {
gpspath = new ArrayList<GpsPoint>();
markers = new ArrayList<Marker>();
int version = Integer.valueOf(br.readLine());
int gpspath_size = Integer.valueOf(br.readLine());
for (int i = 0; i < gpspath_size; i++) {
String line = br.readLine();
String[] values = line.split(" ");
gpspath.add(new GpsPoint(Double.valueOf(values[1]), Double.valueOf(values[0]), Long.valueOf(values[2])));
}
int markers_size = Integer.valueOf(br.readLine());
for (int i = 0; i < markers_size; i++) {
String line = br.readLine();
String[] values = line.split(" ");
Point2D p = new Point2D(Double.valueOf(values[0]), Double.valueOf(values[1]));
GpsPoint g = new GpsPoint(Double.valueOf(values[3]), Double.valueOf(values[2]), Long.valueOf(values[4]));
markers.add(new Marker(p, Long.valueOf(values[5]), g));
}
br.close();
}
}
if (!isFileValid || gpspath == null || markers == null) {
gpspath = new ArrayList<GpsPoint>();
markers = new ArrayList<Marker>();
}
}
@Override
public ArrayList<Marker> getAllMarkers() {
return markers;
}
@Override
public Marker getMarker(Point2D imgpoint) {
for (Marker m : markers) {
if (m.imgpoint.equals(imgpoint))
return m;
}
return null;
}
@Override
public Marker getMarker(GpsPoint realpoint) {
for (Marker m : markers) {
if (m.realpoint.equals(realpoint))
return m;
}
return null;
}
@Override
public Marker getLastMarker() {
if (markers.size() == 0)
return null;
else
return markers.get(markers.size()-1);
}
@Override
public boolean removeMarker(Point2D imgpoint) {
Marker m = getMarker(imgpoint);
if (m == null)
return false;
markers.remove(m);
return true;
}
@Override
public boolean removeMarker(GpsPoint realpoint) {
Marker m = getMarker(realpoint);
if (m == null)
return false;
markers.remove(m);
return true;
}
@Override
public ArrayList<GpsPoint> getAllGpsPoints() {
return gpspath;
}
@Override
public GpsPoint getLastGpsPoint() {
if (gpspath.size() == 0)
return null;
else
return gpspath.get(gpspath.size()-1);
}
@Override
public boolean removeMarker(Marker m) {
return markers.remove(m);
}
@Override
public void removeAllMarkers() {
markers = new ArrayList<Marker>();
}
@Override
public boolean removeGpsPoint(GpsPoint p) {
return gpspath.remove(p);
}
@Override
public void removeAllGpsPoints() {
gpspath = new ArrayList<GpsPoint>();
}
@Override
public void addMarker(Marker m) {
markers.add(m);
}
@Override
public void addGpsPoint(GpsPoint p) {
gpspath.add(p);
}
@Override
public void setLastGpsPointTime(long unixTime) {
time = unixTime;
}
@Override
public long getLastGpsPointTime() {
return this.time;
}
/**
* Prints two csv tables to ps.
* The first represents all the known GpsPoints
* while the second consists of all Markers.
* @param ps
*/
public void printValuesAsCSV(PrintStream ps) {
ps.println("type,name,latitude,longitude");
for (GpsPoint p : getAllGpsPoints())
ps.println("P, " + p.time + ", " + p.latitude + ", " + p.longitude);
ps.println("name,name2,latitude,longitude");
for (Marker m : getAllMarkers())
ps.println(m.imgpoint.x + ", " + m.imgpoint.y + ", "
+ m.realpoint.latitude + ", " + m.realpoint.longitude);
}
/**
* Writes into track format.
*/
private void writeTrackFile(ObjectOutputStream oos) throws IOException {
oos.writeInt(protVersionNumber);
oos.writeObject(gpspath);
oos.writeObject(markers);
oos.writeLong(time);
}
@Override
public void save() {
File dbgTrackfile = new File(filename);
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(dbgTrackfile));
bw.write("4\n");
bw.write(gpspath.size() + "\n");
for (GpsPoint p : gpspath) {
bw.write(p.latitude + " " + p.longitude + " " + p.time + "\n");
}
bw.write(markers.size() + "\n");
for (Marker m : markers) {
bw.write(m.imgpoint.x + " " + m.imgpoint.y + " " +
m.realpoint.latitude + " " + m.realpoint.longitude + " " + m.realpoint.time + " " +
m.time + "\n");
}
bw.write(time + "\n");
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}