【问题标题】:Turf.js merge looks for features in GeoJSONTurf.js 合并在 GeoJSON 中查找特征
【发布时间】:2016-03-10 07:03:58
【问题描述】:

我正在使用 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 中的 features 键,但没有。有人对此有解决方案吗?

###################### 在 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 中的原始数据。我试图得到同样的东西,但合并了。

【问题讨论】:

    标签: node.js leaflet mapbox geojson turfjs


    【解决方案1】:

    来自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
    };
    

    在问题编辑后编辑

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

    但是,如果您在尝试合并之前说它正在工作,那么问题可能是其他问题。

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

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

    【讨论】:

    • 我在问题中添加了更多细节。这似乎确实解决了 geojson 格式问题。不过现在我还有其他问题。
    • 我尝试从多边形中删除括号,并且草皮给了我这个错误:" var type=obj.type;if(!this.parse[type]){throw new Error('Unknown GeoJSON type: '+obj.type);}" poly.txt的输出还是"[object Object]"
    • 您可能应该进一步了解您的polygons 是由什么组成的,或者分享您是如何构建它的。也许尝试输出它的JSON.stringify。您可能应该在一个新问题中这样做。
    • 该文档 URL 已过期 :(
    • @SSHThis 看起来turf.merge 已从最新版本中删除...随时添加您的发现以进行替换! :-)
    猜你喜欢
    • 2021-05-06
    • 1970-01-01
    • 2020-07-26
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 2016-09-02
    相关资源
    最近更新 更多