New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@turf/buffer

Package Overview
Dependencies
Maintainers
6
Versions
70
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@turf/buffer - npm Package Compare versions

Comparing version 6.2.0-alpha.2 to 6.2.0-alpha.3

dist/es/package.json

181

dist/es/index.js
import center from '@turf/center';
import turfBbox from '@turf/bbox';
import { BufferOp, GeoJSONReader, GeoJSONWriter } from 'turf-jsts';
import { toWgs84, toMercator } from '@turf/projection';
import { geomEach, featureEach } from '@turf/meta';
import { geoTransverseMercator } from 'd3-geo';
import { geoAzimuthalEquidistant } from 'd3-geo';
import { feature, featureCollection, radiansToLength, lengthToRadians, earthRadius } from '@turf/helpers';

@@ -23,3 +21,3 @@

* @param {string} [options.units="kilometers"] any of the options supported by turf units
* @param {number} [options.steps=64] number of steps
* @param {number} [options.steps=8] number of steps
* @returns {FeatureCollection|Feature<Polygon|MultiPolygon>|undefined} buffered features

@@ -34,40 +32,38 @@ * @example

function buffer(geojson, radius, options) {
// Optional params
options = options || {};
var units = options.units;
var steps = options.steps || 64;
// Optional params
options = options || {};
// validation
if (!geojson) throw new Error('geojson is required');
if (typeof options !== 'object') throw new Error('options must be an object');
if (typeof steps !== 'number') throw new Error('steps must be an number');
// use user supplied options or default values
var units = options.units || "kilometers";
var steps = options.steps || 8;
// Allow negative buffers ("erosion") or zero-sized buffers ("repair geometry")
if (radius === undefined) throw new Error('radius is required');
if (steps <= 0) throw new Error('steps must be greater than 0');
// validation
if (!geojson) throw new Error("geojson is required");
if (typeof options !== "object") throw new Error("options must be an object");
if (typeof steps !== "number") throw new Error("steps must be an number");
// default params
steps = steps || 64;
units = units || 'kilometers';
// Allow negative buffers ("erosion") or zero-sized buffers ("repair geometry")
if (radius === undefined) throw new Error("radius is required");
if (steps <= 0) throw new Error("steps must be greater than 0");
var results = [];
switch (geojson.type) {
case 'GeometryCollection':
geomEach(geojson, function (geometry) {
var buffered = bufferFeature(geometry, radius, units, steps);
var results = [];
switch (geojson.type) {
case "GeometryCollection":
geomEach(geojson, function (geometry) {
var buffered = bufferFeature(geometry, radius, units, steps);
if (buffered) results.push(buffered);
});
return featureCollection(results);
case "FeatureCollection":
featureEach(geojson, function (feature$$1) {
var multiBuffered = bufferFeature(feature$$1, radius, units, steps);
if (multiBuffered) {
featureEach(multiBuffered, function (buffered) {
if (buffered) results.push(buffered);
});
return featureCollection(results);
case 'FeatureCollection':
featureEach(geojson, function (feature$$1) {
var multiBuffered = bufferFeature(feature$$1, radius, units, steps);
if (multiBuffered) {
featureEach(multiBuffered, function (buffered) {
if (buffered) results.push(buffered);
});
}
});
return featureCollection(results);
}
return bufferFeature(geojson, radius, units, steps);
});
}
});
return featureCollection(results);
}
return bufferFeature(geojson, radius, units, steps);
}

@@ -82,56 +78,44 @@

