Skip to content

Commit

Permalink
Add .polygon()
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Jul 16, 2014
1 parent 6d3adee commit 96b936c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
9 changes: 7 additions & 2 deletions README.md
Expand Up @@ -2,7 +2,7 @@

# extent

a simple geographical extent
A simple geographical extent.

## api

Expand All @@ -21,4 +21,9 @@ Expand the extent to include another extent.
### `extent.bbox()`

Get the extent's value. `null` if no points have
been included yet.
been included yet. Order is `[WSEN]` to match the [GeoJSON](http://geojson.org/)
standard.

### `extent.polygon()`

Get the extent as a [GeoJSON](http://geojson.org/) Polygon geometry object.
21 changes: 21 additions & 0 deletions index.js
Expand Up @@ -30,3 +30,24 @@ Extent.prototype.bbox = function() {
if (!this._valid) return null;
return this._bbox;
};

Extent.prototype.polygon = function() {
if (!this._valid) return null;
return {
type: 'Polygon',
coordinates: [
[
// W, S
[this._bbox[0], this._bbox[1]],
// E, S
[this._bbox[2], this._bbox[1]],
// E, N
[this._bbox[2], this._bbox[3]],
// W, N
[this._bbox[0], this._bbox[3]],
// W, S
[this._bbox[0], this._bbox[1]]
]
]
};
};
8 changes: 8 additions & 0 deletions test/extent.js
Expand Up @@ -17,5 +17,13 @@ test('extent', function(t) {
.include([-10, -10])
.bbox(),
[-10, -10, 10, 10], 'three points');
t.deepEqual(Extent()
.include([0, 0])
.include([10, 10])
.include([-10, -10])
.polygon(), {
type: 'Polygon',
coordinates: [[[-10,-10],[10,-10],[10,10],[-10,10],[-10,-10]]]
}, 'polygon');
t.end();
});

0 comments on commit 96b936c

Please sign in to comment.