使用JavaScript合并多个GeoJSON对象

3

是否可以使用javascript内置函数合并多个GeoJSON对象?如果不行,我该如何在程序运行期间完成此操作?

我尝试了geoJSON1 = geoJSON1.concat(geoJSON2),这是根据JSON的建议(e.g. here),但返回了geoJSON1.concat is not a function。这些GeoJSON是点要素,并且是有效的GeoJSON对象。

这可能是一个简单的问题,答案可能是否定的,但我还没有找到一个确定的答案。

示例GeoJSON:

GeoJSON1 = { "type" : "FeatureCollection",
    "features" : [
         { "type" : "Feature",
        "id" : 1,
            "geometry" : {
                "type" : "Point",
                "coordinates" : ["-119.6165333","34.35935"]},
        "properties" : { "video" : "S105SC_Tape13o.noaudio.mpg", "video_second" : "0", "time" : "19:26:58", "date" : "2005-08-26"}
        },
         { "type" : "Feature",
        "id" : 2,
            "geometry" : {
                "type" : "Point",
                "coordinates" : ["-119.6165167","34.35935"]},
        "properties" : { "video" : "S105SC_Tape13o.noaudio.mpg", "video_second" : "2", "time" : "19:27:00", "date" : "2005-08-26"}
        }
    ]
}

GeoJSON2 = { "type" : "FeatureCollection",
    "features" : [
         { "type" : "Feature",
        "id" : 27,
            "geometry" : {
                "type" : "Point",
                "coordinates" : ["-119.61635","34.3593833"]},
        "properties" : { "video" : "S105SC_Tape13o.noaudio.mpg", "video_second" : "55", "time" : "19:27:53", "date" : "2005-08-26"}
        },
         { "type" : "Feature",
        "id" : 28,
            "geometry" : {
                "type" : "Point",
                "coordinates" : ["-119.6163333","34.3594"]},
        "properties" : { "video" : "S105SC_Tape13o.noaudio.mpg", "video_second" : "56", "time" : "19:27:54", "date" : "2005-08-26"}
        }
    ]
}

期望结果(单个GeoJSON包含所有原始要素):
newGeoJSON = { "type" : "FeatureCollection",
    "features" : [
         { "type" : "Feature",
        "id" : 1,
            "geometry" : {
                "type" : "Point",
                "coordinates" : ["-119.6165333","34.35935"]},
        "properties" : { "video" : "S105SC_Tape13o.noaudio.mpg", "video_second" : "0", "time" : "19:26:58", "date" : "2005-08-26"}
        },
         { "type" : "Feature",
        "id" : 2,
            "geometry" : {
                "type" : "Point",
                "coordinates" : ["-119.6165167","34.35935"]},
        "properties" : { "video" : "S105SC_Tape13o.noaudio.mpg", "video_second" : "2", "time" : "19:27:00", "date" : "2005-08-26"}
        },
         { "type" : "Feature",
        "id" : 27,
            "geometry" : {
                "type" : "Point",
                "coordinates" : ["-119.61635","34.3593833"]},
        "properties" : { "video" : "S105SC_Tape13o.noaudio.mpg", "video_second" : "55", "time" : "19:27:53", "date" : "2005-08-26"}
        },
         { "type" : "Feature",
        "id" : 28,
            "geometry" : {
                "type" : "Point",
                "coordinates" : ["-119.6163333","34.3594"]},
        "properties" : { "video" : "S105SC_Tape13o.noaudio.mpg", "video_second" : "56", "time" : "19:27:54", "date" : "2005-08-26"}
        }
    ]
}

浏览http://geojson.org/可以看出,你有**对象**而不是**数组**。 - Quentin
可以添加一些示例,展示输入的样式和期望的输出样式吗? - kristaps
@Quentin 这就是为什么concat方法不起作用的原因吗? @kristaps 我添加了一些简单的输入和结果示例。 - Evan
2个回答

10
您可以使用数组展开语法(即 展开运算符 ...)。
// Given GeoJSON1 GeoJSON2

var newGeoJSON = { 
    "type" : "FeatureCollection",
    "features": [... GeoJSON1.features, ... GeoJSON2.features]
}

这可以概括为一个函数:
function concatGeoJSON(g1, g2){
    return { 
        "type" : "FeatureCollection",
        "features": [... g1.features, ... g2.features]
    }
}

或者,您可以使用数组concat方法,因为GeoJSON特征字段是一个数组:

function concatGeoJSON(g1, g2){
    return { 
        "type" : "FeatureCollection",
        "features": g1.features.concat(g2.features)
    }
}

0

我认为你所需要的是 Object.assign 函数。在你的情况下,下面是你需要做的。

var GeoJSON1 = { 'bat': 'baz'}

var GeoJSON2 = { 'foo':'bar'}

var both = {};

Object.assign(both, GeoJSON1, GeoJSON2);
console.log(both);
// Object {bat: 'baz', foo:'bar'}

这在你的例子中确实有效,但不幸的是它似乎不能合并GeoJSON对象,它只返回最后一个对象(在这种情况下是GeoJSON2)。可能是由于GeoJSON的结构?感谢你向我展示了一个新的函数。 - Evan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接