Skip to content

Commit

Permalink
Merge 6d98ed9 into 67ee226
Browse files Browse the repository at this point in the history
  • Loading branch information
stebogit committed May 25, 2017
2 parents 67ee226 + 6d98ed9 commit a3e7007
Show file tree
Hide file tree
Showing 53 changed files with 6,722 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/turf-transform-rotate/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.
51 changes: 51 additions & 0 deletions packages/turf-transform-rotate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# @turf/transform-rotate

# rotate

Rotates any geojson Feature or Geometry of a specified angle, around its `centroid` or a given `pivot` point;
all rotations follow the right-hand rule: <https://en.wikipedia.org/wiki/Right-hand_rule>

**Parameters**

- `geojson` **[GeoJSON](http://geojson.org/geojson-spec.html#geojson-objects)** object to be rotated
- `angle` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** of rotation (along the vertical axis), from North in decimal degrees, negative clockwise
- `pivot` **\[([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)&lt;[Point](http://geojson.org/geojson-spec.html#point)> | [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>)]** point around which the rotation will be performed (optional, default `` `centroid` ``)
- `mutate` **\[[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** allows GeoJSON input to be mutated (significant performance increase if true) (optional, default `false`)

**Examples**

```javascript
var poly = turf.polygon([[[0,29],[3.5,29],[2.5,32],[0,29]]]);
var rotatedPoly = turf.rotate(poly, 100, [15, 15]);

//addToMap
rotatedPoly.properties = {stroke: '#F00', 'stroke-width': 4};
var addToMap = [poly, rotatedPoly];
```

Returns **[GeoJSON](http://geojson.org/geojson-spec.html#geojson-objects)** the rotated GeoJSON feature

<!-- 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://github.com/Turfjs/turf) repository, where you can create
PRs and issues.

### Installation

Install this module individually:

```sh
$ npm install @turf/transform-rotate
```

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

```sh
$ npm install @turf/turf
```
58 changes: 58 additions & 0 deletions packages/turf-transform-rotate/bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const fs = require('fs');
const path = require('path');
const load = require('load-json-file');
const Benchmark = require('benchmark');
const rotate = 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)
};
});


/**
* Single Process Benchmark
*
* line: 3.145ms
* multiLine: 0.148ms
* multiPoint: 1.137ms
* multiPolygon: 5.770ms
* no-rotation: 0.009ms
* point: 0.032ms
* polygon-with-hole: 0.823ms
* polygon: 0.045ms
* z-coord: 0.053ms
*/
for (const {name, geojson} of fixtures) {
const {angle, pivot} = geojson.properties || {};
console.time(name);
rotate(geojson, angle, pivot, true);
console.timeEnd(name);
}

/**
* Benchmark Results
*
* line x 66,571 ops/sec ±2.07% (86 runs sampled)
* multiLine x 136,950 ops/sec ±2.39% (87 runs sampled)
* multiPoint x 190,062 ops/sec ±1.96% (85 runs sampled)
* multiPolygon x 9,588 ops/sec ±0.92% (91 runs sampled)
* no-rotation x 35,080,702 ops/sec ±1.17% (87 runs sampled)
* point x 732,603 ops/sec ±1.75% (83 runs sampled)
* polygon-with-hole x 20,217 ops/sec ±4.15% (83 runs sampled)
* polygon x 198,680 ops/sec ±3.38% (81 runs sampled)
* z-coord x 139,558 ops/sec ±1.61% (86 runs sampled)
*/
const suite = new Benchmark.Suite('turf-transform-rotate');
for (const {name, geojson} of fixtures) {
const {angle, pivot} = geojson.properties || {};
suite.add(name, () => rotate(geojson, angle, pivot, true));
}

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

type Point = GeoJSON.Feature<GeoJSON.Point> | GeoJSON.Point | number[];
type Geoms = GeoJSON.Feature<any> | GeoJSON.FeatureCollection<any> | GeoJSON.GeometryObject | GeoJSON.GeometryCollection;

/**
* http://turfjs.org/docs/#transform-rotate
*/
declare function rotate<Geom extends Geoms>(
geojson: Geom,
angle: number,
pivot?: Point,
xRotation?: number,
yRotation?: number,
mutate?: boolean): Geom;

declare namespace rotate { }
export = rotate;
52 changes: 52 additions & 0 deletions packages/turf-transform-rotate/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var meta = require('@turf/meta');
var centroid = require('@turf/centroid');
var invariant = require('@turf/invariant');
var rhumbBearing = require('@turf/rhumb-bearing');
var rhumbDistance = require('@turf/rhumb-distance');
var rhumbDestination = require('@turf/rhumb-destination');
var coordEach = meta.coordEach;
var getCoords = invariant.getCoords;

/**
* Rotates any geojson Feature or Geometry of a specified angle, around its `centroid` or a given `pivot` point;
* all rotations follow the right-hand rule: https://en.wikipedia.org/wiki/Right-hand_rule
*
* @name rotate
* @param {GeoJSON} geojson object to be rotated
* @param {number} angle of rotation (along the vertical axis), from North in decimal degrees, negative clockwise
* @param {Geometry|Feature<Point>|Array<number>} [pivot=`centroid`] point around which the rotation will be performed
* @param {boolean} [mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
* @returns {GeoJSON} the rotated GeoJSON feature
* @example
* var poly = turf.polygon([[[0,29],[3.5,29],[2.5,32],[0,29]]]);
* var rotatedPoly = turf.rotate(poly, 100, [15, 15]);
*
* //addToMap
* rotatedPoly.properties = {stroke: '#F00', 'stroke-width': 4};
* var addToMap = [poly, rotatedPoly];
*/
module.exports = function (geojson, angle, pivot, mutate) {
// Input validation
if (!geojson) throw new Error('geojson is required');
if (angle === undefined || angle === null || isNaN(angle)) throw new Error('angle is required');

// Shortcut no-rotation
if (angle === 0) return geojson;

// Use centroid of GeoJSON if pivot is not provided
if (!pivot) pivot = centroid(geojson);

// Clone geojson to avoid side effects
if (mutate === false || mutate === undefined) geojson = JSON.parse(JSON.stringify(geojson));

// Rotate each coordinate
coordEach(geojson, function (pointCoords) {
var initialAngle = rhumbBearing(pivot, pointCoords);
var finalAngle = initialAngle + angle;
var distance = rhumbDistance(pivot, pointCoords);
var newCoords = getCoords(rhumbDestination(pivot, distance, finalAngle));
pointCoords[0] = newCoords[0];
pointCoords[1] = newCoords[1];
});
return geojson;
};
52 changes: 52 additions & 0 deletions packages/turf-transform-rotate/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "@turf/transform-rotate",
"version": "4.3.0",
"description": "turf transform-rotate module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.js",
"index.d.ts"
],
"scripts": {
"test": "tap test.js",
"bench": "node bench.js"
},
"repository": {
"type": "git",
"url": "git://github.com/Turfjs/turf.git"
},
"keywords": [
"turf",
"transform",
"transformation",
"rotate"
],
"author": "Turf Authors",
"contributors": [
"Stefano Borghi <@stebogit>",
"Denis Carriere <@DenisCarriere>"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/Turfjs/turf/issues"
},
"homepage": "https://github.com/Turfjs/turf",
"devDependencies": {
"@turf/helpers": "^4.3.0",
"@turf/truncate": "^4.3.0",
"benchmark": "^2.1.4",
"load-json-file": "^2.0.0",
"tap": "^10.3.2",
"tape": "^4.6.3",
"write-json-file": "^2.0.0"
},
"dependencies": {
"@turf/centroid": "^4.3.0",
"@turf/invariant": "^4.3.0",
"@turf/meta": "^4.3.0",
"@turf/rhumb-bearing": "^4.3.0",
"@turf/rhumb-destination": "^4.3.0",
"@turf/rhumb-distance": "^4.3.0"
}
}
89 changes: 89 additions & 0 deletions packages/turf-transform-rotate/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const fs = require('fs');
const test = require('tape');
const path = require('path');
const load = require('load-json-file');
const write = require('write-json-file');
const centroid = require('@turf/centroid');
const truncate = require('@turf/truncate');
const {getCoord} = require('@turf/invariant');
const {point, lineString, featureCollection, geometryCollection} = require('@turf/helpers');
const rotate = 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('rotate', t => {
for (const {filename, name, geojson} of fixtures) {
const {angle, pivot} = geojson.properties || {};

const rotated = rotate(geojson, angle, pivot);
const result = featureCollection([colorize(truncate(rotated, 6, 3)), geojson, makePivot(pivot, geojson)]);

if (process.env.REGEN) write.sync(directories.out + filename, result);
t.deepEqual(result, load.sync(directories.out + filename), name);
}

t.end();
});

test('rotate -- throws', t => {
const line = lineString([[10, 10], [12, 15]]);

t.throws(() => rotate(null, 100), /geojson is required/, 'missing geojson');
t.throws(() => rotate(line, null), /angle is required/, 'missing angle');
t.throws(() => rotate(line, 56, 'notApoint'), /coordinates must only contain numbers/, 'invalid pivot');
t.end();
});

test('rotate -- mutated input', t => {
const line = lineString([[10, 10], [12, 15]]);
const lineBefore = JSON.parse(JSON.stringify(line));

rotate(line, 100);
t.deepEqual(line, lineBefore, 'input should not be mutated');
t.end();
});

test('rotate -- geometry support', t => {
const line = lineString([[10, 10], [12, 15]]);
const pt = point([10, 10]);

t.assert(rotate(geometryCollection([line.geometry]), 100), 'geometryCollection support');
t.assert(rotate(geometryCollection([line.geometry]).geometry, 100), 'geometryCollection support');
t.assert(rotate(featureCollection([line]), 100), 'featureCollection support');
t.assert(rotate(line.geometry, 100), 'geometry line support');
t.assert(rotate(line.geometry, 100, pt.geometry), 'geometry pt support');
t.assert(rotate(line.geometry, 100, pt.geometry.coordinates), 'pt coordinate support');
t.end();
});

// style result
function colorize(geojson) {
if (geojson.geometry.type === 'Point' || geojson.geometry.type === 'MultiPoint') {
geojson.properties['marker-color'] = '#F00';
geojson.properties['marker-symbol'] = 'star';
} else {
geojson.properties['stroke'] = '#F00';
geojson.properties['stroke-width'] = 4;
}
return geojson;
}

function makePivot(pivot, geojson) {
if (!pivot) {
const pt = centroid(geojson);
pt.properties['marker-symbol'] = 'circle';
return pt;
}
return point(getCoord(pivot), {'marker-symbol': 'circle'});
}

0 comments on commit a3e7007

Please sign in to comment.