Skip to content

bounding-box

computeBoundingBox

Signature

js
computeBoundingBox (geoJson, options)

Description

Computes the bounding box of any GeoJSON object as a flat coordinate array [west, south, east, north] in 2D, or [west, south, minAlt, east, north, maxAlt] in 3D. Accepts a plain geometry, a Feature, or a FeatureCollection. Features with a null geometry are silently ignored. The 3D form is produced automatically when at least one position carries an altitude value. Returns null if no positions can be extracted.

Parameters

NameTypeRequiredDescription
geoJsonobjectyesAny valid GeoJSON object (geometry, Feature, or FeatureCollection)
options.ignore3DbooleannoIf true, altitude values are ignored and a 2D bbox is always returned (default: false)

Returns

TypeDescription
number[] | nullA 2D bbox [west, south, east, north], a 3D bbox [west, south, minAlt, east, north, maxAlt], or null if no positions are available

Throws

Throws if geoJson is not a valid GeoJSON object. Throws if options does not match the expected schema.

Examples

js
// Point
computeBoundingBox({ type: 'Point', coordinates: [2.349, 48.864] })
// [2.349, 48.864, 2.349, 48.864]
js
// LineString
computeBoundingBox({
  type: 'LineString',
  coordinates: [[-73.985, 40.748], [-87.623, 41.881], [-118.243, 34.052]]
})
// [-118.243, 34.052, -73.985, 41.881]
js
// Polygon with hole — hole coordinates are included in the bbox
computeBoundingBox({
  type: 'Polygon',
  coordinates: [
    [[-10, -10], [10, -10], [10, 10], [-10, 10], [-10, -10]],
    [[-5, -5], [5, -5], [5, 5], [-5, 5], [-5, -5]]
  ]
})
// [-10, -10, 10, 10]
js
// 3D LineString — altitude range included automatically
computeBoundingBox({
  type: 'LineString',
  coordinates: [[6.865, 45.832, 1034], [7.742, 45.921, 672], [7.315, 45.074, 250]]
})
// [6.865, 45.074, 250, 7.742, 45.832, 1034]
js
// Ignore altitude
computeBoundingBox(
  {
    type: 'LineString',
    coordinates: [[6.865, 45.832, 1034], [7.742, 45.921, 672], [7.315, 45.074, 250]]
  },
  { ignore3D: true }
)
// [6.865, 45.074, 7.742, 45.832]
js
// Feature
computeBoundingBox({
  type: 'Feature',
  geometry: { type: 'Point', coordinates: [2.349, 48.864] },
  properties: { name: 'Paris' }
})
// [2.349, 48.864, 2.349, 48.864]
js
// Feature with null geometry — returns null
computeBoundingBox({ type: 'Feature', geometry: null, properties: {} })
// null
js
// FeatureCollection
computeBoundingBox({
  type: 'FeatureCollection',
  features: [
    { type: 'Feature', geometry: { type: 'Point', coordinates: [2.349, 48.864] }, properties: {} },
    { type: 'Feature', geometry: { type: 'Point', coordinates: [28.979, 41.015] }, properties: {} }
  ]
})
// [2.349, 41.015, 28.979, 48.864]
js
// FeatureCollection with null geometries — ignored silently
computeBoundingBox({
  type: 'FeatureCollection',
  features: [
    { type: 'Feature', geometry: { type: 'Point', coordinates: [2.349, 48.864] }, properties: {} },
    { type: 'Feature', geometry: null, properties: {} },
    { type: 'Feature', geometry: { type: 'Point', coordinates: [12.496, 41.902] }, properties: {} }
  ]
})
// [2.349, 41.902, 12.496, 48.864]
js
// Empty FeatureCollection — returns null
computeBoundingBox({ type: 'FeatureCollection', features: [] })
// null
js
// 3D FeatureCollection
computeBoundingBox({
  type: 'FeatureCollection',
  features: [
    { type: 'Feature', geometry: { type: 'Point', coordinates: [0, 0, 100] }, properties: {} },
    { type: 'Feature', geometry: { type: 'LineString', coordinates: [[5, 5, 500], [10, 10, 200]] }, properties: {} }
  ]
})
// [0, 0, 100, 10, 10, 500]
js
// Ignore altitude on a FeatureCollection
computeBoundingBox(
  {
    type: 'FeatureCollection',
    features: [
      { type: 'Feature', geometry: { type: 'Point', coordinates: [0, 0, 100] }, properties: {} },
      { type: 'Feature', geometry: { type: 'LineString', coordinates: [[5, 5, 500], [10, 10, 200]] }, properties: {} }
    ]
  },
  { ignore3D: true }
)
// [0, 0, 10, 10]