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
4
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 4.1.0 to 4.2.0

18

index.d.ts

@@ -14,13 +14,13 @@ /// <reference types="geojson" />

*/
(feature: Point | LineString | Polygon, radius?: number, unit?: Units): Polygon;
(feature: Points | LineStrings | Polygons, radius?: number, unit?: Units): Polygons;
(feature: MultiPoint | MultiLineString | MultiPolygon, radius?: number, unit?: Units): MultiPolygon;
(feature: MultiPoints | MultiLineStrings | MultiPolygons, radius?: number, unit?: Units): MultiPolygons;
(feature: Feature, radius?: number, unit?: Units): Polygon | MultiPolygon;
(feature: Features, radius?: number, unit?: Units): Polygons | MultiPolygons;
(feature: GeoJSON.GeometryObject, radius?: number, unit?: Units): Polygon | MultiPolygon;
(feature: GeoJSON.GeometryCollection, radius?: number, unit?: Units): Polygons | MultiPolygons;
(feature: Point | LineString | Polygon, radius?: number, unit?: Units, steps?: number): Polygon;
(feature: Points | LineStrings | Polygons | MultiPoint | MultiPoints, radius?: number, unit?: Units, steps?: number): Polygons;
(feature: MultiLineString | MultiPolygon, radius?: number, unit?: Units, steps?: number): MultiPolygon;
(feature: MultiLineStrings | MultiPolygons, radius?: number, unit?: Units, steps?: number): MultiPolygons;
(feature: Feature, radius?: number, unit?: Units, steps?: number): Polygon | Polygons | MultiPolygon;
(feature: Features, radius?: number, unit?: Units, steps?: number): Polygons | MultiPolygons;
(feature: GeoJSON.GeometryObject, radius?: number, unit?: Units, steps?: number): Polygon | Polygons | MultiPolygon;
(feature: GeoJSON.GeometryCollection, radius?: number, unit?: Units, steps?: number): Polygons | MultiPolygons;
}
declare const buffer: Buffer;
declare namespace buffer { }
declare namespace buffer {}
export = buffer;

@@ -1,9 +0,11 @@

// http://stackoverflow.com/questions/839899/how-do-i-calculate-a-point-on-a-circles-circumference
// radians = degrees * (pi/180)
// https://github.com/bjornharrtell/jsts/blob/master/examples/buffer.html
var jsts = require('jsts');
var helpers = require('@turf/helpers');
var circle = require('@turf/circle');
var dissolve = require('@turf/dissolve');
var meta = require('@turf/meta');
var coordEach = meta.coordEach;
var featureEach = meta.featureEach;
var featureCollection = helpers.featureCollection;
var jsts = require('jsts');
var normalize = require('@mapbox/geojson-normalize');
var distanceToDegrees = helpers.distanceToDegrees;
var point = helpers.point;

