@turf/buffer 7.0.0-alpha.2 → 7.1.0-alpha.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -51,26 +51,21 @@ Returns **([FeatureCollection][1] | [Feature][3]<([Polygon][7] | [MultiPolygon][
51
51
 
52
52
  [9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined
53
53
 
54
- <!-- This file is automatically generated. Please don't edit it directly:
55
- if you find an error, edit the source file (likely index.js), and re-run
56
- ./scripts/generate-readmes in the turf project. -->
54
+ <!-- This file is automatically generated. Please don't edit it directly. If you find an error, edit the source file of the module in question (likely index.js or index.ts), and re-run "yarn docs" from the root of the turf project. -->
57
55
 
58
56
  ---
59
57
 
60
- This module is part of the [Turfjs project](http://turfjs.org/), an open source
61
- module collection dedicated to geographic algorithms. It is maintained in the
62
- [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
63
- PRs and issues.
58
+ This module is part of the [Turfjs project](https://turfjs.org/), an open source module collection dedicated to geographic algorithms. It is maintained in the [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create PRs and issues.
64
59
 
65
60
  ### Installation
66
61
 
67
- Install this module individually:
62
+ Install this single module individually:
68
63
 
69
64
  ```sh
70
65
  $ npm install @turf/buffer
71
66
  ```
72
67
 
73
- Or install the Turf module that includes it as a function:
68
+ Or install the all-encompassing @turf/turf module that includes all modules as functions:
74
69
 
75
70
  ```sh
76
71
  $ npm install @turf/turf
@@ -0,0 +1,111 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }// index.js
2
+ var _center = require('@turf/center');
3
+ var _jsts = require('@turf/jsts'); var _jsts2 = _interopRequireDefault(_jsts);
4
+ var _meta = require('@turf/meta');
5
+ var _d3geo = require('d3-geo');
6
+
7
+
8
+
9
+
10
+
11
+
12
+ var _helpers = require('@turf/helpers');
13
+ var { BufferOp, GeoJSONReader, GeoJSONWriter } = _jsts2.default;
14
+ function buffer(geojson, radius, options) {
15
+ options = options || {};
16
+ var units = options.units || "kilometers";
17
+ var steps = options.steps || 8;
18
+ if (!geojson)
19
+ throw new Error("geojson is required");
20
+ if (typeof options !== "object")
21
+ throw new Error("options must be an object");
22
+ if (typeof steps !== "number")
23
+ throw new Error("steps must be an number");
24
+ if (radius === void 0)
25
+ throw new Error("radius is required");
26
+ if (steps <= 0)
27
+ throw new Error("steps must be greater than 0");
28
+ var results = [];
29
+ switch (geojson.type) {
30
+ case "GeometryCollection":
31
+ _meta.geomEach.call(void 0, geojson, function(geometry) {
32
+ var buffered = bufferFeature(geometry, radius, units, steps);
33
+ if (buffered)
34
+ results.push(buffered);
35
+ });
36
+ return _helpers.featureCollection.call(void 0, results);
37
+ case "FeatureCollection":
38
+ _meta.featureEach.call(void 0, geojson, function(feature2) {
39
+ var multiBuffered = bufferFeature(feature2, radius, units, steps);
40
+ if (multiBuffered) {
41
+ _meta.featureEach.call(void 0, multiBuffered, function(buffered) {
42
+ if (buffered)
43
+ results.push(buffered);
44
+ });
45
+ }
46
+ });
47
+ return _helpers.featureCollection.call(void 0, results);
48
+ }
49
+ return bufferFeature(geojson, radius, units, steps);
50
+ }
51
+ function bufferFeature(geojson, radius, units, steps) {
52
+ var properties = geojson.properties || {};
53
+ var geometry = geojson.type === "Feature" ? geojson.geometry : geojson;
54
+ if (geometry.type === "GeometryCollection") {
55
+ var results = [];
56
+ _meta.geomEach.call(void 0, geojson, function(geometry2) {
57
+ var buffered2 = bufferFeature(geometry2, radius, units, steps);
58
+ if (buffered2)
59
+ results.push(buffered2);
60
+ });
61
+ return _helpers.featureCollection.call(void 0, results);
62
+ }
63
+ var projection = defineProjection(geometry);
64
+ var projected = {
65
+ type: geometry.type,
66
+ coordinates: projectCoords(geometry.coordinates, projection)
67
+ };
68
+ var reader = new GeoJSONReader();
69
+ var geom = reader.read(projected);
70
+ var distance = _helpers.radiansToLength.call(void 0, _helpers.lengthToRadians.call(void 0, radius, units), "meters");
71
+ var buffered = BufferOp.bufferOp(geom, distance, steps);
72
+ var writer = new GeoJSONWriter();
73
+ buffered = writer.write(buffered);
74
+ if (coordsIsNaN(buffered.coordinates))
75
+ return void 0;
76
+ var result = {
77
+ type: buffered.type,
78
+ coordinates: unprojectCoords(buffered.coordinates, projection)
79
+ };
80
+ return _helpers.feature.call(void 0, result, properties);
81
+ }
82
+ function coordsIsNaN(coords) {
83
+ if (Array.isArray(coords[0]))
84
+ return coordsIsNaN(coords[0]);
85
+ return isNaN(coords[0]);
86
+ }
87
+ function projectCoords(coords, proj) {
88
+ if (typeof coords[0] !== "object")
89
+ return proj(coords);
90
+ return coords.map(function(coord) {
91
+ return projectCoords(coord, proj);
92
+ });
93
+ }
94
+ function unprojectCoords(coords, proj) {
95
+ if (typeof coords[0] !== "object")
96
+ return proj.invert(coords);
97
+ return coords.map(function(coord) {
98
+ return unprojectCoords(coord, proj);
99
+ });
100
+ }
101
+ function defineProjection(geojson) {
102
+ var coords = _center.center.call(void 0, geojson).geometry.coordinates;
103
+ var rotation = [-coords[0], -coords[1]];
104
+ return _d3geo.geoAzimuthalEquidistant.call(void 0, ).rotate(rotation).scale(_helpers.earthRadius);
105
+ }
106
+ var turf_buffer_default = buffer;
107
+
108
+
109
+
110
+ exports.buffer = buffer; exports.default = turf_buffer_default;
111
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.js"],"names":["feature","geometry","buffered"],"mappings":";AAAA,SAAS,cAAc;AACvB,OAAO,UAAU;AACjB,SAAS,UAAU,mBAAmB;AACtC,SAAS,+BAA+B;AACxC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,IAAM,EAAE,UAAU,eAAe,cAAc,IAAI;AAyBnD,SAAS,OAAO,SAAS,QAAQ,SAAS;AAExC,YAAU,WAAW,CAAC;AAGtB,MAAI,Q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C,SAAO,wBAAwB,EAAE,OAAO,QAAQ,EAAE,MAAM,WAAW;AACrE;AAGA,IAAO,sBAAQ","sourcesContent":["import { center } from \"@turf/center\";\nimport jsts from \"@turf/jsts\";\nimport { geomEach, featureEach } from \"@turf/meta\";\nimport { geoAzimuthalEquidistant } from \"d3-geo\";\nimport {\n feature,\n featureCollection,\n radiansToLength,\n lengthToRadians,\n earthRadius,\n} from \"@turf/helpers\";\n\nconst { BufferOp, GeoJSONReader, GeoJSONWriter } = jsts;\n\n/**\n * Calculates a buffer for input features for a given radius. Units supported are miles, kilometers, and degrees.\n *\n * When using a negative radius, the resulting geometry may be invalid if\n * it's too small compared to the radius magnitude. If the input is a\n * FeatureCollection, only valid members will be returned in the output\n * FeatureCollection - i.e., the output collection may have fewer members than\n * the input, or even be empty.\n *\n * @name buffer\n * @param {FeatureCollection|Geometry|Feature<any>} geojson input to be buffered\n * @param {number} radius distance to draw the buffer (negative values are allowed)\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.units=\"kilometers\"] any of the options supported by turf units\n * @param {number} [options.steps=8] number of steps\n * @returns {FeatureCollection|Feature<Polygon|MultiPolygon>|undefined} buffered features\n * @example\n * var point = turf.point([-90.548630, 14.616599]);\n * var buffered = turf.buffer(point, 500, {units: 'miles'});\n *\n * //addToMap\n * var addToMap = [point, buffered]\n */\nfunction buffer(geojson, radius, options) {\n // Optional params\n options = options || {};\n\n // use user supplied options or default values\n var units = options.units || \"kilometers\";\n var steps = options.steps || 8;\n\n // validation\n if (!geojson) throw new Error(\"geojson is required\");\n if (typeof options !== \"object\") throw new Error(\"options must be an object\");\n if (typeof steps !== \"number\") throw new Error(\"steps must be an number\");\n\n // Allow negative buffers (\"erosion\") or zero-sized buffers (\"repair geometry\")\n if (radius === undefined) throw new Error(\"radius is required\");\n if (steps <= 0) throw new Error(\"steps must be greater than 0\");\n\n var results = [];\n switch (geojson.type) {\n case \"GeometryCollection\":\n geomEach(geojson, function (geometry) {\n var buffered = bufferFeature(geometry, radius, units, steps);\n if (buffered) results.push(buffered);\n });\n return featureCollection(results);\n case \"FeatureCollection\":\n featureEach(geojson, function (feature) {\n var multiBuffered = bufferFeature(feature, radius, units, steps);\n if (multiBuffered) {\n featureEach(multiBuffered, function (buffered) {\n if (buffered) results.push(buffered);\n });\n }\n });\n return featureCollection(results);\n }\n return bufferFeature(geojson, radius, units, steps);\n}\n\n/**\n * Buffer single Feature/Geometry\n *\n * @private\n * @param {Feature<any>} geojson input to be buffered\n * @param {number} radius distance to draw the buffer\n * @param {string} [units='kilometers'] any of the options supported by turf units\n * @param {number} [steps=8] number of steps\n * @returns {Feature<Polygon|MultiPolygon>} buffered feature\n */\nfunction bufferFeature(geojson, radius, units, steps) {\n var properties = geojson.properties || {};\n var geometry = geojson.type === \"Feature\" ? geojson.geometry : geojson;\n\n // Geometry Types faster than jsts\n if (geometry.type === \"GeometryCollection\") {\n var results = [];\n geomEach(geojson, function (geometry) {\n var buffered = bufferFeature(geometry, radius, units, steps);\n if (buffered) results.push(buffered);\n });\n return featureCollection(results);\n }\n\n // Project GeoJSON to Azimuthal Equidistant projection (convert to Meters)\n var projection = defineProjection(geometry);\n var projected = {\n type: geometry.type,\n coordinates: projectCoords(geometry.coordinates, projection),\n };\n\n // JSTS buffer operation\n var reader = new GeoJSONReader();\n var geom = reader.read(projected);\n var distance = radiansToLength(lengthToRadians(radius, units), \"meters\");\n var buffered = BufferOp.bufferOp(geom, distance, steps);\n var writer = new GeoJSONWriter();\n buffered = writer.write(buffered);\n\n // Detect if empty geometries\n if (coordsIsNaN(buffered.coordinates)) return undefined;\n\n // Unproject coordinates (convert to Degrees)\n var result = {\n type: buffered.type,\n coordinates: unprojectCoords(buffered.coordinates, projection),\n };\n\n return feature(result, properties);\n}\n\n/**\n * Coordinates isNaN\n *\n * @private\n * @param {Array<any>} coords GeoJSON Coordinates\n * @returns {boolean} if NaN exists\n */\nfunction coordsIsNaN(coords) {\n if (Array.isArray(coords[0])) return coordsIsNaN(coords[0]);\n return isNaN(coords[0]);\n}\n\n/**\n * Project coordinates to projection\n *\n * @private\n * @param {Array<any>} coords to project\n * @param {GeoProjection} proj D3 Geo Projection\n * @returns {Array<any>} projected coordinates\n */\nfunction projectCoords(coords, proj) {\n if (typeof coords[0] !== \"object\") return proj(coords);\n return coords.map(function (coord) {\n return projectCoords(coord, proj);\n });\n}\n\n/**\n * Un-Project coordinates to projection\n *\n * @private\n * @param {Array<any>} coords to un-project\n * @param {GeoProjection} proj D3 Geo Projection\n * @returns {Array<any>} un-projected coordinates\n */\nfunction unprojectCoords(coords, proj) {\n if (typeof coords[0] !== \"object\") return proj.invert(coords);\n return coords.map(function (coord) {\n return unprojectCoords(coord, proj);\n });\n}\n\n/**\n * Define Azimuthal Equidistant projection\n *\n * @private\n * @param {Geometry|Feature<any>} geojson Base projection on center of GeoJSON\n * @returns {GeoProjection} D3 Geo Azimuthal Equidistant Projection\n */\nfunction defineProjection(geojson) {\n var coords = center(geojson).geometry.coordinates;\n var rotation = [-coords[0], -coords[1]];\n return geoAzimuthalEquidistant().rotate(rotation).scale(earthRadius);\n}\n\nexport { buffer };\nexport default buffer;\n"]}
@@ -0,0 +1,30 @@
1
+ import { Feature, GeometryObject, Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, FeatureCollection, GeometryCollection } from 'geojson';
2
+ import { Units } from '@turf/helpers';
3
+
4
+ interface Options {
5
+ units?: Units;
6
+ steps?: number;
7
+ }
8
+
9
+ /**
10
+ * http://turfjs.org/docs/#buffer
11
+ */
12
+ declare function buffer(
13
+ feature:
14
+ | Feature<GeometryObject>
15
+ | Point
16
+ | LineString
17
+ | Polygon
18
+ | MultiPoint
19
+ | MultiLineString
20
+ | MultiPolygon,
21
+ radius?: number,
22
+ options?: Options
23
+ ): Feature<Polygon | MultiPolygon> | undefined;
24
+ declare function buffer(
25
+ feature: FeatureCollection<GeometryObject> | GeometryCollection,
26
+ radius?: number,
27
+ options?: Options
28
+ ): FeatureCollection<Polygon | MultiPolygon> | undefined;
29
+
30
+ export { buffer, buffer as default };
@@ -1,16 +1,5 @@
1
- import {
2
- Point,
3
- LineString,
4
- Polygon,
5
- MultiPoint,
6
- MultiLineString,
7
- MultiPolygon,
8
- GeometryObject,
9
- GeometryCollection,
10
- Feature,
11
- FeatureCollection,
12
- } from "geojson";
13
- import { Units } from "@turf/helpers";
1
+ import { Feature, GeometryObject, Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, FeatureCollection, GeometryCollection } from 'geojson';
2
+ import { Units } from '@turf/helpers';
14
3
 
15
4
  interface Options {
16
5
  units?: Units;
@@ -31,11 +20,11 @@ declare function buffer(
31
20
  | MultiPolygon,
32
21
  radius?: number,
33
22
  options?: Options
34
- ): Feature<Polygon | MultiPolygon>;
23
+ ): Feature<Polygon | MultiPolygon> | undefined;
35
24
  declare function buffer(
36
25
  feature: FeatureCollection<GeometryObject> | GeometryCollection,
37
26
  radius?: number,
38
27
  options?: Options
39
- ): FeatureCollection<Polygon | MultiPolygon>;
28
+ ): FeatureCollection<Polygon | MultiPolygon> | undefined;
40
29
 
41
- export default buffer;
30
+ export { buffer, buffer as default };
@@ -0,0 +1,111 @@
1
+ // index.js
2
+ import { center } from "@turf/center";
3
+ import jsts from "@turf/jsts";
4
+ import { geomEach, featureEach } from "@turf/meta";
5
+ import { geoAzimuthalEquidistant } from "d3-geo";
6
+ import {
7
+ feature,
8
+ featureCollection,
9
+ radiansToLength,
10
+ lengthToRadians,
11
+ earthRadius
12
+ } from "@turf/helpers";
13
+ var { BufferOp, GeoJSONReader, GeoJSONWriter } = jsts;
14
+ function buffer(geojson, radius, options) {
15
+ options = options || {};
16
+ var units = options.units || "kilometers";
17
+ var steps = options.steps || 8;
18
+ if (!geojson)
19
+ throw new Error("geojson is required");
20
+ if (typeof options !== "object")
21
+ throw new Error("options must be an object");
22
+ if (typeof steps !== "number")
23
+ throw new Error("steps must be an number");
24
+ if (radius === void 0)
25
+ throw new Error("radius is required");
26
+ if (steps <= 0)
27
+ throw new Error("steps must be greater than 0");
28
+ var results = [];
29
+ switch (geojson.type) {
30
+ case "GeometryCollection":
31
+ geomEach(geojson, function(geometry) {
32
+ var buffered = bufferFeature(geometry, radius, units, steps);
33
+ if (buffered)
34
+ results.push(buffered);
35
+ });
36
+ return featureCollection(results);
37
+ case "FeatureCollection":
38
+ featureEach(geojson, function(feature2) {
39
+ var multiBuffered = bufferFeature(feature2, radius, units, steps);
40
+ if (multiBuffered) {
41
+ featureEach(multiBuffered, function(buffered) {
42
+ if (buffered)
43
+ results.push(buffered);
44
+ });
45
+ }
46
+ });
47
+ return featureCollection(results);
48
+ }
49
+ return bufferFeature(geojson, radius, units, steps);
50
+ }
51
+ function bufferFeature(geojson, radius, units, steps) {
52
+ var properties = geojson.properties || {};
53
+ var geometry = geojson.type === "Feature" ? geojson.geometry : geojson;
54
+ if (geometry.type === "GeometryCollection") {
55
+ var results = [];
56
+ geomEach(geojson, function(geometry2) {
57
+ var buffered2 = bufferFeature(geometry2, radius, units, steps);
58
+ if (buffered2)
59
+ results.push(buffered2);
60
+ });
61
+ return featureCollection(results);
62
+ }
63
+ var projection = defineProjection(geometry);
64
+ var projected = {
65
+ type: geometry.type,
66
+ coordinates: projectCoords(geometry.coordinates, projection)
67
+ };
68
+ var reader = new GeoJSONReader();
69
+ var geom = reader.read(projected);
70
+ var distance = radiansToLength(lengthToRadians(radius, units), "meters");
71
+ var buffered = BufferOp.bufferOp(geom, distance, steps);
72
+ var writer = new GeoJSONWriter();
73
+ buffered = writer.write(buffered);
74
+ if (coordsIsNaN(buffered.coordinates))
75
+ return void 0;
76
+ var result = {
77
+ type: buffered.type,
78
+ coordinates: unprojectCoords(buffered.coordinates, projection)
79
+ };
80
+ return feature(result, properties);
81
+ }
82
+ function coordsIsNaN(coords) {
83
+ if (Array.isArray(coords[0]))
84
+ return coordsIsNaN(coords[0]);
85
+ return isNaN(coords[0]);
86
+ }
87
+ function projectCoords(coords, proj) {
88
+ if (typeof coords[0] !== "object")
89
+ return proj(coords);
90
+ return coords.map(function(coord) {
91
+ return projectCoords(coord, proj);
92
+ });
93
+ }
94
+ function unprojectCoords(coords, proj) {
95
+ if (typeof coords[0] !== "object")
96
+ return proj.invert(coords);
97
+ return coords.map(function(coord) {
98
+ return unprojectCoords(coord, proj);
99
+ });
100
+ }
101
+ function defineProjection(geojson) {
102
+ var coords = center(geojson).geometry.coordinates;
103
+ var rotation = [-coords[0], -coords[1]];
104
+ return geoAzimuthalEquidistant().rotate(rotation).scale(earthRadius);
105
+ }
106
+ var turf_buffer_default = buffer;
107
+ export {
108
+ buffer,
109
+ turf_buffer_default as default
110
+ };
111
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.js"],"sourcesContent":["import { center } from \"@turf/center\";\nimport jsts from \"@turf/jsts\";\nimport { geomEach, featureEach } from \"@turf/meta\";\nimport { geoAzimuthalEquidistant } from \"d3-geo\";\nimport {\n feature,\n featureCollection,\n radiansToLength,\n lengthToRadians,\n earthRadius,\n} from \"@turf/helpers\";\n\nconst { BufferOp, GeoJSONReader, GeoJSONWriter } = jsts;\n\n/**\n * Calculates a buffer for input features for a given radius. Units supported are miles, kilometers, and degrees.\n *\n * When using a negative radius, the resulting geometry may be invalid if\n * it's too small compared to the radius magnitude. If the input is a\n * FeatureCollection, only valid members will be returned in the output\n * FeatureCollection - i.e., the output collection may have fewer members than\n * the input, or even be empty.\n *\n * @name buffer\n * @param {FeatureCollection|Geometry|Feature<any>} geojson input to be buffered\n * @param {number} radius distance to draw the buffer (negative values are allowed)\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.units=\"kilometers\"] any of the options supported by turf units\n * @param {number} [options.steps=8] number of steps\n * @returns {FeatureCollection|Feature<Polygon|MultiPolygon>|undefined} buffered features\n * @example\n * var point = turf.point([-90.548630, 14.616599]);\n * var buffered = turf.buffer(point, 500, {units: 'miles'});\n *\n * //addToMap\n * var addToMap = [point, buffered]\n */\nfunction buffer(geojson, radius, options) {\n // Optional params\n options = options || {};\n\n // use user supplied options or default values\n var units = options.units || \"kilometers\";\n var steps = options.steps || 8;\n\n // validation\n if (!geojson) throw new Error(\"geojson is required\");\n if (typeof options !== \"object\") throw new Error(\"options must be an object\");\n if (typeof steps !== \"number\") throw new Error(\"steps must be an number\");\n\n // Allow negative buffers (\"erosion\") or zero-sized buffers (\"repair geometry\")\n if (radius === undefined) throw new Error(\"radius is required\");\n if (steps <= 0) throw new Error(\"steps must be greater than 0\");\n\n var results = [];\n switch (geojson.type) {\n case \"GeometryCollection\":\n geomEach(geojson, function (geometry) {\n var buffered = bufferFeature(geometry, radius, units, steps);\n if (buffered) results.push(buffered);\n });\n return featureCollection(results);\n case \"FeatureCollection\":\n featureEach(geojson, function (feature) {\n var multiBuffered = bufferFeature(feature, radius, units, steps);\n if (multiBuffered) {\n featureEach(multiBuffered, function (buffered) {\n if (buffered) results.push(buffered);\n });\n }\n });\n return featureCollection(results);\n }\n return bufferFeature(geojson, radius, units, steps);\n}\n\n/**\n * Buffer single Feature/Geometry\n *\n * @private\n * @param {Feature<any>} geojson input to be buffered\n * @param {number} radius distance to draw the buffer\n * @param {string} [units='kilometers'] any of the options supported by turf units\n * @param {number} [steps=8] number of steps\n * @returns {Feature<Polygon|MultiPolygon>} buffered feature\n */\nfunction bufferFeature(geojson, radius, units, steps) {\n var properties = geojson.properties || {};\n var geometry = geojson.type === \"Feature\" ? geojson.geometry : geojson;\n\n // Geometry Types faster than jsts\n if (geometry.type === \"GeometryCollection\") {\n var results = [];\n geomEach(geojson, function (geometry) {\n var buffered = bufferFeature(geometry, radius, units, steps);\n if (buffered) results.push(buffered);\n });\n return featureCollection(results);\n }\n\n // Project GeoJSON to Azimuthal Equidistant projection (convert to Meters)\n var projection = defineProjection(geometry);\n var projected = {\n type: geometry.type,\n coordinates: projectCoords(geometry.coordinates, projection),\n };\n\n // JSTS buffer operation\n var reader = new GeoJSONReader();\n var geom = reader.read(projected);\n var distance = radiansToLength(lengthToRadians(radius, units), \"meters\");\n var buffered = BufferOp.bufferOp(geom, distance, steps);\n var writer = new GeoJSONWriter();\n buffered = writer.write(buffered);\n\n // Detect if empty geometries\n if (coordsIsNaN(buffered.coordinates)) return undefined;\n\n // Unproject coordinates (convert to Degrees)\n var result = {\n type: buffered.type,\n coordinates: unprojectCoords(buffered.coordinates, projection),\n };\n\n return feature(result, properties);\n}\n\n/**\n * Coordinates isNaN\n *\n * @private\n * @param {Array<any>} coords GeoJSON Coordinates\n * @returns {boolean} if NaN exists\n */\nfunction coordsIsNaN(coords) {\n if (Array.isArray(coords[0])) return coordsIsNaN(coords[0]);\n return isNaN(coords[0]);\n}\n\n/**\n * Project coordinates to projection\n *\n * @private\n * @param {Array<any>} coords to project\n * @param {GeoProjection} proj D3 Geo Projection\n * @returns {Array<any>} projected coordinates\n */\nfunction projectCoords(coords, proj) {\n if (typeof coords[0] !== \"object\") return proj(coords);\n return coords.map(function (coord) {\n return projectCoords(coord, proj);\n });\n}\n\n/**\n * Un-Project coordinates to projection\n *\n * @private\n * @param {Array<any>} coords to un-project\n * @param {GeoProjection} proj D3 Geo Projection\n * @returns {Array<any>} un-projected coordinates\n */\nfunction unprojectCoords(coords, proj) {\n if (typeof coords[0] !== \"object\") return proj.invert(coords);\n return coords.map(function (coord) {\n return unprojectCoords(coord, proj);\n });\n}\n\n/**\n * Define Azimuthal Equidistant projection\n *\n * @private\n * @param {Geometry|Feature<any>} geojson Base projection on center of GeoJSON\n * @returns {GeoProjection} D3 Geo Azimuthal Equidistant Projection\n */\nfunction defineProjection(geojson) {\n var coords = center(geojson).geometry.coordinates;\n var rotation = [-coords[0], -coords[1]];\n return geoAzimuthalEquidistant().rotate(rotation).scale(earthRadius);\n}\n\nexport { buffer };\nexport default buffer;\n"],"mappings":";AAAA,SAAS,cAAc;AACvB,OAAO,UAAU;AACjB,SAAS,UAAU,mBAAmB;AACtC,SAAS,+BAA+B;AACxC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;A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AAiB,SAAS;AACjC,MAAI,SAAS,OAAO,OAAO,EAAE,SAAS;AACtC,MAAI,WAAW,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AACtC,SAAO,wBAAwB,EAAE,OAAO,QAAQ,EAAE,MAAM,WAAW;AACrE;AAGA,IAAO,sBAAQ;","names":["feature","geometry","buffered"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/buffer",
3
- "version": "7.0.0-alpha.2",
3
+ "version": "7.1.0-alpha.7+0ce6ecca0",
4
4
  "description": "turf buffer module",
5
5
  "author": "Turf Authors",
6
6
  "contributors": [
@@ -30,48 +30,55 @@
30
30
  "geojson",
31
31
  "turf"
32
32
  ],
33
- "main": "dist/js/index.js",
34
- "module": "dist/es/index.js",
33
+ "type": "module",
34
+ "main": "dist/cjs/index.cjs",
35
+ "module": "dist/esm/index.js",
36
+ "types": "dist/esm/index.d.ts",
35
37
  "exports": {
36
38
  "./package.json": "./package.json",
37
39
  ".": {
38
- "types": "./index.d.ts",
39
- "import": "./dist/es/index.js",
40
- "require": "./dist/js/index.js"
40
+ "import": {
41
+ "types": "./dist/esm/index.d.ts",
42
+ "default": "./dist/esm/index.js"
43
+ },
44
+ "require": {
45
+ "types": "./dist/cjs/index.d.cts",
46
+ "default": "./dist/cjs/index.cjs"
47
+ }
41
48
  }
42
49
  },
43
- "types": "index.d.ts",
44
50
  "sideEffects": false,
45
51
  "files": [
46
- "dist",
47
- "index.d.ts"
52
+ "dist"
48
53
  ],
49
54
  "scripts": {
50
- "bench": "tsx bench.js",
51
- "build": "rollup -c ../../rollup.config.js && echo '{\"type\":\"module\"}' > dist/es/package.json",
52
- "docs": "tsx ../../scripts/generate-readmes",
53
- "test": "npm-run-all test:*",
54
- "test:tape": "tsx test.js",
55
- "test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
55
+ "bench": "tsx bench.ts",
56
+ "build": "tsup --config ../../tsup.config.ts",
57
+ "docs": "tsx ../../scripts/generate-readmes.ts",
58
+ "test": "npm-run-all --npm-path npm test:*",
59
+ "test:tape": "tsx test.ts",
60
+ "test:types": "tsc --esModuleInterop --module node16 --moduleResolution node16 --noEmit --strict types.ts"
56
61
  },
57
62
  "devDependencies": {
58
- "@turf/truncate": "^7.0.0-alpha.2",
59
- "benchmark": "*",
60
- "load-json-file": "*",
61
- "npm-run-all": "*",
62
- "rollup": "*",
63
- "tape": "*",
64
- "tsx": "*",
65
- "write-json-file": "*"
63
+ "@turf/truncate": "^7.1.0-alpha.7+0ce6ecca0",
64
+ "@types/benchmark": "^2.1.5",
65
+ "@types/tape": "^4.2.32",
66
+ "benchmark": "^2.1.4",
67
+ "load-json-file": "^7.0.1",
68
+ "npm-run-all": "^4.1.5",
69
+ "tape": "^5.7.2",
70
+ "tsup": "^8.0.1",
71
+ "tsx": "^4.6.2",
72
+ "write-json-file": "^5.0.0"
66
73
  },
67
74
  "dependencies": {
68
- "@turf/bbox": "^7.0.0-alpha.2",
69
- "@turf/center": "^7.0.0-alpha.2",
70
- "@turf/helpers": "^7.0.0-alpha.2",
75
+ "@turf/bbox": "^7.1.0-alpha.7+0ce6ecca0",
76
+ "@turf/center": "^7.1.0-alpha.7+0ce6ecca0",
77
+ "@turf/helpers": "^7.1.0-alpha.7+0ce6ecca0",
71
78
  "@turf/jsts": "^2.7.1",
72
- "@turf/meta": "^7.0.0-alpha.2",
73
- "@turf/projection": "^7.0.0-alpha.2",
79
+ "@turf/meta": "^7.1.0-alpha.7+0ce6ecca0",
80
+ "@turf/projection": "^7.1.0-alpha.7+0ce6ecca0",
74
81
  "d3-geo": "1.7.1"
75
82
  },
76
- "gitHead": "dd35b52725945b4fa29a98d9a550733e06cc222e"
83
+ "gitHead": "0ce6ecca05829690270fec6d6bed2003495fe0ea"
77
84
  }
package/dist/es/index.js DELETED
@@ -1,177 +0,0 @@
1
- import center from '@turf/center';
2
- import jsts from '@turf/jsts';
3
- import { featureEach, geomEach } from '@turf/meta';
4
- import { geoAzimuthalEquidistant } from 'd3-geo';
5
- import { featureCollection, earthRadius, radiansToLength, lengthToRadians, feature } from '@turf/helpers';
6
-
7
- const { BufferOp, GeoJSONReader, GeoJSONWriter } = jsts;
8
-
9
- /**
10
- * Calculates a buffer for input features for a given radius. Units supported are miles, kilometers, and degrees.
11
- *
12
- * When using a negative radius, the resulting geometry may be invalid if
13
- * it's too small compared to the radius magnitude. If the input is a
14
- * FeatureCollection, only valid members will be returned in the output
15
- * FeatureCollection - i.e., the output collection may have fewer members than
16
- * the input, or even be empty.
17
- *
18
- * @name buffer
19
- * @param {FeatureCollection|Geometry|Feature<any>} geojson input to be buffered
20
- * @param {number} radius distance to draw the buffer (negative values are allowed)
21
- * @param {Object} [options={}] Optional parameters
22
- * @param {string} [options.units="kilometers"] any of the options supported by turf units
23
- * @param {number} [options.steps=8] number of steps
24
- * @returns {FeatureCollection|Feature<Polygon|MultiPolygon>|undefined} buffered features
25
- * @example
26
- * var point = turf.point([-90.548630, 14.616599]);
27
- * var buffered = turf.buffer(point, 500, {units: 'miles'});
28
- *
29
- * //addToMap
30
- * var addToMap = [point, buffered]
31
- */
32
- function buffer(geojson, radius, options) {
33
- // Optional params
34
- options = options || {};
35
-
36
- // use user supplied options or default values
37
- var units = options.units || "kilometers";
38
- var steps = options.steps || 8;
39
-
40
- // validation
41
- if (!geojson) throw new Error("geojson is required");
42
- if (typeof options !== "object") throw new Error("options must be an object");
43
- if (typeof steps !== "number") throw new Error("steps must be an number");
44
-
45
- // Allow negative buffers ("erosion") or zero-sized buffers ("repair geometry")
46
- if (radius === undefined) throw new Error("radius is required");
47
- if (steps <= 0) throw new Error("steps must be greater than 0");
48
-
49
- var results = [];
50
- switch (geojson.type) {
51
- case "GeometryCollection":
52
- geomEach(geojson, function (geometry) {
53
- var buffered = bufferFeature(geometry, radius, units, steps);
54
- if (buffered) results.push(buffered);
55
- });
56
- return featureCollection(results);
57
- case "FeatureCollection":
58
- featureEach(geojson, function (feature) {
59
- var multiBuffered = bufferFeature(feature, radius, units, steps);
60
- if (multiBuffered) {
61
- featureEach(multiBuffered, function (buffered) {
62
- if (buffered) results.push(buffered);
63
- });
64
- }
65
- });
66
- return featureCollection(results);
67
- }
68
- return bufferFeature(geojson, radius, units, steps);
69
- }
70
-
71
- /**
72
- * Buffer single Feature/Geometry
73
- *
74
- * @private
75
- * @param {Feature<any>} geojson input to be buffered
76
- * @param {number} radius distance to draw the buffer
77
- * @param {string} [units='kilometers'] any of the options supported by turf units
78
- * @param {number} [steps=8] number of steps
79
- * @returns {Feature<Polygon|MultiPolygon>} buffered feature
80
- */
81
- function bufferFeature(geojson, radius, units, steps) {
82
- var properties = geojson.properties || {};
83
- var geometry = geojson.type === "Feature" ? geojson.geometry : geojson;
84
-
85
- // Geometry Types faster than jsts
86
- if (geometry.type === "GeometryCollection") {
87
- var results = [];
88
- geomEach(geojson, function (geometry) {
89
- var buffered = bufferFeature(geometry, radius, units, steps);
90
- if (buffered) results.push(buffered);
91
- });
92
- return featureCollection(results);
93
- }
94
-
95
- // Project GeoJSON to Azimuthal Equidistant projection (convert to Meters)
96
- var projection = defineProjection(geometry);
97
- var projected = {
98
- type: geometry.type,
99
- coordinates: projectCoords(geometry.coordinates, projection),
100
- };
101
-
102
- // JSTS buffer operation
103
- var reader = new GeoJSONReader();
104
- var geom = reader.read(projected);
105
- var distance = radiansToLength(lengthToRadians(radius, units), "meters");
106
- var buffered = BufferOp.bufferOp(geom, distance, steps);
107
- var writer = new GeoJSONWriter();
108
- buffered = writer.write(buffered);
109
-
110
- // Detect if empty geometries
111
- if (coordsIsNaN(buffered.coordinates)) return undefined;
112
-
113
- // Unproject coordinates (convert to Degrees)
114
- var result = {
115
- type: buffered.type,
116
- coordinates: unprojectCoords(buffered.coordinates, projection),
117
- };
118
-
119
- return feature(result, properties);
120
- }
121
-
122
- /**
123
- * Coordinates isNaN
124
- *
125
- * @private
126
- * @param {Array<any>} coords GeoJSON Coordinates
127
- * @returns {boolean} if NaN exists
128
- */
129
- function coordsIsNaN(coords) {
130
- if (Array.isArray(coords[0])) return coordsIsNaN(coords[0]);
131
- return isNaN(coords[0]);
132
- }
133
-
134
- /**
135
- * Project coordinates to projection
136
- *
137
- * @private
138
- * @param {Array<any>} coords to project
139
- * @param {GeoProjection} proj D3 Geo Projection
140
- * @returns {Array<any>} projected coordinates
141
- */
142
- function projectCoords(coords, proj) {
143
- if (typeof coords[0] !== "object") return proj(coords);
144
- return coords.map(function (coord) {
145
- return projectCoords(coord, proj);
146
- });
147
- }
148
-
149
- /**
150
- * Un-Project coordinates to projection
151
- *
152
- * @private
153
- * @param {Array<any>} coords to un-project
154
- * @param {GeoProjection} proj D3 Geo Projection
155
- * @returns {Array<any>} un-projected coordinates
156
- */
157
- function unprojectCoords(coords, proj) {
158
- if (typeof coords[0] !== "object") return proj.invert(coords);
159
- return coords.map(function (coord) {
160
- return unprojectCoords(coord, proj);
161
- });
162
- }
163
-
164
- /**
165
- * Define Azimuthal Equidistant projection
166
- *
167
- * @private
168
- * @param {Geometry|Feature<any>} geojson Base projection on center of GeoJSON
169
- * @returns {GeoProjection} D3 Geo Azimuthal Equidistant Projection
170
- */
171
- function defineProjection(geojson) {
172
- var coords = center(geojson).geometry.coordinates;
173
- var rotation = [-coords[0], -coords[1]];
174
- return geoAzimuthalEquidistant().rotate(rotation).scale(earthRadius);
175
- }
176
-
177
- export default buffer;
@@ -1 +0,0 @@
1
- {"type":"module"}
package/dist/js/index.js DELETED
@@ -1,185 +0,0 @@
1
- 'use strict';
2
-
3
- var center = require('@turf/center');
4
- var jsts = require('@turf/jsts');
5
- var meta = require('@turf/meta');
6
- var d3Geo = require('d3-geo');
7
- var helpers = require('@turf/helpers');
8
-
9
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
-
11
- var center__default = /*#__PURE__*/_interopDefaultLegacy(center);
12
- var jsts__default = /*#__PURE__*/_interopDefaultLegacy(jsts);
13
-
14
- const { BufferOp, GeoJSONReader, GeoJSONWriter } = jsts__default['default'];
15
-
16
- /**
17
- * Calculates a buffer for input features for a given radius. Units supported are miles, kilometers, and degrees.
18
- *
19
- * When using a negative radius, the resulting geometry may be invalid if
20
- * it's too small compared to the radius magnitude. If the input is a
21
- * FeatureCollection, only valid members will be returned in the output
22
- * FeatureCollection - i.e., the output collection may have fewer members than
23
- * the input, or even be empty.
24
- *
25
- * @name buffer
26
- * @param {FeatureCollection|Geometry|Feature<any>} geojson input to be buffered
27
- * @param {number} radius distance to draw the buffer (negative values are allowed)
28
- * @param {Object} [options={}] Optional parameters
29
- * @param {string} [options.units="kilometers"] any of the options supported by turf units
30
- * @param {number} [options.steps=8] number of steps
31
- * @returns {FeatureCollection|Feature<Polygon|MultiPolygon>|undefined} buffered features
32
- * @example
33
- * var point = turf.point([-90.548630, 14.616599]);
34
- * var buffered = turf.buffer(point, 500, {units: 'miles'});
35
- *
36
- * //addToMap
37
- * var addToMap = [point, buffered]
38
- */
39
- function buffer(geojson, radius, options) {
40
- // Optional params
41
- options = options || {};
42
-
43
- // use user supplied options or default values
44
- var units = options.units || "kilometers";
45
- var steps = options.steps || 8;
46
-
47
- // validation
48
- if (!geojson) throw new Error("geojson is required");
49
- if (typeof options !== "object") throw new Error("options must be an object");
50
- if (typeof steps !== "number") throw new Error("steps must be an number");
51
-
52
- // Allow negative buffers ("erosion") or zero-sized buffers ("repair geometry")
53
- if (radius === undefined) throw new Error("radius is required");
54
- if (steps <= 0) throw new Error("steps must be greater than 0");
55
-
56
- var results = [];
57
- switch (geojson.type) {
58
- case "GeometryCollection":
59
- meta.geomEach(geojson, function (geometry) {
60
- var buffered = bufferFeature(geometry, radius, units, steps);
61
- if (buffered) results.push(buffered);
62
- });
63
- return helpers.featureCollection(results);
64
- case "FeatureCollection":
65
- meta.featureEach(geojson, function (feature) {
66
- var multiBuffered = bufferFeature(feature, radius, units, steps);
67
- if (multiBuffered) {
68
- meta.featureEach(multiBuffered, function (buffered) {
69
- if (buffered) results.push(buffered);
70
- });
71
- }
72
- });
73
- return helpers.featureCollection(results);
74
- }
75
- return bufferFeature(geojson, radius, units, steps);
76
- }
77
-
78
- /**
79
- * Buffer single Feature/Geometry
80
- *
81
- * @private
82
- * @param {Feature<any>} geojson input to be buffered
83
- * @param {number} radius distance to draw the buffer
84
- * @param {string} [units='kilometers'] any of the options supported by turf units
85
- * @param {number} [steps=8] number of steps
86
- * @returns {Feature<Polygon|MultiPolygon>} buffered feature
87
- */
88
- function bufferFeature(geojson, radius, units, steps) {
89
- var properties = geojson.properties || {};
90
- var geometry = geojson.type === "Feature" ? geojson.geometry : geojson;
91
-
92
- // Geometry Types faster than jsts
93
- if (geometry.type === "GeometryCollection") {
94
- var results = [];
95
- meta.geomEach(geojson, function (geometry) {
96
- var buffered = bufferFeature(geometry, radius, units, steps);
97
- if (buffered) results.push(buffered);
98
- });
99
- return helpers.featureCollection(results);
100
- }
101
-
102
- // Project GeoJSON to Azimuthal Equidistant projection (convert to Meters)
103
- var projection = defineProjection(geometry);
104
- var projected = {
105
- type: geometry.type,
106
- coordinates: projectCoords(geometry.coordinates, projection),
107
- };
108
-
109
- // JSTS buffer operation
110
- var reader = new GeoJSONReader();
111
- var geom = reader.read(projected);
112
- var distance = helpers.radiansToLength(helpers.lengthToRadians(radius, units), "meters");
113
- var buffered = BufferOp.bufferOp(geom, distance, steps);
114
- var writer = new GeoJSONWriter();
115
- buffered = writer.write(buffered);
116
-
117
- // Detect if empty geometries
118
- if (coordsIsNaN(buffered.coordinates)) return undefined;
119
-
120
- // Unproject coordinates (convert to Degrees)
121
- var result = {
122
- type: buffered.type,
123
- coordinates: unprojectCoords(buffered.coordinates, projection),
124
- };
125
-
126
- return helpers.feature(result, properties);
127
- }
128
-
129
- /**
130
- * Coordinates isNaN
131
- *
132
- * @private
133
- * @param {Array<any>} coords GeoJSON Coordinates
134
- * @returns {boolean} if NaN exists
135
- */
136
- function coordsIsNaN(coords) {
137
- if (Array.isArray(coords[0])) return coordsIsNaN(coords[0]);
138
- return isNaN(coords[0]);
139
- }
140
-
141
- /**
142
- * Project coordinates to projection
143
- *
144
- * @private
145
- * @param {Array<any>} coords to project
146
- * @param {GeoProjection} proj D3 Geo Projection
147
- * @returns {Array<any>} projected coordinates
148
- */
149
- function projectCoords(coords, proj) {
150
- if (typeof coords[0] !== "object") return proj(coords);
151
- return coords.map(function (coord) {
152
- return projectCoords(coord, proj);
153
- });
154
- }
155
-
156
- /**
157
- * Un-Project coordinates to projection
158
- *
159
- * @private
160
- * @param {Array<any>} coords to un-project
161
- * @param {GeoProjection} proj D3 Geo Projection
162
- * @returns {Array<any>} un-projected coordinates
163
- */
164
- function unprojectCoords(coords, proj) {
165
- if (typeof coords[0] !== "object") return proj.invert(coords);
166
- return coords.map(function (coord) {
167
- return unprojectCoords(coord, proj);
168
- });
169
- }
170
-
171
- /**
172
- * Define Azimuthal Equidistant projection
173
- *
174
- * @private
175
- * @param {Geometry|Feature<any>} geojson Base projection on center of GeoJSON
176
- * @returns {GeoProjection} D3 Geo Azimuthal Equidistant Projection
177
- */
178
- function defineProjection(geojson) {
179
- var coords = center__default['default'](geojson).geometry.coordinates;
180
- var rotation = [-coords[0], -coords[1]];
181
- return d3Geo.geoAzimuthalEquidistant().rotate(rotation).scale(helpers.earthRadius);
182
- }
183
-
184
- module.exports = buffer;
185
- module.exports.default = buffer;