@turf/buffer
Advanced tools
Comparing version 4.1.0 to 4.2.0
@@ -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; |
107
index.js
@@ -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)<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)<[Polygon](http://geojson.org/geojson-spec.html#polygon)> | [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<[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)<([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: |
8951
118
59
5
+ Added@turf/circle@^4.2.0
+ Added@turf/dissolve@^4.2.0
+ Added@turf/meta@^4.2.0
+ Added@turf/bbox@4.7.3(transitive)
+ Added@turf/bearing@4.7.3(transitive)
+ Added@turf/boolean-overlap@4.7.3(transitive)
+ Added@turf/boolean-point-on-line@4.7.3(transitive)
+ Added@turf/circle@4.7.3(transitive)
+ Added@turf/destination@4.7.3(transitive)
+ Added@turf/dissolve@4.7.3(transitive)
+ Added@turf/distance@4.7.3(transitive)
+ Added@turf/invariant@4.7.3(transitive)
+ Added@turf/line-intersect@4.7.3(transitive)
+ Added@turf/line-overlap@4.7.3(transitive)
+ Added@turf/line-segment@4.7.3(transitive)
+ Added@turf/point-on-line@4.7.3(transitive)
+ Added@turf/union@4.7.3(transitive)
+ Addedcall-bind@1.0.8(transitive)
+ Addedcall-bind-apply-helpers@1.0.1(transitive)
+ Addedcall-bound@1.0.3(transitive)
+ Addeddeep-equal@1.1.2(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addeddunder-proto@1.0.1(transitive)
+ Addedes-define-property@1.0.1(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.1.1(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedfunctions-have-names@1.2.3(transitive)
+ Addedgeojson-equality@0.1.6(transitive)
+ Addedgeojson-rbush@1.2.0(transitive)
+ Addedgeojson-utils@1.1.0(transitive)
+ Addedget-closest@0.0.4(transitive)
+ Addedget-intrinsic@1.2.7(transitive)
+ Addedget-proto@1.0.1(transitive)
+ Addedgopd@1.2.0(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedis-arguments@1.2.0(transitive)
+ Addedis-date-object@1.1.0(transitive)
+ Addedis-regex@1.2.1(transitive)
+ Addedmath-intrinsics@1.1.0(transitive)
+ Addedobject-is@1.1.6(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedquickselect@1.1.1(transitive)
+ Addedrbush@2.0.2(transitive)
+ Addedregexp.prototype.flags@1.5.4(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedset-function-name@2.0.2(transitive)
- Removed@mapbox/geojson-normalize@0.0.1
- Removed@turf/combine@^4.1.0
- Removed@mapbox/geojson-normalize@0.0.1(transitive)
- Removed@turf/combine@4.7.3(transitive)
Updated@turf/helpers@^4.2.0