Turf.js合并在GeoJSON中查找要素

我正在用Node.js构build一个地图应用程序。 我们有大约40,000个多边形显示在地图上,所以我试图通过在可能的情况下合并来提高性能。 Turf.js有一个合并function,看起来像票。 尽pipe如此,我还是无法完成这项工作。

这里是我试图在我的控制器中使用草皮的代码。

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) 

草皮似乎正在寻找geojson中的function键,但没有。 有没有人有这个解决scheme?

###################### ghybs的回答后编辑

好的,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不是一个数组,所以我添加了[]。 没有更多的草皮错误。 我的地图不能理解输出。

现在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中的原始数据正常工作。 我试图得到相同的东西,但合并。

您的fs.writeFile()debugging中的polygons对象不符合GeoJSON 。 Turf期望一个符合GeoJSON的FeatureCollection ,它必须有一个features成员。

另请参阅Turf合并方法的文档 ,它为您提供了与GeoJSON兼容的FeatureCollection的外观代码示例。

所以看起来应该简单地将polygons对象包装在FeatureCollection中以使其符合:

 polygons = { type: "FeatureCollection", features: polygons // assuming polygons is an array }; 

编辑下面的问题编辑

如果您的初始polygons确实在几何坐标中输出“ [Object] ”,而不是坐标数组,则“草皮”将无法理解您的多边形几何体。

但如果你说在尝试合并之前它正在工作,这个问题可能会是另一回事。

你怎么知道你的polygons不是一个数组? 你确定做[polygons]是正确的解决scheme吗?

对于下一个问题,请打开一个不同的问题。