0

I want to extract my MongoDB query in a GeoJSON format it order to be able to visualize the result. I use Robomongo, which seems to be able to generate a JSON format. After I run the query, this file gets produced:

{"result" : [ 
        {
            "_id" : ObjectId("5627ff79ce2790eea0d96ba3"),
            "type" : "FeatureCollection",
            "crs" : {
                "type" : "name",
                "properties" : {
                    "name" : "urn:ogc:def:crs:OGC:1.3:CRS84"
                }
            },
            "features" : {
                "type" : "Feature",
                "properties" : {
                    "id" : 1250.0000000000000000,
                    "osm_id" : 87222664.0000000000000000,
                    "name" : "29th Avenue Station",
                    "type" : "yes"
                },
                "geometry" : {
                    "type" : "MultiPolygon",
                    "coordinates" : [ 
                        [ 
                            [ 
                                [ 
                                    -123.0463325098070600, 
                                    49.2443216480303650
                                ], 
                                [ 
                                    -123.0462832242164000, 
                                    49.2443726938207080
                                ], 
                                [ 
                                    -123.0453115101814800, 
                                    49.2439719550299630
                                ], 
                                [ 
                                    -123.0453609634102200, 
                                    49.2439209930586800
                                ], 
                                [ 
                                    -123.0463325098070600, 
                                    49.2443216480303650
                                ]
                            ]
                        ]
                    ]
                }
            }
        }, 
        {
            "_id" : ObjectId("5627ff79ce2790eea0d96ba3"),
            "type" : "FeatureCollection",
            "crs" : {
                "type" : "name",
                "properties" : {
                    "name" : "urn:ogc:def:crs:OGC:1.3:CRS84"
                }
            },
            "features" : {
                "type" : "Feature",
                "properties" : {
                    "id" : 9033.0000000000000000,
                    "osm_id" : 197598621.0000000000000000,
                    "name" : "Electric Avenue",
                    "type" : "yes"
                },
                "geometry" : {
                    "type" : "MultiPolygon",
                    "coordinates" : [ 
                        [ 
                            [ 
                                [ 
                                    -123.1247329764166500, 
                                    49.2812495473848170
                                ], 
                                [ 
                                    -123.1243329081781800, 
                                    49.2815073747264310
                                ], 
                                [ 
                                    -123.1244402803578200, 
                                    49.2815791238176080
                                ], 
                                [ 
                                    -123.1247323058644000, 
                                    49.2817758470851000
                                ], 
                                [ 
                                    -123.1243665196099100, 
                                    49.2820086963552630
                                ], 
                                [ 
                                    -123.1243131268866800, 
                                    49.2820425592440760
                                ], 
                                [ 
                                    -123.1242579739638100, 
                                    49.2820068523365650
                                ], 
                                [ 
                                    -123.1237645313239900, 
                                    49.2816875856446760
                                ], 
                                [ 
                                    -123.1237276509500200, 
                                    49.2816641163157950
                                ], 
                                [ 
                                    -123.1236146628952400, 
                                    49.2815920319484920
                                ], 
                                [ 
                                    -123.1237309198922500, 
                                    49.2815131582396190
                                ], 
                                [ 
                                    -123.1237663753426800, 
                                    49.2814915329294370
                                ], 
                                [ 
                                    -123.1240143120385500, 
                                    49.2813217993901840
                                ], 
                                [ 
                                    -123.1240598257727800, 
                                    49.2812880203204030
                                ], 
                                [ 
                                    -123.1244141288199200, 
                                    49.2810538299457330
                                ], 
                                [ 
                                    -123.1246444635191300, 
                                    49.2811970766709630
                                ], 
                                [ 
                                    -123.1247329764166500, 
                                    49.2812495473848170
                                ]
                            ]
                        ]
                    ]
                }
            }
        }, 
        {
            "_id" : ObjectId("5628022bce2790eea0d96baa"),
            "type" : "FeatureCollection",
            "crs" : {
                "type" : "name",
                "properties" : {
                    "name" : "urn:ogc:def:crs:OGC:1.3:CRS84"
                }
            },
            "features" : {
                "type" : "Feature",
                "properties" : {
                    "id" : 721.0000000000000000,
                    "osm_id" : 60007793.0000000000000000,
                    "name" : "1st Avenue Marketplace",
                    "type" : "retail",
                    "area" : 0.0000020000000000,
                    "z_order" : 7.0000000000000000
                },
                "geometry" : {
                    "type" : "MultiPolygon",
                    "coordinates" : [ 
                        [ 
                            [ 
                                [ 
                                    -123.0468456499193300, 
                                    49.2685273271100070
                                ], 
                                [ 
                                    -123.0468340828929600, 
                                    49.2693428862887970
                                ], 
                                [ 
                                    -123.0443349346427300, 
                                    49.2693247813779180
                                ], 
                                [ 
                                    -123.0443671211509300, 
                                    49.2685336973564180
                                ], 
                                [ 
                                    -123.0468456499193300, 
                                    49.2685273271100070
                                ]
                            ]
                        ]
                    ]
                }
            }
        }
    ],
    "ok" : 1.0000000000000000
}

