【问题标题】:Classify hex grids with Turf, Leaflet使用 Turf、Leaflet 对六角网格进行分类
【发布时间】:2016-10-22 14:59:58
【问题描述】:

我有一个过滤的十六进制网格,我想对其进行分类。

我用

创建了十六进制
var hexgrid = turf.hexGrid(bbox, cellWidth, units);

我用

聚合了值
var aggregated = turf.collect(hexgrid, myGeoJson, 'MyValue', 'NewCol');

其中 myGeoJson 是一个多点 FeatureCollection,而 MyValue 是一个特征属性,要么为 null 要么 > 0

我用

过滤了十六进制
var hexFiltered = L.geoJson(aggregated, {
    filter: function(feature, layer) {
        return feature.properties.NewCol.length > 0;
    }
}).addTo(map);

每个十六进制对象都可以用

访问
console.log(hexFiltered["_layers"]);

output = Object { 49: Object, 51: Object, 52: Object, 53: Object...

然后每个对象都有 .feature.properties.NewCol[n] 并且每个数组都有索引 (0, 1, 2) 和值 (null, 1 +)

如何使用数组值的总和对每个十六进制网格进行分类?

我已经用原生 javascript 尝试过这个,但我能实现的只是一个包含每个值的字符串。

var counts = {};
for (var obj in hexFiltered["_layers"]) {
    // Output the id of each obj (hex)
    // console.log("Object: " + obj);
    var cnt = 0;
    for (var i in hexFiltered["_layers"][obj]["feature"]["properties"] ) {
        // print values out as 1 line (i)
        console.log("One line of values :" + hexFiltered["_layers"][obj]["feature"]["properties"][i]);
        // output = One line of values :,,,1,,,,1,1,,1

        // add values
        cnt = cnt + hexFiltered["_layers"][obj]["feature"]["properties"][i];
        console.log(cnt);
        // output = 0,,,1,,,,1,1,,1
    }
    // attach cnt to counts object
    counts += cnt;
}

我哪里错了?有没有更简单的方法?

【问题讨论】:

    标签: javascript leaflet turfjs


    【解决方案1】:

    如果您的问题是您得到的是字符串而不是数组值的总和,那么解决方案是迭代数组并单独添加每个元素,而不是尝试将数组添加到整数。

    如果您添加0 + [1,2,3,4,5],您将获得'01,2,3,4,5'。您应该做的是添加另一个循环,该循环遍历保存数组的属性并单独添加每个元素。如何做到这一点的一个例子是

        for (var j in hexFiltered["_layers"][obj]["feature"]["properties"]["NewCol"] ) {
            cnt += j
        }
    

    此代码应替换您的第二个循环。我不确定您在counts += cnt 的代码末尾到底要做什么,因为 counts 是一个对象。如果您只想要一个值列表,那么您可能应该计算一个数组 (counts = []),以便您可以使用 counts.push cnt 而不是 counts += cnt

    如果这不能回答您的问题,请告诉我,我会更新答案。如果您需要进一步的帮助,请非常具体地说明您的需求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-09
      • 2012-07-18
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 2010-10-04
      相关资源
      最近更新 更多