Skip to content

Commit

Permalink
Converted turf-transform-scale to Typescript (#2652)
Browse files Browse the repository at this point in the history
Converted turf-transform-scale to Typescript. Avoiding in house type AllGeoJSON. Some refactoring to accomodate stricter typing where existing code was potentially vulnerable at runtime. turf-distance README was out of date so regenerating that too.
  • Loading branch information
smallsaucepan authored Jul 23, 2024
1 parent 8cb62b6 commit 047c5f5
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 206 deletions.
22 changes: 10 additions & 12 deletions packages/turf-distance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

## distance

Calculates the distance between two [points][1] in degrees, radians, miles, or kilometers.
Calculates the distance between two [coordinates][1] in degrees, radians, miles, or kilometers.
This uses the [Haversine formula][2] to account for global curvature.

### Parameters

* `from` **([Coord][3] | [Point][4])** origin point or coordinate
* `to` **([Coord][3] | [Point][4])** destination point or coordinate
* `options` **[Object][5]** Optional parameters (optional, default `{}`)
* `from` **[Coord][3]** origin coordinate
* `to` **[Coord][3]** destination coordinate
* `options` **[Object][4]** Optional parameters (optional, default `{}`)

* `options.units` **[string][6]** can be degrees, radians, miles, or kilometers (optional, default `'kilometers'`)
* `options.units` **[string][5]** can be degrees, radians, miles, or kilometers (optional, default `'kilometers'`)

### Examples

Expand All @@ -30,21 +30,19 @@ from.properties.distance = distance;
to.properties.distance = distance;
```

Returns **[number][7]** distance between the two points
Returns **[number][6]** distance between the two coordinates

[1]: https://tools.ietf.org/html/rfc7946#section-3.1.2
[1]: https://tools.ietf.org/html/rfc7946#section-3.1.1

[2]: http://en.wikipedia.org/wiki/Haversine_formula

[3]: https://tools.ietf.org/html/rfc7946#section-3.1.1

[4]: https://tools.ietf.org/html/rfc7946#section-3.1.2
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object

[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String

[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String

[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number

<!-- 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. -->

Expand Down
30 changes: 16 additions & 14 deletions packages/turf-transform-scale/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,40 @@

## transformScale

Scale a GeoJSON from a given point by a factor of scaling (ex: factor=2 would make the GeoJSON 200% larger).
If a FeatureCollection is provided, the origin point will be calculated based on each individual Feature.
Scale GeoJSON objects from a given point by a scaling factor e.g. factor=2
would make each object 200% larger.
If a FeatureCollection is provided, the origin point will be calculated
based on each individual feature *unless* an exact

### Parameters

* `geojson` **[GeoJSON][1]** GeoJSON to be scaled
* `factor` **[number][2]** of scaling, positive values greater than 0. Numbers between 0 and 1 will shrink the geojson, numbers greater than 1 will expand it, a factor of 1 will not change the geojson.
* `options` **[Object][3]** Optional parameters (optional, default `{}`)
* `geojson` **([GeoJSON][1] | [GeometryCollection][2])** objects to be scaled
* `factor` **[number][3]** of scaling, positive values greater than 0. Numbers between 0 and 1 will shrink the geojson, numbers greater than 1 will expand it, a factor of 1 will not change the geojson.
* `options` **[Object][4]** Optional parameters (optional, default `{}`)

* `options.origin` **([string][4] | [Coord][5])** Point from which the scaling will occur (string options: sw/se/nw/ne/center/centroid) (optional, default `'centroid'`)
* `options.mutate` **[boolean][6]** allows GeoJSON input to be mutated (significant performance increase if true) (optional, default `false`)
* `options.origin` **(Corners | [Coord][5])** Point from which the scaling will occur (string options: sw/se/nw/ne/center/centroid) (optional, default `'centroid'`)
* `options.mutate` **[boolean][6]** allows GeoJSON input to be mutated (significant performance improvement if true) (optional, default `false`)

### Examples

```javascript
var poly = turf.polygon([[[0,29],[3.5,29],[2.5,32],[0,29]]]);
var scaledPoly = turf.transformScale(poly, 3);
const poly = turf.polygon([[[0,29],[3.5,29],[2.5,32],[0,29]]]);
const scaledPoly = turf.transformScale(poly, 3);

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

Returns **[GeoJSON][1]** scaled GeoJSON
Returns **([GeoJSON][1] | [GeometryCollection][2])** scaled GeoJSON

[1]: https://tools.ietf.org/html/rfc7946#section-3

[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
[2]: https://tools.ietf.org/html/rfc7946#section-3.1.8

[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number

[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object

[5]: https://tools.ietf.org/html/rfc7946#section-3.1.1

Expand Down
28 changes: 23 additions & 5 deletions packages/turf-transform-scale/bench.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Feature, FeatureCollection } from "geojson";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { loadJsonFileSync } from "load-json-file";
import Benchmark from "benchmark";
import Benchmark, { Event } from "benchmark";
import { Coord, Corners } from "@turf/helpers";
import { transformScale as scale } from "./index.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
Expand All @@ -11,7 +13,9 @@ const directory = path.join(__dirname, "test", "in") + path.sep;
const fixtures = fs.readdirSync(directory).map((filename) => {
return {
name: path.parse(filename).name,
geojson: loadJsonFileSync(directory + filename),
geojson: loadJsonFileSync(directory + filename) as
| FeatureCollection
| Feature,
};
});

Expand All @@ -31,7 +35,14 @@ const fixtures = fs.readdirSync(directory).map((filename) => {
* z-scaling: 0.237ms
*/
for (const { name, geojson } of fixtures) {
const { factor, origin } = geojson.properties || {};
let factor: number = 2,
origin: Coord | Corners = "centroid";
if (geojson.type === "Feature") {
// Override any test options specified in the feature's properties.
factor = geojson.properties?.factor ?? factor;
origin = geojson.properties?.origin ?? origin;
}

console.time(name);
scale(geojson, factor || 2, { origin, mutate: true });
console.timeEnd(name);
Expand All @@ -54,8 +65,15 @@ for (const { name, geojson } of fixtures) {
*/
const suite = new Benchmark.Suite("turf-transform-scale");
for (const { name, geojson } of fixtures) {
const { factor, origin } = geojson.properties || {};
let factor: number = 2,
origin: Coord | Corners = "centroid";
if (geojson.type === "Feature") {
// Override any test options specified in the feature's properties.
factor = geojson.properties?.factor ?? factor;
origin = geojson.properties?.origin ?? origin;
}

suite.add(name, () => scale(geojson, factor || 2, { origin }));
}

suite.on("cycle", (e) => console.log(String(e.target))).run();
suite.on("cycle", (e: Event) => console.log(String(e.target))).run();
16 changes: 0 additions & 16 deletions packages/turf-transform-scale/index.d.ts

This file was deleted.

149 changes: 0 additions & 149 deletions packages/turf-transform-scale/index.js

This file was deleted.

Loading

0 comments on commit 047c5f5

Please sign in to comment.