javascript - 如何在 Node.JS 中处理地名城市数据

标签 javascript node.js mongodb object geonames

所以我知道这不是共享代码的传统方式,但看到堆栈中没有关于如何完成此类任务的任何内容 - 我想我会分享。

为了进一步解释,我到处寻找易于使用的 Node.JS 模块来将地理名称数据处理到 mongo 数据库中。我发现的几个项目最终都失败了。也许人们不分享是因为这真的是一项如此简单的任务。话虽如此,我仍然认为分享我所拥有的东西可能很有值(value)。

以下答案将展示如何处理来自 http://geonames.org 的地名数据变成一个可用的对象,然后可以将其保存到数据库中或直接使用。

最佳答案

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
    }
}

关于javascript - 如何在 Node.JS 中处理地名城市数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20377896/

相关文章:

java - 我们如何在 GWT 中使用 onbefoerunload

javascript - nodejs 使用回调和事件

javascript - '.'不被识别为在 exec() 中使用时抛出的内部或外部命令,但从命令行运行时则不会

javascript - 您可以使用 browserify 来请求 node-bcrypt 客户端,然后将哈希发送到服务器吗?

mongodb - 在 mongodb 集合中按日期分组

javascript - DGrid 编辑器 - 尝试编辑文本单元格时更改显示值

javascript - 即使使用 e.preventDefault 后提交表单也会刷新页面

javascript - 为什么我的函数不显示数据?

spring-data跨店实体管理

python - MongoDB 一共有三个字段