gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:44:31 24 4
gpt4 key购买 nike

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

为了进一步解释,我到处寻找易于使用的 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/

24 4 0
文章推荐: javascript - jQuery 的 HTML 结构附加数组
文章推荐: c - 使用不同的参数运行不同的函数 - 函数指针
文章推荐: c - Malloc 和 free 错误
文章推荐: javascript - 使用 jQuery 在