I am trying to test the validity of my file using GeoJSONLint but it prompts that the file is not serializeable.

Do you know how can I extract a GeoJSON from a query or somehow convert the JSON to GeoJSON? My file seems to follow the GeoJSON specifications but I was not able to find any tool to do the job.

2 Answers 2

2

there's a small issue with that JSON.

First, try jsonlint on the whole thing, you'll see that it complains about the format of the 3 "_id" properties. If you replace those with for example 123, 456, 789, it parses correctly.

Then if you use a graphical JSON viewer such as this one, you'll see that this JSON defines an object that contains a property named "result" being a list of 3 elements, and a status code property. The value of the "features" property of each of the 3 elements in the list is valid GeoJSON, which you can verify by pasting it into geojsonlint, for example this excerpt being the value from the first element:

        {
            "type" : "Feature",
            "properties" : {
                "id" : 1250.0000000000000000,
                "osm_id" : 87222664.0000000000000000,
                "name" : "29th Avenue Station",
                "type" : "yes"
            },
            "geometry" : {
                "type" : "MultiPolygon",
                "coordinates" : [ 
                    [ 
                        [ 
                            [ 
                                -123.0463325098070600, 
                                49.2443216480303650
                            ], 
                            [ 
                                -123.0462832242164000, 
                                49.2443726938207080
                            ], 
                            [ 
                                -123.0453115101814800, 
                                49.2439719550299630
                            ], 
                            [ 
                                -123.0453609634102200, 
                                49.2439209930586800
                            ], 
                            [ 
                                -123.0463325098070600, 
                                49.2443216480303650
                            ]
                        ]
                    ]
                ]
            }
        }
4
  • and on a bit more inspection, I see that the entire value of each of the 3 elements is almost valid geojson - first replace those objectid values like I described, then geojsonlint complains thusly: "A FeatureCollection's "features" property must be an array." If you enclose the value of the "features" property in braces to make it an array, it parses correctly.
    – MC5
    Oct 22, 2015 at 19:18
  • Thanks, I am stuck in: A FeatureCollection must have a "features" property. Aren't my "features" already enclosed by braces already? Can you show the format you are referring to? Oct 22, 2015 at 19:57
  • sorry, meant 'brackets' in that comment, not 'braces'. instead of "features" : {blah blah} it should be "features" : [{blah blah}]
    – MC5
    Oct 22, 2015 at 20:35
  • Thank you for your help. I will upload a complete answer this day; probably I will create a script to do some of the required parsing based on your annotations :) Oct 22, 2015 at 20:59
1

Thanks to MC5, I managed to convert my JSON to GeoJSON and visualized them via Google Drag and Drop GeoJSON. I used the following REGEX's:

  1. "features" : \{.*|"coordinates"[^}]*\K\}|"type" : "Feature"|"ok" : 1.0000000000000000|"id".*,|"_id" : ObjectId\(.*,
  2. "type" : "FeatureCollection",[^}]*\}, replace with "type" : "Feature",

Please notice that your file might follow a different structure, since the way you project the data obviously effects the output file. In such a case, extra modification is required.

You can view the the analytical description of the logic in this detailed answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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