Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split feature collection #49

Open
wiesehahn opened this issue Jun 18, 2024 · 1 comment
Open

split feature collection #49

wiesehahn opened this issue Jun 18, 2024 · 1 comment

Comments

@wiesehahn
Copy link

Sorry if this doesnt fit here, I guess its more a question than an issue.

I would like to read a STAC ItemCollection(a GeoJSON FeatureCollection that is augmented with foreign members) and save individual features to disk as seperate geojson.

t <- yyjsonr::read_json_conn("https://raw.githubusercontent.com/radiantearth/stac-api-spec/main/fragments/itemcollection/examples/itemcollection-sample-full.json")
features <- t$features

for (i in 1:nrow(features)) {
  feature <- features[i,]
  write_json_file(feature,
                  filename = here::here(paste0(feature$id, ".json")),
                  opts = opts_write_json(pretty = TRUE,
                                         auto_unbox = TRUE))
  
}

The output is as expected, except that it is wrapped as array in [ ]. I guess this is due to the json spec.

as is:

[
  {
    "stac_version": "1.0.0",
    "stac_extensions": [],
    "type": "Feature",
    "id": "cs3-20160503_132131_05",
    "bbox": [
      -122.59750209,
      37.48803556,
      -122.2880486,
      37.613537207
    ],
    "geometry": {
      "type": "Polygon",
      "coordinates": [
        [
          [
            -122.308150179,
            37.488035566
          ],
          [
            -122.597502109,
            37.538869539
          ],
          [
            -122.576687533,
            37.613537207
          ],
          [
            -122.2880486,
            37.562818007
          ],
          [
            -122.308150179,
            37.488035566
          ]
        ]
      ]
    },
    "properties": {
      "datetime": "2016-05-03T13:22:30.040Z",
      "title": "A CS3 item",
      "license": "PDDL-1.0",
      "providers": [
        {
          "name": "CoolSat",
          "roles": [
            "producer",
            "licensor"
          ],
          "url": "https://stac-api.example.com"
        }
      ]
    },
    "collection": "cs3",
    "links": [
      {
        "rel": "self",
        "type": "application/json",
        "href": "https://stac-api.example.com/collections/cs3/items/CS3-20160503_132131_05"
      },
      {
        "rel": "root",
        "type": "application/json",
        "href": "https://stac-api.example.com/"
      },
      {
        "rel": "collection",
        "type": "application/json",
        "href": "https://stac-api.example.com/collections/cs3"
      }
    ],
    "assets": {
      "analytic": {
        "href": "https://stac-api.example.com/catalog/cs3-20160503_132130_04/analytic.tif",
        "type": "image/tiff; application=geotiff; profile=cloud-optimized",
        "title": "4-Band Analytic"
      },
      "thumbnail": {
        "href": "https://stac-api.example.com/catalog/cs3-20160503_132130_04/thumbnail.png",
        "type": "image/png",
        "title": "Thumbnail",
        "roles": "thumbnail"
      }
    }
  }
]

as expected:

{
  "stac_version": "1.0.0",
  "stac_extensions": [],
  "type": "Feature",
  "id": "cs3-20160503_132131_05",
  "bbox": [
    -122.59750209,
    37.48803556,
    -122.2880486,
    37.613537207
  ],
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [
          -122.308150179,
          37.488035566
        ],
        [
          -122.597502109,
          37.538869539
        ],
        [
          -122.576687533,
          37.613537207
        ],
        [
          -122.2880486,
          37.562818007
        ],
        [
          -122.308150179,
          37.488035566
        ]
      ]
    ]
  },
  "properties": {
    "datetime": "2016-05-03T13:22:30.040Z",
    "title": "A CS3 item",
    "license": "PDDL-1.0",
    "providers": [
      {
        "name": "CoolSat",
        "roles": [
          "producer",
          "licensor"
        ],
        "url": "https://stac-api.example.com"
      }
    ]
  },
  "collection": "cs3",
  "links": [
    {
      "rel": "self",
      "type": "application/json",
      "href": "https://stac-api.example.com/collections/cs3/items/CS3-20160503_132131_05"
    },
    {
      "rel": "root",
      "type": "application/json",
      "href": "https://stac-api.example.com/"
    },
    {
      "rel": "collection",
      "type": "application/json",
      "href": "https://stac-api.example.com/collections/cs3"
    }
  ],
  "assets": {
    "analytic": {
      "href": "https://stac-api.example.com/catalog/cs3-20160503_132130_04/analytic.tif",
      "type": "image/tiff; application=geotiff; profile=cloud-optimized",
      "title": "4-Band Analytic"
    },
    "thumbnail": {
      "href": "https://stac-api.example.com/catalog/cs3-20160503_132130_04/thumbnail.png",
      "type": "image/png",
      "title": "Thumbnail",
      "roles": "thumbnail"
    }
  }
}

What would be an appropriate way to save the files without the square brackets? Do I somehow need do convert the features to sf first and then use write_geojson_file()?

@wiesehahn
Copy link
Author

While I still didnt found a nice solution the following at least does what it should

# Read the JSON file as a text string
json_content <- readLines(here::here(paste0(feature$id, ".json"), warn = FALSE)

# Collapse the lines into a single string
json_string <- paste(json_content, collapse = "\n")

# Remove the outer square brackets and any leading/trailing whitespace or newlines
json_string <- sub("^\\s*\\[\\s*", "", json_string)
json_string <- sub("\\s*\\]\\s*$", "", json_string)

# Write the modified JSON string to a new file
writeLines(json_string, here::here(paste0(feature$id, ".json"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant