我正在使用 Node.js 构建一个 map 应用程序。我们在 map 上显示了大约 40,000 个多边形,因此我尝试通过尽可能合并它们来提高性能。 Turf.js 有一个看起来像票证的合并功能。但我还没能让它发挥作用。
这是我尝试在 Controller 中使用 turf 的代码。
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var turf = require('turf');
var fs = require('fs');
exports.show = function(req, res) {
res.render('map', {
title: 'Map'
});
};
exports.postSearch = function(req, res) {
// Bunch of query stuff goes into array below, (omitted for post)
mongoose.model('Claim').find({
$and:array
}, function(err, polygons){
// fs.writeFile('poly.txt', polygons);
var test = turf.merge(polygons);
res.json(test);
});
};
我将 fs.writeFile 放在那里以获取从 mongodb 返回的 geojson 样本。这是我得到的:
{
properties: {
TTLTPCD: 'CL',
RNHCTRS: 389,
TNRTPCD: 'C'
},
geometry: {
coordinates: [
[Object]
],
type: 'Polygon'
},
type: 'Feature',
_id: '56d2a2061c601e547e1099ee'
}, {
properties: {
TTLTPCD: 'CS',
RNHCTRS: 261,
TNRTPCD: 'C'
},
geometry: {
coordinates: [
[Object]
],
type: 'Polygon'
},
type: 'Feature',
_id: '56d2a2071c601e547e109d37'
},
// this repeats a couple hundred times on my test query.
我得到了堆栈跟踪,但它对我来说没有意义:
PROJECT_FOLDER/node_modules/turf/node_modules/turf-merge/index.js:55
var merged = clone(polygons.features[0]),
^
TypeError: Cannot read property '0' of undefined
at Object.module.exports [as merge] (/media/ng/DATA/LighthouseLabs/ClaimMaps/nodeMaps/MapEmGems/node_modules/turf/node_modules/turf-merge/index.js:55:39)
at Query.<anonymous> (/media/ng/DATA/LighthouseLabs/ClaimMaps/nodeMaps/MapEmGems/controllers/map.js:75:14)
at /media/ng/DATA/LighthouseLabs/ClaimMaps/nodeMaps/MapEmGems/node_modules/kareem/index.js:177:19
at /media/ng/DATA/LighthouseLabs/ClaimMaps/nodeMaps/MapEmGems/node_modules/kareem/index.js:109:16
at doNTCallback0 (node.js:430:9)
at process._tickCallback (node.js:359:13)
Turf 似乎在 geojson 中寻找功能键,但没有。有人有解决办法吗?
###################### 在ghybs的回答后编辑好的,ghybs 解决了 geojson 格式问题。我将 Controller 的最后一位更改为:
mongoose.model('Claim').find({
$and:array
}, function(err, polygons){
polygons = {
type: "FeatureCollection",
features: [polygons] };
fs.writeFile('poly.txt', polygons);
var test = turf.merge(polygons);
fs.writeFile('test.txt', test);
res.json(test);
});
};
我放置了第二个 fs.writeFile 来查看 turf.merge 返回的内容。原始的 geojson 不是数组,所以我添加了 []。不再出现草坪错误。但我的 map 无法理解输出。
现在,poly.txt 给了我这个:
[object Object]
并且 test.txt 包含以下内容:
{ properties: null,
geometry: undefined,
type: 'Feature',
_id: '56d2a2061c601e547e1099ee',
__v: undefined },{ properties: null,
geometry: undefined,
type: 'Feature',
_id: '56d2a2071c601e547e109d37',
__v: undefined },
// This repeats 336 times
所以我想我又近了一步。有任何想法吗?该 map 与 poly.txt 中的原始数据配合良好。我正在尝试获得相同的东西,但合并了。
最佳答案
fs.writeFile()
调试中的 polygons
对象不是 GeoJSON符合。 Turf 期望符合 GeoJSON FeatureCollection ,它必须有一个 features
成员。
另请参阅doc on Turf merge method ,它为您提供了符合 GeoJSON 的 FeatureCollection 的代码示例。
所以看起来您应该简单地将 polygons
对象包装在 FeatureCollection 中以使其兼容:
polygons = {
type: "FeatureCollection",
features: polygons // assuming polygons is an array
};
编辑问题后进行编辑
如果您的初始多边形
确实在几何坐标中输出“[Object]
”,而不是坐标数组,则草皮将无法理解您的多边形几何形状.
但是,如果您在尝试合并之前说它可以正常工作,那么问题可能是其他问题。
你怎么知道你的多边形
不是数组?您确定[polygons]
是正确的解决方案吗?
对于下一期,请提出不同的问题。
关于node.js - Turf.js 合并寻找 GeoJSON 中的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35909731/