├── .gitignore ├── LICENSE.md ├── README.md ├── dist ├── geojson-bbox.js ├── geojson-bbox.js.map └── geojson-bbox.min.js ├── geojson-bbox.js ├── package-lock.json ├── package.json └── test └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Gagan Bansal 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # geojson-bbox 2 | Calculates extent/bbox for a given valid geojson object. When passed a valid geojson returns an array of [bounding box](http://geojson.org/geojson-spec.html#bounding-boxes) 3 | ## installation 4 | 5 | ``` 6 | npm install geojson-bbox 7 | ``` 8 | 9 | ## usage 10 | 11 | ```javascript 12 | var bbox = require('geojson-bbox'); 13 | var feature = { 14 | type: 'Feature', 15 | geometry: { 16 | type: 'LineString', 17 | coordinates: [ 18 | [10, 40], [40, 30], [20, 20], [30, 10] 19 | ] 20 | } 21 | }; 22 | var extent = bbox(feature); 23 | // extent is array 24 | // [10, 10, 40, 40] 25 | ``` 26 | 27 | ## developing 28 | Once you run 29 | 30 | ```npm isntall``` 31 | 32 | then for running test 33 | 34 | ```npm run test``` 35 | 36 | to create build 37 | 38 | ```npm run build``` 39 | 40 | ## license 41 | This project is licensed under the terms of the MIT license. 42 | -------------------------------------------------------------------------------- /dist/geojson-bbox.js: -------------------------------------------------------------------------------- 1 | (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.bbox = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i dist/geojson-bbox.js", 9 | "build-min": "browserify geojson-bbox.js --standalone bbox | uglifyjs -c -m > dist/geojson-bbox.min.js", 10 | "build": "npm run build-debug && npm run build-min" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/geosquare/geojson-bbox" 15 | }, 16 | "keywords": [ 17 | "geojson", 18 | "extent", 19 | "bbox", 20 | "bounds", 21 | "maps", 22 | "geo", 23 | "geospatial" 24 | ], 25 | "author": "Gagan Bansal ", 26 | "license": "MIT", 27 | "bugs": { 28 | "url": "https://github.com/geosquare/geojson-bbox/issues" 29 | }, 30 | "homepage": "https://github.com/geosquare/geojson-bbox", 31 | "devDependencies": { 32 | "browserify": "^17.0.0", 33 | "chai": "^1.10.0", 34 | "exorcist": "^2.0.0", 35 | "mocha": "^10.1.0", 36 | "uglify-js": "^2.4.16" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var expect = require('chai').expect, 2 | bbox = require('../'); 3 | describe('Tests for all geojson types', function() { 4 | it('Point', function() { 5 | expect( 6 | bbox({type: 'Point', coordinates: [10,20]}) 7 | ).to.be.instanceOf(Array) 8 | .that.to.have.length(4) 9 | .that.to.deep.equal([10, 20, 10, 20]); 10 | }); 11 | it('LineString', function() { 12 | expect( 13 | bbox({ 14 | type: 'LineString', 15 | coordinates: [[30, 10], [10, 30], [40, 40]] 16 | }) 17 | ).to.be.instanceOf(Array) 18 | .that.to.have.length(4) 19 | .that.to.deep.equal([10, 10, 40, 40]); 20 | }); 21 | it('Polygon', function() { 22 | expect( 23 | bbox({ 24 | type: 'Polygon', 25 | coordinates: [ 26 | [[30, 10], [40, 40], [20, 40], [10, 20], [30, 10]] 27 | ] 28 | }) 29 | ).to.be.instanceOf(Array) 30 | .that.to.have.length(4) 31 | .that.to.deep.equal([10, 10, 40, 40]); 32 | }); 33 | it('Polygon with hole', function() { 34 | expect( 35 | bbox({ 36 | type: 'Polygon', 37 | coordinates: [ 38 | [[35, 10], [45, 45], [15, 40], [10, 20], [35, 10]], 39 | [[20, 30], [35, 35], [30, 20], [20, 30]] 40 | ] 41 | }) 42 | ).to.be.instanceOf(Array) 43 | .that.to.have.length(4) 44 | .that.to.deep.equal([10, 10, 45, 45]); 45 | }); 46 | it('MultiPoint', function() { 47 | expect( 48 | bbox({ 49 | type: 'MultiPoint', 50 | coordinates: [ 51 | [10, 40], [40, 30], [20, 20], [30, 10] 52 | ] 53 | }) 54 | ).to.be.instanceOf(Array) 55 | .that.to.have.length(4) 56 | .that.to.deep.equal([10, 10, 40, 40]); 57 | }); 58 | it('MultiLineString', function() { 59 | expect( 60 | bbox({ 61 | type: 'MultiLineString', 62 | coordinates: [ 63 | [[10, 10], [20, 20], [10, 40]], 64 | [[40, 40], [30, 30], [40, 20], [30, 10]] 65 | ] 66 | }) 67 | ).to.be.instanceOf(Array) 68 | .that.to.have.length(4) 69 | .that.to.deep.equal([10, 10, 40, 40]); 70 | }); 71 | it('MultiPolygon', function() { 72 | expect( 73 | bbox({ 74 | type: 'MultiPolygon', 75 | coordinates: [ 76 | [ 77 | [[30, 20], [45, 40], [10, 40], [30, 20]] 78 | ], 79 | [ 80 | [[15, 5], [40, 10], [10, 20], [5, 10], [15, 5]] 81 | ] 82 | ] 83 | }) 84 | ).to.be.instanceOf(Array) 85 | .that.to.have.length(4) 86 | .that.to.deep.equal([5, 5, 45, 40]); 87 | }); 88 | it('MultiPolygon with hole', function() { 89 | expect( 90 | bbox({ 91 | type: 'MultiPolygon', 92 | coordinates: [ 93 | [ 94 | [[40, 40], [20, 45], [45, 30], [40, 40]] 95 | ], 96 | [ 97 | [[20, 35], [10, 30], [10, 10], [30, 5], [45, 20], [20, 35]], 98 | [[30, 20], [20, 15], [20, 25], [30, 20]] 99 | ] 100 | ] 101 | }) 102 | ).to.be.instanceOf(Array) 103 | .that.to.have.length(4) 104 | .that.to.deep.equal([10, 5, 45, 45]); 105 | }); 106 | it('GeometryCollection', function() { 107 | expect( 108 | bbox({ 109 | "type": "GeometryCollection", 110 | "geometries": [ 111 | { "type": "Point", 112 | "coordinates": [100.0, 0.0] 113 | }, 114 | { "type": "LineString", 115 | "coordinates": [ [101.0, 0.0], [102.0, 1.0] ] 116 | } 117 | ] 118 | }) 119 | ).to.be.instanceOf(Array) 120 | .that.to.have.length(4) 121 | .that.to.deep.equal([100, 0, 102, 1]); 122 | }); 123 | it('Feature', function() { 124 | expect( 125 | bbox({ 126 | type: 'Feature', 127 | geometry: { 128 | type: 'LineString', 129 | coordinates: [ 130 | [10, 40], [40, 30], [20, 20], [30, 10] 131 | ] 132 | } 133 | }) 134 | ).to.be.instanceOf(Array) 135 | .that.to.have.length(4) 136 | .that.to.deep.equal([10, 10, 40, 40]); 137 | }); 138 | it('FeatureCollection', function() { 139 | expect( 140 | bbox({ 141 | "type": "FeatureCollection", 142 | "features": [ 143 | { "type": "Feature", 144 | "geometry": {"type": "Point", "coordinates": [102.0, 0.5]}, 145 | "properties": {"prop0": "value0"} 146 | }, 147 | { "type": "Feature", 148 | "geometry": { 149 | "type": "LineString", 150 | "coordinates": [ 151 | [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0] 152 | ] 153 | }, 154 | "properties": { 155 | "prop0": "value0", 156 | "prop1": 0.0 157 | } 158 | }, 159 | { "type": "Feature", 160 | "geometry": { 161 | "type": "Polygon", 162 | "coordinates": [ 163 | [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], 164 | [100.0, 1.0], [100.0, 0.0] ] 165 | ] 166 | }, 167 | "properties": { 168 | "prop0": "value0", 169 | "prop1": {"this": "that"} 170 | } 171 | } 172 | ] 173 | }) 174 | ).to.be.instanceOf(Array) 175 | .that.to.have.length(4) 176 | .that.to.deep.equal([100, 0, 105, 1]); 177 | }); 178 | }); 179 | --------------------------------------------------------------------------------