bounding-box
Implements bounding boxes with usual functions like intersects. Can convert from/to Leaflet bounds and GeoJSON.
Usage
Browser
Include dist-file via script src:
<script src='node_module/boundingbox/dist/boundingbox.js'>
NodeJS
var BoundingBox = require('boundingbox')
API
Constructor: new BoundingBox(data)
Creates a bounding box object.
Data can be one of the following:
- Min/max coordinates: { minlat: 48.123, minlon: 16.23, maxlat: 49.012, maxlon: 16.367 }
- Lat/lon coordinates: { lat: 48.5, lon: 16.5 }
- A BoundingBox object to create a copy
- A L.latLngBounds object (using Leaflet)
- A L.latLng object (using Leaflet)
Example:
var bbox = new BoundingBox({ minlat: 48.123, minlon: 16.23, maxlat: 49.012, maxlon: 16.367 })
Method intersects(bounds)
Checks whether the bounding box 'bounds' intersects (shares any portion of space) the current object.
Example:
var bbox = new BoundingBox({ minlat: 48.123, minlon: 16.23, maxlat: 49.012, maxlon: 16.367 })
var bbox2 = new BoundingBox({ lat: 48.5, lon: 16.5 })
console.log(bbox.intersects(bbox2))
Method toBBoxString()
Returns a string with the bounding box coordinates in a 'sw_lon,sw_lat,ne_lon,ne_lat' format. Useful for sending requests to web services that return geo data.
var bbox = new BoundingBox({ minlat: 48.123, minlon: 16.23, maxlat: 49.012, maxlon: 16.367 })
console.log(bbox.toBBoxString())
Method diagonalLength()
Returns the length of the diagonal of the bounding box.
var bbox = new BoundingBox({ minlat: 48.123, minlon: 16.23, maxlat: 49.012, maxlon: 16.367 })
console.log(bbox.diagonalLength())
Method getCenter()
Returns the center point of the bounding box.
var bbox = new BoundingBox({ minlat: 48.123, minlon: 16.23, maxlat: 49.012, maxlon: 16.367 })
console.log(bbox.getCenter())
Method toGeoJSON()
Returns the bounding box as GeoJSON feature.
Method toLeaflet()
Returns the bounding box as L.latLngBounds object
Method toTile()
(This function is still work in progress)
Fit the bounding box into tiles
Tests
To run tests with nodejs, just call ./run_tests
To run tests in a browser, call ./run_tests
first, which will create all_tests.js
and then open the file test.html
in your favorite browser.