int trj_id = 1;
int eps = 1;
GPSPoint lastPt = null;
GPSPoint pt = null;
Trajectory trj = new Trajectory();
while(results.next()){
Episode newEpisode;
String tag = results.getString("tag");
double lat = results.getDouble("longitude"); //inverted on purpose
double lng = results.getDouble("latitude");
double northing = results.getDouble("northing");
double easting = results.getDouble("easting");
int userid = results.getInt("userid");
Timestamp datetime = results.getTimestamp("datetime");
String name = "";
String desc = "";
if(tag.equalsIgnoreCase("B")){
if(trj.getLastEpisode() != null){
Object[] values = null;
values = new Object[9];
values[0] = (Integer)trj_id; //ID
values[1] = trj.getLifeSpan().getStart(); //START
values[2] = trj.getLifeSpan().getEnd(); //END
values[3] = trj.getMovingObject().getId(); //MOVING_ENTITY
values[4] = ""; //TYPE
values[5] = trj.getAvgSpeed(); //AVG_SPEED
values[6] = trj.getAvgTravelTime(); //AVG_TRAVEL_TIME
values[7] = 0; //GROUP_ID
values[8] = trj.getGeometry();//GEOM
if(values[8] != null){
//populate values from trj obj;
trj_table.insertRow(values);
//populate the list of episodes
ArrayList<Episode> episodes = trj.getEpisodes();
for(int j = 0 ; j < episodes.size() ; j++ ){
Episode episode = episodes.get(j);
Object[] vals = new Object[9];
String type = "";
if(episode instanceof Move ) type = "MOVE";
else type = "STOP";
vals[0] = (Integer)eps; //ID
vals[1] = episode.getLifeSpan().getStart(); //START
vals[2] = episode.getLifeSpan().getEnd(); //END
vals[3] = trj.getMovingObject().getId(); //MOVING_ENTITY
vals[4] = type; //TYPE
vals[5] = episode.getAvgSpeed(); //AVG_SPEED
vals[6] = episode.getVarSpeed(); //AVG_TRAVEL_TIME
vals[7] = (Integer)trj_id; //GROUP_ID
vals[8] = episode.getGeometry(); //GEOM
epi_table.insertRow(vals);
eps++;
}
logger.info(trj_id+" trajectories inserted");
trj_id++;
}
}
trj = new Trajectory();
trj.setMovingObject(new MovingObject(userid));
}
if(pt != null)lastPt = pt;
pt = new GPSPoint(lat,lng,northing,easting,userid,datetime,name,desc);
pt.comparePrePoint(lastPt);
Episode lastEpisode = trj.getLastEpisode();
if(tag.equalsIgnoreCase("B") || tag.equalsIgnoreCase("S")){
if(lastEpisode != null && lastEpisode instanceof Move){
lastEpisode.merge(new Move(pt));
}
newEpisode = new Stop(pt);
}
else{
if(lastEpisode != null && lastEpisode instanceof Stop){
newEpisode = new Move(lastPt);
newEpisode.merge(new Move(pt));
}
else newEpisode = new Move(pt);
}
trj.addEpisode(newEpisode);
i++;
}
}
catch(SQLException e){
e.printStackTrace();