Evan
Evan

Reputation: 2038

Combine multiple GeoJSON objects with javascript

Is it possible to merge multiple GeoJSON objects using javascript built-in functions? If not, how can I do this on the fly?

I tried geoJSON1 = geoJSON1.concat(geoJSON2) as per suggestions for JSON (e.g. here), which returns geoJSON1.concat is not a function. The GeoJSONs are point features and are valid GeoJSON objects.

This is probably a simple question with "no" for an answer, but I haven't been able to find a conclusive answer.

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

Desired result (single GeoJSON with all the features of the originals):

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

Upvotes: 3

Views: 5361

Answers (2)

Nels
Nels

Reputation: 321

You could use array expansion syntax (the spread operator ...).

// Given GeoJSON1 GeoJSON2

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

This can generalize as a function:

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

Alternatively, you can use the array concat method, since the GeoJSON features field is an array:

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

Upvotes: 11

Skam
Skam

Reputation: 7798

I think what you're looking for is the Object.assign function. In your case, here's what you would do.

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

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

var both = {};

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

Upvotes: 0

Related Questions