将多个GeoJSON对象与javascript

时间:2017-07-18 21:33:26

标签: javascript geojson

是否可以使用javascript内置函数合并多个GeoJSON对象?如果没有,我怎么能在飞行中做到这一点?

我根据JSON的建议(例如here)尝试geoJSON1 = geoJSON1.concat(geoJSON2),后者返回geoJSON1.concat is not a function。 GeoJSON是点要素,是有效的GeoJSON对象。

这可能是一个简单的问题,答案为“否”,但我无法找到确凿的答案。

示例GeoJSONs:

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"}
        }
    ]
}

2 个答案:

答案 0 :(得分:3)

您可以使用数组扩展语法(spread operator ...)。

// 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)
    }
}

答案 1 :(得分: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'}