Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/turf-helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ import {
import { Id } from "./lib/geojson.js";
export * from "./lib/geojson.js";

// I'd normally put this in @turf/meta but it isn't full-TypeScript yet
export function* iterFeatures<
G extends GeometryObject,
P extends GeoJsonProperties,
>(
geojson: Feature<G, P> | FeatureCollection<G, P>
): Generator<{ feature: Feature<G, P>; featureIndex: number }, void, unknown> {
if (geojson.type === "Feature") {
yield { feature: geojson, featureIndex: 0 };
} else {
const features = geojson.features;
for (let i = 0; i < features.length; i++) {
yield { feature: features[i], featureIndex: i };
}
}
}

/**
* @module helpers
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/turf-moran-index/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FeatureCollection } from "geojson";
import { distanceWeight as spatialWeight } from "@turf/distance-weight";
import { featureEach } from "@turf/meta";
import { iterFeatures } from "@turf/helpers";

/**
* @typedef {object} MoranIndex
Expand Down Expand Up @@ -86,11 +86,11 @@ function moranIndex(
});

const y: number[] = [];
featureEach(fc, (feature) => {
for (const { feature } of iterFeatures(fc)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This need for destructuring is a little annoying, because we have to also carry featureIndex along. But we're using featureIndex in places, and I'd want signature parity with other iterator tools that will also need to bring along their own other indexes as well.

const feaProperties = feature.properties || {};
// validate inputField exists
y.push(feaProperties[inputField]);
});
}

const yMean = mean(y);
const yVar = variance(y);
Expand Down
8 changes: 4 additions & 4 deletions packages/turf-nearest-point/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Feature, FeatureCollection, GeoJsonProperties, Point } from "geojson";
import { Coord, Units } from "@turf/helpers";
import { Coord, iterFeatures, Units } from "@turf/helpers";
import { clone } from "@turf/clone";
import { distance } from "@turf/distance";
import { featureEach } from "@turf/meta";

interface NearestPoint<P extends GeoJsonProperties = GeoJsonProperties>
extends Feature<Point> {
Expand Down Expand Up @@ -51,13 +50,14 @@ function nearestPoint<P extends GeoJsonProperties = GeoJsonProperties>(

let minDist = Infinity;
let bestFeatureIndex = 0;
featureEach(points, (pt, featureIndex) => {
for (const { feature: pt, featureIndex } of iterFeatures(points)) {
const distanceToPoint = distance(targetPoint, pt, options);
if (distanceToPoint < minDist) {
bestFeatureIndex = featureIndex;
minDist = distanceToPoint;
}
});
}

const nearestPoint = clone(points.features[bestFeatureIndex]);

return {
Expand Down