* @param {string} [units='kilometers'] any of the options supported by turf units
* @param {number} [steps=64] number of steps
* @param {number} [steps=8] number of steps
* @returns {Feature<Polygon|MultiPolygon>} buffered feature
*/
function bufferFeature(geojson, radius, units, steps) {
var properties = geojson.properties || {};
var geometry = (geojson.type === 'Feature') ? geojson.geometry : geojson;
var properties = geojson.properties || {};
var geometry = geojson.type === "Feature" ? geojson.geometry : geojson;
// Geometry Types faster than jsts
if (geometry.type === 'GeometryCollection') {
var results = [];
geomEach(geojson, function (geometry) {
var buffered = bufferFeature(geometry, radius, units, steps);
if (buffered) results.push(buffered);
});
return featureCollection(results);
}
// Geometry Types faster than jsts
if (geometry.type === "GeometryCollection") {
var results = [];
geomEach(geojson, function (geometry) {
var buffered = bufferFeature(geometry, radius, units, steps);
if (buffered) results.push(buffered);
});
return featureCollection(results);
}
// Project GeoJSON to Transverse Mercator projection (convert to Meters)
var projected;
var bbox = turfBbox(geojson);
var needsTransverseMercator = bbox[1] > 50 && bbox[3] > 50;
// Project GeoJSON to Azimuthal Equidistant projection (convert to Meters)
var projection = defineProjection(geometry);
var projected = {
type: geometry.type,
coordinates: projectCoords(geometry.coordinates, projection),
};
if (needsTransverseMercator) {
projected = {
type: geometry.type,
coordinates: projectCoords(geometry.coordinates, defineProjection(geometry))
};
} else {
projected = toMercator(geometry);
}
// JSTS buffer operation
var reader = new GeoJSONReader();
var geom = reader.read(projected);
var distance = radiansToLength(lengthToRadians(radius, units), "meters");
var buffered = BufferOp.bufferOp(geom, distance, steps);
var writer = new GeoJSONWriter();
buffered = writer.write(buffered);
// JSTS buffer operation
var reader = new GeoJSONReader();
var geom = reader.read(projected);
var distance = radiansToLength(lengthToRadians(radius, units), 'meters');
var buffered = BufferOp.bufferOp(geom, distance);
var writer = new GeoJSONWriter();
buffered = writer.write(buffered);
// Detect if empty geometries
if (coordsIsNaN(buffered.coordinates)) return undefined;
// Detect if empty geometries
if (coordsIsNaN(buffered.coordinates)) return undefined;
// Unproject coordinates (convert to Degrees)
var result = {
type: buffered.type,
coordinates: unprojectCoords(buffered.coordinates, projection),
};
// Unproject coordinates (convert to Degrees)
var result;
if (needsTransverseMercator) {
result = {
type: buffered.type,
coordinates: unprojectCoords(buffered.coordinates, defineProjection(geometry))
};
} else {
result = toWgs84(buffered);
}
return (result.geometry) ? result : feature(result, properties);
return feature(result, properties);
}

@@ -147,4 +131,4 @@

function coordsIsNaN(coords) {
if (Array.isArray(coords[0])) return coordsIsNaN(coords[0]);
return isNaN(coords[0]);
if (Array.isArray(coords[0])) return coordsIsNaN(coords[0]);
return isNaN(coords[0]);
}

@@ -161,6 +145,6 @@

function projectCoords(coords, proj) {
if (typeof coords[0] !== 'object') return proj(coords);
return coords.map(function (coord) {
return projectCoords(coord, proj);
});
if (typeof coords[0] !== "object") return proj(coords);
return coords.map(function (coord) {
return projectCoords(coord, proj);
});
}

@@ -177,24 +161,21 @@

function unprojectCoords(coords, proj) {
if (typeof coords[0] !== 'object') return proj.invert(coords);
return coords.map(function (coord) {
return unprojectCoords(coord, proj);
});
if (typeof coords[0] !== "object") return proj.invert(coords);
return coords.map(function (coord) {
return unprojectCoords(coord, proj);
});
}
/**
* Define Transverse Mercator projection
* Define Azimuthal Equidistant projection
*
* @private
* @param {Geometry|Feature<any>} geojson Base projection on center of GeoJSON
* @returns {GeoProjection} D3 Geo Transverse Mercator Projection
* @returns {GeoProjection} D3 Geo Azimuthal Equidistant Projection
*/
function defineProjection(geojson) {
var coords = center(geojson).geometry.coordinates.reverse();
var rotate = coords.map(function (coord) { return -coord; });
return geoTransverseMercator()
.center(coords)
.rotate(rotate)
.scale(earthRadius);
const coords = center(geojson).geometry.coordinates;
const rotation = [-coords[0], -coords[1]];
return geoAzimuthalEquidistant().rotate(rotation).scale(earthRadius);
}
export default buffer;

@@ -6,5 +6,3 @@ 'use strict';

var center = _interopDefault(require('@turf/center'));
var turfBbox = _interopDefault(require('@turf/bbox'));
var turfJsts = require('turf-jsts');
var projection = require('@turf/projection');
var meta = require('@turf/meta');

@@ -28,3 +26,3 @@ var d3Geo = require('d3-geo');

