OpenForm

bbox

Last updated on

Build and validate bounding boxes

Build and validate geographic bounding boxes. The Bbox primitive defines a rectangular area using southwest and northeast corner coordinates.

Examples

Creating bounding boxes

// Object pattern
const area = open.bbox({
  southWest: { lat: 40.7128, lon: -74.0060 },
  northEast: { lat: 40.7580, lon: -73.9855 }
})

// Builder pattern
const area = open.bbox()
  .southWest({ lat: 40.7128, lon: -74.0060 })
  .northEast({ lat: 40.7580, lon: -73.9855 })
  .build()

// Using bounds shorthand
const area = open.bbox()
  .bounds(
    { lat: 40.7128, lon: -74.0060 },
    { lat: 40.7580, lon: -73.9855 }
  )
  .build()

// Copy and modify
const expanded = open.bbox()
  .from(area)
  .northEast({ lat: 40.8000, lon: -73.9500 })
  .build()

Parsing external data

// Parse unknown input (throws on error)
const area = open.bbox.parse(jsonData)

// Safe parsing (returns result object)
const result = open.bbox.safeParse(jsonData)
if (result.success) {
  console.log(`SW: ${result.data.southWest.lat}, ${result.data.southWest.lon}`)
} else {
  console.error(result.error.message)
}

Using with forms

const form = open.form({
  name: 'property',
  fields: {
    propertyBounds: { type: 'bbox', label: 'Property Boundaries' }
  }
})

const filled = form.fill({
  fields: {
    propertyBounds: open.bbox()
      .bounds(
        { lat: 34.0522, lon: -118.2437 },
        { lat: 34.0622, lon: -118.2337 }
      )
      .build()
  }
})

API

Object Pattern

open.bbox(input: Bbox): Bbox

Pass a Bbox object for validation. Returns the validated Bbox or throws on error.

const area = open.bbox({
  southWest: { lat: 51.5074, lon: -0.1278 },
  northEast: { lat: 51.5174, lon: -0.1178 }
})

Builder Pattern

Chain methods to build a Bbox value incrementally:

All methods return BboxBuilder and are chainable.

open.bbox()
southWest: (value: Coordinate) => BboxBuilder
Set southwest corner
northEast: (value: Coordinate) => BboxBuilder
Set northeast corner
bounds: (sw: Coordinate, ne: Coordinate) => BboxBuilder
Set both corners at once
from: (value: Bbox) => BboxBuilder
Initialize from existing Bbox
build: () => Bbox
Build and validate

Static Methods

parse: (input: unknown) => Bbox
Parse unknown input (throws on error)
safeParse: (input: unknown) => Result<Bbox>
Parse unknown input (returns result object)

Properties

southWest: Coordinate
Southwest corner of the bounding box.
northEast: Coordinate
Northeast corner of the bounding box.

Validation

The Bbox primitive validates:

  • southWest must be a valid Coordinate
  • northEast must be a valid Coordinate
  • Both coordinates must have valid lat/lon values
// Valid
open.bbox({
  southWest: { lat: 40.7128, lon: -74.0060 },
  northEast: { lat: 40.7580, lon: -73.9855 }
})

// Invalid - throws Error
open.bbox({
  southWest: { lat: 91, lon: -74 },  // Invalid latitude
  northEast: { lat: 40, lon: -73 }
})
// Error: Invalid Bbox: southWest latitude must be between -90 and 90

Related

On this page