From 7011eadb574fbebb647e50f75b58605336e0ae2f Mon Sep 17 00:00:00 2001 From: Simon Andersson Date: Mon, 17 Mar 2025 12:22:44 +0100 Subject: [PATCH 1/2] turf-hexgrid: fix issue #2841 Typing of mask does not allow for MultiPolygon while the documentation obviously states that this is allowed --- packages/turf-hex-grid/index.ts | 5 +++-- packages/turf-hex-grid/test/in/properties.json | 16 +++++++++------- .../turf-hex-grid/test/out/properties.geojson | 16 +++++++++------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/packages/turf-hex-grid/index.ts b/packages/turf-hex-grid/index.ts index acf7f8f19b..ac20349a29 100644 --- a/packages/turf-hex-grid/index.ts +++ b/packages/turf-hex-grid/index.ts @@ -6,6 +6,7 @@ import { GeoJsonProperties, Polygon, BBox, + MultiPolygon, } from "geojson"; import { polygon, featureCollection, Units } from "@turf/helpers"; @@ -21,7 +22,7 @@ import { polygon, featureCollection, Units } from "@turf/helpers"; * @param {Object} [options={}] Optional parameters * @param {string} [options.units='kilometers'] used in calculating cell size, can be degrees, radians, miles, or kilometers * @param {Object} [options.properties={}] passed to each hexagon or triangle of the grid - * @param {Feature} [options.mask] if passed a Polygon or MultiPolygon, the grid Points will be created only inside it + * @param {Feature} [options.mask] if passed a Polygon or MultiPolygon, the grid Points will be created only inside it * @param {boolean} [options.triangles=false] whether to return as triangles instead of hexagons * @returns {FeatureCollection} a hexagonal grid * @example @@ -41,7 +42,7 @@ function hexGrid

( units?: Units; triangles?: boolean; properties?: P; - mask?: Feature; + mask?: Feature; } = {} ): FeatureCollection { // Issue => https://github.com/Turfjs/turf/issues/1284 diff --git a/packages/turf-hex-grid/test/in/properties.json b/packages/turf-hex-grid/test/in/properties.json index 3d726d9005..543d43df17 100644 --- a/packages/turf-hex-grid/test/in/properties.json +++ b/packages/turf-hex-grid/test/in/properties.json @@ -8,15 +8,17 @@ "type": "Feature", "properties": {}, "geometry": { - "type": "Polygon", + "type": "MultiPolygon", "coordinates": [ [ - [2.223186492919922, 41.416856461155575], - [2.1716880798339844, 41.44182560856202], - [2.1636199951171875, 41.41608406639095], - [2.1334075927734375, 41.407200866420744], - [2.1845626831054683, 41.37977115211044], - [2.223186492919922, 41.416856461155575] + [ + [2.223186492919922, 41.416856461155575], + [2.1716880798339844, 41.44182560856202], + [2.1636199951171875, 41.41608406639095], + [2.1334075927734375, 41.407200866420744], + [2.1845626831054683, 41.37977115211044], + [2.223186492919922, 41.416856461155575] + ] ] ] } diff --git a/packages/turf-hex-grid/test/out/properties.geojson b/packages/turf-hex-grid/test/out/properties.geojson index 8cf50dfc5c..5061257e4f 100644 --- a/packages/turf-hex-grid/test/out/properties.geojson +++ b/packages/turf-hex-grid/test/out/properties.geojson @@ -292,15 +292,17 @@ "fill-opacity": 0 }, "geometry": { - "type": "Polygon", + "type": "MultiPolygon", "coordinates": [ [ - [2.223186492919922, 41.416856461155575], - [2.1716880798339844, 41.44182560856202], - [2.1636199951171875, 41.41608406639095], - [2.1334075927734375, 41.407200866420744], - [2.1845626831054683, 41.37977115211044], - [2.223186492919922, 41.416856461155575] + [ + [2.223186492919922, 41.416856461155575], + [2.1716880798339844, 41.44182560856202], + [2.1636199951171875, 41.41608406639095], + [2.1334075927734375, 41.407200866420744], + [2.1845626831054683, 41.37977115211044], + [2.223186492919922, 41.416856461155575] + ] ] ] } From fcb2b1aed41d5b8b5dc1ac349d2f917c336c1d5a Mon Sep 17 00:00:00 2001 From: Simon Andersson Date: Mon, 17 Mar 2025 12:26:45 +0100 Subject: [PATCH 2/2] feat: update readme of turf-hexgrid to include MultiPolygon --- packages/turf-hex-grid/README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/turf-hex-grid/README.md b/packages/turf-hex-grid/README.md index c87c7a1c78..d7b1517b02 100644 --- a/packages/turf-hex-grid/README.md +++ b/packages/turf-hex-grid/README.md @@ -17,8 +17,8 @@ described in [Hexagonal Grids][3]. * `options.units` **[string][7]** used in calculating cell size, can be degrees, radians, miles, or kilometers (optional, default `'kilometers'`) * `options.properties` **[Object][6]** passed to each hexagon or triangle of the grid (optional, default `{}`) - * `options.mask` **[Feature][8]<[Polygon][2]>?** if passed a Polygon or MultiPolygon, the grid Points will be created only inside it - * `options.triangles` **[boolean][9]** whether to return as triangles instead of hexagons (optional, default `false`) + * `options.mask` **[Feature][8]<([Polygon][2] | [MultiPolygon][9])>?** if passed a Polygon or MultiPolygon, the grid Points will be created only inside it + * `options.triangles` **[boolean][10]** whether to return as triangles instead of hexagons (optional, default `false`) ### Examples @@ -51,7 +51,9 @@ Returns **[FeatureCollection][1]<[Polygon][2]>** a hexagonal grid [8]: https://tools.ietf.org/html/rfc7946#section-3.2 -[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean +[9]: https://tools.ietf.org/html/rfc7946#section-3.1.7 + +[10]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean