@turf/buffer
Advanced tools
Comparing version 4.7.4 to 5.0.0
@@ -1,30 +0,32 @@ | ||
/// <reference types="geojson" /> | ||
import {Units, FeatureGeometryCollection} from '@turf/helpers'; | ||
import { | ||
Point, | ||
LineString, | ||
Polygon, | ||
MultiPoint, | ||
MultiLineString, | ||
MultiPolygon, | ||
GeometryObject, | ||
GeometryCollection, | ||
Feature, | ||
FeatureCollection, | ||
Units, | ||
FeatureGeometryCollection | ||
} from '@turf/helpers'; | ||
type Point = GeoJSON.Point; | ||
type LineString = GeoJSON.LineString; | ||
type Polygon = GeoJSON.Polygon; | ||
type MultiPoint = GeoJSON.MultiPoint; | ||
type MultiLineString = GeoJSON.MultiLineString; | ||
type MultiPolygon = GeoJSON.MultiPolygon; | ||
type GeometryObject = GeoJSON.GeometryObject; | ||
type GeometryCollection = GeoJSON.GeometryCollection; | ||
type Feature<Geom extends GeometryObject> = GeoJSON.Feature<Geom>; | ||
type FeatureCollection<Geom extends GeometryObject> = GeoJSON.FeatureCollection<Geom>; | ||
type Geoms = Point|LineString|Polygon|MultiPoint|MultiLineString|MultiPolygon; | ||
interface Options { | ||
units?: Units; | ||
steps?: number | ||
} | ||
interface Buffer { | ||
/** | ||
* http://turfjs.org/docs/#buffer | ||
*/ | ||
<Geom extends Point|LineString|Polygon>(feature: Feature<Geom>|Geom, radius?: number, unit?: Units, steps?: number): Feature<Polygon> | undefined; | ||
<Geom extends MultiPoint|MultiLineString|MultiPolygon>(feature: Feature<Geom>|Geom, radius?: number, unit?: Units, steps?: number): Feature<MultiPolygon> | undefined; | ||
<Geom extends Point|LineString|Polygon>(feature: FeatureCollection<Geom>, radius?: number, unit?: Units, steps?: number): FeatureCollection<Polygon>; | ||
<Geom extends MultiPoint|MultiLineString|MultiPolygon>(feature: FeatureCollection<Geom>, radius?: number, unit?: Units, steps?: number): FeatureCollection<MultiPolygon>; | ||
(feature: FeatureCollection<any>|FeatureGeometryCollection|GeometryCollection, radius?: number, unit?: Units, steps?: number): FeatureCollection<Polygon|MultiPolygon>; | ||
(feature: Feature<any>|GeometryObject, radius?: number, unit?: Units, steps?: number): Feature<Polygon|MultiPolygon> | undefined; | ||
} | ||
declare const buffer: Buffer; | ||
declare namespace buffer {} | ||
export = buffer; | ||
/** | ||
* http://turfjs.org/docs/#buffer | ||
*/ | ||
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> | FeatureGeometryCollection | 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; |
90
index.js
@@ -1,16 +0,8 @@ | ||
var d3 = require('d3-geo'); | ||
var jsts = require('jsts'); | ||
var meta = require('@turf/meta'); | ||
var center = require('@turf/center'); | ||
var helpers = require('@turf/helpers'); | ||
var turfBbox = require('@turf/bbox'); | ||
var projection = require('@turf/projection'); | ||
var toWgs84 = projection.toWgs84; | ||
var feature = helpers.feature; | ||
var geomEach = meta.geomEach; | ||
var toMercator = projection.toMercator; | ||
var featureEach = meta.featureEach; | ||
var featureCollection = helpers.featureCollection; | ||
var radiansToDistance = helpers.radiansToDistance; | ||
var distanceToRadians = helpers.distanceToRadians; | ||
import center from '@turf/center'; | ||
import turfBbox from '@turf/bbox'; | ||
import { BufferOp, GeoJSONReader, GeoJSONWriter } from 'jsts-es'; | ||
import { toWgs84, toMercator } from '@turf/projection'; | ||
import { geomEach, featureEach } from '@turf/meta'; | ||
import { geoTransverseMercator } from 'd3-geo'; | ||
import { feature, featureCollection, radiansToLength, lengthToRadians, earthRadius } from '@turf/helpers'; | ||
@@ -29,8 +21,9 @@ /** | ||
* @param {number} radius distance to draw the buffer (negative values are allowed) | ||
* @param {string} [units=kilometers] any of the options supported by turf units | ||
* @param {number} [steps=64] number of steps | ||
* @param {Object} [options] Optional parameters | ||
* @param {string} [options.units="kilometers"] any of the options supported by turf units | ||
* @param {number} [options.steps=64] number of steps | ||
* @returns {FeatureCollection|Feature<Polygon|MultiPolygon>|undefined} buffered features | ||
* @example | ||
* var point = turf.point([-90.548630, 14.616599]); | ||
* var buffered = turf.buffer(point, 500, 'miles'); | ||
* var buffered = turf.buffer(point, 500, {units: 'miles'}); | ||
* | ||
@@ -40,5 +33,13 @@ * //addToMap | ||
*/ | ||
module.exports = function (geojson, radius, units, steps) { | ||
function buffer(geojson, radius, options) { | ||
// Optional params | ||
options = options || {}; | ||
var units = options.units; | ||
var steps = options.steps || 64; | ||
// 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'); | ||
// Allow negative buffers ("erosion") or zero-sized buffers ("repair geometry") | ||
@@ -56,3 +57,3 @@ if (radius === undefined) throw new Error('radius is required'); | ||
geomEach(geojson, function (geometry) { | ||
var buffered = buffer(geometry, radius, units, steps); | ||
var buffered = bufferFeature(geometry, radius, units, steps); | ||
if (buffered) results.push(buffered); | ||
@@ -63,3 +64,3 @@ }); | ||
featureEach(geojson, function (feature) { | ||
var multiBuffered = buffer(feature, radius, units, steps); | ||
var multiBuffered = bufferFeature(feature, radius, units, steps); | ||
if (multiBuffered) { | ||
@@ -73,4 +74,4 @@ featureEach(multiBuffered, function (buffered) { | ||
} | ||
return buffer(geojson, radius, units, steps); | ||
}; | ||
return bufferFeature(geojson, radius, units, steps); | ||
} | ||
@@ -87,3 +88,3 @@ /** | ||
*/ | ||
function buffer(geojson, radius, units, steps) { | ||
function bufferFeature(geojson, radius, units, steps) { | ||
var properties = geojson.properties || {}; | ||
@@ -96,3 +97,3 @@ var geometry = (geojson.type === 'Feature') ? geojson.geometry : geojson; | ||
geomEach(geojson, function (geometry) { | ||
var buffered = buffer(geometry, radius, units, steps); | ||
var buffered = bufferFeature(geometry, radius, units, steps); | ||
if (buffered) results.push(buffered); | ||
@@ -109,6 +110,5 @@ }); | ||
if (needsTransverseMercator) { | ||
var projection = defineProjection(geometry); | ||
projected = { | ||
type: geometry.type, | ||
coordinates: projectCoords(geometry.coordinates, projection) | ||
coordinates: projectCoords(geometry.coordinates, defineProjection(geometry)) | ||
}; | ||
@@ -120,7 +120,7 @@ } else { | ||
// JSTS buffer operation | ||
var reader = new jsts.io.GeoJSONReader(); | ||
var reader = new GeoJSONReader(); | ||
var geom = reader.read(projected); | ||
var distance = radiansToDistance(distanceToRadians(radius, units), 'meters'); | ||
var buffered = geom.buffer(distance); | ||
var writer = new jsts.io.GeoJSONWriter(); | ||
var distance = radiansToLength(lengthToRadians(radius, units), 'meters'); | ||
var buffered = BufferOp.bufferOp(geom, distance); | ||
var writer = new GeoJSONWriter(); | ||
buffered = writer.write(buffered); | ||
@@ -136,3 +136,3 @@ | ||
type: buffered.type, | ||
coordinates: unprojectCoords(buffered.coordinates, projection) | ||
coordinates: unprojectCoords(buffered.coordinates, defineProjection(geometry)) | ||
}; | ||
@@ -151,3 +151,3 @@ } else { | ||
* @param {Array<any>} coords GeoJSON Coordinates | ||
* @returns {Boolean} if NaN exists | ||
* @returns {boolean} if NaN exists | ||
*/ | ||
@@ -164,9 +164,9 @@ function coordsIsNaN(coords) { | ||
* @param {Array<any>} coords to project | ||
* @param {GeoProjection} projection D3 Geo Projection | ||
* @param {GeoProjection} proj D3 Geo Projection | ||
* @returns {Array<any>} projected coordinates | ||
*/ | ||
function projectCoords(coords, projection) { | ||
if (typeof coords[0] !== 'object') return projection(coords); | ||
function projectCoords(coords, proj) { | ||
if (typeof coords[0] !== 'object') return proj(coords); | ||
return coords.map(function (coord) { | ||
return projectCoords(coord, projection); | ||
return projectCoords(coord, proj); | ||
}); | ||
@@ -180,9 +180,9 @@ } | ||
* @param {Array<any>} coords to un-project | ||
* @param {GeoProjection} projection D3 Geo Projection | ||
* @param {GeoProjection} proj D3 Geo Projection | ||
* @returns {Array<any>} un-projected coordinates | ||
*/ | ||
function unprojectCoords(coords, projection) { | ||
if (typeof coords[0] !== 'object') return projection.invert(coords); | ||
function unprojectCoords(coords, proj) { | ||
if (typeof coords[0] !== 'object') return proj.invert(coords); | ||
return coords.map(function (coord) { | ||
return unprojectCoords(coord, projection); | ||
return unprojectCoords(coord, proj); | ||
}); | ||
@@ -201,8 +201,8 @@ } | ||
var rotate = coords.map(function (coord) { return -coord; }); | ||
var projection = d3.geoTransverseMercator() | ||
return geoTransverseMercator() | ||
.center(coords) | ||
.rotate(rotate) | ||
.scale(6373000); | ||
.scale(earthRadius); | ||
} | ||
return projection; | ||
} | ||
export default buffer; |
{ | ||
"name": "@turf/buffer", | ||
"version": "4.7.4", | ||
"version": "5.0.0", | ||
"description": "turf buffer module", | ||
"main": "index.js", | ||
"main": "main", | ||
"module": "index", | ||
"jsnext:main": "index", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
"index.d.ts", | ||
"main.js" | ||
], | ||
"scripts": { | ||
"test": "node test.js", | ||
"bench": "node bench.js" | ||
"pretest": "rollup -c ../../rollup.config.js", | ||
"test": "node -r @std/esm test.js", | ||
"bench": "node -r @std/esm bench.js" | ||
}, | ||
@@ -40,17 +44,23 @@ "repository": { | ||
"devDependencies": { | ||
"@turf/truncate": "^4.7.3", | ||
"benchmark": "^2.1.4", | ||
"load-json-file": "^2.0.0", | ||
"tape": "^4.6.3", | ||
"write-json-file": "^2.0.0" | ||
"@std/esm": "*", | ||
"@turf/truncate": "*", | ||
"benchmark": "*", | ||
"load-json-file": "*", | ||
"rollup": "*", | ||
"tape": "*", | ||
"write-json-file": "*" | ||
}, | ||
"dependencies": { | ||
"@turf/bbox": "^4.7.1", | ||
"@turf/center": "4.7.1", | ||
"@turf/helpers": "4.7.1", | ||
"@turf/meta": "4.7.1", | ||
"@turf/projection": "^4.7.1", | ||
"d3-geo": "^1.6.3", | ||
"jsts": "1.3.0" | ||
"@turf/bbox": "5.x", | ||
"@turf/center": "^4.7.3", | ||
"@turf/helpers": "5.x", | ||
"@turf/meta": "5.x", | ||
"@turf/projection": "^4.7.3", | ||
"d3-geo": "1.7.1", | ||
"jsts-es": "*" | ||
}, | ||
"@std/esm": { | ||
"esm": "js", | ||
"cjs": true | ||
} | ||
} |
# @turf/buffer | ||
# buffer | ||
<!-- Generated by documentation.js. Update this documentation by updating the source code. --> | ||
## buffer | ||
Calculates a buffer for input features for a given radius. Units supported are miles, kilometers, and degrees. | ||
@@ -17,4 +19,5 @@ | ||
- `radius` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** distance to draw the buffer (negative values are allowed) | ||
- `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`) | ||
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** Optional parameters | ||
- `options.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"`) | ||
- `options.steps` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** number of steps (optional, default `64`) | ||
@@ -25,3 +28,3 @@ **Examples** | ||
var point = turf.point([-90.548630, 14.616599]); | ||
var buffered = turf.buffer(point, 500, 'miles'); | ||
var buffered = turf.buffer(point, 500, {units: 'miles'}); | ||
@@ -28,0 +31,0 @@ //addToMap |
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
20121
6
383
59
7
1
1
+ Addedjsts-es@*
+ Added@turf/bbox@5.1.5(transitive)
+ Added@turf/center@4.7.3(transitive)
+ Added@turf/helpers@4.7.35.1.5(transitive)
+ Added@turf/meta@5.2.0(transitive)
+ Addedd3-geo@1.7.1(transitive)
+ Addedjsts-es@1.5.4(transitive)
- Removedjsts@1.3.0
- Removed@turf/bbox@4.7.1(transitive)
- Removed@turf/center@4.7.1(transitive)
- Removed@turf/helpers@4.7.1(transitive)
- Removed@turf/meta@4.7.1(transitive)
- Removedd3-geo@1.12.1(transitive)
- Removedjsts@1.3.0(transitive)
Updated@turf/bbox@5.x
Updated@turf/center@^4.7.3
Updated@turf/helpers@5.x
Updated@turf/meta@5.x
Updated@turf/projection@^4.7.3
Updatedd3-geo@1.7.1