RelationalTable.DOUBLE,
RelationalTable.INTEGER,
RelationalTable.LINESTRING};
String trj_tableName = "MILAN_TRAJECTORIES";
RelationalTable trj_table = new RelationalTable(trj_tableName,trj_columns);
trj_table.setTypes(trj_types);
trj_table.setForced(true);
trj_table.loadObject(destinationConnection);
//open episodes table
String[] epi_columns = { "ID",
"START",
"END",
"MOVING_ENTITY",
"TYPE",
"AVG_SPEED",
"VAR_SPEED",
"TRJ_ID",
"geom"};
Integer[] epi_types = { RelationalTable.INTEGER,
RelationalTable.TIMESTAMP,
RelationalTable.TIMESTAMP,
RelationalTable.INTEGER,
RelationalTable.VARCHAR,
RelationalTable.DOUBLE,
RelationalTable.DOUBLE,
RelationalTable.INTEGER,
RelationalTable.LINESTRING};
String epi_tableName = "MILAN_EPISODES";
RelationalTable epi_table = new RelationalTable(epi_tableName,epi_columns);
epi_table.setTypes(epi_types);
epi_table.setForced(true);
epi_table.loadObject(destinationConnection);
//open episode table
//populate trajectory list
//for every row
try{
String query = "SELECT * " +
" FROM milan_gmd3" +
" order by userid, datetime";
ResultSet results = sourceConnection.getSQLQueryResults(query);
int i = 0;
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();
}
//close tables and connections
trj_table.releaseObject();
epi_table.releaseObject();
try{
sourceConnection.closeConnection();
destinationConnection.closeConnection();