我正在使用Node.js构建一个地图应用程序。我们在地图上显示了大约40,000个多边形,因此我尝试通过合并它们来提高性能。 Turf.js有一个看似票的合并功能。我虽然无法让它工作。
以下是我试图在我的控制器中使用草皮的代码。
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有点解决了geojson格式化问题。我将控制器的最后一位更改为:
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不是一个数组,所以我添加了[]&#39; s。没有草皮错误。我的地图无法理解输出。
现在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
所以,我认为,我迈进了一步。有任何想法吗?该地图与poly.txt中的原始数据一起正常工作。我试图得到同样的东西但是合并了。
答案 0 :(得分:2)
polygons
调试中的fs.writeFile()
对象不符合GeoJSON。 Turf期望符合GeoJSON FeatureCollection,必须拥有features
成员。
另请参阅doc on Turf merge method,它为您提供了符合GeoJSON标准的FeatureCollection的代码示例。
所以看起来你应该简单地将你的polygons
对象包装在FeatureCollection中以使其符合要求:
polygons = {
type: "FeatureCollection",
features: polygons // assuming polygons is an array
};
编辑问题后编辑
如果您的初始polygons
确实在几何坐标中输出“[Object]
”,而不是坐标数组,则草皮将无法理解您的多边形几何。
但是如果你说在尝试合并之前它正在工作,问题可能就是其他问题了。
你怎么知道你的polygons
不是阵列?您确定[polygons]
是正确的解决方案吗?
对于下一个问题,请打开另一个问题。