What is the correct way to add a bbox in a GeoJSON object?

Viewed 1334

I'm generating some GeoJSON documents and I was unsure how to properly add a bbox to a geometry.

Do I need to necessarily create a Feature object to add the bbox (example 1) or can I do this in the geometry itself (example 2)?

The GeoSON specification (https://www.rfc-editor.org/rfc/rfc7946#appendix-A) presents examples only with the bbox in a GeoJSON object of type Feature, however, validators accept the two modes of inserting the bbox.

Example 1: In this example, the bbox is an element of the GeoJSON object of type Feature.

{
  "type": "Feature",
  "properties": {"id": 1, "Linha": null},
  "bbox": [-48.573569106948469, -27.837770518544438, -48.417446881093412, -27.381161181879751],
  "geometry": {
    "type": "LineString",
    "coordinates": [
      [-48.417446881093412, -27.381161181879751],
      [-48.573569106948469, -27.837770518544438]
    ]
  }
}

Example 2: In this other example, the bbox was included directly in the GeoJSON object with the geometry (LineString).

{
   "type": "LineString",
   "bbox": [-48.573569106948469, -27.837770518544438, -48.417446881093412, -27.381161181879751],
   "coordinates": [
          [-48.417446881093412, -27.381161181879751],
          [-48.573569106948469, -27.837770518544438]
   ]
}
1 Answers

This is a good question, and you are right that the spec has incomplete examples. From the standard, look at the definition of a GeoJSON Object:

  1. GeoJSON Object

    A GeoJSON object represents a Geometry, Feature, or collection of Features.

    • A GeoJSON object is a JSON object.
    • A GeoJSON object has a member with the name "type". The value of the member MUST be one of the GeoJSON types.
    • A GeoJSON object MAY have a "bbox" member, the value of which MUST be a bounding box array (see Section 5).

Then, from the section on bounding boxes:

A GeoJSON object MAY have a member named "bbox" to include information on the coordinate range for its Geometries, Features, or FeatureCollections.

This is why the validators accept both of your examples. They are both valid geojson objects to have a bounding box on.

To help validate this in my own projects, I developed a set of matchers called jest-geojson that extends the Jest testing framework to make it easier to evaluate GeoJSON. Bounding box evaluation is included.

Related