* @param {string} [options.units="kilometers"] any of the options supported by turf units
* @param {number} [options.steps=64] number of steps
* @param {number} [options.steps=8] number of steps
* @returns {FeatureCollection|Feature<Polygon|MultiPolygon>|undefined} buffered features

@@ -39,40 +37,38 @@ * @example

function buffer(geojson, radius, options) {
// Optional params
options = options || {};
var units = options.units;
var steps = options.steps || 64;
// Optional params
options = options || {};
// validation
if (!geojson) throw new Error('geojson is required');
if (typeof options !== 'object') throw new Error('options must be an object');
if (typeof steps !== 'number') throw new Error('steps must be an number');
// use user supplied options or default values
var units = options.units || "kilometers";
var steps = options.steps || 8;
// Allow negative buffers ("erosion") or zero-sized buffers ("repair geometry")
if (radius === undefined) throw new Error('radius is required');
if (steps <= 0) throw new Error('steps must be greater than 0');
// validation
if (!geojson) throw new Error("geojson is required");
if (typeof options !== "object") throw new Error("options must be an object");
if (typeof steps !== "number") throw new Error("steps must be an number");
// default params
steps = steps || 64;
units = units || 'kilometers';
// Allow negative buffers ("erosion") or zero-sized buffers ("repair geometry")
if (radius === undefined) throw new Error("radius is required");
if (steps <= 0) throw new Error("steps must be greater than 0");
var results = [];
switch (geojson.type) {
case 'GeometryCollection':
meta.geomEach(geojson, function (geometry) {
var buffered = bufferFeature(geometry, radius, units, steps);
var results = [];
switch (geojson.type) {
case "GeometryCollection":
meta.geomEach(geojson, function (geometry) {
var buffered = bufferFeature(geometry, radius, units, steps);
if (buffered) results.push(buffered);
});
return helpers.featureCollection(results);
case "FeatureCollection":
meta.featureEach(geojson, function (feature) {
var multiBuffered = bufferFeature(feature, radius, units, steps);
if (multiBuffered) {
meta.featureEach(multiBuffered, function (buffered) {
if (buffered) results.push(buffered);
});
return helpers.featureCollection(results);
case 'FeatureCollection':
meta.featureEach(geojson, function (feature) {
var multiBuffered = bufferFeature(feature, radius, units, steps);
if (multiBuffered) {
meta.featureEach(multiBuffered, function (buffered) {
if (buffered) results.push(buffered);
});
}
});
return helpers.featureCollection(results);
}
return bufferFeature(geojson, radius, units, steps);
});
}
});
return helpers.featureCollection(results);
}
return bufferFeature(geojson, radius, units, steps);
}

@@ -87,56 +83,44 @@

* @param {string} [units='kilometers'] any of the options supported by turf units
* @param {number} [steps=64] number of steps
* @param {number} [steps=8] number of steps
* @returns {Feature<Polygon|MultiPolygon>} buffered feature
*/
function bufferFeature(geojson, radius, units, steps) {
var properties = geojson.properties || {};
var geometry = (geojson.type === 'Feature') ? geojson.geometry : geojson;
var properties = geojson.properties || {};
var geometry = geojson.type === "Feature" ? geojson.geometry : geojson;
// Geometry Types faster than jsts
if (geometry.type === 'GeometryCollection') {
var results = [];
meta.geomEach(geojson, function (geometry) {
var buffered = bufferFeature(geometry, radius, units, steps);
if (buffered) results.push(buffered);
});
return helpers.featureCollection(results);
}
// Geometry Types faster than jsts
if (geometry.type === "GeometryCollection") {
var results = [];
meta.geomEach(geojson, function (geometry) {
var buffered = bufferFeature(geometry, radius, units, steps);
if (buffered) results.push(buffered);
});
return helpers.featureCollection(results);
}
// Project GeoJSON to Transverse Mercator projection (convert to Meters)
var projected;
var bbox = turfBbox(geojson);
var needsTransverseMercator = bbox[1] > 50 && bbox[3] > 50;
// Project GeoJSON to Azimuthal Equidistant projection (convert to Meters)
var projection = defineProjection(geometry);
var projected = {
type: geometry.type,
coordinates: projectCoords(geometry.coordinates, projection),
};
if (needsTransverseMercator) {
projected = {
type: geometry.type,
coordinates: projectCoords(geometry.coordinates, defineProjection(geometry))
};
} else {
projected = projection.toMercator(geometry);
}
// JSTS buffer operation
var reader = new turfJsts.GeoJSONReader();
var geom = reader.read(projected);
var distance = helpers.radiansToLength(helpers.lengthToRadians(radius, units), "meters");
var buffered = turfJsts.BufferOp.bufferOp(geom, distance, steps);
var writer = new turfJsts.GeoJSONWriter();
buffered = writer.write(buffered);
// JSTS buffer operation
var reader = new turfJsts.GeoJSONReader();
var geom = reader.read(projected);
var distance = helpers.radiansToLength(helpers.lengthToRadians(radius, units), 'meters');
var buffered = turfJsts.BufferOp.bufferOp(geom, distance);
var writer = new turfJsts.GeoJSONWriter();
buffered = writer.write(buffered);
// Detect if empty geometries
if (coordsIsNaN(buffered.coordinates)) return undefined;
// Detect if empty geometries
if (coordsIsNaN(buffered.coordinates)) return undefined;
// Unproject coordinates (convert to Degrees)
var result = {
type: buffered.type,
coordinates: unprojectCoords(buffered.coordinates, projection),
};
// Unproject coordinates (convert to Degrees)
var result;
if (needsTransverseMercator) {
result = {
type: buffered.type,
coordinates: unprojectCoords(buffered.coordinates, defineProjection(geometry))
};
} else {
result = projection.toWgs84(buffered);
}
return (result.geometry) ? result : helpers.feature(result, properties);
return helpers.feature(result, properties);
}

