Skip to content

Commit

Permalink
turf-voronoi - retain properties from points (#2421)
Browse files Browse the repository at this point in the history
* voronoi - retain properties from points

* use cloneProperties

* undo README change

* fix CHANGELOG after fork sync

---------

Co-authored-by: James Beard <james@smallsaucepan.com>
  • Loading branch information
maral and smallsaucepan authored Nov 29, 2023
1 parent 66f9b2d commit ba35a93
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-clone/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion packages/turf-voronoi/index.js
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand Down Expand Up @@ -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),
});
})
);
}

Expand Down
91 changes: 91 additions & 0 deletions packages/turf-voronoi/test/in/with-properties.json
Original file line number Diff line number Diff line change
@@ -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]
}
}
]
}
170 changes: 170 additions & 0 deletions packages/turf-voronoi/test/out/with-properties.json
Original file line number Diff line number Diff line change
@@ -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]
]
]
}
}
]
}

0 comments on commit ba35a93

Please sign in to comment.