我正在尝试过滤,或者更好地将GeoJSON对象‘拆分’为一个特性属性中的值,并创建n个新的geoJSON,将它们存储在一个数组中。
这是我正在做的GeoJSON:http://www.brainsengineering.com/1.json
我必须将其除以“Level”属性,通过在上面的示例文件中创建与原始GeoJSON中存在的级别数量相同的新GeoJSON变量,有4个级别。
下面是部分工作的代码:
//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":...
是否有一种方法可以重新创建对象,同时保留这些元素?
发布于 2017-09-30 00:06:50
通过将缺失的字符串连接到字符串化对象,然后将整个新字符串解析回JSON对象,我目前已经解决了问题:
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
})) + '}');
}不太优雅,但很有效。有人知道一个更优雅、更有效的方法来达到同样的效果吗?
https://stackoverflow.com/questions/46498169
复制相似问题