@turf/buffer
Advanced tools
+2
-2
@@ -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; |
+99
-46
@@ -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; | ||
| } |
+7
-6
| { | ||
| "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" | ||
| } | ||
| } |
+1
-3
@@ -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
18.53%163
38.14%6
20%57
-3.39%+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated
Updated
Updated