1

したがって、これがコードを共有する従来の方法ではないことは理解していますが、そのようなタスクを達成する方法についてスタックに何もないことを見て、共有したいと思いました。

さらに説明すると、ジオネーム データを mongo データベースに処理するための使いやすい Node.JS モジュールをあらゆる場所で探しました。私が見つけたいくつかのプロジェクトは失敗に終わりました。それは本当に簡単な作業なので、おそらく人々は共有していません。そうは言っても、私が持っているものを共有することはまだ価値があると思います.

次の回答は、 http ://geonames.org からの geonames データを処理して、データベースに保存したり、そのまま使用したりできる使用可能なオブジェクトにする方法を示しています。

4

2 に答える 2

2
var lineReader = require('line-reader');

lineReader.eachLine('./geonames-data/US/US.txt', function(line, last) {

  var city = line.split('\t').structure();

  if (city.is) { // if this is a city.

    // Delete un-needed object properties so we don't add useless info to DB.
    delete city.is;
    delete city.type;

    // Do something with the city object.
    console.log(JSON.stringify(city, true, 3) + '\n');
  }

});


Array.prototype.structure = function() {
    // from geonames readme: - P.PPL    populated place a city, town, village, or other agglomeration of buildings where people live and work
    return {
        _id: Number(this[0]),
        name: this[1],
        state: this[10],
        country: this[8],
        coordinates: [this[4], this[5]],
        timezone: this[17],
        is: ((this[6] == "P") ? true : false), // if object type is city.
        type: ((this[6] == "P") ? "city" : "other") // todo: add a parse function to parse other geonames db types
    }
}
于 2013-12-04T14:25:32.327 に答える