Skip to content

Commit 1dec2f2

Browse files
Fixed union documentation where types out of sync with function. Reworded some of the descriptions as well. (#2732)
Co-authored-by: Tim Welch <[email protected]>
1 parent 3b5f94a commit 1dec2f2

File tree

2 files changed

+81
-45
lines changed

2 files changed

+81
-45
lines changed

packages/turf-union/README.md

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,64 @@
44

55
## union
66

7-
Takes input [(Multi)Polygon(s)][1] and returns a combined polygon. If the input polygons are not contiguous, this function returns a [MultiPolygon][2] feature.
7+
Takes a collection of input polygons and returns a combined polygon. If the
8+
input polygons are not contiguous, this function returns a multi-polygon
9+
feature.
810

911
### Parameters
1012

11-
* `features` **[FeatureCollection][3]<([Polygon][1] | [MultiPolygon][2])>**&#x20;
13+
* `features` **[FeatureCollection][1]<([Polygon][2] | [MultiPolygon][3])>** input polygon features
1214
* `options` **[Object][4]** Optional Parameters (optional, default `{}`)
1315

14-
* `options.properties` **[Object][4]** Translate Properties to output Feature (optional, default `{}`)
15-
* `polygon1` **[Feature][5]<([Polygon][1] | [MultiPolygon][2])>** input Polygon features
16+
* `options.properties` **[GeoJsonProperties][5]** properties to assign to output feature (optional, default `{}`)
1617

1718
### Examples
1819

1920
```javascript
20-
var poly1 = turf.polygon([[
21-
[-82.574787, 35.594087],
22-
[-82.574787, 35.615581],
23-
[-82.545261, 35.615581],
24-
[-82.545261, 35.594087],
25-
[-82.574787, 35.594087]
26-
]], {"fill": "#0f0"});
27-
var poly2 = turf.polygon([[
28-
[-82.560024, 35.585153],
29-
[-82.560024, 35.602602],
30-
[-82.52964, 35.602602],
31-
[-82.52964, 35.585153],
32-
[-82.560024, 35.585153]
33-
]], {"fill": "#00f"});
34-
35-
var union = turf.union(turf.featureCollection([poly1, poly2]));
21+
const poly1 = turf.polygon(
22+
[
23+
[
24+
[-82.574787, 35.594087],
25+
[-82.574787, 35.615581],
26+
[-82.545261, 35.615581],
27+
[-82.545261, 35.594087],
28+
[-82.574787, 35.594087],
29+
],
30+
],
31+
{ fill: "#0f0" }
32+
);
33+
34+
const poly2 = turf.polygon(
35+
[
36+
[
37+
[-82.560024, 35.585153],
38+
[-82.560024, 35.602602],
39+
[-82.52964, 35.602602],
40+
[-82.52964, 35.585153],
41+
[-82.560024, 35.585153],
42+
],
43+
],
44+
);
45+
46+
const union = turf.union(turf.featureCollection([poly1, poly2]));
3647

3748
//addToMap
38-
var addToMap = [poly1, poly2, union];
49+
const addToMap = { poly1, poly2, union };
50+
51+
poly1.properties.fill = "#0f0";
52+
poly2.properties.fill = "#00f";
53+
union.properties.stroke = "red";
54+
union.properties["stroke-width"] = 4;
55+
union.properties.fill = "transparent";
3956
```
4057

41-
Returns **[Feature][5]<([Polygon][1] | [MultiPolygon][2])>** a combined [Polygon][1] or [MultiPolygon][2] feature, or null if the inputs are empty
58+
Returns **([Feature][5]<([Polygon][2] | [MultiPolygon][3])> | null)** a combined polygon or multi-polygon feature, or null if there were no input polygons to combine
4259

43-
[1]: https://tools.ietf.org/html/rfc7946#section-3.1.6
60+
[1]: https://tools.ietf.org/html/rfc7946#section-3.3
4461

45-
[2]: https://tools.ietf.org/html/rfc7946#section-3.1.7
62+
[2]: https://tools.ietf.org/html/rfc7946#section-3.1.6
4663

47-
[3]: https://tools.ietf.org/html/rfc7946#section-3.3
64+
[3]: https://tools.ietf.org/html/rfc7946#section-3.1.7
4865

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

packages/turf-union/index.ts

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,52 @@ import {
1010
} from "geojson";
1111

1212
/**
13-
* Takes input {@link (Multi)Polygon(s)} and returns a combined polygon. If the input polygons are not contiguous, this function returns a {@link MultiPolygon} feature.
13+
* Takes a collection of input polygons and returns a combined polygon. If the
14+
* input polygons are not contiguous, this function returns a multi-polygon
15+
* feature.
1416
*
1517
* @function
16-
* @param {Feature<Polygon|MultiPolygon>} polygon1 input Polygon features
18+
* @param {FeatureCollection<Polygon|MultiPolygon>} features input polygon features
1719
* @param {Object} [options={}] Optional Parameters
18-
* @param {Object} [options.properties={}] Translate Properties to output Feature
19-
* @returns {Feature<(Polygon|MultiPolygon)>} a combined {@link Polygon} or {@link MultiPolygon} feature, or null if the inputs are empty
20+
* @param {GeoJsonProperties} [options.properties={}] properties to assign to output feature
21+
* @returns {Feature<(Polygon|MultiPolygon)>|null} a combined polygon or multi-polygon feature, or null if there were no input polygons to combine
2022
* @example
21-
* var poly1 = turf.polygon([[
22-
* [-82.574787, 35.594087],
23-
* [-82.574787, 35.615581],
24-
* [-82.545261, 35.615581],
25-
* [-82.545261, 35.594087],
26-
* [-82.574787, 35.594087]
27-
* ]], {"fill": "#0f0"});
28-
* var poly2 = turf.polygon([[
29-
* [-82.560024, 35.585153],
30-
* [-82.560024, 35.602602],
31-
* [-82.52964, 35.602602],
32-
* [-82.52964, 35.585153],
33-
* [-82.560024, 35.585153]
34-
* ]], {"fill": "#00f"});
3523
*
36-
* var union = turf.union(turf.featureCollection([poly1, poly2]));
24+
* const poly1 = turf.polygon(
25+
* [
26+
* [
27+
* [-82.574787, 35.594087],
28+
* [-82.574787, 35.615581],
29+
* [-82.545261, 35.615581],
30+
* [-82.545261, 35.594087],
31+
* [-82.574787, 35.594087],
32+
* ],
33+
* ],
34+
* { fill: "#0f0" }
35+
* );
36+
*
37+
* const poly2 = turf.polygon(
38+
* [
39+
* [
40+
* [-82.560024, 35.585153],
41+
* [-82.560024, 35.602602],
42+
* [-82.52964, 35.602602],
43+
* [-82.52964, 35.585153],
44+
* [-82.560024, 35.585153],
45+
* ],
46+
* ],
47+
* );
48+
*
49+
* const union = turf.union(turf.featureCollection([poly1, poly2]));
3750
*
3851
* //addToMap
39-
* var addToMap = [poly1, poly2, union];
52+
* const addToMap = { poly1, poly2, union };
53+
*
54+
* poly1.properties.fill = "#0f0";
55+
* poly2.properties.fill = "#00f";
56+
* union.properties.stroke = "red";
57+
* union.properties["stroke-width"] = 4;
58+
* union.properties.fill = "transparent";
4059
*/
4160
function union<P extends GeoJsonProperties = GeoJsonProperties>(
4261
features: FeatureCollection<Polygon | MultiPolygon>,

0 commit comments

Comments
 (0)