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

New module @turf/line-arc #657

Merged
merged 5 commits into from
Apr 10, 2017
Merged
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
20 changes: 20 additions & 0 deletions packages/turf-line-arc/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2017 TurfJS

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
63 changes: 63 additions & 0 deletions packages/turf-line-arc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# @turf/line-arc

# line-arc

Creates a circular arc, of a circle of the given radius and center point, between bearing1 and bearing2;
0 bearing is North of center point, positive clockwise.

**Parameters**

- `center` **[Feature](http://geojson.org/geojson-spec.html#feature-objects)<[Point](http://geojson.org/geojson-spec.html#point)>** center point
- `radius` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** radius of the circle
- `bearing1` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** angle, in decimal degrees, of the first radius of the arc
- `bearing2` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** angle, in decimal degrees, of the second radius of the arc
- `steps` **\[[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)]** number of steps (optional, default `64`)
- `units` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** miles, kilometers, degrees, or radians (optional, default `kilometers`)

**Examples**

```javascript
var center = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [-75, 40]
}
}
var radius = 5;
var bearing1 = 25;
var bearing2 = 47;

var arc = turf.lineArc(center, radius, bearing1, bearing2);

//addToMap
var addToMap = [center, arc]
```

Returns **[Feature](http://geojson.org/geojson-spec.html#feature-objects)<[LineString](http://geojson.org/geojson-spec.html#linestring)>** line arc

<!-- This file is automatically generated. Please don't edit it directly:
if you find an error, edit the source file (likely index.js), and re-run
./scripts/generate-readmes in the turf project. -->

---

This module is part of the [Turfjs project](http://turfjs.org/), an open source
module collection dedicated to geographic algorithms. It is maintained in the
[Turfjs/turf](https:/Turfjs/turf) repository, where you can create
PRs and issues.

### Installation

Install this module individually:

```sh
$ npm install @turf/line-arc
```

Or install the Turf module that includes it as a function:

```sh
$ npm install @turf/turf
```
36 changes: 36 additions & 0 deletions packages/turf-line-arc/bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const Benchmark = require('benchmark');
const path = require('path');
const fs = require('fs');
const load = require('load-json-file');
const lineArc = require('./');

const directory = path.join(__dirname, 'test', 'in') + path.sep;
const fixtures = fs.readdirSync(directory).map(filename => {
return {
name: path.parse(filename).name,
geojson: load.sync(directory + filename)
};
});

/**
* Benchmark Results
*
* line-arc-full-360 x 38,879 ops/sec ±5.53% (78 runs sampled)
* line-arc-greater-360 x 435,501 ops/sec ±13.18% (66 runs sampled)
* line-arc1 x 249,765 ops/sec ±6.43% (69 runs sampled)
* line-arc2 x 92,964 ops/sec ±6.81% (72 runs sampled)
* line-arc3 x 47,342 ops/sec ±4.41% (74 runs sampled)
* line-arc4 x 267,112 ops/sec ±5.95% (71 runs sampled)
* line-arc5 x 35,259 ops/sec ±4.43% (69 runs sampled)
* line-arc6 x 38,674 ops/sec ±4.86% (75 runs sampled)
*/
const suite = new Benchmark.Suite('turf-line-arc');
for (const {name, geojson} of fixtures) {
const {radius, bearing1, bearing2, steps, units} = geojson.properties;
suite.add(name, () => lineArc(geojson, radius, bearing1, bearing2, steps, units));
}

suite
.on('cycle', e => console.log(String(e.target)))
.on('complete', () => {})
.run();
10 changes: 10 additions & 0 deletions packages/turf-line-arc/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference types="geojson" />

import {Point, LineString, Units} from '@turf/helpers';

/**
* http://turfjs.org/docs/#linearc
*/
declare function lineArc(center: Point, radius: number, bearing1: number, bearing2: number, steps?: number, units?: string): LineString;
declare namespace lineArc { }
export = lineArc;
86 changes: 86 additions & 0 deletions packages/turf-line-arc/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
var destination = require('@turf/destination');
var circle = require('@turf/circle');
var lineString = require('@turf/helpers').lineString;

/**
* Creates a circular arc, of a circle of the given radius and center point, between bearing1 and bearing2;
* 0 bearing is North of center point, positive clockwise.
*
* @name line-arc
* @param {Feature<Point>} center center point
* @param {number} radius radius of the circle
* @param {number} bearing1 angle, in decimal degrees, of the first radius of the arc
* @param {number} bearing2 angle, in decimal degrees, of the second radius of the arc
* @param {number} [steps=64] number of steps
* @param {string} [units="kilometers""] miles, kilometers, degrees, or radians
* @returns {Feature<LineString>} line arc
* @example
* var center = {
* "type": "Feature",
* "properties": {},
* "geometry": {
* "type": "Point",
* "coordinates": [-75, 40]
* }
* }
* var radius = 5;
* var bearing1 = 25;
* var bearing2 = 47;
*
* var arc = turf.lineArc(center, radius, bearing1, bearing2);
*
* //addToMap
* var addToMap = [center, arc]
*/
module.exports = function (center, radius, bearing1, bearing2, steps, units) {
// validation
if (!center) throw new Error('center is required');
if (bearing1 === undefined || bearing1 === null) throw new Error('bearing1 is required');
if (bearing2 === undefined || bearing2 === null) throw new Error('bearing2 is required');
if (!radius) throw new Error('radius is required');

// default params
steps = steps || 64;

var angle1 = convertAngleTo360(bearing1);
var angle2 = convertAngleTo360(bearing2);
var properties = center.properties;

// handle angle parameters
if (angle1 === angle2) {
return lineString(circle(center, radius, steps, units).geometry.coordinates[0], properties);
}
var arcStartDegree = angle1;
var arcEndDegree = (angle1 < angle2) ? angle2 : angle2 + 360;

var alfa = arcStartDegree;
var coordinates = [];
var i = 0;

while (alfa < arcEndDegree) {
coordinates.push(destination(center, radius, alfa, units).geometry.coordinates);
i++;
alfa = arcStartDegree + i * 360 / steps;
}
if (alfa > arcEndDegree) {
coordinates.push(destination(center, radius, arcEndDegree, units).geometry.coordinates);
}
return lineString(coordinates, properties);
};


/**
* Takes any angle in degrees
* and returns a valid angle between 0-360 degrees
*
* @private
* @param {number} alfa angle between -180-180 degrees
* @returns {number} angle between 0-360 degrees
*/
function convertAngleTo360(alfa) {
var beta = alfa % 360;
if (beta < 0) {
beta += 360;
}
return beta;
}
42 changes: 42 additions & 0 deletions packages/turf-line-arc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@turf/line-arc",
"version": "4.0.0",
"description": "turf line-arc module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.js",
"index.d.ts"
],
"scripts": {
"test": "node test.js",
"bench": "node bench.js"
},
"repository": {
"type": "git",
"url": "git:/Turfjs/turf.git"
},
"keywords": [
"turf",
"gif"
],
"author": "Turf Authors",
"license": "MIT",
"bugs": {
"url": "https:/Turfjs/turf/issues"
},
"homepage": "https:/Turfjs/turf",
"devDependencies": {
"@turf/truncate": "^4.0.1",
"benchmark": "^2.1.4",
"load-json-file": "^2.0.0",
"tape": "^4.6.3",
"write-json-file": "^2.0.0"
},
"dependencies": {
"@turf/circle": "^4.0.1",
"@turf/destination": "^4.0.1",
"@turf/helpers": "^4.0.1",
"@turf/meta": "^4.0.1"
}
}
33 changes: 33 additions & 0 deletions packages/turf-line-arc/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const test = require('tape');
const fs = require('fs');
const path = require('path');
const load = require('load-json-file');
const write = require('write-json-file');
const truncate = require('@turf/truncate');
const featureCollection = require('@turf/helpers').featureCollection;
const lineArc = require('./');

const directories = {
in: path.join(__dirname, 'test', 'in') + path.sep,
out: path.join(__dirname, 'test', 'out') + path.sep
};

const fixtures = fs.readdirSync(directories.in).map(filename => {
return {
filename,
name: path.parse(filename).name,
geojson: load.sync(directories.in + filename)
};
});

test('turf-line-arc', t => {
for (const {filename, name, geojson} of fixtures) {
const {radius, bearing1, bearing2, steps, units} = geojson.properties;
const arc = truncate(lineArc(geojson, radius, bearing1, bearing2, steps, units));
const results = featureCollection([geojson, arc]);

if (process.env.REGEN) write.sync(directories.out + filename, results);
t.deepEquals(results, load.sync(directories.out + filename), name);
}
t.end();
});
15 changes: 15 additions & 0 deletions packages/turf-line-arc/test/in/line-arc-full-360.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "Feature",
"properties": {
"radius": 5,
"bearing1": 180,
"bearing2": -180
},
"geometry": {
"type": "Point",
"coordinates": [
11.343,
44.495
]
}
}
15 changes: 15 additions & 0 deletions packages/turf-line-arc/test/in/line-arc-greater-360.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "Feature",
"properties": {
"radius": 5,
"bearing1": 0,
"bearing2": 380
},
"geometry": {
"type": "Point",
"coordinates": [
11.343,
44.495
]
}
}
15 changes: 15 additions & 0 deletions packages/turf-line-arc/test/in/line-arc1.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "Feature",
"properties": {
"radius": 5,
"bearing1": 20,
"bearing2": 60
},
"geometry": {
"type": "Point",
"coordinates": [
11.343,
44.495
]
}
}
15 changes: 15 additions & 0 deletions packages/turf-line-arc/test/in/line-arc2.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "Feature",
"properties": {
"radius": 5,
"bearing1": 90,
"bearing2": -135
},
"geometry": {
"type": "Point",
"coordinates": [
11.343,
44.495
]
}
}
15 changes: 15 additions & 0 deletions packages/turf-line-arc/test/in/line-arc3.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "Feature",
"properties": {
"radius": 5,
"bearing1": 45,
"bearing2": -45
},
"geometry": {
"type": "Point",
"coordinates": [
11.343,
44.495
]
}
}
15 changes: 15 additions & 0 deletions packages/turf-line-arc/test/in/line-arc4.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "Feature",
"properties": {
"radius": 5,
"bearing1": -50,
"bearing2": -15
},
"geometry": {
"type": "Point",
"coordinates": [
11.343,
44.495
]
}
}
Loading