首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对特征属性值进行GeoJSON过滤

对特征属性值进行GeoJSON过滤
EN

Stack Overflow用户
提问于 2017-09-29 23:43:04
回答 1查看 1.3K关注 0票数 2

我正在尝试过滤,或者更好地将GeoJSON对象‘拆分’为一个特性属性中的值,并创建n个新的geoJSON,将它们存储在一个数组中。

这是我正在做的GeoJSON:http://www.brainsengineering.com/1.json

我必须将其除以“Level”属性,通过在上面的示例文件中创建与原始GeoJSON中存在的级别数量相同的新GeoJSON变量,有4个级别。

下面是部分工作的代码:

代码语言:javascript
复制
//geoData is the variable holding the GeoJSON
var featureCount = geoData.features.length;
var maxLevel;
var geoByLevel = []; 

//retrieve the maximum number of levels
for (var i = 0; i < featureCount; i++) {
    if (geoData.features[i].properties.Level > maxLevel) {
        maxLevel = geoData.features[i].properties.Level;
    }
}

//Split geoData content into the geoByLevel array by level
for (var i = 1; i <= maxLevel; i++) {
    geoByLevel[i-1] = geoData.features.filter(function (el) {
            return el.properties.Level == i
    });
}

这段代码部分是有效的,因为我正确地将按级别拆分到数组中的特性,但它们是无效的GeoJSON,因为我松散了第一个对象。

{"type":"FeatureCollection","hc-transform":{“默认”:{"crs":"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"}},"features":...

是否有一种方法可以重新创建对象,同时保留这些元素?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-30 00:06:50

通过将缺失的字符串连接到字符串化对象,然后将整个新字符串解析回JSON对象,我目前已经解决了问题:

代码语言:javascript
复制
for (var i = 1; i <= maxLevel; i++) {
    geoByLevel[i - 1] = JSON.parse('{"type":"FeatureCollection","hc-transform": {"default": {"crs": "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"}},"features":' + 
        JSON.stringify(geoData.features.filter(function (el) {
        return el.properties.Level == i
    })) + '}');
}

不太优雅,但很有效。有人知道一个更优雅、更有效的方法来达到同样的效果吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46498169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档