简体   繁体   中英

How do I convert a JSON document collection into a GeoJSON FeatureCollection

I have a RESTapi that pulls data from a mongoDB and passes it to my mapbox gl app as a JSON array using the following code:

$.ajax(
            {
                type: "GET",
                contentType: "application/json; charset=utf-8",
                url: myUrl,
                cache: false,
                async: true,
                timeout: 5000,
                dataType: "json",
                success: function (data)
                {
                    console.log("Reading Data");
                    console.log(data);
                },
                error: function (xhr, ajaxOptions, thrownError)
                {
                    console.log("http Status Response: " + xhr.status);
                    console.log(thrownError);
                }
            });

The data is stored as individual documents in mongo, and a get pulls it in the following format.

[
        {
            "_id": "588a3d5a524da321dd937891",
            "__v": 0,
            "geometry": {
                "type": "Point",
                "coordinates": [ -113.5299938027191, 53.42859997065679 ]
            },
            "type": "Feature",
            "properties": {
                "icon": "horse-riding-15",
                "title": "A Horse",
                "description": "A Horse description",
                "date": "2017-01-26T18:18:02.175Z"
            }
        },
        {
            "_id": "588ac68aa99e6a38134997b5",
            "__v": 0,
            "geometry": {
                "type": "Point",
                "coordinates": [ -113.56076949999999, 53.4528447 ]
            },
            "type": "Feature",
            "properties": {
                "icon": "dog-park-15",
                "title": "A Dog",
                "description": "A Dog description",
                "date": "2017-01-27T04:03:22.381Z"
            }
        }
]

To read this in mapbox it must be part of a GeoJSON Feature Collection, which should look like this (The extra type info at the start, and {} wrapper):

{
    "type": "FeatureCollection",
    "features":     [
            {
                "_id": "588a3d5a524da321dd937891",
                "__v": 0,
                "geometry": {
                    "type": "Point",
                    "coordinates": [ -113.5299938027191, 53.42859997065679 ]
                },
                "type": "Feature",
                "properties": {
                    "icon": "horse-riding-15",
                    "title": "A Horse",
                    "description": "A Horse description",
                    "date": "2017-01-26T18:18:02.175Z"
                }
            },
            {
                "_id": "588ac68aa99e6a38134997b5",
                "__v": 0,
                "geometry": {
                    "type": "Point",
                    "coordinates": [ -113.56076949999999, 53.4528447 ]
                },
                "type": "Feature",
                "properties": {
                    "icon": "dog-park-15",
                    "title": "A Dog",
                    "description": "A Dog description",
                    "date": "2017-01-27T04:03:22.381Z"
                }
            }
    ]
}

I'm not sure if there's a preferred method of conversion, a trick I'm missing, or some client side code that reformats and adds the extra data after download, but I'm unsure what's the best course of action to add in the extra wrapper data.

You can create your own object to get desired output. This example might hel you.

 var dataReturned = [ { "_id": "588a3d5a524da321dd937891", "__v": 0, "geometry": { "type": "Point", "coordinates": [ -113.5299938027191, 53.42859997065679 ] }, "type": "Feature", "properties": { "icon": "horse-riding-15", "title": "A Horse", "description": "A Horse description", "date": "2017-01-26T18:18:02.175Z" } }, { "_id": "588ac68aa99e6a38134997b5", "__v": 0, "geometry": { "type": "Point", "coordinates": [ -113.56076949999999, 53.4528447 ] }, "type": "Feature", "properties": { "icon": "dog-park-15", "title": "A Dog", "description": "A Dog description", "date": "2017-01-27T04:03:22.381Z" } } ]; var geoJSON = {}; geoJSON["type"] = "FeatureCollection"; geoJSON["features"] = dataReturned; console.log(JSON.stringify(geoJSON)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM