Skip to content
Open
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
42 changes: 0 additions & 42 deletions packages/turf-buffer/index.d.ts

This file was deleted.

56 changes: 40 additions & 16 deletions packages/turf-buffer/index.js → packages/turf-buffer/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { center } from "@turf/center";
// @ts-expect-error We're hopefully dropping @turf/jsts entirely soon, so lets just go without types for now
import jsts from "@turf/jsts";
import { geomEach, featureEach } from "@turf/meta";
import { geoAzimuthalEquidistant } from "d3-geo";
import { geoAzimuthalEquidistant, GeoProjection } from "d3-geo";
import {
feature,
featureCollection,
radiansToLength,
lengthToRadians,
earthRadius,
Units,
} from "@turf/helpers";
import {
Feature,
FeatureCollection,
GeometryCollection,
MultiPolygon,
Polygon,
} from "geojson";

const { BufferOp, GeoJSONReader, GeoJSONWriter } = jsts;

Expand All @@ -35,7 +44,13 @@ const { BufferOp, GeoJSONReader, GeoJSONWriter } = jsts;
* //addToMap
* var addToMap = [point, buffered]
*/
function buffer(geojson, radius, options) {
function buffer<T extends GeoJSON.GeoJSON>(
geojson: T,
radius?: number,
options?: { units?: Units; steps?: number }
): T extends FeatureCollection | GeometryCollection
? FeatureCollection<Polygon | MultiPolygon> | undefined
: Feature<Polygon | MultiPolygon> | undefined {
// Optional params
options = options || {};

Expand All @@ -52,26 +67,26 @@ function buffer(geojson, radius, options) {
if (radius === undefined) throw new Error("radius is required");
if (steps <= 0) throw new Error("steps must be greater than 0");

var results = [];
var results: any[] = [];
switch (geojson.type) {
case "GeometryCollection":
geomEach(geojson, function (geometry) {
var buffered = bufferFeature(geometry, radius, units, steps);
if (buffered) results.push(buffered);
});
return featureCollection(results);
return featureCollection(results) as any;
case "FeatureCollection":
featureEach(geojson, function (feature) {
var multiBuffered = bufferFeature(feature, radius, units, steps);
if (multiBuffered) {
featureEach(multiBuffered, function (buffered) {
featureEach(multiBuffered as Feature, function (buffered) {
if (buffered) results.push(buffered);
});
}
});
return featureCollection(results);
return featureCollection(results) as any;
}
return bufferFeature(geojson, radius, units, steps);
return bufferFeature(geojson, radius, units, steps) as any;
}

/**
Expand All @@ -84,13 +99,18 @@ function buffer(geojson, radius, options) {
* @param {number} [steps=8] number of steps
* @returns {Feature<Polygon|MultiPolygon>} buffered feature
*/
function bufferFeature(geojson, radius, units, steps) {
function bufferFeature(
geojson: any,
radius: number,
units: Units,
steps: number
) {
var properties = geojson.properties || {};
var geometry = geojson.type === "Feature" ? geojson.geometry : geojson;

// Geometry Types faster than jsts
if (geometry.type === "GeometryCollection") {
var results = [];
var results: any[] = [];
geomEach(geojson, function (geometry) {
var buffered = bufferFeature(geometry, radius, units, steps);
if (buffered) results.push(buffered);
Expand Down Expand Up @@ -132,7 +152,7 @@ function bufferFeature(geojson, radius, units, steps) {
* @param {Array<any>} coords GeoJSON Coordinates
* @returns {boolean} if NaN exists
*/
function coordsIsNaN(coords) {
function coordsIsNaN(coords: any[]) {
if (Array.isArray(coords[0])) return coordsIsNaN(coords[0]);
return isNaN(coords[0]);
}
Expand All @@ -145,8 +165,10 @@ function coordsIsNaN(coords) {
* @param {GeoProjection} proj D3 Geo Projection
* @returns {Array<any>} projected coordinates
*/
function projectCoords(coords, proj) {
if (typeof coords[0] !== "object") return proj(coords);
function projectCoords(coords: any[], proj: GeoProjection): any[] {
if (typeof coords[0] !== "object") {
return proj(coords as [number, number])!;
}
return coords.map(function (coord) {
return projectCoords(coord, proj);
});
Expand All @@ -160,8 +182,10 @@ function projectCoords(coords, proj) {
* @param {GeoProjection} proj D3 Geo Projection
* @returns {Array<any>} un-projected coordinates
*/
function unprojectCoords(coords, proj) {
if (typeof coords[0] !== "object") return proj.invert(coords);
function unprojectCoords(coords: any[], proj: GeoProjection): any[] {
if (typeof coords[0] !== "object") {
return proj.invert!(coords as [number, number])!;
}
return coords.map(function (coord) {
return unprojectCoords(coord, proj);
});
Expand All @@ -174,9 +198,9 @@ function unprojectCoords(coords, proj) {
* @param {Geometry|Feature<any>} geojson Base projection on center of GeoJSON
* @returns {GeoProjection} D3 Geo Azimuthal Equidistant Projection
*/
function defineProjection(geojson) {
function defineProjection(geojson: GeoJSON.GeoJSON) {
var coords = center(geojson).geometry.coordinates;
var rotation = [-coords[0], -coords[1]];
var rotation: [number, number] = [-coords[0], -coords[1]];
return geoAzimuthalEquidistant().rotate(rotation).scale(earthRadius);
}

Expand Down
5 changes: 4 additions & 1 deletion packages/turf-buffer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@
"devDependencies": {
"@turf/truncate": "workspace:*",
"@types/benchmark": "^2.1.5",
"@types/d3-geo": "^3.1.0",
"@types/tape": "^5.8.1",
"benchmark": "^2.1.4",
"load-json-file": "^7.0.1",
"tape": "^5.9.0",
"tsup": "^8.4.0",
"tsx": "^4.19.4",
"typescript": "^5.8.3",
"write-json-file": "^6.0.0"
},
"dependencies": {
Expand All @@ -78,6 +80,7 @@
"@turf/meta": "workspace:*",
"@turf/projection": "workspace:*",
"@types/geojson": "^7946.0.10",
"d3-geo": "1.7.1"
"d3-geo": "^3.1.1",
"tslib": "^2.8.1"
}
}
44 changes: 35 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.