Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pointArray function #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ util.geo.multi({
})
```

#### util.geo.pointArray({ longitude, latitude, elevation })

Never worry about getting the order of parameters in a GeoJSON point array wrong again. Returns a properly formed GeoJSON array for use in a point.

Returns null if either longitude or latitude is missing. If elevation is null, it is simply ignored.

```js
{
type: 'Point',
coordinates: util.geo.pointArray({ longitude: -112.052790, latitude: 33.505195, elevation: null })
}
```

#### util.geo.simplify(value)

Simplifies any given GeoJSON geometry to the precision of a meter. Values that do not need simplification will be returned with no modifications.
Expand Down
6 changes: 6 additions & 0 deletions src/util/geo/pointArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import _ from 'lodash'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import { compact } from 'lodash'


export default ({ longitude, latitude, elevation }) => {
if (!longitude || !latitude) return null
return _.compact([ longitude, latitude, elevation ])
}
22 changes: 22 additions & 0 deletions test/geo/pointArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*eslint no-console: 0*/
import should from 'should'
import _ from 'lodash'
import createUtil from '../../src/util'

const util = createUtil()

describe('geo#pointArray', () => {
it('should exist', async () => {
should.exist(util.geo.pointArray)
should.equal(typeof util.geo.pointArray, 'function')
})
it('should return valid point array for coords', async () => {
_.isEqual(util.geo.pointArray({ longitude: -118.250587, latitude: 34.031179, elevation: 1024 }), [ -118.250587 , 34.031179 , 1024 ]).should.be.true()

_.isEqual(util.geo.pointArray({ longitude: -118.250587, latitude: 34.031179 }), [ -118.250587 , 34.031179 ]).should.be.true()
})
it('should return null when either latitude or longitude is left out', async () => {
should(util.geo.pointArray({ longitude: -118.250587, latitude: null })).be.null()
should(util.geo.pointArray({ longitude: null, latitude: 34.031179 })).be.null()
})
})