【问题标题】:How to process geonames city data in Node.JS如何在 Node.JS 中处理地名城市数据
【发布时间】:2013-12-04 14:25:32
【问题描述】:

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

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

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

【问题讨论】:

    标签: javascript node.js mongodb object geonames


    【解决方案1】:
    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
        }
    }
    

    【讨论】:

    • 这是用 node.js 完成的吗? line-reader 是内置函数吗?
    • 请问你能解释一下你的代码吗?对于 biginers 来说,这很难理解.. 就像 .structure() 是什么?我在 node.js 文档中找不到它。
    • line-reader 是另一个模块,可以使用 npm 安装。 structure() 是添加到所有数组对象的原型函数。它基本上只是返回一个组合数组参数的对象。
    • 如果您想了解如何使用上述代码,请查看另一个答案中的 Github 链接。 import.js 在这种情况下特别有趣。
    【解决方案2】:

    进一步扩展上述答案。我已经包含了一个使用来自 Github 的 geonames Node.JS 模块的示例。 (https://github.com/bugs181/geonames)

    免责声明:我是上述 github repo 的作者。

    这个例子也可以在 Github repo 上找到。

    var geonames = require('./geonames.js');
    
    // Intialize geonames module.
    geonames.init();
    
    // 34.0500, -118.2500 - Los Angeles, California - More specifically Bunker Hill, California
    // 34.057°N 118.238°W - San Francisco, California - More specifically Opera Plaza, California
    
    geonames.city(34.0500, -118.2500, function(err, result) {
            if (err) {
                    console.log("There was an error resolving coordinates.");
                    console.log(err);
                    return;
            }
    
            console.log("Result: " + JSON.stringify(result, true, 3));
    });
    


    输出:

    [geonames] Resolve city from coordinates. [34.05, -118.25]
    Result: {
       "city": "Bunker Hill",
       "state": "CA"
    }
    


    您也可以使用字符串作为纬度和经度,但您必须将它们转换为 Mongo 数据库。使用Number() 方法来执行此操作。

    例如:

    var lat = "34.0500";
    var lon = "-118.2500";
    
    geonames.city(Number( lat ), Number( lon ), function(err, result) {
    

    【讨论】:

    • Github 仓库的链接不再公开
    • 抱歉,由于 Github 迁移,此 repo 已被删除。如果您仔细查看此答案的发布日期,您会发现已经超过 7 年了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多