将多个 GeoJSON 对象与 javascript 结合

Bir*_*ird 2 javascript geojson

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

geoJSON1 = geoJSON1.concat(geoJSON2)按照 JSON 的建议进行了尝试(例如这里),它返回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"}
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

所需的结果(具有原始所有功能的单个 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"}
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

Nel*_*els 7

您可以使用数组扩展语法(扩展运算符 ...)。

// Given GeoJSON1 GeoJSON2

var newGeoJSON = { 
    "type" : "FeatureCollection",
    "features": [... GeoJSON1.features, ... GeoJSON2.features]
}
Run Code Online (Sandbox Code Playgroud)

这可以概括为一个函数:

function concatGeoJSON(g1, g2){
    return { 
        "type" : "FeatureCollection",
        "features": [... g1.features, ... g2.features]
    }
}
Run Code Online (Sandbox Code Playgroud)

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

function concatGeoJSON(g1, g2){
    return { 
        "type" : "FeatureCollection",
        "features": g1.features.concat(g2.features)
    }
}
Run Code Online (Sandbox Code Playgroud)