@@ -14,9 +16,9 @@ /**

* @name buffer
* @param {(Feature|FeatureCollection)} feature input to be buffered
* @param {FeatureCollection|Feature<any>} feature input to be buffered
* @param {number} radius distance to draw the buffer
* @param {string} units any of the options supported by turf units
* @return {FeatureCollection<Polygon>|FeatureCollection<MultiPolygon>|Polygon|MultiPolygon} buffered features
* @addToMap pt, buffered
* @param {string} [units=kilometers] any of the options supported by turf units
* @param {number} [steps=64] number of steps
* @return {FeatureCollection|Feature<Polygon|MultiPolygon>} buffered features
* @example
* var pt = {
* var point = {
* "type": "Feature",

@@ -31,31 +33,72 @@ * "properties": {},

*
* var buffered = turf.buffer(pt, 500, unit);
* var buffered = turf.buffer(point, 500, unit);
*
* //=buffered
* //addToMap
* var addToMap = [point, buffered]
*/
module.exports = function (feature, radius, units) {
module.exports = function (geojson, radius, units, steps) {
// validation
if (radius === undefined || radius === null) throw new Error('radius is required');
var degrees = helpers.distanceToDegrees(radius, units);
var fc = normalize(feature);
var buffered = normalize(featureCollection(fc.features.map(function (f) {
return bufferOp(f, degrees);
})));
// default params
steps = steps || 64;
if (buffered.features.length > 1) return buffered;
else if (buffered.features.length === 1) return buffered.features[0];
switch (geojson.type) {
case 'GeometryCollection':
case 'FeatureCollection':
var results = [];
var features = (geojson.features) ? geojson.features : geojson.geometries || [];
features.forEach(function (feature) {
featureEach(buffer(feature, radius, units, steps), function (buffered) {
results.push(buffered);
});
});
return featureCollection(results);
}
return buffer(geojson, radius, units, steps);
};
function bufferOp(feature, radius) {
var reader = new jsts.io.GeoJSONReader();
var geom = reader.read(feature.geometry);
var buffered = geom.buffer(radius);
var writer = new jsts.io.GeoJSONWriter();
buffered = writer.write(buffered);
/**
* Buffer single Feature
*
* @private
* @param {Feature<any>} feature input to be buffered
* @param {number} radius distance to draw the buffer
* @param {string} [units='kilometers'] any of the options supported by turf units
* @param {number} [steps=64] number of steps
* @returns {Feature<Polygon|MultiPolygon>} buffered feature
*/
function buffer(feature, radius, units, steps) {
var properties = feature.properties || {};
var distance = distanceToDegrees(radius, units);
var geometry = (feature.type === 'Feature') ? feature.geometry : feature;
return {
type: 'Feature',
geometry: buffered,
properties: {}
};
switch (geometry.type) {
case 'Point':
var poly = circle(feature, radius, steps, units);
poly.properties = properties;
return poly;
case 'MultiPoint':
var polys = [];
coordEach(feature, function (coord) {
var poly = circle(point(coord, properties), radius, steps, units);
poly.properties = properties;
polys.push(poly);
});
return dissolve(featureCollection(polys));
case 'LineString':
case 'MultiLineString':
case 'Polygon':
case 'MultiPolygon':
var reader = new jsts.io.GeoJSONReader();
var geom = reader.read(geometry);
var buffered = geom.buffer(distance);
var writer = new jsts.io.GeoJSONWriter();
buffered = writer.write(buffered);
return helpers.feature(buffered, properties);
default:
throw new Error('geometry type ' + geometry.type + ' not supported');
}
}
{
"name": "@turf/buffer",
"version": "4.1.0",
"version": "4.2.0",
"description": "turf buffer module",

@@ -29,2 +29,6 @@ "main": "index.js",

"author": "Turf Authors",
"contributors": [
"Tom MacWright <@tmcw>",
"Denis Carriere <@DenisCarriere>"
],
"license": "MIT",

@@ -36,3 +40,3 @@ "bugs": {

"devDependencies": {
"@turf/truncate": "^4.1.0",
"@turf/truncate": "^4.2.0",
"benchmark": "^2.1.4",

@@ -44,7 +48,8 @@ "load-json-file": "^2.0.0",

"dependencies": {
"@mapbox/geojson-normalize": "0.0.1",
"@turf/combine": "^4.1.0",
"@turf/helpers": "^4.1.0",
"@turf/circle": "^4.2.0",
"@turf/dissolve": "^4.2.0",
"@turf/helpers": "^4.2.0",
"@turf/meta": "^4.2.0",
"jsts": "1.3.0"
}
}

@@ -9,5 +9,6 @@ # @turf/buffer

- `feature` **([Feature](http://geojson.org/geojson-spec.html#feature-objects) \| [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects))** input to be buffered
- `feature` **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)&lt;any>)** input to be buffered
- `radius` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** distance to draw the buffer
- `units` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** any of the options supported by turf units
- `units` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** any of the options supported by turf units (optional, default `kilometers`)
- `steps` **\[[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)]** number of steps (optional, default `64`)

@@ -17,3 +18,3 @@ **Examples**

```javascript
var pt = {
var point = {
"type": "Feature",

@@ -28,8 +29,9 @@ "properties": {},

var buffered = turf.buffer(pt, 500, unit);
var buffered = turf.buffer(point, 500, unit);
//=buffered
//addToMap
var addToMap = [point, buffered]
```
Returns **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)&lt;[Polygon](http://geojson.org/geojson-spec.html#polygon)> | [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)&lt;[MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon)> | [Polygon](http://geojson.org/geojson-spec.html#polygon) \| [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon))** buffered features
Returns **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)&lt;([Polygon](http://geojson.org/geojson-spec.html#polygon) \| [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon))>)** buffered features

@@ -36,0 +38,0 @@ <!-- This file is automatically generated. Please don't edit it directly:

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