_parseGeoNamesCitiesCsv: function(pathToCsv, callback) {
   DEBUG && console.log('Started parsing cities .csv (this  may take a ' +
       'while)');
   var data = [];
   var lenI = GEONAMES_COLUMNS.length;
   var that = this;
   lazy.readFile(pathToCsv).lines().each(function(line) {
     var lineObj = {};
     line = line.split('\t');
     for (var i = 0; i < lenI; i++) {
       var column = line[i] || null;
       lineObj[GEONAMES_COLUMNS[i]] = column;
     }
     data.push(lineObj);
   }).onComplete(function() {
     DEBUG && console.log('Finished parsing cities .csv');
     DEBUG && console.log('Started building cities k-d tree (this may take ' +
         'a while)');
     var dimensions = [
       'latitude',
       'longitude'
     ];
     that._kdTree = kdTree.createKdTree(data, that._distanceFunc, dimensions);
     DEBUG && console.log('Finished building cities k-d tree');
     return callback();
   });
 },
Example #2
0
 _parseGeoNamesAlternateNamesCsv: function(pathToCsv, callback) {
   var that = this;
   that._alternateNames = {};
   lazy.readFile(pathToCsv).split('\n').each(function(line) {
     line = line.split('\t');
     // Load postal codes
     if (line[2] === 'post') {
       if (!that._alternateNames[line[1]]) {
         that._alternateNames[line[1]] = {};
       }
       // Key on second column which is the geoNameId
       that._alternateNames[line[1]][line[2]] = line[3];
     }
   }).onComplete(function() {
     return callback();
   });
 },
 _parseGeoNamesAdmin2CodesCsv: function(pathToCsv, callback) {
   var that = this;
   var lenI = GEONAMES_ADMIN_CODES_COLUMNS.length;
   lazy.readFile(pathToCsv).lines().each(function(line) {
     line = line.split('\t');
     for (var i = 0; i < lenI; i++) {
       var value = line[i] || null;
       if (i === 0) {
         that._admin2Codes[value] = {};
       } else {
         that._admin2Codes[line[0]][GEONAMES_ADMIN_CODES_COLUMNS[i]] = value;
       }
     }
   }).onComplete(function() {
     return callback();
   });
 },
Example #4
0
  _parseGeoNamesAllCountriesCsv: function(pathToCsv, callback) {
    DEBUG && console.log('Started parsing all countries.txt (this  may take ' +
        'a while)');
    var lenI = GEONAMES_COLUMNS.length;
    var that = this;
    // Indexes
    var featureCodeIndex = GEONAMES_COLUMNS.indexOf('featureCode');
    var countryCodeIndex = GEONAMES_COLUMNS.indexOf('countryCode');
    var admin1CodeIndex = GEONAMES_COLUMNS.indexOf('admin1Code');
    var admin2CodeIndex = GEONAMES_COLUMNS.indexOf('admin2Code');
    var admin3CodeIndex = GEONAMES_COLUMNS.indexOf('admin3Code');
    var admin4CodeIndex = GEONAMES_COLUMNS.indexOf('admin4Code');
    var nameIndex = GEONAMES_COLUMNS.indexOf('name');
    var asciiNameIndex = GEONAMES_COLUMNS.indexOf('asciiName');
    var geoNameIdIndex = GEONAMES_COLUMNS.indexOf('geoNameId');

    var counter = 0;
    that._admin3Codes = {};
    that._admin4Codes = {};
    lazy.readFile(pathToCsv).split('\n').each(function(line) {
      line = line.split('\t');
      var featureCode = line[featureCodeIndex];
      if ((featureCode === 'ADM3') || (featureCode === 'ADM4')) {
        var lineObj = {
          name: line[nameIndex],
          asciiName: line[asciiNameIndex],
          geoNameId: line[geoNameIdIndex]
        };
        var key = line[countryCodeIndex] + '.' + line[admin1CodeIndex] + '.' +
            line[admin2CodeIndex] + '.' + line[admin3CodeIndex];
        if (featureCode === 'ADM3') {
          that._admin3Codes[key] = lineObj;
        } else if (featureCode === 'ADM4') {
          that._admin4Codes[key + '.' + line[admin4CodeIndex]] = lineObj;
        }
      }
      if (counter % 100000 === 0) {
        DEBUG && console.log('Parsing progress all countries ' + counter);
      }
      counter++;
    }).onComplete(function() {
      DEBUG && console.log('Finished parsing all countries.txt');
      return callback();
    });
  },
Example #5
0
  _parseGeoNamesCitiesCsv: function(pathToCsv, callback) {
    DEBUG && console.log('Started parsing cities.txt (this  may take a ' +
        'while)');
    var data = [];
    var lenI = GEONAMES_COLUMNS.length;
    var that = this;
    var latitudeIndex = GEONAMES_COLUMNS.indexOf('latitude');
    var longitudeIndex = GEONAMES_COLUMNS.indexOf('longitude');

    lazy.readFile(pathToCsv).split('\n').each(function(line) {
      var lineObj = {};
      line = line.split('\t');
      for (var i = 0; i < lenI; i++) {
        var column = line[i] || null;
        lineObj[GEONAMES_COLUMNS[i]] = column;
      }

      var lng = lineObj[GEONAMES_COLUMNS[latitudeIndex]];
      var lat = lineObj[GEONAMES_COLUMNS[longitudeIndex]];
      //dont add lineObj without lat/lng pair
      if (lng !== null && lng !== undefined && !isNaN(lng) &&
          lat !== null && lat !== undefined && !isNaN(lat)) {
        data.push(lineObj);
      } else {
        DEBUG && console.log('found null or undefined geo coords:', lineObj);
      }
    }).onComplete(function() {
      DEBUG && console.log('Finished parsing cities.txt');
      DEBUG && console.log('Started building cities k-d tree (this may take ' +
          'a while)');
      var dimensions = [
        'latitude',
        'longitude'
      ];
      that._kdTree = kdTree.createKdTree(data, that._distanceFunc, dimensions);
      DEBUG && console.log('Finished building cities k-d tree');
      return callback();
    });
  },
var fs = require('fs'),
//	path = require('path'),
	Lazy = require('lazy.js'),
	filename = process.argv[2];
	var json = {};
Lazy.readFile(filename)
  .lines()
  .each(function (line) {
  	if(!line.match(/^#/) && !line.match(/^}/) && !line.match('return')){
  		var str = line.split('='),
  			field = str[0].replace(/^\[\"/, '').replace(/\"\]/, '').replace(/ $/,''),
  			attr = str[1].replace(/^ /,'').replace(/\"/g, '').replace(';','');
  		json[field] = attr;
  	}
  	if(line.match(/^}/)) output();
  });

function output()
{
	var newfilename = filename.replace('lua','json');
	fs.writeFile(newfilename, JSON.stringify(json), function(err) {
    if(err) {
        console.log(err);
    } else {
        console.log("The file was saved!");
    }
	}); 
}
Example #7
0
var Lazy = require('lazy.js')

// 流处理
Lazy(stream)
  .take(5) // Read just the first 5 chunks of data read into the buffer.
  .each(processData);

// Read the first 5 lines from a file:
Lazy.readFile("path/to/file")
  .lines()
  .take(5)
  .each(doSomething);

// Read lines 5-10 from an HTTP response.
Lazy.makeHttpRequest("http://example.com")
  .lines()
  .drop(5)
  .take(5)
  .each(doSomething);