diff --git a/CHANGELOG.md b/CHANGELOG.md index f96cbe32d5..fc6f4a4570 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ We intend to keep making breaking changes before 7.0.0 is fully released. If you - [`@turf/line-intersect`][line-intersect] Move to sweepline-intersections library for performance (#2033) - [`@turf/boolean-contains`](boolean-contains) Add multipolygon support (#2338) - [`@turf/nearest-point`](nearest-point) Add unit option (#2415) +- [`@turf/voronoi`](voronoi) Retain properties from points (#1450) ## 🐛 Bug Fixes - [`@turf/polygon-smooth`](polygon-smooth) Options argument is now actually optional (#2149) diff --git a/packages/turf-clone/index.ts b/packages/turf-clone/index.ts index 1ffc67e293..2f23643f03 100644 --- a/packages/turf-clone/index.ts +++ b/packages/turf-clone/index.ts @@ -73,7 +73,7 @@ function cloneFeature(geojson: any) { * @param {Object} properties GeoJSON Properties * @returns {Object} cloned Properties */ -function cloneProperties(properties: GeoJsonProperties) { +export function cloneProperties(properties: GeoJsonProperties) { const cloned: { [key: string]: any } = {}; if (!properties) { return cloned; diff --git a/packages/turf-voronoi/index.js b/packages/turf-voronoi/index.js index 772dc908be..2844bfcc28 100644 --- a/packages/turf-voronoi/index.js +++ b/packages/turf-voronoi/index.js @@ -1,5 +1,6 @@ import { polygon, featureCollection, isObject } from "@turf/helpers"; import { collectionOf } from "@turf/invariant"; +import { cloneProperties } from "@turf/clone"; import * as d3voronoi from "d3-voronoi"; /** @@ -60,7 +61,11 @@ function voronoi(points, options) { [bbox[2], bbox[3]], ]) .polygons(points.features) - .map(coordsToPolygon) + .map(function (coords, index) { + return Object.assign(coordsToPolygon(coords), { + properties: cloneProperties(points.features[index].properties), + }); + }) ); } diff --git a/packages/turf-voronoi/test/in/with-properties.json b/packages/turf-voronoi/test/in/with-properties.json new file mode 100644 index 0000000000..c13460a3ff --- /dev/null +++ b/packages/turf-voronoi/test/in/with-properties.json @@ -0,0 +1,91 @@ +{ + "type": "FeatureCollection", + "bbox": [143, -38, 146, -35], + "features": [ + { + "type": "Feature", + "properties": { + "property1": 1, + "property2": 2 + }, + "geometry": { + "type": "Point", + "coordinates": [144.33837890625, -37.14280344371683] + } + }, + { + "type": "Feature", + "properties": { + "property1": 1 + }, + "geometry": { + "type": "Point", + "coordinates": [144.931640625, -37.35269280367274] + } + }, + { + "type": "Feature", + "properties": { + "stringProperty": "string" + }, + "geometry": { + "type": "Point", + "coordinates": [145.140380859375, -36.456636011596196] + } + }, + { + "type": "Feature", + "properties": { + "multiLevelProperty": { + "property1": 1 + } + }, + "geometry": { + "type": "Point", + "coordinates": [145.469970703125, -36.77409249464194] + } + }, + { + "type": "Feature", + "properties": { + "floatProperty": 1.1 + }, + "geometry": { + "type": "Point", + "coordinates": [145.755615234375, -37.090239803072066] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [145.4150390625, -37.52715361723378] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [145.887451171875, -37.483576550426996] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [144.60205078125, -36.57142382346275] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [144.86572265625, -37.596824001083654] + } + } + ] +} diff --git a/packages/turf-voronoi/test/out/with-properties.json b/packages/turf-voronoi/test/out/with-properties.json new file mode 100644 index 0000000000..f8d4d30747 --- /dev/null +++ b/packages/turf-voronoi/test/out/with-properties.json @@ -0,0 +1,170 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "property1": 1, + "property2": 2 + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [143, -36.17865921750102], + [144.7306872649525, -36.97731260260012], + [144.58469067823404, -37.38997746052463], + [144.05948693381836, -38], + [143, -38], + [143, -36.17865921750102] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "property1": 1 + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [144.58469067823404, -37.38997746052463], + [144.7306872649525, -36.97731260260012], + [144.9502509842771, -36.88468636726737], + [145.02826439034735, -36.902859896834244], + [145.28197625539775, -37.13891325365974], + [145.13749583645568, -37.53924080856172], + [144.58469067823404, -37.38997746052463] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stringProperty": "string" + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [146, -35.89398200640089], + [145.02826439034735, -36.902859896834244], + [144.9502509842771, -36.88468636726737], + [144.54837968093918, -35], + [146, -35], + [146, -35.89398200640089] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "multiLevelProperty": { + "property1": 1 + } + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [145.02826439034735, -36.902859896834244], + [146, -35.89398200640089], + [146, -36.58231923105632], + [145.37634807465002, -37.145797192532186], + [145.28197625539775, -37.13891325365974], + [145.02826439034735, -36.902859896834244] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "floatProperty": 1.1 + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [145.37634807465002, -37.145797192532186], + [146, -36.58231923105632], + [146, -37.22709115402949], + [145.63680507441603, -37.348824340099675], + [145.37634807465002, -37.145797192532186] + ] + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [145.13749583645568, -37.53924080856172], + [145.28197625539775, -37.13891325365974], + [145.37634807465002, -37.145797192532186], + [145.63680507441603, -37.348824340099675], + [145.69687188417421, -38], + [145.1959341358453, -38], + [145.13749583645568, -37.53924080856172] + ] + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [145.63680507441603, -37.348824340099675], + [146, -37.22709115402949], + [146, -38], + [145.69687188417421, -38], + [145.63680507441603, -37.348824340099675] + ] + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [144.54837968093918, -35], + [144.9502509842771, -36.88468636726737], + [144.7306872649525, -36.97731260260012], + [143, -36.17865921750102], + [143, -35], + [144.54837968093918, -35] + ] + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [144.05948693381836, -38], + [144.58469067823404, -37.38997746052463], + [145.13749583645568, -37.53924080856172], + [145.1959341358453, -38], + [144.05948693381836, -38] + ] + ] + } + } + ] +}