@@ -152,4 +136,4 @@

function coordsIsNaN(coords) {
if (Array.isArray(coords[0])) return coordsIsNaN(coords[0]);
return isNaN(coords[0]);
if (Array.isArray(coords[0])) return coordsIsNaN(coords[0]);
return isNaN(coords[0]);
}

@@ -166,6 +150,6 @@

function projectCoords(coords, proj) {
if (typeof coords[0] !== 'object') return proj(coords);
return coords.map(function (coord) {
return projectCoords(coord, proj);
});
if (typeof coords[0] !== "object") return proj(coords);
return coords.map(function (coord) {
return projectCoords(coord, proj);
});
}

@@ -182,25 +166,21 @@

function unprojectCoords(coords, proj) {
if (typeof coords[0] !== 'object') return proj.invert(coords);
return coords.map(function (coord) {
return unprojectCoords(coord, proj);
});
if (typeof coords[0] !== "object") return proj.invert(coords);
return coords.map(function (coord) {
return unprojectCoords(coord, proj);
});
}
/**
* Define Transverse Mercator projection
* Define Azimuthal Equidistant projection
*
* @private
* @param {Geometry|Feature<any>} geojson Base projection on center of GeoJSON
* @returns {GeoProjection} D3 Geo Transverse Mercator Projection
* @returns {GeoProjection} D3 Geo Azimuthal Equidistant Projection
*/
function defineProjection(geojson) {
var coords = center(geojson).geometry.coordinates.reverse();
var rotate = coords.map(function (coord) { return -coord; });
return d3Geo.geoTransverseMercator()
.center(coords)
.rotate(rotate)
.scale(helpers.earthRadius);
const coords = center(geojson).geometry.coordinates;
const rotation = [-coords[0], -coords[1]];
return d3Geo.geoAzimuthalEquidistant().rotate(rotation).scale(helpers.earthRadius);
}
module.exports = buffer;
module.exports.default = buffer;

@@ -1,19 +0,18 @@

import {
Point,
LineString,
Polygon,
MultiPoint,
MultiLineString,
MultiPolygon,
GeometryObject,
GeometryCollection,
Feature,
FeatureCollection,
Units,
} from '@turf/helpers';
Point,
LineString,
Polygon,
MultiPoint,
MultiLineString,
MultiPolygon,
GeometryObject,
GeometryCollection,
Feature,
FeatureCollection,
Units,
} from "@turf/helpers";
interface Options {
units?: Units;
steps?: number
units?: Units;
steps?: number;
}

@@ -24,9 +23,40 @@

