From f9adf2b4866c6ce12a29e4e74d24cb16d689ea39 Mon Sep 17 00:00:00 2001 From: rowanwins Date: Thu, 11 May 2017 09:36:01 +1000 Subject: [PATCH 01/20] Initial commit of lineOffset module --- packages/turf-line-offset/LICENSE | 20 ++++++ packages/turf-line-offset/bench.js | 64 +++++++++++++++++ packages/turf-line-offset/index.d.ts | 10 +++ packages/turf-line-offset/index.js | 71 +++++++++++++++++++ packages/turf-line-offset/package.json | 43 +++++++++++ packages/turf-line-offset/test.js | 31 ++++++++ .../test/in/linestring-feature.geojson | 25 +++++++ .../test/in/linestring-geometry.geojson | 21 ++++++ .../test/out/linestring-feature.geojson | 25 +++++++ .../test/out/linestring-geometry.geojson | 25 +++++++ 10 files changed, 335 insertions(+) create mode 100644 packages/turf-line-offset/LICENSE create mode 100644 packages/turf-line-offset/bench.js create mode 100644 packages/turf-line-offset/index.d.ts create mode 100644 packages/turf-line-offset/index.js create mode 100644 packages/turf-line-offset/package.json create mode 100644 packages/turf-line-offset/test.js create mode 100644 packages/turf-line-offset/test/in/linestring-feature.geojson create mode 100644 packages/turf-line-offset/test/in/linestring-geometry.geojson create mode 100644 packages/turf-line-offset/test/out/linestring-feature.geojson create mode 100644 packages/turf-line-offset/test/out/linestring-geometry.geojson diff --git a/packages/turf-line-offset/LICENSE b/packages/turf-line-offset/LICENSE new file mode 100644 index 0000000000..96ce51b76f --- /dev/null +++ b/packages/turf-line-offset/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2017 TurfJS + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/packages/turf-line-offset/bench.js b/packages/turf-line-offset/bench.js new file mode 100644 index 0000000000..1175c82667 --- /dev/null +++ b/packages/turf-line-offset/bench.js @@ -0,0 +1,64 @@ +var along = require('./'); +var Benchmark = require('benchmark'); +var fs = require('fs'); + +var line = { + type: "Feature", + properties: {}, + geometry: { + type: "LineString", + coordinates: [ + [ + -77.0316696166992, + 38.878605901789236 + ], + [ + -77.02960968017578, + 38.88194668656296 + ], + [ + -77.02033996582031, + 38.88408470638821 + ], + [ + -77.02566146850586, + 38.885821800123196 + ], + [ + -77.02188491821289, + 38.88956308852534 + ], + [ + -77.01982498168944, + 38.89236892551996 + ] + ] + } +}; + +var route = JSON.parse(fs.readFileSync(__dirname + '/test/fixtures/route.geojson')); + +var suite = new Benchmark.Suite('turf-along'); +suite + .add('turf-along',function () { + along(line, 1, 'miles'); + }) + .add('turf-along#route 1 mile',function () { + along(route, 1, 'miles'); + }) + .add('turf-along#route 10 miles',function () { + along(route, 10, 'miles'); + }) + .add('turf-along#route 50 miles',function () { + along(route, 50, 'miles'); + }) + .add('turf-along#route 100 miles',function () { + along(route, 100, 'miles'); + }) + .on('cycle', function (event) { + console.log(String(event.target)); + }) + .on('complete', function () { + + }) + .run(); diff --git a/packages/turf-line-offset/index.d.ts b/packages/turf-line-offset/index.d.ts new file mode 100644 index 0000000000..4d8f3cc4ed --- /dev/null +++ b/packages/turf-line-offset/index.d.ts @@ -0,0 +1,10 @@ +/// + +type LineString = GeoJSON.Feature; + +/** + * http://turfjs.org/docs/#lineOffset + */ +declare function lineOffset(line: LineString | GeoJSON.LineString, distance: number, units?: string): LineString; +declare namespace lineOffset { } +export = lineOffset; diff --git a/packages/turf-line-offset/index.js b/packages/turf-line-offset/index.js new file mode 100644 index 0000000000..f076efa135 --- /dev/null +++ b/packages/turf-line-offset/index.js @@ -0,0 +1,71 @@ +var getCoords = require('@turf/invariant').getCoords; +var coordEach = require('@turf/meta').coordEach; +var intersection = require('intersection'); +var helpers = require('@turf/helpers') +var lineString = helpers.lineString; +var distanceToDegrees = helpers.distanceToDegrees; + +/** + * Takes a {@link LineString|line} and returns a {@link LineString|line} at offset by the specified distance. + * + * @name lineOffset + * @param {Feature|Geometry} line input line + * @param {number} offset distance to offset the line + * @param {string} [units=kilometers] can be degrees, radians, miles, kilometers, inches, yards, meters + * @returns {Feature} Line offset from the input line + * @example + * var line = { + * "type": "Feature", + * "properties": {}, + * "geometry": { + * "type": "LineString", + * "coordinates": [[-83, 30], [-84, 36], [-78, 41]] + * } + * }; + * + * var offsetLine = turf.lineOffset(line, 2, 'miles'); + * + * //addToMap + * var addToMap = [offsetLine, line] + */ +module.exports = function (line, offset, units) { + var segments = []; + var offsetDegrees = distanceToDegrees(offset, units); + var coords = getCoords(line); + + coordEach(line, function (currentCoords, currentIndex) { + if (currentIndex !== coords.length - 1) { + var outCoords = processSegment(currentCoords, coords[currentIndex + 1], offsetDegrees); + segments.push(outCoords); + } + }); + segments.forEach(function (segment, index) { + if (index !== segments.length - 1) { + var seg2Coords = segments[index + 1]; + var seg1 = {start:{x:segment[0][0], y:segment[0][1]}, end:{x:segment[1][0], y:segment[1][1]}}; + var seg2 = {start:{x:seg2Coords[0][0], y:seg2Coords[0][1]}, end:{x:seg2Coords[1][0], y:seg2Coords[1][1]}}; + var int = intersection.intersect(seg1, seg2); + segment[1][0] = int.x; + segment[1][1] = int.y; + seg2Coords[0][0] = int.x; + seg2Coords[0][1] = int.y; + } + }); + var finalCoords = segments.map(function (segment, index) { + return segment[0]; + }) + finalCoords.push(segments[segments.length -1][1]); + return lineString(finalCoords); +}; + +// Inspiration taken from http://stackoverflow.com/questions/2825412/draw-a-parallel-line +function processSegment (point1, point2, offset) { + var L = Math.sqrt((point1[0] - point2[0]) * (point1[0] - point2[0]) + (point1[1] - point2[1]) * (point1[1] - point2[1])) + var offsetPixels = offset + + var out1x = point1[0] + offsetPixels * (point2[1] - point1[1]) / L + var out2x = point2[0] + offsetPixels * (point2[1] - point1[1]) / L + var out1y = point1[1] + offsetPixels * (point1[0] - point2[0]) / L + var out2y = point2[1] + offsetPixels * (point1[0] - point2[0]) / L + return [[out1x, out1y], [out2x, out2y]] +} \ No newline at end of file diff --git a/packages/turf-line-offset/package.json b/packages/turf-line-offset/package.json new file mode 100644 index 0000000000..aeca54efab --- /dev/null +++ b/packages/turf-line-offset/package.json @@ -0,0 +1,43 @@ +{ + "name": "@turf/line-offset", + "version": "4.1.0", + "description": "turf line offset module", + "main": "index.js", + "types": "index.d.ts", + "files": [ + "index.js", + "index.d.ts" + ], + "scripts": { + "test": "node test.js", + "bench": "node bench.js" + }, + "repository": { + "type": "git", + "url": "git://github.com/Turfjs/turf.git" + }, + "keywords": [ + "along", + "line", + "linestring", + "turf", + "distance" + ], + "author": "Turf Authors", + "license": "MIT", + "bugs": { + "url": "https://github.com/Turfjs/turf/issues" + }, + "homepage": "https://github.com/Turfjs/turf", + "devDependencies": { + "benchmark": "^1.0.0", + "tape": "^3.5.0", + "@turf/helpers": "^4.1.0" + }, + "dependencies": { + "@turf/helpers": "^4.1.0", + "@turf/invariant": "^4.2.0", + "@turf/meta": "^4.2.0", + "intersection": "0.0.1" + } +} diff --git a/packages/turf-line-offset/test.js b/packages/turf-line-offset/test.js new file mode 100644 index 0000000000..60410bd4a5 --- /dev/null +++ b/packages/turf-line-offset/test.js @@ -0,0 +1,31 @@ +const test = require('tape'); +const fs = require('fs'); +const path = require('path'); +const load = require('load-json-file'); +const write = require('write-json-file'); +const lineOffset = require('./'); +const helpers = require('@turf/helpers'); +const feature = helpers.feature; +const fc = helpers.featureCollection; + +const directories = { + in: path.join(__dirname, 'test', 'in') + path.sep, + out: path.join(__dirname, 'test', 'out') + path.sep +}; + +const fixtures = fs.readdirSync(directories.in).map(filename => { + return { + filename, + name: path.parse(filename).name, + geojson: load.sync(directories.in + filename) + }; +}); + +test('turf-lineOffset', t => { + for (const {name, geojson} of fixtures) { + const results = lineOffset(geojson, 10, 'kilometers'); + if (process.env.REGEN) write.sync(directories.out + name + '.geojson', results); + t.deepEqual(results, load.sync(directories.out + name + '.geojson'), name); + } + t.end(); +}); diff --git a/packages/turf-line-offset/test/in/linestring-feature.geojson b/packages/turf-line-offset/test/in/linestring-feature.geojson new file mode 100644 index 0000000000..adec6f644d --- /dev/null +++ b/packages/turf-line-offset/test/in/linestring-feature.geojson @@ -0,0 +1,25 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 20.390625, + 62.2679226294176 + ], + [ + 27.0703125, + 56.36525013685606 + ], + [ + 22.5, + 53.54030739150022 + ], + [ + 26.015625, + 49.83798245308484 + ] + ] + } +} \ No newline at end of file diff --git a/packages/turf-line-offset/test/in/linestring-geometry.geojson b/packages/turf-line-offset/test/in/linestring-geometry.geojson new file mode 100644 index 0000000000..316e301df5 --- /dev/null +++ b/packages/turf-line-offset/test/in/linestring-geometry.geojson @@ -0,0 +1,21 @@ +{ + "type": "LineString", + "coordinates": [ + [ + 20.390625, + 62.2679226294176 + ], + [ + 27.0703125, + 56.36525013685606 + ], + [ + 22.5, + 53.54030739150022 + ], + [ + 26.015625, + 49.83798245308484 + ] + ] +} \ No newline at end of file diff --git a/packages/turf-line-offset/test/out/linestring-feature.geojson b/packages/turf-line-offset/test/out/linestring-feature.geojson new file mode 100644 index 0000000000..6591ff0ba6 --- /dev/null +++ b/packages/turf-line-offset/test/out/linestring-feature.geojson @@ -0,0 +1,25 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 20.3310925287305, + 62.20055343272168 + ], + [ + 26.92004538337184, + 56.37806087302091 + ], + [ + 22.358632609895093, + 53.558619112089545 + ], + [ + 25.950430749121104, + 49.77607580197814 + ] + ] + } +} diff --git a/packages/turf-line-offset/test/out/linestring-geometry.geojson b/packages/turf-line-offset/test/out/linestring-geometry.geojson new file mode 100644 index 0000000000..6591ff0ba6 --- /dev/null +++ b/packages/turf-line-offset/test/out/linestring-geometry.geojson @@ -0,0 +1,25 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 20.3310925287305, + 62.20055343272168 + ], + [ + 26.92004538337184, + 56.37806087302091 + ], + [ + 22.358632609895093, + 53.558619112089545 + ], + [ + 25.950430749121104, + 49.77607580197814 + ] + ] + } +} From d8e1f4b01dbfc8f6c184b6a477c0ba75d7fe4cf9 Mon Sep 17 00:00:00 2001 From: rowanwins Date: Thu, 11 May 2017 09:42:47 +1000 Subject: [PATCH 02/20] Fix linting --- packages/turf-line-offset/index.js | 55 +++++++++++++------------- packages/turf-line-offset/package.json | 3 +- packages/turf-line-offset/test.js | 3 -- 3 files changed, 28 insertions(+), 33 deletions(-) diff --git a/packages/turf-line-offset/index.js b/packages/turf-line-offset/index.js index f076efa135..66eead2d46 100644 --- a/packages/turf-line-offset/index.js +++ b/packages/turf-line-offset/index.js @@ -1,7 +1,7 @@ var getCoords = require('@turf/invariant').getCoords; var coordEach = require('@turf/meta').coordEach; var intersection = require('intersection'); -var helpers = require('@turf/helpers') +var helpers = require('@turf/helpers'); var lineString = helpers.lineString; var distanceToDegrees = helpers.distanceToDegrees; @@ -34,38 +34,37 @@ module.exports = function (line, offset, units) { var coords = getCoords(line); coordEach(line, function (currentCoords, currentIndex) { - if (currentIndex !== coords.length - 1) { - var outCoords = processSegment(currentCoords, coords[currentIndex + 1], offsetDegrees); - segments.push(outCoords); - } + if (currentIndex !== coords.length - 1) { + var outCoords = processSegment(currentCoords, coords[currentIndex + 1], offsetDegrees); + segments.push(outCoords); + } }); segments.forEach(function (segment, index) { - if (index !== segments.length - 1) { - var seg2Coords = segments[index + 1]; - var seg1 = {start:{x:segment[0][0], y:segment[0][1]}, end:{x:segment[1][0], y:segment[1][1]}}; - var seg2 = {start:{x:seg2Coords[0][0], y:seg2Coords[0][1]}, end:{x:seg2Coords[1][0], y:seg2Coords[1][1]}}; - var int = intersection.intersect(seg1, seg2); - segment[1][0] = int.x; - segment[1][1] = int.y; - seg2Coords[0][0] = int.x; - seg2Coords[0][1] = int.y; - } + if (index !== segments.length - 1) { + var seg2Coords = segments[index + 1]; + var seg1 = {start: {x: segment[0][0], y: segment[0][1]}, end: {x: segment[1][0], y: segment[1][1]}}; + var seg2 = {start: {x: seg2Coords[0][0], y: seg2Coords[0][1]}, end: {x: seg2Coords[1][0], y: seg2Coords[1][1]}}; + var int = intersection.intersect(seg1, seg2); + segment[1][0] = int.x; + segment[1][1] = int.y; + seg2Coords[0][0] = int.x; + seg2Coords[0][1] = int.y; + } }); - var finalCoords = segments.map(function (segment, index) { - return segment[0]; - }) - finalCoords.push(segments[segments.length -1][1]); + var finalCoords = segments.map(function (segment) { + return segment[0]; + }); + finalCoords.push(segments[segments.length - 1][1]); return lineString(finalCoords); }; // Inspiration taken from http://stackoverflow.com/questions/2825412/draw-a-parallel-line -function processSegment (point1, point2, offset) { - var L = Math.sqrt((point1[0] - point2[0]) * (point1[0] - point2[0]) + (point1[1] - point2[1]) * (point1[1] - point2[1])) - var offsetPixels = offset +function processSegment(point1, point2, offset) { + var L = Math.sqrt((point1[0] - point2[0]) * (point1[0] - point2[0]) + (point1[1] - point2[1]) * (point1[1] - point2[1])); - var out1x = point1[0] + offsetPixels * (point2[1] - point1[1]) / L - var out2x = point2[0] + offsetPixels * (point2[1] - point1[1]) / L - var out1y = point1[1] + offsetPixels * (point1[0] - point2[0]) / L - var out2y = point2[1] + offsetPixels * (point1[0] - point2[0]) / L - return [[out1x, out1y], [out2x, out2y]] -} \ No newline at end of file + var out1x = point1[0] + offset * (point2[1] - point1[1]) / L; + var out2x = point2[0] + offset * (point2[1] - point1[1]) / L; + var out1y = point1[1] + offset * (point1[0] - point2[0]) / L; + var out2y = point2[1] + offset * (point1[0] - point2[0]) / L; + return [[out1x, out1y], [out2x, out2y]]; +} diff --git a/packages/turf-line-offset/package.json b/packages/turf-line-offset/package.json index aeca54efab..16d0ee4bba 100644 --- a/packages/turf-line-offset/package.json +++ b/packages/turf-line-offset/package.json @@ -17,11 +17,10 @@ "url": "git://github.com/Turfjs/turf.git" }, "keywords": [ - "along", "line", "linestring", "turf", - "distance" + "offset" ], "author": "Turf Authors", "license": "MIT", diff --git a/packages/turf-line-offset/test.js b/packages/turf-line-offset/test.js index 60410bd4a5..13df94f3b9 100644 --- a/packages/turf-line-offset/test.js +++ b/packages/turf-line-offset/test.js @@ -4,9 +4,6 @@ const path = require('path'); const load = require('load-json-file'); const write = require('write-json-file'); const lineOffset = require('./'); -const helpers = require('@turf/helpers'); -const feature = helpers.feature; -const fc = helpers.featureCollection; const directories = { in: path.join(__dirname, 'test', 'in') + path.sep, From 8be7b24a78502dcd02758b49e89ca3ca906e785f Mon Sep 17 00:00:00 2001 From: rowanwins Date: Thu, 11 May 2017 10:35:57 +1000 Subject: [PATCH 03/20] Fis dependencies and jsdocs --- packages/turf-line-offset/bench.js | 85 ++++++++------------------ packages/turf-line-offset/index.d.ts | 3 +- packages/turf-line-offset/index.js | 4 +- packages/turf-line-offset/package.json | 12 +++- 4 files changed, 38 insertions(+), 66 deletions(-) diff --git a/packages/turf-line-offset/bench.js b/packages/turf-line-offset/bench.js index 1175c82667..cc4863e853 100644 --- a/packages/turf-line-offset/bench.js +++ b/packages/turf-line-offset/bench.js @@ -1,64 +1,29 @@ -var along = require('./'); -var Benchmark = require('benchmark'); -var fs = require('fs'); +const Benchmark = require('benchmark'); +const path = require('path'); +const fs = require('fs'); +const load = require('load-json-file'); +const lineOffset = require('./'); -var line = { - type: "Feature", - properties: {}, - geometry: { - type: "LineString", - coordinates: [ - [ - -77.0316696166992, - 38.878605901789236 - ], - [ - -77.02960968017578, - 38.88194668656296 - ], - [ - -77.02033996582031, - 38.88408470638821 - ], - [ - -77.02566146850586, - 38.885821800123196 - ], - [ - -77.02188491821289, - 38.88956308852534 - ], - [ - -77.01982498168944, - 38.89236892551996 - ] - ] - } -}; +const directory = path.join(__dirname, 'test', 'in') + path.sep; +const fixtures = fs.readdirSync(directory).map(filename => { + return { + filename, + name: path.parse(filename).name, + geojson: load.sync(directory + filename) + }; +}); -var route = JSON.parse(fs.readFileSync(__dirname + '/test/fixtures/route.geojson')); +/** + * Benchmark Results + * linestring-feature x 640,105 ops/sec ±1.90% (87 runs sampled) + * linestring-geometry x 633,886 ops/sec ±1.53% (92 runs sampled) + */ +const suite = new Benchmark.Suite('turf-line-offset'); +for (const {name, geojson} of fixtures) { + suite.add(name, () => lineOffset(geojson, 100, 'meters')); +} -var suite = new Benchmark.Suite('turf-along'); suite - .add('turf-along',function () { - along(line, 1, 'miles'); - }) - .add('turf-along#route 1 mile',function () { - along(route, 1, 'miles'); - }) - .add('turf-along#route 10 miles',function () { - along(route, 10, 'miles'); - }) - .add('turf-along#route 50 miles',function () { - along(route, 50, 'miles'); - }) - .add('turf-along#route 100 miles',function () { - along(route, 100, 'miles'); - }) - .on('cycle', function (event) { - console.log(String(event.target)); - }) - .on('complete', function () { - - }) - .run(); + .on('cycle', e => { console.log(String(e.target)); }) + .on('complete', () => {}) + .run(); diff --git a/packages/turf-line-offset/index.d.ts b/packages/turf-line-offset/index.d.ts index 4d8f3cc4ed..a79aee9c7b 100644 --- a/packages/turf-line-offset/index.d.ts +++ b/packages/turf-line-offset/index.d.ts @@ -1,10 +1,11 @@ /// type LineString = GeoJSON.Feature; +import {Units} from '@turf/helpers'; /** * http://turfjs.org/docs/#lineOffset */ -declare function lineOffset(line: LineString | GeoJSON.LineString, distance: number, units?: string): LineString; +declare function lineOffset(line: LineString | GeoJSON.LineString, distance: number, units?: Units): LineString; declare namespace lineOffset { } export = lineOffset; diff --git a/packages/turf-line-offset/index.js b/packages/turf-line-offset/index.js index 66eead2d46..97227047aa 100644 --- a/packages/turf-line-offset/index.js +++ b/packages/turf-line-offset/index.js @@ -9,7 +9,7 @@ var distanceToDegrees = helpers.distanceToDegrees; * Takes a {@link LineString|line} and returns a {@link LineString|line} at offset by the specified distance. * * @name lineOffset - * @param {Feature|Geometry} line input line + * @param {Geometry|Feature} line input line * @param {number} offset distance to offset the line * @param {string} [units=kilometers] can be degrees, radians, miles, kilometers, inches, yards, meters * @returns {Feature} Line offset from the input line @@ -55,7 +55,7 @@ module.exports = function (line, offset, units) { return segment[0]; }); finalCoords.push(segments[segments.length - 1][1]); - return lineString(finalCoords); + return lineString(finalCoords, line.properties); }; // Inspiration taken from http://stackoverflow.com/questions/2825412/draw-a-parallel-line diff --git a/packages/turf-line-offset/package.json b/packages/turf-line-offset/package.json index 16d0ee4bba..64a377ad80 100644 --- a/packages/turf-line-offset/package.json +++ b/packages/turf-line-offset/package.json @@ -23,15 +23,21 @@ "offset" ], "author": "Turf Authors", + "contributors": [ + "Rowan Winsemius <@rowanwins>", + "Denis Carriere <@DenisCarriere>" + ], "license": "MIT", "bugs": { "url": "https://github.com/Turfjs/turf/issues" }, "homepage": "https://github.com/Turfjs/turf", "devDependencies": { - "benchmark": "^1.0.0", - "tape": "^3.5.0", - "@turf/helpers": "^4.1.0" + "@turf/helpers": "^4.1.0", + "benchmark": "^2.1.4", + "load-json-file": "^2.0.0", + "tape": "^4.6.3", + "write-json-file": "^2.0.0" }, "dependencies": { "@turf/helpers": "^4.1.0", From 275f29f281b9d07428102d78023886249633acdb Mon Sep 17 00:00:00 2001 From: rowanwins Date: Thu, 11 May 2017 23:39:16 +1000 Subject: [PATCH 04/20] Updated looping approach. Fix test truncate. --- packages/turf-line-offset/bench.js | 4 +- packages/turf-line-offset/index.js | 41 +++++++++---------- packages/turf-line-offset/package.json | 1 + packages/turf-line-offset/test.js | 3 +- .../test/out/linestring-feature.geojson | 16 ++++---- .../test/out/linestring-geometry.geojson | 16 ++++---- 6 files changed, 41 insertions(+), 40 deletions(-) diff --git a/packages/turf-line-offset/bench.js b/packages/turf-line-offset/bench.js index cc4863e853..57ade45ac6 100644 --- a/packages/turf-line-offset/bench.js +++ b/packages/turf-line-offset/bench.js @@ -15,8 +15,8 @@ const fixtures = fs.readdirSync(directory).map(filename => { /** * Benchmark Results - * linestring-feature x 640,105 ops/sec ±1.90% (87 runs sampled) - * linestring-geometry x 633,886 ops/sec ±1.53% (92 runs sampled) + * linestring-feature x 293,412 ops/sec ±1.42% (76 runs sampled) + * linestring-geometry x 288,033 ops/sec ±2.62% (79 runs sampled) */ const suite = new Benchmark.Suite('turf-line-offset'); for (const {name, geojson} of fixtures) { diff --git a/packages/turf-line-offset/index.js b/packages/turf-line-offset/index.js index 97227047aa..2d330c1631 100644 --- a/packages/turf-line-offset/index.js +++ b/packages/turf-line-offset/index.js @@ -32,29 +32,28 @@ module.exports = function (line, offset, units) { var segments = []; var offsetDegrees = distanceToDegrees(offset, units); var coords = getCoords(line); - - coordEach(line, function (currentCoords, currentIndex) { - if (currentIndex !== coords.length - 1) { - var outCoords = processSegment(currentCoords, coords[currentIndex + 1], offsetDegrees); - segments.push(outCoords); - } - }); - segments.forEach(function (segment, index) { - if (index !== segments.length - 1) { - var seg2Coords = segments[index + 1]; - var seg1 = {start: {x: segment[0][0], y: segment[0][1]}, end: {x: segment[1][0], y: segment[1][1]}}; - var seg2 = {start: {x: seg2Coords[0][0], y: seg2Coords[0][1]}, end: {x: seg2Coords[1][0], y: seg2Coords[1][1]}}; - var int = intersection.intersect(seg1, seg2); - segment[1][0] = int.x; - segment[1][1] = int.y; - seg2Coords[0][0] = int.x; - seg2Coords[0][1] = int.y; + var finalCoords = []; + coordEach(line, function (currentCoords, index) { + if (index !== coords.length - 1) { + var segment = processSegment(currentCoords, coords[index + 1], offsetDegrees); + segments.push(segment); + if (index > 0) { + var seg2Coords = segments[index - 1]; + var seg1 = {start: {x: segment[0][0], y: segment[0][1]}, end: {x: segment[1][0], y: segment[1][1]}}; + var seg2 = {start: {x: seg2Coords[0][0], y: seg2Coords[0][1]}, end: {x: seg2Coords[1][0], y: seg2Coords[1][1]}}; + var int = intersection.intersect(seg1, seg2); + seg2Coords[1][0] = int.x; + seg2Coords[1][1] = int.y; + segment[0][0] = int.x; + segment[0][1] = int.y; + finalCoords.push(seg2Coords[0]); + if (index === coords.length - 2) { + finalCoords.push(segment[0]); + finalCoords.push(segment[1]); + } + } } }); - var finalCoords = segments.map(function (segment) { - return segment[0]; - }); - finalCoords.push(segments[segments.length - 1][1]); return lineString(finalCoords, line.properties); }; diff --git a/packages/turf-line-offset/package.json b/packages/turf-line-offset/package.json index 64a377ad80..fa641cf13d 100644 --- a/packages/turf-line-offset/package.json +++ b/packages/turf-line-offset/package.json @@ -34,6 +34,7 @@ "homepage": "https://github.com/Turfjs/turf", "devDependencies": { "@turf/helpers": "^4.1.0", + "@turf/truncate": "^4.2.0", "benchmark": "^2.1.4", "load-json-file": "^2.0.0", "tape": "^4.6.3", diff --git a/packages/turf-line-offset/test.js b/packages/turf-line-offset/test.js index 13df94f3b9..3d84c03a74 100644 --- a/packages/turf-line-offset/test.js +++ b/packages/turf-line-offset/test.js @@ -4,6 +4,7 @@ const path = require('path'); const load = require('load-json-file'); const write = require('write-json-file'); const lineOffset = require('./'); +const truncate = require('@turf/truncate'); const directories = { in: path.join(__dirname, 'test', 'in') + path.sep, @@ -20,7 +21,7 @@ const fixtures = fs.readdirSync(directories.in).map(filename => { test('turf-lineOffset', t => { for (const {name, geojson} of fixtures) { - const results = lineOffset(geojson, 10, 'kilometers'); + const results = truncate(lineOffset(geojson, 10, 'kilometers')); if (process.env.REGEN) write.sync(directories.out + name + '.geojson', results); t.deepEqual(results, load.sync(directories.out + name + '.geojson'), name); } diff --git a/packages/turf-line-offset/test/out/linestring-feature.geojson b/packages/turf-line-offset/test/out/linestring-feature.geojson index 6591ff0ba6..4fcf87944c 100644 --- a/packages/turf-line-offset/test/out/linestring-feature.geojson +++ b/packages/turf-line-offset/test/out/linestring-feature.geojson @@ -5,20 +5,20 @@ "type": "LineString", "coordinates": [ [ - 20.3310925287305, - 62.20055343272168 + 20.331093, + 62.200553 ], [ - 26.92004538337184, - 56.37806087302091 + 26.920045, + 56.378061 ], [ - 22.358632609895093, - 53.558619112089545 + 22.358633, + 53.558619 ], [ - 25.950430749121104, - 49.77607580197814 + 25.950431, + 49.776076 ] ] } diff --git a/packages/turf-line-offset/test/out/linestring-geometry.geojson b/packages/turf-line-offset/test/out/linestring-geometry.geojson index 6591ff0ba6..4fcf87944c 100644 --- a/packages/turf-line-offset/test/out/linestring-geometry.geojson +++ b/packages/turf-line-offset/test/out/linestring-geometry.geojson @@ -5,20 +5,20 @@ "type": "LineString", "coordinates": [ [ - 20.3310925287305, - 62.20055343272168 + 20.331093, + 62.200553 ], [ - 26.92004538337184, - 56.37806087302091 + 26.920045, + 56.378061 ], [ - 22.358632609895093, - 53.558619112089545 + 22.358633, + 53.558619 ], [ - 25.950430749121104, - 49.77607580197814 + 25.950431, + 49.776076 ] ] } From 5e08ff5eaa48553fef04d2c172c32b70c9738b40 Mon Sep 17 00:00:00 2001 From: rowanwins Date: Fri, 12 May 2017 11:09:53 +1000 Subject: [PATCH 05/20] Add handling for straight lines and lines with only 1 segment. Inc tests. --- packages/turf-line-offset/index.js | 18 +- .../test/in/linestring-long.geojson | 169 ++++++++++++++++++ .../in/linestring-singleSegmentOnly.geojson | 17 ++ .../test/in/linestring-straight.geojson | 21 +++ .../test/out/linestring-long.geojson | 169 ++++++++++++++++++ .../out/linestring-singleSegmentOnly.geojson | 17 ++ .../test/out/linestring-straight.geojson | 21 +++ 7 files changed, 428 insertions(+), 4 deletions(-) create mode 100644 packages/turf-line-offset/test/in/linestring-long.geojson create mode 100644 packages/turf-line-offset/test/in/linestring-singleSegmentOnly.geojson create mode 100644 packages/turf-line-offset/test/in/linestring-straight.geojson create mode 100644 packages/turf-line-offset/test/out/linestring-long.geojson create mode 100644 packages/turf-line-offset/test/out/linestring-singleSegmentOnly.geojson create mode 100644 packages/turf-line-offset/test/out/linestring-straight.geojson diff --git a/packages/turf-line-offset/index.js b/packages/turf-line-offset/index.js index 2d330c1631..b8d05a8531 100644 --- a/packages/turf-line-offset/index.js +++ b/packages/turf-line-offset/index.js @@ -42,16 +42,26 @@ module.exports = function (line, offset, units) { var seg1 = {start: {x: segment[0][0], y: segment[0][1]}, end: {x: segment[1][0], y: segment[1][1]}}; var seg2 = {start: {x: seg2Coords[0][0], y: seg2Coords[0][1]}, end: {x: seg2Coords[1][0], y: seg2Coords[1][1]}}; var int = intersection.intersect(seg1, seg2); - seg2Coords[1][0] = int.x; - seg2Coords[1][1] = int.y; - segment[0][0] = int.x; - segment[0][1] = int.y; + + // Handling for line segments that aren't straight + if (int !== false) { + seg2Coords[1][0] = int.x; + seg2Coords[1][1] = int.y; + segment[0][0] = int.x; + segment[0][1] = int.y; + } + finalCoords.push(seg2Coords[0]); if (index === coords.length - 2) { finalCoords.push(segment[0]); finalCoords.push(segment[1]); } } + // Handling for lines that only have 1 segment + if (coords.length === 2) { + finalCoords.push(segment[0]); + finalCoords.push(segment[1]); + } } }); return lineString(finalCoords, line.properties); diff --git a/packages/turf-line-offset/test/in/linestring-long.geojson b/packages/turf-line-offset/test/in/linestring-long.geojson new file mode 100644 index 0000000000..6c40f4101c --- /dev/null +++ b/packages/turf-line-offset/test/in/linestring-long.geojson @@ -0,0 +1,169 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -24.2578125, + 41.244772343082076 + ], + [ + -6.328125, + 45.336701909968134 + ], + [ + 0.703125, + 49.83798245308484 + ], + [ + 2.8125, + 44.33956524809713 + ], + [ + -6.328125, + 39.095962936305476 + ], + [ + -5.44921875, + 36.4566360115962 + ], + [ + -0.17578125, + 37.3002752813443 + ], + [ + 1.58203125, + 39.36827914916014 + ], + [ + 3.33984375, + 40.58058466412761 + ], + [ + 6.15234375, + 41.244772343082076 + ], + [ + 7.3828125, + 39.50404070558415 + ], + [ + 4.5703125, + 37.020098201368114 + ], + [ + 3.69140625, + 35.31736632923788 + ], + [ + 4.921875, + 31.653381399664 + ], + [ + 8.61328125, + 32.54681317351514 + ], + [ + 10.72265625, + 34.016241889667015 + ], + [ + 10.546875, + 36.03133177633187 + ], + [ + 10.37109375, + 38.272688535980976 + ], + [ + 10.01953125, + 39.232253141714885 + ], + [ + 9.4921875, + 40.58058466412761 + ], + [ + 9.31640625, + 41.902277040963696 + ], + [ + 11.42578125, + 44.08758502824516 + ], + [ + 13.53515625, + 43.96119063892024 + ], + [ + 14.765625, + 42.8115217450979 + ], + [ + 15.8203125, + 37.71859032558816 + ], + [ + 15.644531250000002, + 36.1733569352216 + ], + [ + 14.94140625, + 34.59704151614417 + ], + [ + 13.359375, + 31.952162238024975 + ], + [ + 11.77734375, + 22.755920681486405 + ], + [ + 15.8203125, + 29.22889003019423 + ], + [ + 16.875, + 31.50362930577303 + ], + [ + 17.2265625, + 33.43144133557529 + ], + [ + 17.402343749999996, + 35.02999636902566 + ], + [ + 19.51171875, + 41.244772343082076 + ], + [ + 21.4453125, + 41.11246878918088 + ], + [ + 20.7421875, + 38.8225909761771 + ], + [ + 21.09375, + 36.1733569352216 + ], + [ + 21.26953125, + 34.016241889667015 + ], + [ + 21.26953125, + 31.353636941500987 + ], + [ + 26.015625, + 33.284619968887675 + ] + ] + } +} \ No newline at end of file diff --git a/packages/turf-line-offset/test/in/linestring-singleSegmentOnly.geojson b/packages/turf-line-offset/test/in/linestring-singleSegmentOnly.geojson new file mode 100644 index 0000000000..71783eedb3 --- /dev/null +++ b/packages/turf-line-offset/test/in/linestring-singleSegmentOnly.geojson @@ -0,0 +1,17 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 1, + 1 + ], + [ + 1, + 10 + ] + ] + } +} diff --git a/packages/turf-line-offset/test/in/linestring-straight.geojson b/packages/turf-line-offset/test/in/linestring-straight.geojson new file mode 100644 index 0000000000..487137fe49 --- /dev/null +++ b/packages/turf-line-offset/test/in/linestring-straight.geojson @@ -0,0 +1,21 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 1, + -10 + ], + [ + 1, + 1 + ], + [ + 1, + 10 + ] + ] + } +} diff --git a/packages/turf-line-offset/test/out/linestring-long.geojson b/packages/turf-line-offset/test/out/linestring-long.geojson new file mode 100644 index 0000000000..d67127afe6 --- /dev/null +++ b/packages/turf-line-offset/test/out/linestring-long.geojson @@ -0,0 +1,169 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -24.237809, + 41.157122 + ], + [ + -6.292847, + 45.252538 + ], + [ + 0.658696, + 49.702791 + ], + [ + 2.700986, + 44.379241 + ], + [ + -6.436663, + 39.137346 + ], + [ + -5.510398, + 36.355801 + ], + [ + -0.128788, + 37.216746 + ], + [ + 1.642845, + 39.301009 + ], + [ + 3.376965, + 40.496974 + ], + [ + 6.113954, + 41.143329 + ], + [ + 7.262832, + 39.518023 + ], + [ + 4.498159, + 37.07632 + ], + [ + 3.594071, + 35.324803 + ], + [ + 4.862895, + 31.546607 + ], + [ + 8.650827, + 32.463401 + ], + [ + 10.816742, + 33.972216 + ], + [ + 10.636473, + 36.038753 + ], + [ + 10.459757, + 38.292025 + ], + [ + 10.103613, + 39.264094 + ], + [ + 9.579874, + 40.60321 + ], + [ + 9.411251, + 41.871083 + ], + [ + 11.461721, + 43.995366 + ], + [ + 13.497446, + 43.873385 + ], + [ + 14.68338, + 42.765326 + ], + [ + 15.729358, + 37.714452 + ], + [ + 15.556772, + 36.197307 + ], + [ + 14.861491, + 34.638577 + ], + [ + 13.273615, + 31.983926 + ], + [ + 11.611149, + 22.320126 + ], + [ + 15.899525, + 29.186003 + ], + [ + 16.961388, + 31.476217 + ], + [ + 17.315579, + 33.418447 + ], + [ + 17.490635, + 35.010404 + ], + [ + 19.574614, + 41.150355 + ], + [ + 21.3261, + 41.030512 + ], + [ + 20.650483, + 38.830219 + ], + [ + 21.004328, + 36.163786 + ], + [ + 21.179627, + 34.012585 + ], + [ + 21.179627, + 31.219999 + ], + [ + 26.049506, + 33.201345 + ] + ] + } +} diff --git a/packages/turf-line-offset/test/out/linestring-singleSegmentOnly.geojson b/packages/turf-line-offset/test/out/linestring-singleSegmentOnly.geojson new file mode 100644 index 0000000000..c507515eab --- /dev/null +++ b/packages/turf-line-offset/test/out/linestring-singleSegmentOnly.geojson @@ -0,0 +1,17 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 1.089904, + 1 + ], + [ + 1.089904, + 10 + ] + ] + } +} diff --git a/packages/turf-line-offset/test/out/linestring-straight.geojson b/packages/turf-line-offset/test/out/linestring-straight.geojson new file mode 100644 index 0000000000..8868b3114b --- /dev/null +++ b/packages/turf-line-offset/test/out/linestring-straight.geojson @@ -0,0 +1,21 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 1.089904, + -10 + ], + [ + 1.089904, + 1 + ], + [ + 1.089904, + 10 + ] + ] + } +} From 7153dd85148e1356196ea86cabb10a6790771264 Mon Sep 17 00:00:00 2001 From: rowanwins Date: Fri, 12 May 2017 11:12:53 +1000 Subject: [PATCH 06/20] Test for a horizontal line for completeness --- .../test/in/line-horizontal.geojson | 17 +++++++++++++++++ .../test/out/line-horizontal.geojson | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 packages/turf-line-offset/test/in/line-horizontal.geojson create mode 100644 packages/turf-line-offset/test/out/line-horizontal.geojson diff --git a/packages/turf-line-offset/test/in/line-horizontal.geojson b/packages/turf-line-offset/test/in/line-horizontal.geojson new file mode 100644 index 0000000000..4000ce3ebd --- /dev/null +++ b/packages/turf-line-offset/test/in/line-horizontal.geojson @@ -0,0 +1,17 @@ + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 10, + 0 + ], + [ + 20, + 0 + ] + ] + } + } \ No newline at end of file diff --git a/packages/turf-line-offset/test/out/line-horizontal.geojson b/packages/turf-line-offset/test/out/line-horizontal.geojson new file mode 100644 index 0000000000..2659710034 --- /dev/null +++ b/packages/turf-line-offset/test/out/line-horizontal.geojson @@ -0,0 +1,17 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 10, + -0.089904 + ], + [ + 20, + -0.089904 + ] + ] + } +} From a420455fafbb01b2bc6fcfeb6ae74337a0dbcbf5 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 12 May 2017 00:53:05 -0400 Subject: [PATCH 07/20] Add Readme docs & update JSDocs --- packages/turf-line-offset/README.md | 56 +++++++++++++++++++ packages/turf-line-offset/index.js | 12 +++- .../test/out/linestring-long.geojson | 2 +- 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 packages/turf-line-offset/README.md diff --git a/packages/turf-line-offset/README.md b/packages/turf-line-offset/README.md new file mode 100644 index 0000000000..e52cd72f63 --- /dev/null +++ b/packages/turf-line-offset/README.md @@ -0,0 +1,56 @@ +# @turf/line-offset + +# lineOffset + +Takes a [line](http://geojson.org/geojson-spec.html#linestring) and returns a [line](http://geojson.org/geojson-spec.html#linestring) at offset by the specified distance. + +**Parameters** + +- `line` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<[LineString](http://geojson.org/geojson-spec.html#linestring)>)** input line +- `offset` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** distance to offset the line +- `units` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** can be degrees, radians, miles, kilometers, inches, yards, meters (optional, default `kilometers`) + +**Examples** + +```javascript +var line = { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": [[-83, 30], [-84, 36], [-78, 41]] + } +}; + +var offsetLine = turf.lineOffset(line, 2, 'miles'); + +//addToMap +var addToMap = [offsetLine, line] +``` + +Returns **[Feature](http://geojson.org/geojson-spec.html#feature-objects)<[LineString](http://geojson.org/geojson-spec.html#linestring)>** Line offset from the input line + + + +--- + +This module is part of the [Turfjs project](http://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. + +### Installation + +Install this module individually: + +```sh +$ npm install @turf/line-offset +``` + +Or install the Turf module that includes it as a function: + +```sh +$ npm install @turf/turf +``` diff --git a/packages/turf-line-offset/index.js b/packages/turf-line-offset/index.js index b8d05a8531..e2f8492fea 100644 --- a/packages/turf-line-offset/index.js +++ b/packages/turf-line-offset/index.js @@ -1,6 +1,6 @@ var getCoords = require('@turf/invariant').getCoords; var coordEach = require('@turf/meta').coordEach; -var intersection = require('intersection'); +var intersection = require('./intersection'); var helpers = require('@turf/helpers'); var lineString = helpers.lineString; var distanceToDegrees = helpers.distanceToDegrees; @@ -67,7 +67,15 @@ module.exports = function (line, offset, units) { return lineString(finalCoords, line.properties); }; -// Inspiration taken from http://stackoverflow.com/questions/2825412/draw-a-parallel-line +/** + * Process Segment + * Inspiration taken from http://stackoverflow.com/questions/2825412/draw-a-parallel-line + * + * @param {Array} point1 Point coordinates + * @param {Array} point2 Point coordinates + * @param {number} offset Offset + * @returns {Array>} offset points + */ function processSegment(point1, point2, offset) { var L = Math.sqrt((point1[0] - point2[0]) * (point1[0] - point2[0]) + (point1[1] - point2[1]) * (point1[1] - point2[1])); diff --git a/packages/turf-line-offset/test/out/linestring-long.geojson b/packages/turf-line-offset/test/out/linestring-long.geojson index d67127afe6..d349c41c74 100644 --- a/packages/turf-line-offset/test/out/linestring-long.geojson +++ b/packages/turf-line-offset/test/out/linestring-long.geojson @@ -85,7 +85,7 @@ 40.60321 ], [ - 9.411251, + 9.41125, 41.871083 ], [ From d4a3d2b0e5574cf7bf90b522abd831637fe2d824 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 12 May 2017 00:54:22 -0400 Subject: [PATCH 08/20] Remove local intersection module --- packages/turf-line-offset/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/turf-line-offset/index.js b/packages/turf-line-offset/index.js index e2f8492fea..4afc2eb1f4 100644 --- a/packages/turf-line-offset/index.js +++ b/packages/turf-line-offset/index.js @@ -1,6 +1,6 @@ var getCoords = require('@turf/invariant').getCoords; var coordEach = require('@turf/meta').coordEach; -var intersection = require('./intersection'); +var intersection = require('intersection'); var helpers = require('@turf/helpers'); var lineString = helpers.lineString; var distanceToDegrees = helpers.distanceToDegrees; @@ -71,6 +71,7 @@ module.exports = function (line, offset, units) { * Process Segment * Inspiration taken from http://stackoverflow.com/questions/2825412/draw-a-parallel-line * + * @private * @param {Array} point1 Point coordinates * @param {Array} point2 Point coordinates * @param {number} offset Offset From 489bbf9d23e9c7a21a9b0b303de08aed8e958986 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 12 May 2017 01:42:48 -0400 Subject: [PATCH 09/20] Refactored intersection module --- packages/turf-line-offset/index.js | 4 +- packages/turf-line-offset/intersection.js | 109 ++++++++++++++++++++++ 2 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 packages/turf-line-offset/intersection.js diff --git a/packages/turf-line-offset/index.js b/packages/turf-line-offset/index.js index 4afc2eb1f4..b0b3a4faba 100644 --- a/packages/turf-line-offset/index.js +++ b/packages/turf-line-offset/index.js @@ -1,6 +1,6 @@ var getCoords = require('@turf/invariant').getCoords; var coordEach = require('@turf/meta').coordEach; -var intersection = require('intersection'); +var intersection = require('./intersection'); var helpers = require('@turf/helpers'); var lineString = helpers.lineString; var distanceToDegrees = helpers.distanceToDegrees; @@ -41,7 +41,7 @@ module.exports = function (line, offset, units) { var seg2Coords = segments[index - 1]; var seg1 = {start: {x: segment[0][0], y: segment[0][1]}, end: {x: segment[1][0], y: segment[1][1]}}; var seg2 = {start: {x: seg2Coords[0][0], y: seg2Coords[0][1]}, end: {x: seg2Coords[1][0], y: seg2Coords[1][1]}}; - var int = intersection.intersect(seg1, seg2); + var int = intersection(seg1, seg2); // Handling for line segments that aren't straight if (int !== false) { diff --git a/packages/turf-line-offset/intersection.js b/packages/turf-line-offset/intersection.js new file mode 100644 index 0000000000..55f60ea683 --- /dev/null +++ b/packages/turf-line-offset/intersection.js @@ -0,0 +1,109 @@ +/** + * AB + * + * @param {Array>} segment - 2 vertex line segment + * @returns {Array} coordinates [x, y] + */ +function AB(segment) { + var start = segment.start; + var end = segment.end; + return {x: end.x - start.x, y: end.y - start.y}; +} + +/** + * Cross Product + * + * @param {Array} v1 coordinates [x, y] + * @param {Array} v2 coordinates [x, y] + * @returns {Array} Cross Product + */ +function crossProduct(v1, v2) { + return (v1.x * v2.y) - (v2.x * v1.y); +} + +/** + * Add + * + * @param {Array} v1 coordinates [x, y] + * @param {Array} v2 coordinates [x, y] + * @returns {Array} Add + */ +function add(v1, v2) { + return {x: v1.x + v2.x, y: v1.y + v2.y}; +} + +/** + * Sub + * + * @param {Array} v1 coordinates [x, y] + * @param {Array} v2 coordinates [x, y] + * @returns {Array} Sub + */ +function sub(v1, v2) { + return {x: v1.x - v2.x, y: v1.y - v2.y}; +} + +/** + * scalarMult + * + * @param {number} s scalar + * @param {Array} v coordinates [x, y] + * @returns {Array} scalarMult + */ +function scalarMult(s, v) { + return {x: s * v.x, y: s * v.y}; +} + +/** + * Intersect Segments + * + * @param {Array} a coordinates [x, y] + * @param {Array} b coordinates [x, y] + * @returns {Array} intersection + */ +function intersectSegments(a, b) { + var p = a.start; + var r = AB(a); + var q = b.start; + var s = AB(b); + + var cross = crossProduct(r, s); + var qmp = sub(q, p); + var numerator = crossProduct(qmp, s); + var t = numerator / cross; + var intersection = add(p, scalarMult(t, r)); + return intersection; +} + +/** + * Is Parallel + * + * @param {Array} a coordinates [x, y] + * @param {Array} b coordinates [x, y] + * @returns {boolean} true if a and b are parallel (or co-linear) + */ +function isParallel(a, b) { + var r = AB(a); + var s = AB(b); + return (crossProduct(r, s) === 0); +} + +/** + * Interesct + * + * @param {Array} a coordinates [x, y] + * @param {Array} b coordinates [x, y] + * @returns {Array|false} true if a and b are parallel (or co-linear) + */ +function intersect(a, b) { + // a = JSON.parse(JSON.stringify(a)); + // b = JSON.parse(JSON.stringify(b)); + // d = [[a.start.x, a.start.y], [a.end.x, a.end.y]]; + // e = [[b.start.x, b.start.y], [b.end.x, b.end.y]]; + if (isParallel(a, b) === false) { + return intersectSegments(a, b); + } else { + return false; + } +} +module.exports = intersect; From 61ed3872cdd3f5a7bf82b8f6ff18f6ccbb633f2b Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 12 May 2017 01:57:28 -0400 Subject: [PATCH 10/20] Stable refactor of intersetion using Array --- packages/turf-line-offset/index.js | 8 +++---- packages/turf-line-offset/intersection.js | 29 ++++++++++------------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/packages/turf-line-offset/index.js b/packages/turf-line-offset/index.js index b0b3a4faba..d901952a54 100644 --- a/packages/turf-line-offset/index.js +++ b/packages/turf-line-offset/index.js @@ -45,10 +45,10 @@ module.exports = function (line, offset, units) { // Handling for line segments that aren't straight if (int !== false) { - seg2Coords[1][0] = int.x; - seg2Coords[1][1] = int.y; - segment[0][0] = int.x; - segment[0][1] = int.y; + seg2Coords[1][0] = int[0]; + seg2Coords[1][1] = int[1]; + segment[0][0] = int[0]; + segment[0][1] = int[1]; } finalCoords.push(seg2Coords[0]); diff --git a/packages/turf-line-offset/intersection.js b/packages/turf-line-offset/intersection.js index 55f60ea683..3b508c25dc 100644 --- a/packages/turf-line-offset/intersection.js +++ b/packages/turf-line-offset/intersection.js @@ -5,9 +5,9 @@ * @returns {Array} coordinates [x, y] */ function AB(segment) { - var start = segment.start; - var end = segment.end; - return {x: end.x - start.x, y: end.y - start.y}; + var start = segment[0]; + var end = segment[1]; + return [end[0] - start[0], end[1] - start[1]]; } /** @@ -18,7 +18,7 @@ function AB(segment) { * @returns {Array} Cross Product */ function crossProduct(v1, v2) { - return (v1.x * v2.y) - (v2.x * v1.y); + return (v1[0] * v2[1]) - (v2[0] * v1[1]); } /** @@ -29,7 +29,7 @@ function crossProduct(v1, v2) { * @returns {Array} Add */ function add(v1, v2) { - return {x: v1.x + v2.x, y: v1.y + v2.y}; + return [v1[0] + v2[0], v1[1] + v2[1]]; } /** @@ -40,7 +40,7 @@ function add(v1, v2) { * @returns {Array} Sub */ function sub(v1, v2) { - return {x: v1.x - v2.x, y: v1.y - v2.y}; + return [v1[0] - v2[0], v1[1] - v2[1]]; } /** @@ -51,7 +51,7 @@ function sub(v1, v2) { * @returns {Array} scalarMult */ function scalarMult(s, v) { - return {x: s * v.x, y: s * v.y}; + return [s * v[0], s * v[1]]; } /** @@ -62,9 +62,9 @@ function scalarMult(s, v) { * @returns {Array} intersection */ function intersectSegments(a, b) { - var p = a.start; + var p = a[0]; var r = AB(a); - var q = b.start; + var q = b[0]; var s = AB(b); var cross = crossProduct(r, s); @@ -98,12 +98,9 @@ function isParallel(a, b) { function intersect(a, b) { // a = JSON.parse(JSON.stringify(a)); // b = JSON.parse(JSON.stringify(b)); - // d = [[a.start.x, a.start.y], [a.end.x, a.end.y]]; - // e = [[b.start.x, b.start.y], [b.end.x, b.end.y]]; - if (isParallel(a, b) === false) { - return intersectSegments(a, b); - } else { - return false; - } + a = [[a.start.x, a.start.y], [a.end.x, a.end.y]]; + b = [[b.start.x, b.start.y], [b.end.x, b.end.y]]; + if (isParallel(a, b)) return false; + return intersectSegments(a, b); } module.exports = intersect; From b7fb86686ade1f2ffe5d06eecc16437c5b0c8dd6 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 12 May 2017 02:29:40 -0400 Subject: [PATCH 11/20] Improve testing & implement intersection - Implement intersection directly in index.js - Replaced Geometry Testing with tape test - Colorized geojson outputs - Update JSDocs offset --- packages/turf-line-offset/README.md | 2 +- packages/turf-line-offset/index.js | 129 ++++- packages/turf-line-offset/intersection.js | 106 ---- packages/turf-line-offset/test.js | 20 +- .../test/in/line-horizontal.geojson | 5 +- .../test/in/linestring-feature.geojson | 25 - .../test/in/linestring-geometry.geojson | 21 - .../test/in/linestring-long.geojson | 5 +- .../in/linestring-singleSegmentOnly.geojson | 5 +- .../test/in/linestring-straight.geojson | 5 +- .../test/out/line-horizontal.geojson | 58 +- .../test/out/linestring-feature.geojson | 25 - .../test/out/linestring-geometry.geojson | 25 - .../test/out/linestring-long.geojson | 514 ++++++++++++------ .../out/linestring-singleSegmentOnly.geojson | 58 +- .../test/out/linestring-straight.geojson | 70 ++- 16 files changed, 636 insertions(+), 437 deletions(-) delete mode 100644 packages/turf-line-offset/intersection.js delete mode 100644 packages/turf-line-offset/test/in/linestring-feature.geojson delete mode 100644 packages/turf-line-offset/test/in/linestring-geometry.geojson delete mode 100644 packages/turf-line-offset/test/out/linestring-feature.geojson delete mode 100644 packages/turf-line-offset/test/out/linestring-geometry.geojson diff --git a/packages/turf-line-offset/README.md b/packages/turf-line-offset/README.md index e52cd72f63..23633785a7 100644 --- a/packages/turf-line-offset/README.md +++ b/packages/turf-line-offset/README.md @@ -7,7 +7,7 @@ Takes a [line](http://geojson.org/geojson-spec.html#linestring) and returns a [l **Parameters** - `line` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<[LineString](http://geojson.org/geojson-spec.html#linestring)>)** input line -- `offset` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** distance to offset the line +- `offset` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** distance to offset the line (can be of negative value) - `units` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** can be degrees, radians, miles, kilometers, inches, yards, meters (optional, default `kilometers`) **Examples** diff --git a/packages/turf-line-offset/index.js b/packages/turf-line-offset/index.js index d901952a54..d304e2698a 100644 --- a/packages/turf-line-offset/index.js +++ b/packages/turf-line-offset/index.js @@ -1,6 +1,5 @@ var getCoords = require('@turf/invariant').getCoords; var coordEach = require('@turf/meta').coordEach; -var intersection = require('./intersection'); var helpers = require('@turf/helpers'); var lineString = helpers.lineString; var distanceToDegrees = helpers.distanceToDegrees; @@ -10,7 +9,7 @@ var distanceToDegrees = helpers.distanceToDegrees; * * @name lineOffset * @param {Geometry|Feature} line input line - * @param {number} offset distance to offset the line + * @param {number} offset distance to offset the line (can be of negative value) * @param {string} [units=kilometers] can be degrees, radians, miles, kilometers, inches, yards, meters * @returns {Feature} Line offset from the input line * @example @@ -29,6 +28,9 @@ var distanceToDegrees = helpers.distanceToDegrees; * var addToMap = [offsetLine, line] */ module.exports = function (line, offset, units) { + if (!line) throw new Error('line is required'); + if (offset === undefined || offset === null || isNaN(offset)) throw new Error('offset is required'); + var segments = []; var offsetDegrees = distanceToDegrees(offset, units); var coords = getCoords(line); @@ -39,16 +41,12 @@ module.exports = function (line, offset, units) { segments.push(segment); if (index > 0) { var seg2Coords = segments[index - 1]; - var seg1 = {start: {x: segment[0][0], y: segment[0][1]}, end: {x: segment[1][0], y: segment[1][1]}}; - var seg2 = {start: {x: seg2Coords[0][0], y: seg2Coords[0][1]}, end: {x: seg2Coords[1][0], y: seg2Coords[1][1]}}; - var int = intersection(seg1, seg2); + var intersects = intersection(segment, seg2Coords); // Handling for line segments that aren't straight - if (int !== false) { - seg2Coords[1][0] = int[0]; - seg2Coords[1][1] = int[1]; - segment[0][0] = int[0]; - segment[0][1] = int[1]; + if (intersects !== false) { + seg2Coords[1] = intersects; + segment[0] = intersects; } finalCoords.push(seg2Coords[0]); @@ -86,3 +84,114 @@ function processSegment(point1, point2, offset) { var out2y = point2[1] + offset * (point1[0] - point2[0]) / L; return [[out1x, out1y], [out2x, out2y]]; } + +/** + * AB + * + * @private + * @param {Array>} segment - 2 vertex line segment + * @returns {Array} coordinates [x, y] + */ +function ab(segment) { + var start = segment[0]; + var end = segment[1]; + return [end[0] - start[0], end[1] - start[1]]; +} + +/** + * Cross Product + * + * @private + * @param {Array} v1 coordinates [x, y] + * @param {Array} v2 coordinates [x, y] + * @returns {Array} Cross Product + */ +function crossProduct(v1, v2) { + return (v1[0] * v2[1]) - (v2[0] * v1[1]); +} + +/** + * Add + * + * @private + * @param {Array} v1 coordinates [x, y] + * @param {Array} v2 coordinates [x, y] + * @returns {Array} Add + */ +function add(v1, v2) { + return [v1[0] + v2[0], v1[1] + v2[1]]; +} + +/** + * Sub + * + * @private + * @param {Array} v1 coordinates [x, y] + * @param {Array} v2 coordinates [x, y] + * @returns {Array} Sub + */ +function sub(v1, v2) { + return [v1[0] - v2[0], v1[1] - v2[1]]; +} + +/** + * scalarMult + * + * @private + * @param {number} s scalar + * @param {Array} v coordinates [x, y] + * @returns {Array} scalarMult + */ +function scalarMult(s, v) { + return [s * v[0], s * v[1]]; +} + +/** + * Intersect Segments + * + * @private + * @param {Array} a coordinates [x, y] + * @param {Array} b coordinates [x, y] + * @returns {Array} intersection + */ +function intersectSegments(a, b) { + var p = a[0]; + var r = ab(a); + var q = b[0]; + var s = ab(b); + + var cross = crossProduct(r, s); + var qmp = sub(q, p); + var numerator = crossProduct(qmp, s); + var t = numerator / cross; + var intersection = add(p, scalarMult(t, r)); + return intersection; +} + +/** + * Is Parallel + * + * @private + * @param {Array} a coordinates [x, y] + * @param {Array} b coordinates [x, y] + * @returns {boolean} true if a and b are parallel (or co-linear) + */ +function isParallel(a, b) { + var r = ab(a); + var s = ab(b); + return (crossProduct(r, s) === 0); +} + +/** + * Intersection + * https://github.com/rook2pawn/node-intersection/blob/master/index.js + * + * @private + * @param {Array} a coordinates [x, y] + * @param {Array} b coordinates [x, y] + * @returns {Array|boolean} true if a and b are parallel (or co-linear) + */ +function intersection(a, b) { + if (isParallel(a, b)) return false; + return intersectSegments(a, b); +} diff --git a/packages/turf-line-offset/intersection.js b/packages/turf-line-offset/intersection.js deleted file mode 100644 index 3b508c25dc..0000000000 --- a/packages/turf-line-offset/intersection.js +++ /dev/null @@ -1,106 +0,0 @@ -/** - * AB - * - * @param {Array>} segment - 2 vertex line segment - * @returns {Array} coordinates [x, y] - */ -function AB(segment) { - var start = segment[0]; - var end = segment[1]; - return [end[0] - start[0], end[1] - start[1]]; -} - -/** - * Cross Product - * - * @param {Array} v1 coordinates [x, y] - * @param {Array} v2 coordinates [x, y] - * @returns {Array} Cross Product - */ -function crossProduct(v1, v2) { - return (v1[0] * v2[1]) - (v2[0] * v1[1]); -} - -/** - * Add - * - * @param {Array} v1 coordinates [x, y] - * @param {Array} v2 coordinates [x, y] - * @returns {Array} Add - */ -function add(v1, v2) { - return [v1[0] + v2[0], v1[1] + v2[1]]; -} - -/** - * Sub - * - * @param {Array} v1 coordinates [x, y] - * @param {Array} v2 coordinates [x, y] - * @returns {Array} Sub - */ -function sub(v1, v2) { - return [v1[0] - v2[0], v1[1] - v2[1]]; -} - -/** - * scalarMult - * - * @param {number} s scalar - * @param {Array} v coordinates [x, y] - * @returns {Array} scalarMult - */ -function scalarMult(s, v) { - return [s * v[0], s * v[1]]; -} - -/** - * Intersect Segments - * - * @param {Array} a coordinates [x, y] - * @param {Array} b coordinates [x, y] - * @returns {Array} intersection - */ -function intersectSegments(a, b) { - var p = a[0]; - var r = AB(a); - var q = b[0]; - var s = AB(b); - - var cross = crossProduct(r, s); - var qmp = sub(q, p); - var numerator = crossProduct(qmp, s); - var t = numerator / cross; - var intersection = add(p, scalarMult(t, r)); - return intersection; -} - -/** - * Is Parallel - * - * @param {Array} a coordinates [x, y] - * @param {Array} b coordinates [x, y] - * @returns {boolean} true if a and b are parallel (or co-linear) - */ -function isParallel(a, b) { - var r = AB(a); - var s = AB(b); - return (crossProduct(r, s) === 0); -} - -/** - * Interesct - * - * @param {Array} a coordinates [x, y] - * @param {Array} b coordinates [x, y] - * @returns {Array|false} true if a and b are parallel (or co-linear) - */ -function intersect(a, b) { - // a = JSON.parse(JSON.stringify(a)); - // b = JSON.parse(JSON.stringify(b)); - a = [[a.start.x, a.start.y], [a.end.x, a.end.y]]; - b = [[b.start.x, b.start.y], [b.end.x, b.end.y]]; - if (isParallel(a, b)) return false; - return intersectSegments(a, b); -} -module.exports = intersect; diff --git a/packages/turf-line-offset/test.js b/packages/turf-line-offset/test.js index 3d84c03a74..7b36cd3642 100644 --- a/packages/turf-line-offset/test.js +++ b/packages/turf-line-offset/test.js @@ -1,10 +1,11 @@ -const test = require('tape'); const fs = require('fs'); +const test = require('tape'); const path = require('path'); const load = require('load-json-file'); const write = require('write-json-file'); -const lineOffset = require('./'); const truncate = require('@turf/truncate'); +const {featureCollection, lineString} = require('@turf/helpers'); +const lineOffset = require('./'); const directories = { in: path.join(__dirname, 'test', 'in') + path.sep, @@ -19,11 +20,22 @@ const fixtures = fs.readdirSync(directories.in).map(filename => { }; }); -test('turf-lineOffset', t => { +test('turf-line-offset', t => { for (const {name, geojson} of fixtures) { - const results = truncate(lineOffset(geojson, 10, 'kilometers')); + const offset = truncate(lineOffset(geojson, 50, 'kilometers')); + offset.properties.stroke = '#00F'; + const results = featureCollection([offset, geojson]); + if (process.env.REGEN) write.sync(directories.out + name + '.geojson', results); t.deepEqual(results, load.sync(directories.out + name + '.geojson'), name); } + t.throws(() => lineOffset(), /line is required/); + t.throws(() => lineOffset(lineString([[10, 10], [0, 0]])), /offset is required/); + t.end(); +}); + +test('turf-line-offset - Support Geometry Objects', t => { + const line = lineString([[10, 10], [0, 0]]); + t.ok(lineOffset(line.geometry, 10), 'Geometry Object'); t.end(); }); diff --git a/packages/turf-line-offset/test/in/line-horizontal.geojson b/packages/turf-line-offset/test/in/line-horizontal.geojson index 4000ce3ebd..97ae5bef0b 100644 --- a/packages/turf-line-offset/test/in/line-horizontal.geojson +++ b/packages/turf-line-offset/test/in/line-horizontal.geojson @@ -1,6 +1,9 @@ { "type": "Feature", - "properties": {}, + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, "geometry": { "type": "LineString", "coordinates": [ diff --git a/packages/turf-line-offset/test/in/linestring-feature.geojson b/packages/turf-line-offset/test/in/linestring-feature.geojson deleted file mode 100644 index adec6f644d..0000000000 --- a/packages/turf-line-offset/test/in/linestring-feature.geojson +++ /dev/null @@ -1,25 +0,0 @@ -{ - "type": "Feature", - "properties": {}, - "geometry": { - "type": "LineString", - "coordinates": [ - [ - 20.390625, - 62.2679226294176 - ], - [ - 27.0703125, - 56.36525013685606 - ], - [ - 22.5, - 53.54030739150022 - ], - [ - 26.015625, - 49.83798245308484 - ] - ] - } -} \ No newline at end of file diff --git a/packages/turf-line-offset/test/in/linestring-geometry.geojson b/packages/turf-line-offset/test/in/linestring-geometry.geojson deleted file mode 100644 index 316e301df5..0000000000 --- a/packages/turf-line-offset/test/in/linestring-geometry.geojson +++ /dev/null @@ -1,21 +0,0 @@ -{ - "type": "LineString", - "coordinates": [ - [ - 20.390625, - 62.2679226294176 - ], - [ - 27.0703125, - 56.36525013685606 - ], - [ - 22.5, - 53.54030739150022 - ], - [ - 26.015625, - 49.83798245308484 - ] - ] -} \ No newline at end of file diff --git a/packages/turf-line-offset/test/in/linestring-long.geojson b/packages/turf-line-offset/test/in/linestring-long.geojson index 6c40f4101c..e9f886e7cb 100644 --- a/packages/turf-line-offset/test/in/linestring-long.geojson +++ b/packages/turf-line-offset/test/in/linestring-long.geojson @@ -1,6 +1,9 @@ { "type": "Feature", - "properties": {}, + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, "geometry": { "type": "LineString", "coordinates": [ diff --git a/packages/turf-line-offset/test/in/linestring-singleSegmentOnly.geojson b/packages/turf-line-offset/test/in/linestring-singleSegmentOnly.geojson index 71783eedb3..66f8cf2d08 100644 --- a/packages/turf-line-offset/test/in/linestring-singleSegmentOnly.geojson +++ b/packages/turf-line-offset/test/in/linestring-singleSegmentOnly.geojson @@ -1,6 +1,9 @@ { "type": "Feature", - "properties": {}, + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, "geometry": { "type": "LineString", "coordinates": [ diff --git a/packages/turf-line-offset/test/in/linestring-straight.geojson b/packages/turf-line-offset/test/in/linestring-straight.geojson index 487137fe49..03a9c2f33f 100644 --- a/packages/turf-line-offset/test/in/linestring-straight.geojson +++ b/packages/turf-line-offset/test/in/linestring-straight.geojson @@ -1,6 +1,9 @@ { "type": "Feature", - "properties": {}, + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, "geometry": { "type": "LineString", "coordinates": [ diff --git a/packages/turf-line-offset/test/out/line-horizontal.geojson b/packages/turf-line-offset/test/out/line-horizontal.geojson index 2659710034..a7f9efbca1 100644 --- a/packages/turf-line-offset/test/out/line-horizontal.geojson +++ b/packages/turf-line-offset/test/out/line-horizontal.geojson @@ -1,17 +1,45 @@ { - "type": "Feature", - "properties": {}, - "geometry": { - "type": "LineString", - "coordinates": [ - [ - 10, - -0.089904 - ], - [ - 20, - -0.089904 - ] - ] - } + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 10, + -0.44952 + ], + [ + 20, + -0.44952 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 10, + 0 + ], + [ + 20, + 0 + ] + ] + } + } + ] } diff --git a/packages/turf-line-offset/test/out/linestring-feature.geojson b/packages/turf-line-offset/test/out/linestring-feature.geojson deleted file mode 100644 index 4fcf87944c..0000000000 --- a/packages/turf-line-offset/test/out/linestring-feature.geojson +++ /dev/null @@ -1,25 +0,0 @@ -{ - "type": "Feature", - "properties": {}, - "geometry": { - "type": "LineString", - "coordinates": [ - [ - 20.331093, - 62.200553 - ], - [ - 26.920045, - 56.378061 - ], - [ - 22.358633, - 53.558619 - ], - [ - 25.950431, - 49.776076 - ] - ] - } -} diff --git a/packages/turf-line-offset/test/out/linestring-geometry.geojson b/packages/turf-line-offset/test/out/linestring-geometry.geojson deleted file mode 100644 index 4fcf87944c..0000000000 --- a/packages/turf-line-offset/test/out/linestring-geometry.geojson +++ /dev/null @@ -1,25 +0,0 @@ -{ - "type": "Feature", - "properties": {}, - "geometry": { - "type": "LineString", - "coordinates": [ - [ - 20.331093, - 62.200553 - ], - [ - 26.920045, - 56.378061 - ], - [ - 22.358633, - 53.558619 - ], - [ - 25.950431, - 49.776076 - ] - ] - } -} diff --git a/packages/turf-line-offset/test/out/linestring-long.geojson b/packages/turf-line-offset/test/out/linestring-long.geojson index d349c41c74..778964ec50 100644 --- a/packages/turf-line-offset/test/out/linestring-long.geojson +++ b/packages/turf-line-offset/test/out/linestring-long.geojson @@ -1,169 +1,349 @@ { - "type": "Feature", - "properties": {}, - "geometry": { - "type": "LineString", - "coordinates": [ - [ - -24.237809, - 41.157122 - ], - [ - -6.292847, - 45.252538 - ], - [ - 0.658696, - 49.702791 - ], - [ - 2.700986, - 44.379241 - ], - [ - -6.436663, - 39.137346 - ], - [ - -5.510398, - 36.355801 - ], - [ - -0.128788, - 37.216746 - ], - [ - 1.642845, - 39.301009 - ], - [ - 3.376965, - 40.496974 - ], - [ - 6.113954, - 41.143329 - ], - [ - 7.262832, - 39.518023 - ], - [ - 4.498159, - 37.07632 - ], - [ - 3.594071, - 35.324803 - ], - [ - 4.862895, - 31.546607 - ], - [ - 8.650827, - 32.463401 - ], - [ - 10.816742, - 33.972216 - ], - [ - 10.636473, - 36.038753 - ], - [ - 10.459757, - 38.292025 - ], - [ - 10.103613, - 39.264094 - ], - [ - 9.579874, - 40.60321 - ], - [ - 9.41125, - 41.871083 - ], - [ - 11.461721, - 43.995366 - ], - [ - 13.497446, - 43.873385 - ], - [ - 14.68338, - 42.765326 - ], - [ - 15.729358, - 37.714452 - ], - [ - 15.556772, - 36.197307 - ], - [ - 14.861491, - 34.638577 - ], - [ - 13.273615, - 31.983926 - ], - [ - 11.611149, - 22.320126 - ], - [ - 15.899525, - 29.186003 - ], - [ - 16.961388, - 31.476217 - ], - [ - 17.315579, - 33.418447 - ], - [ - 17.490635, - 35.010404 - ], - [ - 19.574614, - 41.150355 - ], - [ - 21.3261, - 41.030512 - ], - [ - 20.650483, - 38.830219 - ], - [ - 21.004328, - 36.163786 - ], - [ - 21.179627, - 34.012585 - ], - [ - 21.179627, - 31.219999 - ], - [ - 26.049506, - 33.201345 - ] - ] - } + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -24.157794, + 40.806521 + ], + [ + -6.151735, + 44.91588 + ], + [ + 0.480981, + 49.162026 + ], + [ + 2.254932, + 44.537944 + ], + [ + -6.870816, + 39.302876 + ], + [ + -5.755116, + 35.952463 + ], + [ + 0.059187, + 36.88263 + ], + [ + 1.886099, + 39.031926 + ], + [ + 3.525449, + 40.162532 + ], + [ + 5.960393, + 40.737558 + ], + [ + 6.782909, + 39.573953 + ], + [ + 4.209543, + 37.301209 + ], + [ + 3.204728, + 35.35455 + ], + [ + 4.626974, + 31.119508 + ], + [ + 8.801011, + 32.129751 + ], + [ + 11.193085, + 33.796113 + ], + [ + 10.994865, + 36.068438 + ], + [ + 10.814411, + 38.369372 + ], + [ + 10.439942, + 39.391459 + ], + [ + 9.93062, + 40.693712 + ], + [ + 9.790627, + 41.746309 + ], + [ + 11.605479, + 43.626492 + ], + [ + 13.346604, + 43.522163 + ], + [ + 14.3544, + 42.580544 + ], + [ + 15.36554, + 37.697899 + ], + [ + 15.205735, + 36.293108 + ], + [ + 14.541829, + 34.804717 + ], + [ + 12.930574, + 32.11098 + ], + [ + 10.946372, + 20.576949 + ], + [ + 16.216376, + 29.014455 + ], + [ + 17.306938, + 31.366569 + ], + [ + 17.671647, + 33.36647 + ], + [ + 17.843801, + 34.932034 + ], + [ + 19.826193, + 40.772684 + ], + [ + 20.849251, + 40.702683 + ], + [ + 20.283665, + 38.860733 + ], + [ + 20.64664, + 36.125504 + ], + [ + 20.820012, + 33.997957 + ], + [ + 20.820012, + 30.685446 + ], + [ + 26.185031, + 32.868243 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -24.2578125, + 41.244772343082076 + ], + [ + -6.328125, + 45.336701909968134 + ], + [ + 0.703125, + 49.83798245308484 + ], + [ + 2.8125, + 44.33956524809713 + ], + [ + -6.328125, + 39.095962936305476 + ], + [ + -5.44921875, + 36.4566360115962 + ], + [ + -0.17578125, + 37.3002752813443 + ], + [ + 1.58203125, + 39.36827914916014 + ], + [ + 3.33984375, + 40.58058466412761 + ], + [ + 6.15234375, + 41.244772343082076 + ], + [ + 7.3828125, + 39.50404070558415 + ], + [ + 4.5703125, + 37.020098201368114 + ], + [ + 3.69140625, + 35.31736632923788 + ], + [ + 4.921875, + 31.653381399664 + ], + [ + 8.61328125, + 32.54681317351514 + ], + [ + 10.72265625, + 34.016241889667015 + ], + [ + 10.546875, + 36.03133177633187 + ], + [ + 10.37109375, + 38.272688535980976 + ], + [ + 10.01953125, + 39.232253141714885 + ], + [ + 9.4921875, + 40.58058466412761 + ], + [ + 9.31640625, + 41.902277040963696 + ], + [ + 11.42578125, + 44.08758502824516 + ], + [ + 13.53515625, + 43.96119063892024 + ], + [ + 14.765625, + 42.8115217450979 + ], + [ + 15.8203125, + 37.71859032558816 + ], + [ + 15.644531250000002, + 36.1733569352216 + ], + [ + 14.94140625, + 34.59704151614417 + ], + [ + 13.359375, + 31.952162238024975 + ], + [ + 11.77734375, + 22.755920681486405 + ], + [ + 15.8203125, + 29.22889003019423 + ], + [ + 16.875, + 31.50362930577303 + ], + [ + 17.2265625, + 33.43144133557529 + ], + [ + 17.402343749999996, + 35.02999636902566 + ], + [ + 19.51171875, + 41.244772343082076 + ], + [ + 21.4453125, + 41.11246878918088 + ], + [ + 20.7421875, + 38.8225909761771 + ], + [ + 21.09375, + 36.1733569352216 + ], + [ + 21.26953125, + 34.016241889667015 + ], + [ + 21.26953125, + 31.353636941500987 + ], + [ + 26.015625, + 33.284619968887675 + ] + ] + } + } + ] } diff --git a/packages/turf-line-offset/test/out/linestring-singleSegmentOnly.geojson b/packages/turf-line-offset/test/out/linestring-singleSegmentOnly.geojson index c507515eab..066d52d74f 100644 --- a/packages/turf-line-offset/test/out/linestring-singleSegmentOnly.geojson +++ b/packages/turf-line-offset/test/out/linestring-singleSegmentOnly.geojson @@ -1,17 +1,45 @@ { - "type": "Feature", - "properties": {}, - "geometry": { - "type": "LineString", - "coordinates": [ - [ - 1.089904, - 1 - ], - [ - 1.089904, - 10 - ] - ] - } + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 1.44952, + 1 + ], + [ + 1.44952, + 10 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 1, + 1 + ], + [ + 1, + 10 + ] + ] + } + } + ] } diff --git a/packages/turf-line-offset/test/out/linestring-straight.geojson b/packages/turf-line-offset/test/out/linestring-straight.geojson index 8868b3114b..51b8dbe41d 100644 --- a/packages/turf-line-offset/test/out/linestring-straight.geojson +++ b/packages/turf-line-offset/test/out/linestring-straight.geojson @@ -1,21 +1,53 @@ { - "type": "Feature", - "properties": {}, - "geometry": { - "type": "LineString", - "coordinates": [ - [ - 1.089904, - -10 - ], - [ - 1.089904, - 1 - ], - [ - 1.089904, - 10 - ] - ] - } + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 1.44952, + -10 + ], + [ + 1.44952, + 1 + ], + [ + 1.44952, + 10 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 1, + -10 + ], + [ + 1, + 1 + ], + [ + 1, + 10 + ] + ] + } + } + ] } From d2a9dcf8df0519b11e736e0e6d69f8ef5cbf2ed9 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 12 May 2017 02:41:05 -0400 Subject: [PATCH 12/20] Truncate to 4 decimals --- packages/turf-line-offset/test.js | 2 +- .../test/out/line-horizontal.geojson | 4 +- .../test/out/linestring-long.geojson | 158 +++++++++--------- .../out/linestring-singleSegmentOnly.geojson | 4 +- .../test/out/linestring-straight.geojson | 6 +- 5 files changed, 87 insertions(+), 87 deletions(-) diff --git a/packages/turf-line-offset/test.js b/packages/turf-line-offset/test.js index 7b36cd3642..5675f1da04 100644 --- a/packages/turf-line-offset/test.js +++ b/packages/turf-line-offset/test.js @@ -22,7 +22,7 @@ const fixtures = fs.readdirSync(directories.in).map(filename => { test('turf-line-offset', t => { for (const {name, geojson} of fixtures) { - const offset = truncate(lineOffset(geojson, 50, 'kilometers')); + const offset = truncate(lineOffset(geojson, 50, 'kilometers'), 4); offset.properties.stroke = '#00F'; const results = featureCollection([offset, geojson]); diff --git a/packages/turf-line-offset/test/out/line-horizontal.geojson b/packages/turf-line-offset/test/out/line-horizontal.geojson index a7f9efbca1..e9696f37d8 100644 --- a/packages/turf-line-offset/test/out/line-horizontal.geojson +++ b/packages/turf-line-offset/test/out/line-horizontal.geojson @@ -12,11 +12,11 @@ "coordinates": [ [ 10, - -0.44952 + -0.4495 ], [ 20, - -0.44952 + -0.4495 ] ] } diff --git a/packages/turf-line-offset/test/out/linestring-long.geojson b/packages/turf-line-offset/test/out/linestring-long.geojson index 778964ec50..7febfe18a8 100644 --- a/packages/turf-line-offset/test/out/linestring-long.geojson +++ b/packages/turf-line-offset/test/out/linestring-long.geojson @@ -11,164 +11,164 @@ "type": "LineString", "coordinates": [ [ - -24.157794, - 40.806521 + -24.1578, + 40.8065 ], [ - -6.151735, - 44.91588 + -6.1517, + 44.9159 ], [ - 0.480981, - 49.162026 + 0.481, + 49.162 ], [ - 2.254932, - 44.537944 + 2.2549, + 44.5379 ], [ - -6.870816, - 39.302876 + -6.8708, + 39.3029 ], [ - -5.755116, - 35.952463 + -5.7551, + 35.9525 ], [ - 0.059187, - 36.88263 + 0.0592, + 36.8826 ], [ - 1.886099, - 39.031926 + 1.8861, + 39.0319 ], [ - 3.525449, - 40.162532 + 3.5254, + 40.1625 ], [ - 5.960393, - 40.737558 + 5.9604, + 40.7376 ], [ - 6.782909, - 39.573953 + 6.7829, + 39.574 ], [ - 4.209543, - 37.301209 + 4.2095, + 37.3012 ], [ - 3.204728, - 35.35455 + 3.2047, + 35.3545 ], [ - 4.626974, - 31.119508 + 4.627, + 31.1195 ], [ - 8.801011, - 32.129751 + 8.801, + 32.1298 ], [ - 11.193085, - 33.796113 + 11.1931, + 33.7961 ], [ - 10.994865, - 36.068438 + 10.9949, + 36.0684 ], [ - 10.814411, - 38.369372 + 10.8144, + 38.3694 ], [ - 10.439942, - 39.391459 + 10.4399, + 39.3915 ], [ - 9.93062, - 40.693712 + 9.9306, + 40.6937 ], [ - 9.790627, - 41.746309 + 9.7906, + 41.7463 ], [ - 11.605479, - 43.626492 + 11.6055, + 43.6265 ], [ - 13.346604, - 43.522163 + 13.3466, + 43.5222 ], [ 14.3544, - 42.580544 + 42.5805 ], [ - 15.36554, - 37.697899 + 15.3655, + 37.6979 ], [ - 15.205735, - 36.293108 + 15.2057, + 36.2931 ], [ - 14.541829, - 34.804717 + 14.5418, + 34.8047 ], [ - 12.930574, - 32.11098 + 12.9306, + 32.111 ], [ - 10.946372, - 20.576949 + 10.9464, + 20.5769 ], [ - 16.216376, - 29.014455 + 16.2164, + 29.0145 ], [ - 17.306938, - 31.366569 + 17.3069, + 31.3666 ], [ - 17.671647, - 33.36647 + 17.6716, + 33.3665 ], [ - 17.843801, - 34.932034 + 17.8438, + 34.932 ], [ - 19.826193, - 40.772684 + 19.8262, + 40.7727 ], [ - 20.849251, - 40.702683 + 20.8493, + 40.7027 ], [ - 20.283665, - 38.860733 + 20.2837, + 38.8607 ], [ - 20.64664, - 36.125504 + 20.6466, + 36.1255 ], [ - 20.820012, - 33.997957 + 20.82, + 33.998 ], [ - 20.820012, - 30.685446 + 20.82, + 30.6854 ], [ - 26.185031, - 32.868243 + 26.185, + 32.8682 ] ] } diff --git a/packages/turf-line-offset/test/out/linestring-singleSegmentOnly.geojson b/packages/turf-line-offset/test/out/linestring-singleSegmentOnly.geojson index 066d52d74f..5d2d9553ec 100644 --- a/packages/turf-line-offset/test/out/linestring-singleSegmentOnly.geojson +++ b/packages/turf-line-offset/test/out/linestring-singleSegmentOnly.geojson @@ -11,11 +11,11 @@ "type": "LineString", "coordinates": [ [ - 1.44952, + 1.4495, 1 ], [ - 1.44952, + 1.4495, 10 ] ] diff --git a/packages/turf-line-offset/test/out/linestring-straight.geojson b/packages/turf-line-offset/test/out/linestring-straight.geojson index 51b8dbe41d..825e7a7d5e 100644 --- a/packages/turf-line-offset/test/out/linestring-straight.geojson +++ b/packages/turf-line-offset/test/out/linestring-straight.geojson @@ -11,15 +11,15 @@ "type": "LineString", "coordinates": [ [ - 1.44952, + 1.4495, -10 ], [ - 1.44952, + 1.4495, 1 ], [ - 1.44952, + 1.4495, 10 ] ] From 412cbad7085e92f21a5a91ac7de57713e27254d7 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 12 May 2017 02:50:31 -0400 Subject: [PATCH 13/20] Update benchmark results with new fixtures --- packages/turf-line-offset/bench.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/turf-line-offset/bench.js b/packages/turf-line-offset/bench.js index 57ade45ac6..a0b23f502d 100644 --- a/packages/turf-line-offset/bench.js +++ b/packages/turf-line-offset/bench.js @@ -1,7 +1,7 @@ -const Benchmark = require('benchmark'); -const path = require('path'); const fs = require('fs'); +const path = require('path'); const load = require('load-json-file'); +const Benchmark = require('benchmark'); const lineOffset = require('./'); const directory = path.join(__dirname, 'test', 'in') + path.sep; @@ -15,8 +15,11 @@ const fixtures = fs.readdirSync(directory).map(filename => { /** * Benchmark Results - * linestring-feature x 293,412 ops/sec ±1.42% (76 runs sampled) - * linestring-geometry x 288,033 ops/sec ±2.62% (79 runs sampled) + * + * line-horizontal x 1,816,451 ops/sec ±15.31% (62 runs sampled) + * linestring-long x 144,640 ops/sec ±3.35% (82 runs sampled) + * linestring-singleSegmentOnly x 2,649,959 ops/sec ±1.54% (76 runs sampled) + * linestring-straight x 1,857,452 ops/sec ±5.83% (77 runs sampled) */ const suite = new Benchmark.Suite('turf-line-offset'); for (const {name, geojson} of fixtures) { @@ -24,6 +27,6 @@ for (const {name, geojson} of fixtures) { } suite - .on('cycle', e => { console.log(String(e.target)); }) + .on('cycle', e => console.log(String(e.target))) .on('complete', () => {}) .run(); From 05de4a9a8924bcbfc43d8cef8804eadbe50e126e Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 12 May 2017 02:55:38 -0400 Subject: [PATCH 14/20] drop unussed dependencies --- packages/turf-line-offset/index.js | 14 +++++++------- packages/turf-line-offset/package.json | 14 ++++++-------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/packages/turf-line-offset/index.js b/packages/turf-line-offset/index.js index d304e2698a..f724821971 100644 --- a/packages/turf-line-offset/index.js +++ b/packages/turf-line-offset/index.js @@ -34,7 +34,7 @@ module.exports = function (line, offset, units) { var segments = []; var offsetDegrees = distanceToDegrees(offset, units); var coords = getCoords(line); - var finalCoords = []; + var result = []; coordEach(line, function (currentCoords, index) { if (index !== coords.length - 1) { var segment = processSegment(currentCoords, coords[index + 1], offsetDegrees); @@ -49,20 +49,20 @@ module.exports = function (line, offset, units) { segment[0] = intersects; } - finalCoords.push(seg2Coords[0]); + result.push(seg2Coords[0]); if (index === coords.length - 2) { - finalCoords.push(segment[0]); - finalCoords.push(segment[1]); + result.push(segment[0]); + result.push(segment[1]); } } // Handling for lines that only have 1 segment if (coords.length === 2) { - finalCoords.push(segment[0]); - finalCoords.push(segment[1]); + result.push(segment[0]); + result.push(segment[1]); } } }); - return lineString(finalCoords, line.properties); + return lineString(result, line.properties); }; /** diff --git a/packages/turf-line-offset/package.json b/packages/turf-line-offset/package.json index fa641cf13d..d16ff4458c 100644 --- a/packages/turf-line-offset/package.json +++ b/packages/turf-line-offset/package.json @@ -1,7 +1,7 @@ { "name": "@turf/line-offset", - "version": "4.1.0", - "description": "turf line offset module", + "version": "4.0.0", + "description": "turf line-offset module", "main": "index.js", "types": "index.d.ts", "files": [ @@ -33,17 +33,15 @@ }, "homepage": "https://github.com/Turfjs/turf", "devDependencies": { - "@turf/helpers": "^4.1.0", - "@turf/truncate": "^4.2.0", + "@turf/truncate": "^4.3.0", "benchmark": "^2.1.4", "load-json-file": "^2.0.0", "tape": "^4.6.3", "write-json-file": "^2.0.0" }, "dependencies": { - "@turf/helpers": "^4.1.0", - "@turf/invariant": "^4.2.0", - "@turf/meta": "^4.2.0", - "intersection": "0.0.1" + "@turf/helpers": "^4.3.0", + "@turf/invariant": "^4.3.0", + "@turf/meta": "^4.2.0" } } From bd3571736405e21d3cf9d8fe0dae1ad3565ef123 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 12 May 2017 03:04:52 -0400 Subject: [PATCH 15/20] Change result to finalCoords --- packages/turf-line-offset/index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/turf-line-offset/index.js b/packages/turf-line-offset/index.js index f724821971..d304e2698a 100644 --- a/packages/turf-line-offset/index.js +++ b/packages/turf-line-offset/index.js @@ -34,7 +34,7 @@ module.exports = function (line, offset, units) { var segments = []; var offsetDegrees = distanceToDegrees(offset, units); var coords = getCoords(line); - var result = []; + var finalCoords = []; coordEach(line, function (currentCoords, index) { if (index !== coords.length - 1) { var segment = processSegment(currentCoords, coords[index + 1], offsetDegrees); @@ -49,20 +49,20 @@ module.exports = function (line, offset, units) { segment[0] = intersects; } - result.push(seg2Coords[0]); + finalCoords.push(seg2Coords[0]); if (index === coords.length - 2) { - result.push(segment[0]); - result.push(segment[1]); + finalCoords.push(segment[0]); + finalCoords.push(segment[1]); } } // Handling for lines that only have 1 segment if (coords.length === 2) { - result.push(segment[0]); - result.push(segment[1]); + finalCoords.push(segment[0]); + finalCoords.push(segment[1]); } } }); - return lineString(result, line.properties); + return lineString(finalCoords, line.properties); }; /** From 1176d01d9f453b1865e4f8fc6ee12c73142357dd Mon Sep 17 00:00:00 2001 From: Denis Date: Tue, 16 May 2017 21:56:27 -0400 Subject: [PATCH 16/20] Add prevent input mutation tests --- packages/turf-line-offset/test.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/turf-line-offset/test.js b/packages/turf-line-offset/test.js index 5675f1da04..b6b2ce8100 100644 --- a/packages/turf-line-offset/test.js +++ b/packages/turf-line-offset/test.js @@ -39,3 +39,12 @@ test('turf-line-offset - Support Geometry Objects', t => { t.ok(lineOffset(line.geometry, 10), 'Geometry Object'); t.end(); }); + +test('turf-line-offset - Prevent Input Mutation', t => { + const line = lineString([[10, 10], [0, 0]]); + const before = JSON.parse(JSON.stringify(line)); + lineOffset(line.geometry, 10); + + t.deepEqual(line, before, 'input does not mutate'); + t.end(); +}); From 34378db7a11bf191dbdabc459e01ae71c63c3567 Mon Sep 17 00:00:00 2001 From: Denis Date: Tue, 16 May 2017 21:57:12 -0400 Subject: [PATCH 17/20] Separate intersection.js --- packages/turf-line-offset/index.js | 121 ++-------------------- packages/turf-line-offset/intersection.js | 117 +++++++++++++++++++++ packages/turf-line-offset/package.json | 4 +- 3 files changed, 127 insertions(+), 115 deletions(-) create mode 100644 packages/turf-line-offset/intersection.js diff --git a/packages/turf-line-offset/index.js b/packages/turf-line-offset/index.js index d304e2698a..e03ca77229 100644 --- a/packages/turf-line-offset/index.js +++ b/packages/turf-line-offset/index.js @@ -1,6 +1,7 @@ +var helpers = require('@turf/helpers'); var getCoords = require('@turf/invariant').getCoords; var coordEach = require('@turf/meta').coordEach; -var helpers = require('@turf/helpers'); +var intersection = require('./intersection'); var lineString = helpers.lineString; var distanceToDegrees = helpers.distanceToDegrees; @@ -15,16 +16,19 @@ var distanceToDegrees = helpers.distanceToDegrees; * @example * var line = { * "type": "Feature", - * "properties": {}, + * "properties": { + * "stroke": "#F00" + * }, * "geometry": { * "type": "LineString", * "coordinates": [[-83, 30], [-84, 36], [-78, 41]] * } * }; * - * var offsetLine = turf.lineOffset(line, 2, 'miles'); + * var offsetLine = turf.lineOffset(line, 2, "miles"); * * //addToMap + * offsetLine.properties.stroke = "#00F" * var addToMap = [offsetLine, line] */ module.exports = function (line, offset, units) { @@ -84,114 +88,3 @@ function processSegment(point1, point2, offset) { var out2y = point2[1] + offset * (point1[0] - point2[0]) / L; return [[out1x, out1y], [out2x, out2y]]; } - -/** - * AB - * - * @private - * @param {Array>} segment - 2 vertex line segment - * @returns {Array} coordinates [x, y] - */ -function ab(segment) { - var start = segment[0]; - var end = segment[1]; - return [end[0] - start[0], end[1] - start[1]]; -} - -/** - * Cross Product - * - * @private - * @param {Array} v1 coordinates [x, y] - * @param {Array} v2 coordinates [x, y] - * @returns {Array} Cross Product - */ -function crossProduct(v1, v2) { - return (v1[0] * v2[1]) - (v2[0] * v1[1]); -} - -/** - * Add - * - * @private - * @param {Array} v1 coordinates [x, y] - * @param {Array} v2 coordinates [x, y] - * @returns {Array} Add - */ -function add(v1, v2) { - return [v1[0] + v2[0], v1[1] + v2[1]]; -} - -/** - * Sub - * - * @private - * @param {Array} v1 coordinates [x, y] - * @param {Array} v2 coordinates [x, y] - * @returns {Array} Sub - */ -function sub(v1, v2) { - return [v1[0] - v2[0], v1[1] - v2[1]]; -} - -/** - * scalarMult - * - * @private - * @param {number} s scalar - * @param {Array} v coordinates [x, y] - * @returns {Array} scalarMult - */ -function scalarMult(s, v) { - return [s * v[0], s * v[1]]; -} - -/** - * Intersect Segments - * - * @private - * @param {Array} a coordinates [x, y] - * @param {Array} b coordinates [x, y] - * @returns {Array} intersection - */ -function intersectSegments(a, b) { - var p = a[0]; - var r = ab(a); - var q = b[0]; - var s = ab(b); - - var cross = crossProduct(r, s); - var qmp = sub(q, p); - var numerator = crossProduct(qmp, s); - var t = numerator / cross; - var intersection = add(p, scalarMult(t, r)); - return intersection; -} - -/** - * Is Parallel - * - * @private - * @param {Array} a coordinates [x, y] - * @param {Array} b coordinates [x, y] - * @returns {boolean} true if a and b are parallel (or co-linear) - */ -function isParallel(a, b) { - var r = ab(a); - var s = ab(b); - return (crossProduct(r, s) === 0); -} - -/** - * Intersection - * https://github.com/rook2pawn/node-intersection/blob/master/index.js - * - * @private - * @param {Array} a coordinates [x, y] - * @param {Array} b coordinates [x, y] - * @returns {Array|boolean} true if a and b are parallel (or co-linear) - */ -function intersection(a, b) { - if (isParallel(a, b)) return false; - return intersectSegments(a, b); -} diff --git a/packages/turf-line-offset/intersection.js b/packages/turf-line-offset/intersection.js new file mode 100644 index 0000000000..c84e4a89a0 --- /dev/null +++ b/packages/turf-line-offset/intersection.js @@ -0,0 +1,117 @@ +/** + * https://github.com/rook2pawn/node-intersection + * + * Author @rook2pawn + */ + +/** + * AB + * + * @private + * @param {Array>} segment - 2 vertex line segment + * @returns {Array} coordinates [x, y] + */ +function ab(segment) { + var start = segment[0]; + var end = segment[1]; + return [end[0] - start[0], end[1] - start[1]]; +} + +/** + * Cross Product + * + * @private + * @param {Array} v1 coordinates [x, y] + * @param {Array} v2 coordinates [x, y] + * @returns {Array} Cross Product + */ +function crossProduct(v1, v2) { + return (v1[0] * v2[1]) - (v2[0] * v1[1]); +} + +/** + * Add + * + * @private + * @param {Array} v1 coordinates [x, y] + * @param {Array} v2 coordinates [x, y] + * @returns {Array} Add + */ +function add(v1, v2) { + return [v1[0] + v2[0], v1[1] + v2[1]]; +} + +/** + * Sub + * + * @private + * @param {Array} v1 coordinates [x, y] + * @param {Array} v2 coordinates [x, y] + * @returns {Array} Sub + */ +function sub(v1, v2) { + return [v1[0] - v2[0], v1[1] - v2[1]]; +} + +/** + * scalarMult + * + * @private + * @param {number} s scalar + * @param {Array} v coordinates [x, y] + * @returns {Array} scalarMult + */ +function scalarMult(s, v) { + return [s * v[0], s * v[1]]; +} + +/** + * Intersect Segments + * + * @private + * @param {Array} a coordinates [x, y] + * @param {Array} b coordinates [x, y] + * @returns {Array} intersection + */ +function intersectSegments(a, b) { + var p = a[0]; + var r = ab(a); + var q = b[0]; + var s = ab(b); + + var cross = crossProduct(r, s); + var qmp = sub(q, p); + var numerator = crossProduct(qmp, s); + var t = numerator / cross; + var intersection = add(p, scalarMult(t, r)); + return intersection; +} + +/** + * Is Parallel + * + * @private + * @param {Array} a coordinates [x, y] + * @param {Array} b coordinates [x, y] + * @returns {boolean} true if a and b are parallel (or co-linear) + */ +function isParallel(a, b) { + var r = ab(a); + var s = ab(b); + return (crossProduct(r, s) === 0); +} + +/** + * Intersection + * + * @private + * @param {Array} a coordinates [x, y] + * @param {Array} b coordinates [x, y] + * @returns {Array|boolean} true if a and b are parallel (or co-linear) + */ +function intersection(a, b) { + if (isParallel(a, b)) return false; + return intersectSegments(a, b); +} + +module.exports = intersection; diff --git a/packages/turf-line-offset/package.json b/packages/turf-line-offset/package.json index d16ff4458c..6fbd2eddc2 100644 --- a/packages/turf-line-offset/package.json +++ b/packages/turf-line-offset/package.json @@ -6,7 +6,8 @@ "types": "index.d.ts", "files": [ "index.js", - "index.d.ts" + "index.d.ts", + "intersection.js" ], "scripts": { "test": "node test.js", @@ -24,6 +25,7 @@ ], "author": "Turf Authors", "contributors": [ + "David Wee <@rook2pawn>", "Rowan Winsemius <@rowanwins>", "Denis Carriere <@DenisCarriere>" ], From 4199f1a4df118f4b3f7840cdd94bc4de3d970541 Mon Sep 17 00:00:00 2001 From: Denis Date: Tue, 16 May 2017 22:15:07 -0400 Subject: [PATCH 18/20] Add switch statement for Geometry expansion --- packages/turf-line-offset/index.js | 27 +++++++++++++++++++++++---- packages/turf-line-offset/test.js | 9 +++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/packages/turf-line-offset/index.js b/packages/turf-line-offset/index.js index e03ca77229..7eea25885a 100644 --- a/packages/turf-line-offset/index.js +++ b/packages/turf-line-offset/index.js @@ -9,7 +9,7 @@ var distanceToDegrees = helpers.distanceToDegrees; * Takes a {@link LineString|line} and returns a {@link LineString|line} at offset by the specified distance. * * @name lineOffset - * @param {Geometry|Feature} line input line + * @param {Geometry|Feature} geojson input GeoJSON * @param {number} offset distance to offset the line (can be of negative value) * @param {string} [units=kilometers] can be degrees, radians, miles, kilometers, inches, yards, meters * @returns {Feature} Line offset from the input line @@ -31,10 +31,29 @@ var distanceToDegrees = helpers.distanceToDegrees; * offsetLine.properties.stroke = "#00F" * var addToMap = [offsetLine, line] */ -module.exports = function (line, offset, units) { - if (!line) throw new Error('line is required'); +module.exports = function (geojson, offset, units) { + if (!geojson) throw new Error('geojson is required'); if (offset === undefined || offset === null || isNaN(offset)) throw new Error('offset is required'); + var type = (geojson.type === 'Feature') ? geojson.geometry.type : geojson.type; + + switch (type) { + case 'LineString': + return lineOffset(geojson, offset, units); + default: + throw new Error('geometry ' + type + ' is not supported'); + } +}; +/** + * Line Offset + * + * @private + * @param {Geometry|Feature} line input line + * @param {number} offset distance to offset the line (can be of negative value) + * @param {string} [units=kilometers] units + * @returns {Feature} Line offset from the input line + */ +function lineOffset(line, offset, units) { var segments = []; var offsetDegrees = distanceToDegrees(offset, units); var coords = getCoords(line); @@ -67,7 +86,7 @@ module.exports = function (line, offset, units) { } }); return lineString(finalCoords, line.properties); -}; +} /** * Process Segment diff --git a/packages/turf-line-offset/test.js b/packages/turf-line-offset/test.js index b6b2ce8100..678aae2a30 100644 --- a/packages/turf-line-offset/test.js +++ b/packages/turf-line-offset/test.js @@ -29,8 +29,13 @@ test('turf-line-offset', t => { if (process.env.REGEN) write.sync(directories.out + name + '.geojson', results); t.deepEqual(results, load.sync(directories.out + name + '.geojson'), name); } - t.throws(() => lineOffset(), /line is required/); - t.throws(() => lineOffset(lineString([[10, 10], [0, 0]])), /offset is required/); + t.end(); +}); + +test('turf-line-offset - Throws Errors', t => { + const line = lineString([[10, 10], [0, 0]]); + t.throws(() => lineOffset(), /geojson is required/); + t.throws(() => lineOffset(line, /offset is required/)); t.end(); }); From bc34c926a11a424992b9e08643603fa81d54329f Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 17 May 2017 00:29:58 -0400 Subject: [PATCH 19/20] Add MultiLineString support --- packages/turf-line-offset/bench.js | 3 +- packages/turf-line-offset/index.d.ts | 9 +- packages/turf-line-offset/index.js | 31 +++-- packages/turf-line-offset/test.js | 11 +- .../test/in/linestring-same-start-end.geojson | 36 +++++ .../test/in/multi-linestring.geojson | 14 ++ .../test/in/northern-line.geojson | 60 +++++++++ .../out/linestring-same-start-end.geojson | 77 +++++++++++ .../test/out/multi-linestring.geojson | 85 ++++++++++++ .../test/out/northern-line.geojson | 125 ++++++++++++++++++ packages/turf-line-offset/test/types.ts | 11 ++ 11 files changed, 443 insertions(+), 19 deletions(-) create mode 100644 packages/turf-line-offset/test/in/linestring-same-start-end.geojson create mode 100644 packages/turf-line-offset/test/in/multi-linestring.geojson create mode 100644 packages/turf-line-offset/test/in/northern-line.geojson create mode 100644 packages/turf-line-offset/test/out/linestring-same-start-end.geojson create mode 100644 packages/turf-line-offset/test/out/multi-linestring.geojson create mode 100644 packages/turf-line-offset/test/out/northern-line.geojson create mode 100644 packages/turf-line-offset/test/types.ts diff --git a/packages/turf-line-offset/bench.js b/packages/turf-line-offset/bench.js index a0b23f502d..00dce3c4dc 100644 --- a/packages/turf-line-offset/bench.js +++ b/packages/turf-line-offset/bench.js @@ -5,13 +5,14 @@ const Benchmark = require('benchmark'); const lineOffset = require('./'); const directory = path.join(__dirname, 'test', 'in') + path.sep; -const fixtures = fs.readdirSync(directory).map(filename => { +let fixtures = fs.readdirSync(directory).map(filename => { return { filename, name: path.parse(filename).name, geojson: load.sync(directory + filename) }; }); +// fixtures = fixtures.filter(fixture => fixture.name === 'polygon'); /** * Benchmark Results diff --git a/packages/turf-line-offset/index.d.ts b/packages/turf-line-offset/index.d.ts index a79aee9c7b..9fd6b7a973 100644 --- a/packages/turf-line-offset/index.d.ts +++ b/packages/turf-line-offset/index.d.ts @@ -1,11 +1,14 @@ /// -type LineString = GeoJSON.Feature; import {Units} from '@turf/helpers'; +type GeometryObject = GeoJSON.GeometryObject; +type Feature = GeoJSON.Feature; +type Geoms = GeoJSON.LineString | GeoJSON.MultiLineString; + /** - * http://turfjs.org/docs/#lineOffset + * http://turfjs.org/docs/#lineoffset */ -declare function lineOffset(line: LineString | GeoJSON.LineString, distance: number, units?: Units): LineString; +declare function lineOffset(line: Feature | Geom, distance: number, units?: Units): Feature; declare namespace lineOffset { } export = lineOffset; diff --git a/packages/turf-line-offset/index.js b/packages/turf-line-offset/index.js index 7eea25885a..a11ac08b45 100644 --- a/packages/turf-line-offset/index.js +++ b/packages/turf-line-offset/index.js @@ -1,18 +1,20 @@ +var meta = require('@turf/meta'); var helpers = require('@turf/helpers'); var getCoords = require('@turf/invariant').getCoords; -var coordEach = require('@turf/meta').coordEach; var intersection = require('./intersection'); +var flattenEach = meta.flattenEach; var lineString = helpers.lineString; +var multiLineString = helpers.multiLineString; var distanceToDegrees = helpers.distanceToDegrees; /** * Takes a {@link LineString|line} and returns a {@link LineString|line} at offset by the specified distance. * * @name lineOffset - * @param {Geometry|Feature} geojson input GeoJSON - * @param {number} offset distance to offset the line (can be of negative value) + * @param {Geometry|Feature} geojson input GeoJSON + * @param {number} distance distance to offset the line (can be of negative value) * @param {string} [units=kilometers] can be degrees, radians, miles, kilometers, inches, yards, meters - * @returns {Feature} Line offset from the input line + * @returns {Feature} Line offset from the input line * @example * var line = { * "type": "Feature", @@ -31,14 +33,21 @@ var distanceToDegrees = helpers.distanceToDegrees; * offsetLine.properties.stroke = "#00F" * var addToMap = [offsetLine, line] */ -module.exports = function (geojson, offset, units) { +module.exports = function (geojson, distance, units) { if (!geojson) throw new Error('geojson is required'); - if (offset === undefined || offset === null || isNaN(offset)) throw new Error('offset is required'); + if (distance === undefined || distance === null || isNaN(distance)) throw new Error('distance is required'); var type = (geojson.type === 'Feature') ? geojson.geometry.type : geojson.type; + var properties = geojson.properties; switch (type) { case 'LineString': - return lineOffset(geojson, offset, units); + return lineOffset(geojson, distance, units); + case 'MultiLineString': + var coords = []; + flattenEach(geojson, function (feature) { + coords.push(lineOffset(feature, distance, units).geometry.coordinates); + }); + return multiLineString(coords, properties); default: throw new Error('geometry ' + type + ' is not supported'); } @@ -49,16 +58,16 @@ module.exports = function (geojson, offset, units) { * * @private * @param {Geometry|Feature} line input line - * @param {number} offset distance to offset the line (can be of negative value) + * @param {number} distance distance to offset the line (can be of negative value) * @param {string} [units=kilometers] units * @returns {Feature} Line offset from the input line */ -function lineOffset(line, offset, units) { +function lineOffset(line, distance, units) { var segments = []; - var offsetDegrees = distanceToDegrees(offset, units); + var offsetDegrees = distanceToDegrees(distance, units); var coords = getCoords(line); var finalCoords = []; - coordEach(line, function (currentCoords, index) { + coords.forEach(function (currentCoords, index) { if (index !== coords.length - 1) { var segment = processSegment(currentCoords, coords[index + 1], offsetDegrees); segments.push(segment); diff --git a/packages/turf-line-offset/test.js b/packages/turf-line-offset/test.js index 678aae2a30..96741c2c1c 100644 --- a/packages/turf-line-offset/test.js +++ b/packages/turf-line-offset/test.js @@ -12,19 +12,22 @@ const directories = { out: path.join(__dirname, 'test', 'out') + path.sep }; -const fixtures = fs.readdirSync(directories.in).map(filename => { +let fixtures = fs.readdirSync(directories.in).map(filename => { return { filename, name: path.parse(filename).name, geojson: load.sync(directories.in + filename) }; }); +// fixtures = fixtures.filter(fixture => fixture.name === 'polygon'); test('turf-line-offset', t => { for (const {name, geojson} of fixtures) { - const offset = truncate(lineOffset(geojson, 50, 'kilometers'), 4); - offset.properties.stroke = '#00F'; - const results = featureCollection([offset, geojson]); + let {distance, units} = geojson.properties || {}; + distance = distance || 50; + const output = truncate(lineOffset(geojson, distance, units), 4); + output.properties.stroke = '#00F'; + const results = featureCollection([output, geojson]); if (process.env.REGEN) write.sync(directories.out + name + '.geojson', results); t.deepEqual(results, load.sync(directories.out + name + '.geojson'), name); diff --git a/packages/turf-line-offset/test/in/linestring-same-start-end.geojson b/packages/turf-line-offset/test/in/linestring-same-start-end.geojson new file mode 100644 index 0000000000..f7b5f7b1af --- /dev/null +++ b/packages/turf-line-offset/test/in/linestring-same-start-end.geojson @@ -0,0 +1,36 @@ +{ + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 122.431640625, + -19.559790136497398 + ], + [ + 122.51953124999999, + -26.902476886279807 + ], + [ + 134.82421875, + -27.215556209029675 + ], + [ + 139.306640625, + -22.998851594142913 + ], + [ + 133.9453125, + -14.434680215297268 + ], + [ + 122.431640625, + -19.559790136497398 + ] + ] + } +} \ No newline at end of file diff --git a/packages/turf-line-offset/test/in/multi-linestring.geojson b/packages/turf-line-offset/test/in/multi-linestring.geojson new file mode 100644 index 0000000000..dc64a6014f --- /dev/null +++ b/packages/turf-line-offset/test/in/multi-linestring.geojson @@ -0,0 +1,14 @@ +{ + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "MultiLineString", + "coordinates": [ + [[10, 10], [2, 2], [0, 0]], + [[25, 5], [20, 10], [15, 5]] + ] + } +} diff --git a/packages/turf-line-offset/test/in/northern-line.geojson b/packages/turf-line-offset/test/in/northern-line.geojson new file mode 100644 index 0000000000..ecb97bfaa1 --- /dev/null +++ b/packages/turf-line-offset/test/in/northern-line.geojson @@ -0,0 +1,60 @@ +{ + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -93.66943359374999, + 75.2782047773442 + ], + [ + -94.15283203125, + 75.52189820596192 + ], + [ + -94.68017578125, + 75.60133818586581 + ], + [ + -95.64697265625, + 75.55208098028335 + ], + [ + -95.8447265625, + 75.37837872661018 + ], + [ + -96.339111328125, + 75.10975495781904 + ], + [ + -96.251220703125, + 74.89940428652537 + ], + [ + -95.20751953125, + 74.73829331009176 + ], + [ + -94.50439453125, + 74.63965846462719 + ], + [ + -93.878173828125, + 74.65129477919845 + ], + [ + -93.526611328125, + 74.73250841433554 + ], + [ + -93.50463867187499, + 75.09845822124332 + ] + ] + } +} \ No newline at end of file diff --git a/packages/turf-line-offset/test/out/linestring-same-start-end.geojson b/packages/turf-line-offset/test/out/linestring-same-start-end.geojson new file mode 100644 index 0000000000..9d60ae863c --- /dev/null +++ b/packages/turf-line-offset/test/out/linestring-same-start-end.geojson @@ -0,0 +1,77 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 121.9822, + -19.5652 + ], + [ + 122.0752, + -27.3408 + ], + [ + 134.9976, + -27.6696 + ], + [ + 139.8836, + -23.0733 + ], + [ + 134.1192, + -13.8652 + ], + [ + 122.2488, + -19.1491 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 122.431640625, + -19.559790136497398 + ], + [ + 122.51953124999999, + -26.902476886279807 + ], + [ + 134.82421875, + -27.215556209029675 + ], + [ + 139.306640625, + -22.998851594142913 + ], + [ + 133.9453125, + -14.434680215297268 + ], + [ + 122.431640625, + -19.559790136497398 + ] + ] + } + } + ] +} diff --git a/packages/turf-line-offset/test/out/multi-linestring.geojson b/packages/turf-line-offset/test/out/multi-linestring.geojson new file mode 100644 index 0000000000..cc7c5d6340 --- /dev/null +++ b/packages/turf-line-offset/test/out/multi-linestring.geojson @@ -0,0 +1,85 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "MultiLineString", + "coordinates": [ + [ + [ + 9.6821, + 10.3179 + ], + [ + 1.6821, + 2.3179 + ], + [ + -0.3179, + 0.3179 + ] + ], + [ + [ + 25.3179, + 5.3179 + ], + [ + 20, + 10.6357 + ], + [ + 14.6821, + 5.3179 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "MultiLineString", + "coordinates": [ + [ + [ + 10, + 10 + ], + [ + 2, + 2 + ], + [ + 0, + 0 + ] + ], + [ + [ + 25, + 5 + ], + [ + 20, + 10 + ], + [ + 15, + 5 + ] + ] + ] + } + } + ] +} diff --git a/packages/turf-line-offset/test/out/northern-line.geojson b/packages/turf-line-offset/test/out/northern-line.geojson new file mode 100644 index 0000000000..bbc616bbdb --- /dev/null +++ b/packages/turf-line-offset/test/out/northern-line.geojson @@ -0,0 +1,125 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -93.4671, + 75.6796 + ], + [ + -94.0147, + 75.9557 + ], + [ + -94.6579, + 76.0526 + ], + [ + -95.8261, + 75.9931 + ], + [ + -96.1036, + 75.7493 + ], + [ + -96.9104, + 75.311 + ], + [ + -96.5688, + 74.4936 + ], + [ + -95.273, + 74.2936 + ], + [ + -94.5316, + 74.1896 + ], + [ + -93.8228, + 74.2027 + ], + [ + -93.098, + 74.3702 + ], + [ + -93.0559, + 75.0715 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -93.66943359374999, + 75.2782047773442 + ], + [ + -94.15283203125, + 75.52189820596192 + ], + [ + -94.68017578125, + 75.60133818586581 + ], + [ + -95.64697265625, + 75.55208098028335 + ], + [ + -95.8447265625, + 75.37837872661018 + ], + [ + -96.339111328125, + 75.10975495781904 + ], + [ + -96.251220703125, + 74.89940428652537 + ], + [ + -95.20751953125, + 74.73829331009176 + ], + [ + -94.50439453125, + 74.63965846462719 + ], + [ + -93.878173828125, + 74.65129477919845 + ], + [ + -93.526611328125, + 74.73250841433554 + ], + [ + -93.50463867187499, + 75.09845822124332 + ] + ] + } + } + ] +} diff --git a/packages/turf-line-offset/test/types.ts b/packages/turf-line-offset/test/types.ts new file mode 100644 index 0000000000..358953f71c --- /dev/null +++ b/packages/turf-line-offset/test/types.ts @@ -0,0 +1,11 @@ +import * as lineOffset from '../' +import {lineString, multiLineString} from '@turf/helpers' + +const line = lineString([[0, 0], [10, 10]]) +const multiLine = multiLineString([[[0, 0], [10, 10]], [[5, 5], [15, 15]]]) + +lineOffset(line, 50) +lineOffset(line.geometry, 50) +lineOffset(multiLine, 50) +lineOffset(multiLine.geometry, 50) +lineOffset(line, 50, 'miles') From a82fd5b36dc463e7408c90cefaf489e4d4c6db70 Mon Sep 17 00:00:00 2001 From: stebogit Date: Thu, 18 May 2017 22:33:17 -0700 Subject: [PATCH 20/20] added line-concave test; renamed single-segment-only --- .../test/in/line-concave.geojson | 38 +++++++++ ...=> linestring-single-segment-only.geojson} | 0 .../test/out/line-concave.geojson | 81 +++++++++++++++++++ ...=> linestring-single-segment-only.geojson} | 0 4 files changed, 119 insertions(+) create mode 100644 packages/turf-line-offset/test/in/line-concave.geojson rename packages/turf-line-offset/test/in/{linestring-singleSegmentOnly.geojson => linestring-single-segment-only.geojson} (100%) create mode 100644 packages/turf-line-offset/test/out/line-concave.geojson rename packages/turf-line-offset/test/out/{linestring-singleSegmentOnly.geojson => linestring-single-segment-only.geojson} (100%) diff --git a/packages/turf-line-offset/test/in/line-concave.geojson b/packages/turf-line-offset/test/in/line-concave.geojson new file mode 100644 index 0000000000..6de65e3ac3 --- /dev/null +++ b/packages/turf-line-offset/test/in/line-concave.geojson @@ -0,0 +1,38 @@ +{ + "type": "Feature", + "properties": { + "stroke-width": 5, + "stroke": "red", + "distance": 150, + "units": "miles" + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 0.9667968749999999, + 21.69826549685252 + ], + [ + -2.5927734375, + 24.44714958973082 + ], + [ + 0.3955078125, + 29.726222319395504 + ], + [ + 7.207031249999999, + 29.916852233070173 + ], + [ + 11.337890625, + 23.079731762449878 + ], + [ + 3.2080078125, + 21.04349121680354 + ] + ] + } +} \ No newline at end of file diff --git a/packages/turf-line-offset/test/in/linestring-singleSegmentOnly.geojson b/packages/turf-line-offset/test/in/linestring-single-segment-only.geojson similarity index 100% rename from packages/turf-line-offset/test/in/linestring-singleSegmentOnly.geojson rename to packages/turf-line-offset/test/in/linestring-single-segment-only.geojson diff --git a/packages/turf-line-offset/test/out/line-concave.geojson b/packages/turf-line-offset/test/out/line-concave.geojson new file mode 100644 index 0000000000..8885f2bdb2 --- /dev/null +++ b/packages/turf-line-offset/test/out/line-concave.geojson @@ -0,0 +1,81 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke-width": 5, + "stroke": "#00F", + "distance": 150, + "units": "miles" + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 2.2933, + 23.416 + ], + [ + 0.2226, + 25.0151 + ], + [ + 1.6807, + 27.591 + ], + [ + 6.0035, + 27.712 + ], + [ + 7.9614, + 24.4714 + ], + [ + 2.6807, + 23.1488 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke-width": 5, + "stroke": "red", + "distance": 150, + "units": "miles" + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 0.9667968749999999, + 21.69826549685252 + ], + [ + -2.5927734375, + 24.44714958973082 + ], + [ + 0.3955078125, + 29.726222319395504 + ], + [ + 7.207031249999999, + 29.916852233070173 + ], + [ + 11.337890625, + 23.079731762449878 + ], + [ + 3.2080078125, + 21.04349121680354 + ] + ] + } + } + ] +} \ No newline at end of file diff --git a/packages/turf-line-offset/test/out/linestring-singleSegmentOnly.geojson b/packages/turf-line-offset/test/out/linestring-single-segment-only.geojson similarity index 100% rename from packages/turf-line-offset/test/out/linestring-singleSegmentOnly.geojson rename to packages/turf-line-offset/test/out/linestring-single-segment-only.geojson