@turf/buffer
Advanced tools
Comparing version 4.2.0 to 4.3.0
@@ -18,4 +18,4 @@ /// <reference types="geojson" /> | ||
(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: Feature<any>, radius?: number, unit?: Units, steps?: number): Polygon | Polygons | MultiPolygon; | ||
(feature: Features<any>, radius?: number, unit?: Units, steps?: number): Polygons | MultiPolygons; | ||
(feature: GeoJSON.GeometryObject, radius?: number, unit?: Units, steps?: number): Polygon | Polygons | MultiPolygon; | ||
@@ -22,0 +22,0 @@ (feature: GeoJSON.GeometryCollection, radius?: number, unit?: Units, steps?: number): Polygons | MultiPolygons; |
145
index.js
@@ -0,11 +1,13 @@ | ||
var d3 = require('d3-geo'); | ||
var jsts = require('jsts'); | ||
var meta = require('@turf/meta'); | ||
var circle = require('@turf/circle'); | ||
var center = require('@turf/center'); | ||
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 feature = helpers.feature; | ||
var geomEach = meta.geomEach; | ||
var featureEach = meta.featureEach; | ||
var featureCollection = helpers.featureCollection; | ||
var distanceToDegrees = helpers.distanceToDegrees; | ||
var point = helpers.point; | ||
var radiansToDistance = helpers.radiansToDistance; | ||
var distanceToRadians = helpers.distanceToRadians; | ||
@@ -30,6 +32,4 @@ /** | ||
* }; | ||
* var unit = 'miles'; | ||
* var buffered = turf.buffer(point, 500, 'miles'); | ||
* | ||
* var buffered = turf.buffer(point, 500, unit); | ||
* | ||
* //addToMap | ||
@@ -41,17 +41,24 @@ * var addToMap = [point, buffered] | ||
// validation | ||
if (radius === undefined || radius === null) throw new Error('radius is required'); | ||
if (!geojson) throw new Error('geojson is required'); | ||
if (!radius) throw new Error('radius is required'); | ||
if (radius <= 0) throw new Error('radius must be greater than 0'); | ||
if (steps <= 0) throw new Error('steps must be greater than 0'); | ||
// prevent input mutation | ||
// geojson = JSON.parse(JSON.stringify(geojson)); | ||
// default params | ||
steps = steps || 64; | ||
units = units || 'kilometers'; | ||
var results = []; | ||
switch (geojson.type) { | ||
case 'GeometryCollection': | ||
geomEach(geojson, function (geometry) { | ||
results.push(buffer(geometry, radius, units, steps)); | ||
}); | ||
return featureCollection(results); | ||
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); | ||
}); | ||
featureEach(geojson, function (feature) { | ||
results.push(buffer(feature, radius, units, steps)); | ||
}); | ||
@@ -64,6 +71,6 @@ return featureCollection(results); | ||
/** | ||
* Buffer single Feature | ||
* Buffer single Feature/Geometry | ||
* | ||
* @private | ||
* @param {Feature<any>} feature input to be buffered | ||
* @param {Feature<any>} geojson input to be buffered | ||
* @param {number} radius distance to draw the buffer | ||
@@ -74,33 +81,79 @@ * @param {string} [units='kilometers'] any of the options supported by turf units | ||
*/ | ||
function buffer(feature, radius, units, steps) { | ||
var properties = feature.properties || {}; | ||
var distance = distanceToDegrees(radius, units); | ||
var geometry = (feature.type === 'Feature') ? feature.geometry : feature; | ||
function buffer(geojson, radius, units, steps) { | ||
var properties = geojson.properties || {}; | ||
var geometry = (geojson.type === 'Feature') ? geojson.geometry : geojson; | ||
// Geometry Types faster than jsts | ||
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'); | ||
return circle(geometry.coordinates, radius, steps, units, properties); | ||
} | ||
// Project GeoJSON to Transverse Mercator projection (convert to Meters) | ||
var distance = radiansToDistance(distanceToRadians(radius, units), 'meters'); | ||
var projection = defineProjection(geojson); | ||
var projected = { | ||
type: geometry.type, | ||
coordinates: projectCoords(geometry.coordinates, projection) | ||
}; | ||
// JSTS buffer operation | ||
var reader = new jsts.io.GeoJSONReader(); | ||
var geom = reader.read(projected); | ||
var buffered = geom.buffer(distance); | ||
var writer = new jsts.io.GeoJSONWriter(); | ||
buffered = writer.write(buffered); | ||
// Unproject coordinates (convert to Degrees) | ||
buffered.coordinates = unprojectCoords(buffered.coordinates, projection); | ||
return feature(buffered, properties); | ||
} | ||
/** | ||
* Project coordinates to projection | ||
* | ||
* @private | ||
* @param {Array<any>} coords to project | ||
* @param {GeoProjection} projection D3 Geo Projection | ||
* @returns {Array<any>} projected coordinates | ||
*/ | ||
function projectCoords(coords, projection) { | ||
if (typeof coords[0] !== 'object') return projection(coords); | ||
return coords.map(function (coord) { | ||
return projectCoords(coord, projection); | ||
}); | ||
} | ||
/** | ||
* Un-Project coordinates to projection | ||
* | ||
* @private | ||
* @param {Array<any>} coords to un-project | ||
* @param {GeoProjection} projection D3 Geo Projection | ||
* @returns {Array<any>} un-projected coordinates | ||
*/ | ||
function unprojectCoords(coords, projection) { | ||
if (typeof coords[0] !== 'object') return projection.invert(coords); | ||
return coords.map(function (coord) { | ||
return unprojectCoords(coord, projection); | ||
}); | ||
} | ||
/** | ||
* Define Transverse Mercator projection | ||
* | ||
* @private | ||
* @param {Geometry|Feature<any>} geojson Base projection on center of GeoJSON | ||
* @returns {GeoProjection} D3 Geo Transverse Mercator Projection | ||
*/ | ||
function defineProjection(geojson) { | ||
var coords = center(geojson).geometry.coordinates.reverse(); | ||
var rotate = coords.map(function (coord) { return -coord; }); | ||
var projection = d3.geoTransverseMercator() | ||
.center(coords) | ||
.rotate(rotate) | ||
.scale(6373000); | ||
return projection; | ||
} |
{ | ||
"name": "@turf/buffer", | ||
"version": "4.2.0", | ||
"version": "4.3.0", | ||
"description": "turf buffer module", | ||
@@ -39,3 +39,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@turf/truncate": "^4.2.0", | ||
"@turf/truncate": "^4.3.0", | ||
"benchmark": "^2.1.4", | ||
@@ -47,8 +47,9 @@ "load-json-file": "^2.0.0", | ||
"dependencies": { | ||
"@turf/circle": "^4.2.0", | ||
"@turf/dissolve": "^4.2.0", | ||
"@turf/helpers": "^4.2.0", | ||
"@turf/meta": "^4.2.0", | ||
"@turf/center": "^4.3.0", | ||
"@turf/circle": "^4.3.0", | ||
"@turf/helpers": "^4.3.0", | ||
"@turf/meta": "^4.3.0", | ||
"d3-geo": "^1.6.3", | ||
"jsts": "1.3.0" | ||
} | ||
} |
@@ -25,6 +25,4 @@ # @turf/buffer | ||
}; | ||
var unit = 'miles'; | ||
var buffered = turf.buffer(point, 500, 'miles'); | ||
var buffered = turf.buffer(point, 500, unit); | ||
//addToMap | ||
@@ -31,0 +29,0 @@ var addToMap = [point, buffered] |
10610
163
6
57
+ Added@turf/center@^4.3.0
+ Addedd3-geo@^1.6.3
+ Added@turf/center@4.7.3(transitive)
+ Addedd3-array@1.2.4(transitive)
+ Addedd3-geo@1.12.1(transitive)
- Removed@turf/dissolve@^4.2.0
- Removed@turf/bearing@4.7.3(transitive)
- Removed@turf/boolean-overlap@4.7.3(transitive)
- Removed@turf/boolean-point-on-line@4.7.3(transitive)
- Removed@turf/dissolve@4.7.3(transitive)
- Removed@turf/distance@4.7.3(transitive)
- Removed@turf/line-intersect@4.7.3(transitive)
- Removed@turf/line-overlap@4.7.3(transitive)
- Removed@turf/line-segment@4.7.3(transitive)
- Removed@turf/point-on-line@4.7.3(transitive)
- Removed@turf/union@4.7.3(transitive)
- Removedcall-bind@1.0.8(transitive)
- Removedcall-bind-apply-helpers@1.0.1(transitive)
- Removedcall-bound@1.0.3(transitive)
- Removeddeep-equal@1.1.2(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removeddefine-properties@1.2.1(transitive)
- Removeddunder-proto@1.0.1(transitive)
- Removedes-define-property@1.0.1(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedes-object-atoms@1.1.1(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedfunctions-have-names@1.2.3(transitive)
- Removedgeojson-equality@0.1.6(transitive)
- Removedgeojson-rbush@1.2.0(transitive)
- Removedgeojson-utils@1.1.0(transitive)
- Removedget-closest@0.0.4(transitive)
- Removedget-intrinsic@1.2.7(transitive)
- Removedget-proto@1.0.1(transitive)
- Removedgopd@1.2.0(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-symbols@1.1.0(transitive)
- Removedhas-tostringtag@1.0.2(transitive)
- Removedhasown@2.0.2(transitive)
- Removedis-arguments@1.2.0(transitive)
- Removedis-date-object@1.1.0(transitive)
- Removedis-regex@1.2.1(transitive)
- Removedmath-intrinsics@1.1.0(transitive)
- Removedobject-is@1.1.6(transitive)
- Removedobject-keys@1.1.1(transitive)
- Removedquickselect@1.1.1(transitive)
- Removedrbush@2.0.2(transitive)
- Removedregexp.prototype.flags@1.5.4(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedset-function-name@2.0.2(transitive)
Updated@turf/circle@^4.3.0
Updated@turf/helpers@^4.3.0
Updated@turf/meta@^4.3.0