9

I want to use this json file it is no GeoJSON file yet but I noticed that it contains several Features and not one which I find confusing. I wanted to ask if you know some tool where I can merge all Features/FeatureCollections into one valid GeoJSON file so I can use it for like D3.js? The original file is here and I already got rid of the stuff which is not needed for the geojson.

Here is an excerpt of the GeoJson, it is fairly big so I'ts just a snippet

{"points": [{
        "type": "FeatureCollection",
        "features": [{
            "type": "Feature",
            "geometry": {
                "coordinates": [41.9773865, 36.3372536],
                "type": "Point"
            },
            "properties": {
                "attacks": 1,
                "location": "Sinjar",
                "date": "2015-10-16"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [43.4873886, 34.9301605],
                "type": "Point"
            },
            "properties": {
                "attacks": 2,
                "location": "Baiji",
                "date": "2015-10-16"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [42.4509315, 36.3707008],
                "type": "Point"
            },
            "properties": {
                "attacks": 3,
                "location": "Tal Afar",
                "date": "2015-10-16"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [43.76667, 35.31667],
                "type": "Point"
            },
            "properties": {
                "attacks": 1,
                "location": "Hawija",
                "date": "2015-10-16"
            }
        }]
    }, {
        "type": "FeatureCollection",
        "features": [{
            "type": "Feature",
            "geometry": {
                "coordinates": [43.7820587, 33.3516083],
                "type": "Point"
            },
            "properties": {
                "attacks": 4,
                "location": "Fallujah",
                "date": "2015-04-24"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [43.2637405, 33.4324112],
                "type": "Point"
            },
            "properties": {
                "attacks": 1,
                "location": "Ramadi",
                "date": "2015-04-24"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [43.1170998, 36.3246002],
                "type": "Point"
            },
            "properties": {
                "attacks": 5,
                "location": "Mosul",
                "date": "2015-04-24"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [38.3535004, 36.8908997],
                "type": "Point"
            },
            "properties": {
                "attacks": 4,
                "location": "Kobane",
                "date": "2015-04-24"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [42.4509315, 36.3707008],
                "type": "Point"
            },
            "properties": {
                "attacks": 1,
                "location": "Tal Afar",
                "date": "2015-04-24"
            }
        }]
    }, {
        "type": "FeatureCollection",
        "features": [{
            "type": "Feature",
            "geometry": {
                "coordinates": [43.7820587, 33.3516083],
                "type": "Point"
            },
            "properties": {
                "attacks": 1,
                "location": "Fallujah",
                "date": "2015-09-09"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [43.2637405, 33.4324112],
                "type": "Point"
            },
            "properties": {
                "attacks": 3,
                "location": "Ramadi",
                "date": "2015-09-09"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [41.9773865, 36.3372536],
                "type": "Point"
            },
            "properties": {
                "attacks": 1,
                "location": "Sinjar",
                "date": "2015-09-09"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [43.4873886, 34.9301605],
                "type": "Point"
            },
            "properties": {
                "attacks": 1,
                "location": "Baiji",
                "date": "2015-09-09"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [42.4509315, 36.3707008],
                "type": "Point"
            },
            "properties": {
                "attacks": 2,
                "location": "Tal Afar",
                "date": "2015-09-09"
            }
        }, 

Do you have some ideas how to solve this problem and get a proper GeoJSON file?

10

You could write a simple script in (for example) Python that will process the data for you.

import json
from itertools import chain

Open the file and read the data into a Python dictionary:

isil = json.load(open('isil.en.json'))

The points object is just a list of feature collections, so you can use the python itertools library to help chain the features in those collections together:

features = list(chain.from_iterable(fc['feature'] for fc in isil['points']))

And finally write a new feature collection with all 2818 features out to a file.

feature_collection = {
    "type": "FeatureCollection":,
    "features": features
}

with open("isil_points.geojson", "w") as f:
    json.dump(feature_collection, f)

And that should be able to be loaded up into a system of your choice. Looking at the data You'll probably have to do some manual cleaning as well (some "total" locations and a few points that don't have a location), but that should be a start.

The points from each of the feature collections, merged.

7

Because it's a "oneshot", you can do it manually (also possible running via Node)

Open a JavaScript console in your browser.

You need to loop to get an array of array of Feature (because each FeatureCollection have one or more Feature)

Then, you will use the flatten function to transform the array of array into an array (a recursive function borrowed from https://stackoverflow.com/a/15030117)

The full code is below (except the file content, not complete to keep things readable)

// Copy/paste the text from you source https://raw.githubusercontent.com/RitterLean/Geojson/master/geofile.json 
content = {
"points": [{
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "geometry": {
            "coordinates": [41.9773865, 36.3372536],
            "type": "Point"
        },
        "properties": {
            "attacks": 1,
            "location": "Sinjar",
            "date": "2015-10-16"
        }
    }, {
        "type": "Feature",
        "geometry": {
            "coordinates": [43.4873886, 34.9301605],
            "type": "Point"
        },
        "properties": {
            "attacks": 2,
            "location": "Baiji",
            "date": "2015-10-16"
        }
    }, {
    ...
    // Be careful, incomplete because shortened for illustration 

intermediate_result = content['points'].map(function(el){
    return el.features;
});

function flatten(arr) {
  return arr.reduce(function (flat, toFlatten) {
    return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
  }, []);
};

geojson_output = {
        "type": "FeatureCollection",
        "features": flatten(intermediate_result)
}
// Transform the object to a string you can paste into a file
console.log(JSON.stringify(geojson_output));

The result can be seen at http://geojson.io/#id=gist:anonymous/da10ab9afc9a5941ba66&map=4/19.48/22.32

You will see that some results have wrong coordinates (0, 0). It's due to the original content.

From this demo, you can also export to GeoJSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.