*/
declare function buffer<Geom extends Point | LineString | Polygon>(feature: Feature<Geom>|Geom, radius?: number, options?: Options): Feature<Polygon>;
declare function buffer<Geom extends MultiPoint | MultiLineString | MultiPolygon>(feature: Feature<Geom>|Geom, radius?: number, options?: Options): Feature<MultiPolygon>;
declare function buffer<Geom extends Point | LineString | Polygon>(feature: FeatureCollection<Geom>, radius?: number, options?: Options): FeatureCollection<Polygon>;
declare function buffer<Geom extends MultiPoint | MultiLineString | MultiPolygon>(feature: FeatureCollection<Geom>, radius?: number, options?: Options): FeatureCollection<MultiPolygon>;
declare function buffer(feature: FeatureCollection<any> | Feature<GeometryCollection> | GeometryCollection, radius?: number, options?: Options): FeatureCollection<Polygon | MultiPolygon>;
declare function buffer(feature: Feature<any> | GeometryObject, radius?: number, options?: Options): Feature<Polygon | MultiPolygon>;
declare function buffer<Geom extends Point | LineString | Polygon>(
feature: Feature<Geom> | Geom,
radius?: number,
options?: Options
): Feature<Polygon>;
declare function buffer<
Geom extends MultiPoint | MultiLineString | MultiPolygon
>(
feature: Feature<Geom> | Geom,
radius?: number,
options?: Options
): Feature<MultiPolygon>;
declare function buffer<Geom extends Point | LineString | Polygon>(
feature: FeatureCollection<Geom>,
radius?: number,
options?: Options
): FeatureCollection<Polygon>;
declare function buffer<
Geom extends MultiPoint | MultiLineString | MultiPolygon
>(
feature: FeatureCollection<Geom>,
radius?: number,
options?: Options
): FeatureCollection<MultiPolygon>;
declare function buffer(
feature:
| FeatureCollection<any>
| Feature<GeometryCollection>
| GeometryCollection,
radius?: number,
options?: Options
): FeatureCollection<Polygon | MultiPolygon>;
declare function buffer(
feature: Feature<any> | GeometryObject,
radius?: number,
options?: Options
): Feature<Polygon | MultiPolygon>;
export default buffer;
{
"name": "@turf/buffer",
"version": "6.2.0-alpha.2",
"version": "6.2.0-alpha.3",
"description": "turf buffer module",

@@ -34,2 +34,6 @@ "author": "Turf Authors",

"module": "dist/es/index.js",
"exports": {
"import": "./dist/es/index.js",
"require": "./dist/js/index.js"
},
"types": "index.d.ts",

@@ -42,13 +46,12 @@ "sideEffects": false,

"scripts": {
"bench": "npm-run-all prepare bench:run",
"bench:run": "node bench.js",
"bench": "node -r esm bench.js",
"build": "rollup -c ../../rollup.config.js && echo '{\"type\":\"module\"}' > dist/es/package.json",
"docs": "node ../../scripts/generate-readmes",
"posttest": "node -r esm ../../scripts/validate-es5-dependencies.js",
"prepare": "rollup -c ../../rollup.config.js",
"test": "npm-run-all prepare test:*",
"test": "npm-run-all test:*",
"test:tape": "node -r esm test.js",
"test:types": "tsc --noEmit types.ts"
"test:types": "tsc --esModuleInterop --noEmit types.ts"
},
"devDependencies": {
"@turf/truncate": "^6.2.0-alpha.2",
"@turf/truncate": "^6.2.0-alpha.3",
"benchmark": "*",

@@ -62,11 +65,11 @@ "load-json-file": "*",

"dependencies": {
"@turf/bbox": "^6.2.0-alpha.2",
"@turf/center": "^6.2.0-alpha.2",
"@turf/helpers": "^6.2.0-alpha.2",
"@turf/meta": "^6.2.0-alpha.2",
"@turf/projection": "^6.2.0-alpha.2",
"@turf/bbox": "^6.2.0-alpha.3",
"@turf/center": "^6.2.0-alpha.3",
"@turf/helpers": "^6.2.0-alpha.3",
"@turf/meta": "^6.2.0-alpha.3",
"@turf/projection": "^6.2.0-alpha.3",
"d3-geo": "1.7.1",
"turf-jsts": "*"
},
"gitHead": "23d5cb91d77e0c1e2e903a2252f525797f1d0d09"
"gitHead": "dce9edfc705352e8cb9e0083c9330ba0e8d77409"
}

@@ -21,3 +21,3 @@ # @turf/buffer

- `options.units` **[string][6]** any of the options supported by turf units (optional, default `"kilometers"`)
- `options.steps` **[number][4]** number of steps (optional, default `64`)
- `options.steps` **[number][4]** number of steps (optional, default `8`)

@@ -24,0 +24,0 @@ **Examples**

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc