Skip to content
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
20 changes: 20 additions & 0 deletions packages/turf-multi-geometry-scale/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2019 Morgan Herlocker

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.
23 changes: 23 additions & 0 deletions packages/turf-multi-geometry-scale/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# @turf/multiGeometryScale

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

<!-- This file is automatically generated. Please don't edit it directly. If you find an error, edit the source file of the module in question (likely index.js or index.ts), and re-run "yarn docs" from the root of the turf project. -->

---

This module is part of the [Turfjs project](https://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 single module individually:

```sh
$ npm install @turf/multiGeometryScale
```

Or install the all-encompassing @turf/turf module that includes all modules as functions:

```sh
$ npm install @turf/turf
```
75 changes: 75 additions & 0 deletions packages/turf-multi-geometry-scale/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { AllGeoJSON } from "@turf/helpers";
import { centerOfMass } from "@turf/center-of-mass";

function multiGeometryScale<T extends AllGeoJSON>(
geojson: any,
scaleX: number,
scaleY: number,
angle: number
): T {
//Input validation
if (!geojson) throw new Error("geojson is invalid");

if (
geojson.geometry.type !== "MultiPolygon" &&
geojson.geometry.type !== "MultiLineString" &&
geojson.geometry.type !== "MultiPoint"
)
throw new Error(
"Only Grouped Geometry allowed. etc. MultiPolygon/MultiLineString/MultiPoint"
);

if (scaleX === undefined || scaleX === null || isNaN(scaleX))
throw new Error("scaleX is required");
if (scaleY === undefined || scaleY === null || isNaN(scaleY))
throw new Error("scaleY is required");
if (angle === undefined || angle === null || isNaN(angle))
throw new Error("angle is required");

// Shortcut no-translate
if (scaleX === 1 && scaleY === 1) return geojson;

let pivot = centerOfMass(geojson).geometry.coordinates;

const angleRadian = (angle * Math.PI) / 180;
const cos = Math.cos(angleRadian);
const sin = Math.sin(angleRadian);

function transformCoord([x, y]: [number, number]) {
// 1️⃣ translate to pivot
const dx = x - pivot[0];
const dy = y - pivot[1];

// 2️⃣ rotate -θ
const rx = dx * cos + dy * sin;
const ry = -dx * sin + dy * cos;

// 3️⃣ scale
const sx = rx * scaleX;
const sy = ry * scaleY;

// 4️⃣ rotate +θ
const fx = sx * cos - sy * sin;
const fy = sx * sin + sy * cos;

// 5️⃣ translate back
return [fx + pivot[0], fy + pivot[1]];
}

function walk(coords: any): any {
return coords.map((c: any) =>
typeof c[0] === "number" ? transformCoord(c) : walk(c)
);
}

return {
...geojson,
geometry: {
...geojson.geometry,
coordinates: walk(geojson.geometry.coordinates),
},
};
}

export { multiGeometryScale };
export default multiGeometryScale;
64 changes: 64 additions & 0 deletions packages/turf-multi-geometry-scale/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"name": "@turf/multiGeometryScale",
"version": "1.0.0",
"description": "Scales a multiGeometry without disturbing the relative distance",
"author": "Mishor Patra",
"contributors": [],
"license": "",
"bugs": {
"url": "https://github.com/Turfjs/turf/issues"
},
"homepage": "https://github.com/Turfjs/turf",
"repository": {
"type": "git",
"url": "git://github.com/Turfjs/turf.git"
},
"funding": "https://opencollective.com/turf",
"publishConfig": {
"access": "public"
},
"keywords": [
"turf",
"translate",
"multiPolygon",
"multiPoint",
"multiLineString"
],
"type": "module",
"exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"sideEffects": false,
"files": [
"dist",
"!**/*.tsbuildinfo"
],
"scripts": {
"bench": "tsx bench.ts",
"build": "tsc --build",
"test": "pnpm run /test:.*/",
"test:tape": "tsx test.ts"
},
"devDependencies": {
"@types/benchmark": "catalog:",
"@types/tape": "catalog:",
"benchmark": "catalog:",
"load-json-file": "^7.0.1",
"tape": "catalog:",
"tsx": "catalog:",
"typescript": "catalog:",
"write-json-file": "catalog:"
},
"dependencies": {
"@turf/center-of-mass": "workspace:*",
"@turf/helpers": "^7.4.0",
"@types/geojson": "catalog:"
},
"engines": {
"node": ">=22"
}
}
66 changes: 66 additions & 0 deletions packages/turf-multi-geometry-scale/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import test from "tape";
import {
point,
lineString,
polygon,
multiPolygon,
multiLineString,
multiPoint,
} from "@turf/helpers";
import { multiGeometryScale } from "./index";

test("turf-group-tranlate-multi-polygon", (t) => {
const multiPoly = multiPolygon([
[
[
[102.0, 2.0],
[103.0, 2.0],
[103.0, 3.0],
[102.0, 3.0],
[102.0, 2.0],
],
],
[
[
[100.0, 0.0],
[101.0, 0.0],
[101.0, 1.0],
[100.0, 1.0],
[100.0, 0.0],
],
[
[100.2, 0.2],
[100.8, 0.2],
[100.8, 0.8],
[100.2, 0.8],
[100.2, 0.2],
],
],
]);

const multiLine = multiLineString([
[
[100.0, 0.0],
[101.0, 1.0],
],
[
[102.0, 2.0],
[103.0, 3.0],
],
]);

const multiPt = multiPoint([
[100, 0],
[0, 100],
]);

const scaleX = 1.5;
const scaleY = 1.5;
const angle = 2;

let strechedPoly = multiGeometryScale(multiPoly, scaleX, scaleY, angle);
let strechedLine = multiGeometryScale(multiLine, scaleX, scaleY, angle);
let strechedPoint = multiGeometryScale(multiPt, scaleX, scaleY, angle);

t.end();
});
11 changes: 11 additions & 0 deletions packages/turf-multi-geometry-scale/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.shared.json",
"references": [
{
"path": "../turf-center-of-mass"
},
{
"path": "../turf-helpers"
}
]
}
45 changes: 45 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.