3

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"}
        }
    ]
}
3
  • Glancing at geojson.org suggests you have objects not arrays. Commented Jul 18, 2017 at 21:35
  • Could you add some examples of what the input looks like and what the desired output looks like? Commented Jul 18, 2017 at 21:36
  • @Quentin Is that why the concat method doesn't work? @kristaps I added some simple examples of input and result. Commented Jul 18, 2017 at 22:00

2 Answers 2

11

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)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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'}

1 Comment

That does work with your example, but unfortunately it doesn't seem to combine the GeoJSON objects, it only returns the last object (in this case GeoJSON2). Must be due to the structure of GeoJSONs? Thanks for showing me a new function though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.