R sf: extract nested geoJSON features nested inside a JSON

343 Views Asked by At

I have a JSON file that has geoJSON feature collections nested inside of it.

Is it possible to read in the JSON file using jsonlite::read_json(), extract the geoJSON bits, and then convert the resulting list to a sf object? The alternative is to write the list back to JSON (text) and read the geoJSON using a package like geojsonio.

This is what my JSON code looks like:

{
  "all": [
    {
      "type": "Feature",
      "geometry": {
        "type": "GeometryCollection",
        "geometries": [
          {
            "type": "Point",
            "coordinates": [
              -75.155727,
              39.956318
            ]
          },{
            "type": "LineString",
            "coordinates": [
              [
                -75.15567895337301,
                39.95653558798881
              ],[
                -75.15575995337292,
                39.95616931624319
              ]
            ]
          },{
            "type": "Point",
            "coordinates": [
              -75.15566,
              39.956432
            ]
          }
        ]
      },
      "properties": {
        # properties
      }
    },{ 
      # more features of mixed type
    }
  ]
}
1

There are 1 best solutions below

0
On

perhaps

x <- '{
  "all": [
    {
      "type": "Feature",
      "geometry": {
        "type": "GeometryCollection",
        "geometries": [
          {
            "type": "Point",
            "coordinates": [
              -75.155727,
              39.956318
            ]
          },{
            "type": "LineString",
            "coordinates": [
              [
                -75.15567895337301,
                39.95653558798881
              ],[
                -75.15575995337292,
                39.95616931624319
              ]
            ]
          },{
            "type": "Point",
            "coordinates": [
              -75.15566,
              39.956432
            ]
          }
        ]
      },
      "properties": null
    }
  ]
}'
sf::st_read(jqr::jq(x, ".all[]"))

(string edited to be valid JSON)