index.js
/* create distance matrix
requires:
data.feature feature to calculate the timematrix for
data.id ID of the run (timestamp)
data.geometryID ID of the input geometry
optional:
maxTime int with the maximum time for the buffer in seconds
maxSpeed int with the maximum speed in km/h

returns:
location of CSV file
*/
create distance matrix
requires:
data.feature feature to calculate the timematrix for
data.id ID of the run (timestamp)
data.geometryID ID of the input geometry
optional:
maxTime int with the maximum time for the buffer in seconds
maxSpeed int with the maximum speed in km/h

location of CSV file
create distance matrix requires : data . feature feature to calculate the timematrix for data . id ID of the run ( timestamp ) data . geometryID ID of the input geometry optional : maxTime int with the maximum time for the buffer in seconds maxSpeed int with the maximum speed in km / h location of CSV file
function createTimeMatrix(data) {
  var idx =getProjectIdx(data.project);
  var c = CONFIGURATION[idx];
  var p = PROJECTS[data.project];
  var osrm = __dirname+"/"+c.activeOSRM.dir+'/'+c.activeOSRM.files.osrm;
  var cETA = fork('./scripts/node/calculateETA.js');
  beginTime = new Date().getTime();
  if(!data||!data.feature) {
    console.warn('no data');
    return false;
  }
  data.maxTime = data.maxTime || c.maxTime;
  data.maxSpeed = data.maxSpeed || c.maxSpeed;
  io.emit('status',{id:data.id,msg:'srv_creating_tm',project:data.project});
  var box = envelope(data.feature);
  var extent =[box.geometry.coordinates[0][0][0],box.geometry.coordinates[0][0][1],box.geometry.coordinates[0][2][0],box.geometry.coordinates[0][2][1]];
  var squares =  squareGrid(extent,30, 'kilometers');
  io.emit('status',{id:data.id,msg:'srv_split_squares',p0:squares.features.length,project:data.project});
  cETA.send({data:data,squares:squares.features,POIs:p.POIs,villages:p.villages,osrm:osrm,id:data.id,project:data.project});
  var remaining = squares.features.length;
  cETA.on('message',function(msg){
    if(msg.type == 'status') {
      io.emit('status',{id:msg.id,msg:msg.data});
    }
    else if(msg.type=='square') {
      remaining--;
      io.emit('status',{id:msg.id,msg:'srv_remaining_squares',p0:remaining});
    }
    else if(msg.type =='done') {
      var calculationTime = (new Date().getTime()-beginTime)/1000;
      var timing = Math.round(calculationTime);
      if(calculationTime>60) {
        timing = Math.round(calculationTime/60);
        io.emit('status',{id:msg.id,msg:'srv_calculated_in_m',p0:timing});
      }
      else {
        io.emit('status',{id:msg.id,msg:'srv_calculated_in_s',p0:timing});
      }
      io.emit('status',{id:msg.id,msg:'srv_writing'});
      var networkfile = msg.osrm.split('/')[msg.osrm.split('/').length-1];
      var osrmfile = networkfile.split('.')[0];
      var print = d3.csv.format(msg.data);
      var subfile = data.geometryId+'-'+msg.id+'-'+osrmfile;
      var file = subfile+'.csv';
      var fullpath = './web/data/'+data.project+'/csv/';
      var meta = {
        "created":{
          "time":new Date().getTime(),
          "user":"steven"
        },
        "name":subfile,
        "csvfile":file
      };
      var metafile = fullpath+subfile+'.json';
      fs.writeFile(metafile,JSON.stringify(meta),function(err){
        if(err) return console.log(err);
        io.emit('csvMetaFinished',{id:msg.id,project:data.project});
      });
      fs.writeFile(fullpath+file, print, function(err){
        if(err) {
          return console.log(err);
        }
        io.emit('status',{id:msg.id,msg:'srv_finished',project:data.project});
        io.emit('csvFinished',{id:msg.id,project:data.project});
        cETA.disconnect();
      });
    }
  });
}
function createTimeMatrix ( data ) { var idx = getProjectIdx ( data . project ) ; var c = CONFIGURATION [ idx ] ; var p = PROJECTS [ data . project ] ; var osrm = __dirname + "/" + c . activeOSRM . dir + '/' + c . activeOSRM . files . osrm ; var cETA = fork ( './scripts/node/calculateETA.js' ) ; beginTime = new Date ( ) . getTime ( ) ; if ( ! data || ! data . feature ) { console . warn ( 'no data' ) ; return false ; } data . maxTime = data . maxTime || c . maxTime ; data . maxSpeed = data . maxSpeed || c . maxSpeed ; io . emit ( 'status' , { id : data . id , msg : 'srv_creating_tm' , project : data . project } ) ; var box = envelope ( data . feature ) ; var extent = [ box . geometry . coordinates [ 0 ] [ 0 ] [ 0 ] , box . geometry . coordinates [ 0 ] [ 0 ] [ 1 ] , box . geometry . coordinates [ 0 ] [ 2 ] [ 0 ] , box . geometry . coordinates [ 0 ] [ 2 ] [ 1 ] ] ; var squares = squareGrid ( extent , 30 , 'kilometers' ) ; io . emit ( 'status' , { id : data . id , msg : 'srv_split_squares' , p0 : squares . features . length , project : data . project } ) ; cETA . send ( { data : data , squares : squares . features , POIs : p . POIs , villages : p . villages , osrm : osrm , id : data . id , project : data . project } ) ; var remaining = squares . features . length ; cETA . on ( 'message' , function ( msg ) { if ( msg . type == 'status' ) { io . emit ( 'status' , { id : msg . id , msg : msg . data } ) ; } else if ( msg . type == 'square' ) { remaining -- ; io . emit ( 'status' , { id : msg . id , msg : 'srv_remaining_squares' , p0 : remaining } ) ; } else if ( msg . type == 'done' ) { var calculationTime = ( new Date ( ) . getTime ( ) - beginTime ) / 1000 ; var timing = Math . round ( calculationTime ) ; if ( calculationTime > 60 ) { timing = Math . round ( calculationTime / 60 ) ; io . emit ( 'status' , { id : msg . id , msg : 'srv_calculated_in_m' , p0 : timing } ) ; } else { io . emit ( 'status' , { id : msg . id , msg : 'srv_calculated_in_s' , p0 : timing } ) ; } io . emit ( 'status' , { id : msg . id , msg : 'srv_writing' } ) ; var networkfile = msg . osrm . split ( '/' ) [ msg . osrm . split ( '/' ) . length - 1 ] ; var osrmfile = networkfile . split ( '.' ) [ 0 ] ; var print = d3 . csv . format ( msg . data ) ; var subfile = data . geometryId + '-' + msg . id + '-' + osrmfile ; var file = subfile + '.csv' ; var fullpath = './web/data/' + data . project + '/csv/' ; var meta = { "created" : { "time" : new Date ( ) . getTime ( ) , "user" : "steven" } , "name" : subfile , "csvfile" : file } ; var metafile = fullpath + subfile + '.json' ; fs . writeFile ( metafile , JSON . stringify ( meta ) , function ( err ) { if ( err ) return console . log ( err ) ; io . emit ( 'csvMetaFinished' , { id : msg . id , project : data . project } ) ; } ) ; fs . writeFile ( fullpath + file , print , function ( err ) { if ( err ) { return console . log ( err ) ; } io . emit ( 'status' , { id : msg . id , msg : 'srv_finished' , project : data . project } ) ; io . emit ( 'csvFinished' , { id : msg . id , project : data . project } ) ; cETA . disconnect ( ) ; } ) ; } } ) ; }
create distance matrix
requires:
data.feature feature to calculate the timematrix for
data.id ID of the run (timestamp)
data.geometryID ID of the input geometry
optional:
maxTime int with the maximum time for the buffer in seconds
maxSpeed int with the maximum speed in km/h
create distance matrix requires : data . feature feature to calculate the timematrix for data . id ID of the run ( timestamp ) data . geometryID ID of the input geometry optional : maxTime int with the maximum time for the buffer in seconds maxSpeed int with the maximum speed in km / h
//split the input region in squares for parallelisation //tell the client how many squares there are //we are done, save as csv and send the filename