Skip to content

Commit

Permalink
engage
Browse files Browse the repository at this point in the history
  • Loading branch information
ungoldman committed Mar 19, 2015
0 parents commit 294a096
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: node_js

node_js:
- '0.12'
- 'iojs'
sudo: false
cache:
directories:
- node_modules

script:
- npm test
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# esri-extent change log
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## 1.0.0
* engage
13 changes: 13 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2015, Nate Goldman <[email protected]>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# esri-extent

[![](https://img.shields.io/npm/v/esri-extent.svg?style=flat-square)](https://www.npmjs.com/package/esri-extent)
[![](https://img.shields.io/travis/ngoldman/esri-extent.svg?style=flat-square)](https://travis-ci.org/ngoldman/esri-extent)

Get the Esri-formatted extent of a GeoJSON feature collection.

```
npm install esri-extent
```

## Usage

```js
var esriExtent = require('esri-extent')
var example = require('./test/geojson-spec-example.json')

var extent = esriExtent(example)

// OR

esriExtent(example, function (err, extent) {
if (err) throw err

console.log(extent)
// {
// xmin: 100,
// ymin: 0,
// xmax: 105,
// ymax: 1,
// spatialReference: {
// wkid: 4326,
// latestWkid: 4326
// }
// }
})
```

## License

[ISC](LICENSE.md)
46 changes: 46 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var bbox2extent = require('bbox2extent')
var bbox = [Infinity, Infinity, -Infinity, -Infinity]

module.exports = function (collection, callback) {
// fallback if features passed directly
var features = collection.features ? collection.features : collection

features.forEach(function (f, i) {
if (!f.geometry) return

var isPoint = f.geometry.type === 'Point'
var isLine = f.geometry.type === 'LineString'
var isMultiLine = f.geometry.type === 'MultiLineString'
var isPolygon = f.geometry.type === 'Polygon'
var isMultiPolygon = f.geometry.type === 'MultiPolygon'
var coords = f.geometry.coordinates

if (isPoint) extend(bbox, coords)

if (isLine || isMultiLine) {
if (isMultiLine) coords = coords[0]
coords.forEach(function (c, j) {
extend(bbox, c)
})
}

if (isPolygon || isMultiPolygon) {
coords = isMultiPolygon ? coords[0][0] : coords[0]
coords.forEach(function (c, j) {
extend(bbox, c)
})
}
})

var extent = bbox2extent(bbox)

if (callback) callback(null, extent)
else return extent
}

function extend (bbox, coord) {
bbox[0] = Math.min(bbox[0], coord[0])
bbox[1] = Math.min(bbox[1], coord[1])
bbox[2] = Math.max(bbox[2], coord[0])
bbox[3] = Math.max(bbox[3], coord[1])
}
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "esri-extent",
"description": "Get the Esri-formatted extent of a GeoJSON feature collection.",
"version": "1.0.0",
"author": "Nate Goldman <[email protected]>",
"bugs": {
"url": "https://github.com/ngoldman/esri-extent/issues"
},
"devDependencies": {
"standard": "^3.2.0",
"tap-spec": "^2.2.2",
"tape": "^3.5.0"
},
"homepage": "https://github.com/ngoldman/esri-extent",
"keywords": [
"bounding",
"box",
"collection",
"esri",
"extent",
"feature",
"geojson"
],
"license": "ISC",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/ngoldman/esri-extent.git"
},
"scripts": {
"test": "standard && node test | tap-spec"
},
"dependencies": {
"bbox2extent": "^1.0.1"
}
}
83 changes: 83 additions & 0 deletions test/geojson-spec-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
102.0,
0.5
]
},
"properties": {
"prop0": "value0"
}
},
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[
102.0,
0.0
],
[
103.0,
1.0
],
[
104.0,
0.0
],
[
105.0,
1.0
]
]
},
"properties": {
"prop0": "value0",
"prop1": 0.0
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
100.0,
0.0
],
[
101.0,
0.0
],
[
101.0,
1.0
],
[
100.0,
1.0
],
[
100.0,
0.0
]
]
]
},
"properties": {
"prop0": "value0",
"prop1": {
"this": "that"
}
}
}
]
}
22 changes: 22 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var esriExtent = require('..')
var example = require('./geojson-spec-example.json')
var test = require('tape')
var expected = {
xmin: 100,
ymin: 0,
xmax: 105,
ymax: 1,
spatialReference: {
wkid: 4326,
latestWkid: 4326
}
}

test('get correct extent from geojson spec example', function (t) {
esriExtent(example, function (err, extent) {
if (err) throw err

t.deepEqual(extent, expected)
t.end()
})
})

0 comments on commit 294a096

Please sign in to comment.