From b0a4630eb451661fd98ceb822c5c10c84bd56e3b Mon Sep 17 00:00:00 2001 From: Denis Date: Sun, 16 Apr 2017 01:21:43 -0400 Subject: [PATCH 1/5] Initial module publish turf-linestring-to-polygon --- packages/turf-linestring-to-polygon/LICENSE | 21 +++ packages/turf-linestring-to-polygon/README.md | 54 +++++++ packages/turf-linestring-to-polygon/bench.js | 36 +++++ .../turf-linestring-to-polygon/index.d.ts | 16 ++ packages/turf-linestring-to-polygon/index.js | 91 +++++++++++ .../turf-linestring-to-polygon/package.json | 42 +++++ packages/turf-linestring-to-polygon/test.js | 36 +++++ .../test/in/collection-linestring.geojson | 69 ++++++++ .../test/in/geometry-linestring.geojson | 25 +++ .../test/in/linestring-incomplete.geojson | 41 +++++ .../test/in/linestring.geojson | 33 ++++ .../in/multi-linestring-incomplete.geojson | 65 ++++++++ .../in/multi-linestring-with-hole.geojson | 56 +++++++ .../in/multi-linestrings-with-holes.geojson | 149 +++++++++++++++++ .../test/out/collection-linestring.geojson | 73 +++++++++ .../test/out/geometry-linestring.geojson | 31 ++++ .../test/out/linestring-incomplete.geojson | 47 ++++++ .../test/out/linestring.geojson | 34 ++++ .../out/multi-linestring-incomplete.geojson | 73 +++++++++ .../out/multi-linestring-with-hole.geojson | 56 +++++++ .../out/multi-linestrings-with-holes.geojson | 151 ++++++++++++++++++ .../turf-linestring-to-polygon/test/types.ts | 8 + 22 files changed, 1207 insertions(+) create mode 100644 packages/turf-linestring-to-polygon/LICENSE create mode 100644 packages/turf-linestring-to-polygon/README.md create mode 100644 packages/turf-linestring-to-polygon/bench.js create mode 100644 packages/turf-linestring-to-polygon/index.d.ts create mode 100644 packages/turf-linestring-to-polygon/index.js create mode 100644 packages/turf-linestring-to-polygon/package.json create mode 100644 packages/turf-linestring-to-polygon/test.js create mode 100644 packages/turf-linestring-to-polygon/test/in/collection-linestring.geojson create mode 100644 packages/turf-linestring-to-polygon/test/in/geometry-linestring.geojson create mode 100644 packages/turf-linestring-to-polygon/test/in/linestring-incomplete.geojson create mode 100644 packages/turf-linestring-to-polygon/test/in/linestring.geojson create mode 100644 packages/turf-linestring-to-polygon/test/in/multi-linestring-incomplete.geojson create mode 100644 packages/turf-linestring-to-polygon/test/in/multi-linestring-with-hole.geojson create mode 100644 packages/turf-linestring-to-polygon/test/in/multi-linestrings-with-holes.geojson create mode 100644 packages/turf-linestring-to-polygon/test/out/collection-linestring.geojson create mode 100644 packages/turf-linestring-to-polygon/test/out/geometry-linestring.geojson create mode 100644 packages/turf-linestring-to-polygon/test/out/linestring-incomplete.geojson create mode 100644 packages/turf-linestring-to-polygon/test/out/linestring.geojson create mode 100644 packages/turf-linestring-to-polygon/test/out/multi-linestring-incomplete.geojson create mode 100644 packages/turf-linestring-to-polygon/test/out/multi-linestring-with-hole.geojson create mode 100644 packages/turf-linestring-to-polygon/test/out/multi-linestrings-with-holes.geojson create mode 100644 packages/turf-linestring-to-polygon/test/types.ts diff --git a/packages/turf-linestring-to-polygon/LICENSE b/packages/turf-linestring-to-polygon/LICENSE new file mode 100644 index 0000000000..7042f5203c --- /dev/null +++ b/packages/turf-linestring-to-polygon/LICENSE @@ -0,0 +1,21 @@ +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-linestring-to-polygon/README.md b/packages/turf-linestring-to-polygon/README.md new file mode 100644 index 0000000000..547dc86603 --- /dev/null +++ b/packages/turf-linestring-to-polygon/README.md @@ -0,0 +1,54 @@ +# @turf/linestring-to-polygon + +# lineStringToPolygon + +Converts a [LineString](http://geojson.org/geojson-spec.html#linestring) or [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring) to a [Polygon](http://geojson.org/geojson-spec.html#polygon). + +**Parameters** + +- `lines` **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<([LineString](http://geojson.org/geojson-spec.html#linestring) \| [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring))>)** Features to convert +- `autoComplete` **\[[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** auto complete linestrings (optional, default `true`) + +**Examples** + +```javascript +var line = { + 'type': 'Feature', + 'properties': {}, + 'geometry': { + 'type': 'LineString', + 'coordinates': [[125, -30], [145, -30], [145, -20], [125, -20], [125, -30]] + } +} +var polygon = turf.lineStringToPolygon(line); + +//addToMap +var addToMap = [polygon]; +``` + +Returns **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<[Polygon](http://geojson.org/geojson-spec.html#polygon)>)** converted to Polygons + + + +--- + +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/linestring-to-polygon +``` + +Or install the Turf module that includes it as a function: + +```sh +$ npm install @turf/turf +``` diff --git a/packages/turf-linestring-to-polygon/bench.js b/packages/turf-linestring-to-polygon/bench.js new file mode 100644 index 0000000000..0730fa931c --- /dev/null +++ b/packages/turf-linestring-to-polygon/bench.js @@ -0,0 +1,36 @@ +const Benchmark = require('benchmark'); +const path = require('path'); +const fs = require('fs'); +const load = require('load-json-file'); +const lineStringToPolygon = require('./'); + +const directory = path.join(__dirname, 'test', 'in') + path.sep; +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 === 'multi-linestrings-with-holes'); + +/** + * Benchmark Results + * + * collection-linestring x 2,337,816 ops/sec ±3.08% (86 runs sampled) + * geometry-linestring x 6,574,088 ops/sec ±3.62% (83 runs sampled) + * linestring-incomplete x 6,768,527 ops/sec ±3.60% (84 runs sampled) + * linestring x 6,752,969 ops/sec ±1.94% (84 runs sampled) + * multi-linestring-incomplete x 3,263,278 ops/sec ±2.56% (88 runs sampled) + * multi-linestring-with-hole x 3,381,728 ops/sec ±1.75% (88 runs sampled) + * multi-linestrings-with-holes x 1,084,043 ops/sec ±2.28% (83 runs sampled) + */ +const suite = new Benchmark.Suite('turf-linestring-to-polygon'); +for (const {name, geojson} of fixtures) { + suite.add(name, () => lineStringToPolygon(geojson)); +} + +suite + .on('cycle', e => console.log(String(e.target))) + .on('complete', () => {}) + .run(); diff --git a/packages/turf-linestring-to-polygon/index.d.ts b/packages/turf-linestring-to-polygon/index.d.ts new file mode 100644 index 0000000000..04f1c16626 --- /dev/null +++ b/packages/turf-linestring-to-polygon/index.d.ts @@ -0,0 +1,16 @@ +/// + +import { + Polygon, + Polygons, + LineString, + LineStrings, + MultiLineString, + MultiLineStrings} from '@turf/helpers' + +interface LineStringToPolygon { + (lines: LineString | MultiLineString): Polygon + (lines: LineStrings | MultiLineStrings): Polygons +} +declare const lineStringToPolygon: LineStringToPolygon; +export = lineStringToPolygon; diff --git a/packages/turf-linestring-to-polygon/index.js b/packages/turf-linestring-to-polygon/index.js new file mode 100644 index 0000000000..f22018f0fd --- /dev/null +++ b/packages/turf-linestring-to-polygon/index.js @@ -0,0 +1,91 @@ +var getCoords = require('@turf/invariant').getCoords; +var helpers = require('@turf/helpers'); +var polygon = helpers.polygon; +var featureCollection = helpers.featureCollection; + +/** + * Converts a {@link LineString} or {@link MultiLineString} to a {@link Polygon}. + * + * @name lineStringToPolygon + * @param {FeatureCollection|Feature} lines Features to convert + * @param {boolean} [autoComplete=true] auto complete linestrings + * @returns {FeatureCollection|Feature} converted to Polygons + * @example + * var line = { + * 'type': 'Feature', + * 'properties': {}, + * 'geometry': { + * 'type': 'LineString', + * 'coordinates': [[125, -30], [145, -30], [145, -20], [125, -20], [125, -30]] + * } + * } + * var polygon = turf.lineStringToPolygon(line); + * + * //addToMap + * var addToMap = [polygon]; + */ +module.exports = function (lines, autoComplete) { + // validation + if (!lines) throw new Error('lines is required'); + + // default params + autoComplete = (autoComplete !== undefined) ? autoComplete : true; + var type = geomType(lines); + + switch (type) { + case 'FeatureCollection': + case 'GeometryCollection': + var results = []; + var features = (lines.features) ? lines.features : lines.geometries; + features.forEach(function (line) { + results.push(lineStringToPolygon(line, autoComplete)); + }); + return featureCollection(results); + } + return lineStringToPolygon(lines, autoComplete); +}; + +/** + * LineString to Polygon + * + * @private + * @param {Feature} line line + * @param {boolean} [autoComplete=true] auto complete linestrings + * @returns {Feature} line converted to Polygon + */ +function lineStringToPolygon(line, autoComplete) { + var properties = line.properties; + var coords = getCoords(line); + var type = geomType(line); + if (!coords.length) throw new Error('line must contain coordinates'); + + switch (type) { + case 'LineString': + if (autoComplete) coords = autoCompleteCoords(coords); + return polygon([coords], properties); + case 'MultiLineString': + if (autoComplete) coords.forEach(function (coord) { + coord = autoCompleteCoords(coord); + }); + return polygon(coords, properties); + default: + throw new Error('geometry type ' + type + ' is not supported'); + } +} + +function geomType(feature) { + return (feature.geometry) ? feature.geometry.type : feature.type; +} + +function autoCompleteCoords(coords) { + var first = coords[0]; + var x1 = first[0]; + var y1 = first[1]; + var last = coords[coords.length - 1]; + var x2 = last[0]; + var y2 = last[1]; + if (x1 !== x2 || y1 !== y2) { + coords.push(first); + } + return coords; +} diff --git a/packages/turf-linestring-to-polygon/package.json b/packages/turf-linestring-to-polygon/package.json new file mode 100644 index 0000000000..a4aacc6dc8 --- /dev/null +++ b/packages/turf-linestring-to-polygon/package.json @@ -0,0 +1,42 @@ +{ + "name": "@turf/linestring-to-polygon", + "version": "4.0.0", + "description": "turf linestring-to-polygon 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": [ + "turf", + "gis" + ], + "author": "Turf Authors", + "contributors": [ + "Denis Carriere <@DenisCarriere>" + ], + "license": "MIT", + "bugs": { + "url": "https://github.com/Turfjs/turf/issues" + }, + "homepage": "https://github.com/Turfjs/turf", + "devDependencies": { + "benchmark": "^2.1.3", + "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.1.0" + } +} diff --git a/packages/turf-linestring-to-polygon/test.js b/packages/turf-linestring-to-polygon/test.js new file mode 100644 index 0000000000..44bcaa4c12 --- /dev/null +++ b/packages/turf-linestring-to-polygon/test.js @@ -0,0 +1,36 @@ +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 {point, lineString} = require('@turf/helpers'); +const lineStringToPolygon = require('./'); + +const directories = { + in: path.join(__dirname, 'test', 'in') + path.sep, + out: path.join(__dirname, 'test', 'out') + path.sep +}; + +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 === 'multi-linestrings-with-holes'); + +test('turf-linestring-to-polygon', t => { + for (const {name, filename, geojson} of fixtures) { + const results = lineStringToPolygon(geojson); + + if (process.env.REGEN) write.sync(directories.out + filename, results); + t.deepEqual(load.sync(directories.out + filename), results, name); + } + // Handle Errors + t.throws(() => lineStringToPolygon(point([10, 5])), 'throws - invalid geometry'); + t.throws(() => lineStringToPolygon(lineString([])), 'throws - empty coordinates'); + t.end(); +}); + + diff --git a/packages/turf-linestring-to-polygon/test/in/collection-linestring.geojson b/packages/turf-linestring-to-polygon/test/in/collection-linestring.geojson new file mode 100644 index 0000000000..4b4b5c9509 --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/in/collection-linestring.geojson @@ -0,0 +1,69 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "name": "poly1", + "population": 400 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 102, + 2 + ], + [ + 103, + 2 + ], + [ + 103, + 3 + ], + [ + 102, + 3 + ], + [ + 102, + 2 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "name": "poly1", + "population": 400 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 100, + 0 + ], + [ + 101, + 0 + ], + [ + 101, + 1 + ], + [ + 100, + 1 + ], + [ + 100, + 0 + ] + ] + } + } + ] +} diff --git a/packages/turf-linestring-to-polygon/test/in/geometry-linestring.geojson b/packages/turf-linestring-to-polygon/test/in/geometry-linestring.geojson new file mode 100644 index 0000000000..9eaf29e41d --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/in/geometry-linestring.geojson @@ -0,0 +1,25 @@ +{ + "type": "LineString", + "coordinates": [ + [ + -2.275543, + 53.464547 + ], + [ + -2.275543, + 53.489271 + ], + [ + -2.215118, + 53.489271 + ], + [ + -2.215118, + 53.464547 + ], + [ + -2.275543, + 53.464547 + ] + ] +} \ No newline at end of file diff --git a/packages/turf-linestring-to-polygon/test/in/linestring-incomplete.geojson b/packages/turf-linestring-to-polygon/test/in/linestring-incomplete.geojson new file mode 100644 index 0000000000..e862649925 --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/in/linestring-incomplete.geojson @@ -0,0 +1,41 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 131.0009765625, + -30.939924331023455 + ], + [ + 125.46386718749999, + -29.878755346037963 + ], + [ + 121.77246093750001, + -26.07652055985696 + ], + [ + 121.5087890625, + -21.902277966668624 + ], + [ + 127.79296875, + -16.1724728083975 + ], + [ + 133.154296875, + -14.944784875088372 + ], + [ + 137.4169921875, + -20.097206227083888 + ], + [ + 138.515625, + -26.82407078047018 + ] + ] + } +} \ No newline at end of file diff --git a/packages/turf-linestring-to-polygon/test/in/linestring.geojson b/packages/turf-linestring-to-polygon/test/in/linestring.geojson new file mode 100644 index 0000000000..90b5a3d22f --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/in/linestring.geojson @@ -0,0 +1,33 @@ + +{ + "type": "Feature", + "properties": { + "name": "poly1", + "population": 400 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -2.275543, + 53.464547 + ], + [ + -2.275543, + 53.489271 + ], + [ + -2.215118, + 53.489271 + ], + [ + -2.215118, + 53.464547 + ], + [ + -2.275543, + 53.464547 + ] + ] + } +} diff --git a/packages/turf-linestring-to-polygon/test/in/multi-linestring-incomplete.geojson b/packages/turf-linestring-to-polygon/test/in/multi-linestring-incomplete.geojson new file mode 100644 index 0000000000..1d7d285d6b --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/in/multi-linestring-incomplete.geojson @@ -0,0 +1,65 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "MultiLineString", + "coordinates": [ + [ + [ + 129.28710937499997, + -31.57853542647337 + ], + [ + 120.05859375, + -26.35249785815401 + ], + [ + 120.05859375, + -19.72534224805787 + ], + [ + 124.62890625, + -17.056784609942543 + ], + [ + 131.572265625, + -17.224758206624628 + ], + [ + 133.330078125, + -23.644524198573677 + ] + ], + [ + [ + 145.01953124999997, + -27.137368359795584 + ], + [ + 142.734375, + -29.535229562948444 + ], + [ + 139.306640625, + -28.690587654250685 + ], + [ + 137.8125, + -22.43134015636061 + ], + [ + 137.98828125, + -17.392579271057766 + ], + [ + 144.228515625, + -17.056784609942543 + ], + [ + 146.07421875, + -18.145851771694467 + ] + ] + ] + } +} \ No newline at end of file diff --git a/packages/turf-linestring-to-polygon/test/in/multi-linestring-with-hole.geojson b/packages/turf-linestring-to-polygon/test/in/multi-linestring-with-hole.geojson new file mode 100644 index 0000000000..d21fdb65a0 --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/in/multi-linestring-with-hole.geojson @@ -0,0 +1,56 @@ +{ + "type": "Feature", + "properties": { + "name": "poly1", + "population": 400 + }, + "geometry": { + "type": "MultiLineString", + "coordinates": [ + [ + [ + 100, + 0 + ], + [ + 101, + 0 + ], + [ + 101, + 1 + ], + [ + 100, + 1 + ], + [ + 100, + 0 + ] + ], + [ + [ + 100.2, + 0.2 + ], + [ + 100.8, + 0.2 + ], + [ + 100.8, + 0.8 + ], + [ + 100.2, + 0.8 + ], + [ + 100.2, + 0.2 + ] + ] + ] + } +} \ No newline at end of file diff --git a/packages/turf-linestring-to-polygon/test/in/multi-linestrings-with-holes.geojson b/packages/turf-linestring-to-polygon/test/in/multi-linestrings-with-holes.geojson new file mode 100644 index 0000000000..7a7bc8d796 --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/in/multi-linestrings-with-holes.geojson @@ -0,0 +1,149 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "name": "poly1", + "population": 400 + }, + "geometry": { + "type": "MultiLineString", + "coordinates": [ + [ + [ + 102, + 2 + ], + [ + 103, + 2 + ], + [ + 103, + 3 + ], + [ + 102, + 3 + ], + [ + 102, + 2 + ] + ], + [ + [ + 102.227783203125, + 2.191238104506552 + ], + [ + 102.227783203125, + 2.8223442468940902 + ], + [ + 102.843017578125, + 2.8223442468940902 + ], + [ + 102.843017578125, + 2.191238104506552 + ], + [ + 102.227783203125, + 2.191238104506552 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "name": "poly1", + "population": 400 + }, + "geometry": { + "type": "MultiLineString", + "coordinates": [ + [ + [ + 100, + 0 + ], + [ + 101, + 0 + ], + [ + 101, + 1 + ], + [ + 100, + 1 + ], + [ + 100, + 0 + ] + ], + [ + [ + 100.206298828125, + 0.2526847277643438 + ], + [ + 100.206298828125, + 0.7909904981540058 + ], + [ + 100.8050537109375, + 0.7909904981540058 + ], + [ + 100.8050537109375, + 0.2526847277643438 + ], + [ + 100.206298828125, + 0.2526847277643438 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "name": "poly1", + "population": 400 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 101.700439453125, + 0.5273363048115169 + ], + [ + 102.645263671875, + 0.5273363048115169 + ], + [ + 102.645263671875, + 1.3511930983018892 + ], + [ + 101.700439453125, + 1.3511930983018892 + ], + [ + 101.700439453125, + 0.5273363048115169 + ] + ] + } + } + ] +} diff --git a/packages/turf-linestring-to-polygon/test/out/collection-linestring.geojson b/packages/turf-linestring-to-polygon/test/out/collection-linestring.geojson new file mode 100644 index 0000000000..d127ea24b2 --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/out/collection-linestring.geojson @@ -0,0 +1,73 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "name": "poly1", + "population": 400 + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 102, + 2 + ], + [ + 103, + 2 + ], + [ + 103, + 3 + ], + [ + 102, + 3 + ], + [ + 102, + 2 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "name": "poly1", + "population": 400 + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 100, + 0 + ], + [ + 101, + 0 + ], + [ + 101, + 1 + ], + [ + 100, + 1 + ], + [ + 100, + 0 + ] + ] + ] + } + } + ] +} diff --git a/packages/turf-linestring-to-polygon/test/out/geometry-linestring.geojson b/packages/turf-linestring-to-polygon/test/out/geometry-linestring.geojson new file mode 100644 index 0000000000..7be1a77cbf --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/out/geometry-linestring.geojson @@ -0,0 +1,31 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + -2.275543, + 53.464547 + ], + [ + -2.275543, + 53.489271 + ], + [ + -2.215118, + 53.489271 + ], + [ + -2.215118, + 53.464547 + ], + [ + -2.275543, + 53.464547 + ] + ] + ] + } +} diff --git a/packages/turf-linestring-to-polygon/test/out/linestring-incomplete.geojson b/packages/turf-linestring-to-polygon/test/out/linestring-incomplete.geojson new file mode 100644 index 0000000000..34bba44b9e --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/out/linestring-incomplete.geojson @@ -0,0 +1,47 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 131.0009765625, + -30.939924331023455 + ], + [ + 125.46386718749999, + -29.878755346037963 + ], + [ + 121.77246093750001, + -26.07652055985696 + ], + [ + 121.5087890625, + -21.902277966668624 + ], + [ + 127.79296875, + -16.1724728083975 + ], + [ + 133.154296875, + -14.944784875088372 + ], + [ + 137.4169921875, + -20.097206227083888 + ], + [ + 138.515625, + -26.82407078047018 + ], + [ + 131.0009765625, + -30.939924331023455 + ] + ] + ] + } +} diff --git a/packages/turf-linestring-to-polygon/test/out/linestring.geojson b/packages/turf-linestring-to-polygon/test/out/linestring.geojson new file mode 100644 index 0000000000..09670a8a7d --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/out/linestring.geojson @@ -0,0 +1,34 @@ +{ + "type": "Feature", + "properties": { + "name": "poly1", + "population": 400 + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + -2.275543, + 53.464547 + ], + [ + -2.275543, + 53.489271 + ], + [ + -2.215118, + 53.489271 + ], + [ + -2.215118, + 53.464547 + ], + [ + -2.275543, + 53.464547 + ] + ] + ] + } +} diff --git a/packages/turf-linestring-to-polygon/test/out/multi-linestring-incomplete.geojson b/packages/turf-linestring-to-polygon/test/out/multi-linestring-incomplete.geojson new file mode 100644 index 0000000000..9da0b7a48b --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/out/multi-linestring-incomplete.geojson @@ -0,0 +1,73 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 129.28710937499997, + -31.57853542647337 + ], + [ + 120.05859375, + -26.35249785815401 + ], + [ + 120.05859375, + -19.72534224805787 + ], + [ + 124.62890625, + -17.056784609942543 + ], + [ + 131.572265625, + -17.224758206624628 + ], + [ + 133.330078125, + -23.644524198573677 + ], + [ + 129.28710937499997, + -31.57853542647337 + ] + ], + [ + [ + 145.01953124999997, + -27.137368359795584 + ], + [ + 142.734375, + -29.535229562948444 + ], + [ + 139.306640625, + -28.690587654250685 + ], + [ + 137.8125, + -22.43134015636061 + ], + [ + 137.98828125, + -17.392579271057766 + ], + [ + 144.228515625, + -17.056784609942543 + ], + [ + 146.07421875, + -18.145851771694467 + ], + [ + 145.01953124999997, + -27.137368359795584 + ] + ] + ] + } +} diff --git a/packages/turf-linestring-to-polygon/test/out/multi-linestring-with-hole.geojson b/packages/turf-linestring-to-polygon/test/out/multi-linestring-with-hole.geojson new file mode 100644 index 0000000000..308ee22477 --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/out/multi-linestring-with-hole.geojson @@ -0,0 +1,56 @@ +{ + "type": "Feature", + "properties": { + "name": "poly1", + "population": 400 + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 100, + 0 + ], + [ + 101, + 0 + ], + [ + 101, + 1 + ], + [ + 100, + 1 + ], + [ + 100, + 0 + ] + ], + [ + [ + 100.2, + 0.2 + ], + [ + 100.8, + 0.2 + ], + [ + 100.8, + 0.8 + ], + [ + 100.2, + 0.8 + ], + [ + 100.2, + 0.2 + ] + ] + ] + } +} diff --git a/packages/turf-linestring-to-polygon/test/out/multi-linestrings-with-holes.geojson b/packages/turf-linestring-to-polygon/test/out/multi-linestrings-with-holes.geojson new file mode 100644 index 0000000000..cd81ba9f31 --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/out/multi-linestrings-with-holes.geojson @@ -0,0 +1,151 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "name": "poly1", + "population": 400 + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 102, + 2 + ], + [ + 103, + 2 + ], + [ + 103, + 3 + ], + [ + 102, + 3 + ], + [ + 102, + 2 + ] + ], + [ + [ + 102.227783203125, + 2.191238104506552 + ], + [ + 102.227783203125, + 2.8223442468940902 + ], + [ + 102.843017578125, + 2.8223442468940902 + ], + [ + 102.843017578125, + 2.191238104506552 + ], + [ + 102.227783203125, + 2.191238104506552 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "name": "poly1", + "population": 400 + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 100, + 0 + ], + [ + 101, + 0 + ], + [ + 101, + 1 + ], + [ + 100, + 1 + ], + [ + 100, + 0 + ] + ], + [ + [ + 100.206298828125, + 0.2526847277643438 + ], + [ + 100.206298828125, + 0.7909904981540058 + ], + [ + 100.8050537109375, + 0.7909904981540058 + ], + [ + 100.8050537109375, + 0.2526847277643438 + ], + [ + 100.206298828125, + 0.2526847277643438 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "name": "poly1", + "population": 400 + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 101.700439453125, + 0.5273363048115169 + ], + [ + 102.645263671875, + 0.5273363048115169 + ], + [ + 102.645263671875, + 1.3511930983018892 + ], + [ + 101.700439453125, + 1.3511930983018892 + ], + [ + 101.700439453125, + 0.5273363048115169 + ] + ] + ] + } + } + ] +} diff --git a/packages/turf-linestring-to-polygon/test/types.ts b/packages/turf-linestring-to-polygon/test/types.ts new file mode 100644 index 0000000000..ff8203fa75 --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/types.ts @@ -0,0 +1,8 @@ +import {featureCollection, lineString} from '@turf/helpers' +import * as polygonToLineString from '../' + +const line1 = lineString([[125, -30], [145, -30], [145, -20], [125, -20], [125, -30]]); +const line2 = lineString([[125, -30], [145, -30], [145, -20], [125, -20], [125, -30]]); + +const poly = polygonToLineString(line1); +const polys = polygonToLineString(featureCollection([line1, line2])); From 5f9276fac054df92e326a550deb8a3c2ce2b1d8f Mon Sep 17 00:00:00 2001 From: Denis Date: Sun, 16 Apr 2017 01:31:36 -0400 Subject: [PATCH 2/5] Update description --- packages/turf-linestring-to-polygon/README.md | 2 +- packages/turf-linestring-to-polygon/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/turf-linestring-to-polygon/README.md b/packages/turf-linestring-to-polygon/README.md index 547dc86603..924432c3a0 100644 --- a/packages/turf-linestring-to-polygon/README.md +++ b/packages/turf-linestring-to-polygon/README.md @@ -2,7 +2,7 @@ # lineStringToPolygon -Converts a [LineString](http://geojson.org/geojson-spec.html#linestring) or [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring) to a [Polygon](http://geojson.org/geojson-spec.html#polygon). +Converts (Multi)LineString(s) to Polygon(s). **Parameters** diff --git a/packages/turf-linestring-to-polygon/index.js b/packages/turf-linestring-to-polygon/index.js index f22018f0fd..76b53f17a0 100644 --- a/packages/turf-linestring-to-polygon/index.js +++ b/packages/turf-linestring-to-polygon/index.js @@ -4,7 +4,7 @@ var polygon = helpers.polygon; var featureCollection = helpers.featureCollection; /** - * Converts a {@link LineString} or {@link MultiLineString} to a {@link Polygon}. + * Converts (Multi)LineString(s) to Polygon(s). * * @name lineStringToPolygon * @param {FeatureCollection|Feature} lines Features to convert From 7d27206c70ab1a241773781e85470b7e31fa02f0 Mon Sep 17 00:00:00 2001 From: Denis Date: Sun, 16 Apr 2017 01:50:03 -0400 Subject: [PATCH 3/5] Add line colors to tests --- .../in/multi-linestring-incomplete.geojson | 133 ++++---- .../in/multi-linestrings-with-holes.geojson | 305 +++++++++--------- .../out/multi-linestring-incomplete.geojson | 149 +++++---- .../out/multi-linestrings-with-holes.geojson | 9 + 4 files changed, 316 insertions(+), 280 deletions(-) diff --git a/packages/turf-linestring-to-polygon/test/in/multi-linestring-incomplete.geojson b/packages/turf-linestring-to-polygon/test/in/multi-linestring-incomplete.geojson index 1d7d285d6b..285598c0c7 100644 --- a/packages/turf-linestring-to-polygon/test/in/multi-linestring-incomplete.geojson +++ b/packages/turf-linestring-to-polygon/test/in/multi-linestring-incomplete.geojson @@ -1,65 +1,74 @@ { - "type": "Feature", - "properties": {}, - "geometry": { - "type": "MultiLineString", - "coordinates": [ - [ - [ - 129.28710937499997, - -31.57853542647337 - ], - [ - 120.05859375, - -26.35249785815401 - ], - [ - 120.05859375, - -19.72534224805787 - ], - [ - 124.62890625, - -17.056784609942543 - ], - [ - 131.572265625, - -17.224758206624628 - ], - [ - 133.330078125, - -23.644524198573677 + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#ff0000", + "stroke-width": 6, + "stroke-opacity": 1 + }, + "geometry": { + "type": "MultiLineString", + "coordinates": [ + [ + [ + 129.28710937499997, + -31.57853542647337 + ], + [ + 120.05859375, + -26.35249785815401 + ], + [ + 120.05859375, + -19.72534224805787 + ], + [ + 124.62890625, + -17.056784609942543 + ], + [ + 131.572265625, + -17.224758206624628 + ], + [ + 133.330078125, + -23.644524198573677 + ] + ], + [ + [ + 145.01953124999997, + -27.137368359795584 + ], + [ + 142.734375, + -29.535229562948444 + ], + [ + 139.306640625, + -28.690587654250685 + ], + [ + 137.8125, + -22.43134015636061 + ], + [ + 137.98828125, + -17.392579271057766 + ], + [ + 144.228515625, + -17.056784609942543 + ], + [ + 146.07421875, + -18.145851771694467 + ] + ] ] - ], - [ - [ - 145.01953124999997, - -27.137368359795584 - ], - [ - 142.734375, - -29.535229562948444 - ], - [ - 139.306640625, - -28.690587654250685 - ], - [ - 137.8125, - -22.43134015636061 - ], - [ - 137.98828125, - -17.392579271057766 - ], - [ - 144.228515625, - -17.056784609942543 - ], - [ - 146.07421875, - -18.145851771694467 - ] - ] - ] - } + } + } + ] } \ No newline at end of file diff --git a/packages/turf-linestring-to-polygon/test/in/multi-linestrings-with-holes.geojson b/packages/turf-linestring-to-polygon/test/in/multi-linestrings-with-holes.geojson index 7a7bc8d796..1f2e2d615f 100644 --- a/packages/turf-linestring-to-polygon/test/in/multi-linestrings-with-holes.geojson +++ b/packages/turf-linestring-to-polygon/test/in/multi-linestrings-with-holes.geojson @@ -1,149 +1,158 @@ { - "type": "FeatureCollection", - "features": [ - { - "type": "Feature", - "properties": { - "name": "poly1", - "population": 400 - }, - "geometry": { - "type": "MultiLineString", - "coordinates": [ - [ - [ - 102, - 2 - ], - [ - 103, - 2 - ], - [ - 103, - 3 - ], - [ - 102, - 3 - ], - [ - 102, - 2 - ] - ], - [ - [ - 102.227783203125, - 2.191238104506552 - ], - [ - 102.227783203125, - 2.8223442468940902 - ], - [ - 102.843017578125, - 2.8223442468940902 - ], - [ - 102.843017578125, - 2.191238104506552 - ], - [ - 102.227783203125, - 2.191238104506552 - ] - ] - ] - } - }, - { - "type": "Feature", - "properties": { - "name": "poly1", - "population": 400 - }, - "geometry": { - "type": "MultiLineString", - "coordinates": [ - [ - [ - 100, - 0 - ], - [ - 101, - 0 - ], - [ - 101, - 1 - ], - [ - 100, - 1 - ], - [ - 100, - 0 - ] - ], - [ - [ - 100.206298828125, - 0.2526847277643438 - ], - [ - 100.206298828125, - 0.7909904981540058 - ], - [ - 100.8050537109375, - 0.7909904981540058 - ], - [ - 100.8050537109375, - 0.2526847277643438 - ], - [ - 100.206298828125, - 0.2526847277643438 - ] - ] - ] - } - }, - { - "type": "Feature", - "properties": { - "name": "poly1", - "population": 400 - }, - "geometry": { - "type": "LineString", - "coordinates": [ - [ - 101.700439453125, - 0.5273363048115169 - ], - [ - 102.645263671875, - 0.5273363048115169 - ], - [ - 102.645263671875, - 1.3511930983018892 - ], - [ - 101.700439453125, - 1.3511930983018892 - ], - [ - 101.700439453125, - 0.5273363048115169 - ] - ] - } - } - ] -} + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#0000ff", + "stroke-width": 6, + "stroke-opacity": 1, + "name": "poly1", + "population": 400 + }, + "geometry": { + "type": "MultiLineString", + "coordinates": [ + [ + [ + 102, + 2 + ], + [ + 103, + 2 + ], + [ + 103, + 3 + ], + [ + 102, + 3 + ], + [ + 102, + 2 + ] + ], + [ + [ + 102.227783203125, + 2.191238104506552 + ], + [ + 102.227783203125, + 2.8223442468940902 + ], + [ + 102.843017578125, + 2.8223442468940902 + ], + [ + 102.843017578125, + 2.191238104506552 + ], + [ + 102.227783203125, + 2.191238104506552 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#ff8000", + "stroke-width": 6, + "stroke-opacity": 1, + "name": "poly1", + "population": 400 + }, + "geometry": { + "type": "MultiLineString", + "coordinates": [ + [ + [ + 100, + 0 + ], + [ + 101, + 0 + ], + [ + 101, + 1 + ], + [ + 100, + 1 + ], + [ + 100, + 0 + ] + ], + [ + [ + 100.206298828125, + 0.2526847277643438 + ], + [ + 100.206298828125, + 0.7909904981540058 + ], + [ + 100.8050537109375, + 0.7909904981540058 + ], + [ + 100.8050537109375, + 0.2526847277643438 + ], + [ + 100.206298828125, + 0.2526847277643438 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#008000", + "stroke-width": 6, + "stroke-opacity": 1, + "name": "poly1", + "population": 400 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 101.700439453125, + 0.5273363048115169 + ], + [ + 102.645263671875, + 0.5273363048115169 + ], + [ + 102.645263671875, + 1.3511930983018892 + ], + [ + 101.700439453125, + 1.3511930983018892 + ], + [ + 101.700439453125, + 0.5273363048115169 + ] + ] + } + } + ] +} \ No newline at end of file diff --git a/packages/turf-linestring-to-polygon/test/out/multi-linestring-incomplete.geojson b/packages/turf-linestring-to-polygon/test/out/multi-linestring-incomplete.geojson index 9da0b7a48b..1283678c9f 100644 --- a/packages/turf-linestring-to-polygon/test/out/multi-linestring-incomplete.geojson +++ b/packages/turf-linestring-to-polygon/test/out/multi-linestring-incomplete.geojson @@ -1,73 +1,82 @@ { - "type": "Feature", - "properties": {}, - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [ - 129.28710937499997, - -31.57853542647337 - ], - [ - 120.05859375, - -26.35249785815401 - ], - [ - 120.05859375, - -19.72534224805787 - ], - [ - 124.62890625, - -17.056784609942543 - ], - [ - 131.572265625, - -17.224758206624628 - ], - [ - 133.330078125, - -23.644524198573677 - ], - [ - 129.28710937499997, - -31.57853542647337 + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#ff0000", + "stroke-width": 6, + "stroke-opacity": 1 + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 129.28710937499997, + -31.57853542647337 + ], + [ + 120.05859375, + -26.35249785815401 + ], + [ + 120.05859375, + -19.72534224805787 + ], + [ + 124.62890625, + -17.056784609942543 + ], + [ + 131.572265625, + -17.224758206624628 + ], + [ + 133.330078125, + -23.644524198573677 + ], + [ + 129.28710937499997, + -31.57853542647337 + ] + ], + [ + [ + 145.01953124999997, + -27.137368359795584 + ], + [ + 142.734375, + -29.535229562948444 + ], + [ + 139.306640625, + -28.690587654250685 + ], + [ + 137.8125, + -22.43134015636061 + ], + [ + 137.98828125, + -17.392579271057766 + ], + [ + 144.228515625, + -17.056784609942543 + ], + [ + 146.07421875, + -18.145851771694467 + ], + [ + 145.01953124999997, + -27.137368359795584 + ] + ] ] - ], - [ - [ - 145.01953124999997, - -27.137368359795584 - ], - [ - 142.734375, - -29.535229562948444 - ], - [ - 139.306640625, - -28.690587654250685 - ], - [ - 137.8125, - -22.43134015636061 - ], - [ - 137.98828125, - -17.392579271057766 - ], - [ - 144.228515625, - -17.056784609942543 - ], - [ - 146.07421875, - -18.145851771694467 - ], - [ - 145.01953124999997, - -27.137368359795584 - ] - ] - ] - } + } + } + ] } diff --git a/packages/turf-linestring-to-polygon/test/out/multi-linestrings-with-holes.geojson b/packages/turf-linestring-to-polygon/test/out/multi-linestrings-with-holes.geojson index cd81ba9f31..f0a3193080 100644 --- a/packages/turf-linestring-to-polygon/test/out/multi-linestrings-with-holes.geojson +++ b/packages/turf-linestring-to-polygon/test/out/multi-linestrings-with-holes.geojson @@ -4,6 +4,9 @@ { "type": "Feature", "properties": { + "stroke": "#0000ff", + "stroke-width": 6, + "stroke-opacity": 1, "name": "poly1", "population": 400 }, @@ -60,6 +63,9 @@ { "type": "Feature", "properties": { + "stroke": "#ff8000", + "stroke-width": 6, + "stroke-opacity": 1, "name": "poly1", "population": 400 }, @@ -116,6 +122,9 @@ { "type": "Feature", "properties": { + "stroke": "#008000", + "stroke-width": 6, + "stroke-opacity": 1, "name": "poly1", "population": 400 }, From 67071e21b10bc066b6b5fd1fd59d69f3b3f03382 Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 19 Apr 2017 14:36:44 -0400 Subject: [PATCH 4/5] Convert FeatureCollection to MultiPolygon Changes based on https://github.com/Turfjs/turf/pull/672#issuecomment-295360393 - Convert FeatureCollection & GeometryCollection to MultiPolygon - Colorize test fixtures (easier to visualize) - Update Typescript defintion & tests CC: @stebogit --- .../turf-linestring-to-polygon/index.d.ts | 26 +- packages/turf-linestring-to-polygon/index.js | 29 +- packages/turf-linestring-to-polygon/test.js | 5 +- .../test/in/collection-linestring.geojson | 8 +- .../test/in/linestring-incomplete.geojson | 5 +- .../test/in/linestring.geojson | 4 +- .../in/multi-linestring-incomplete.geojson | 136 +++++---- .../in/multi-linestring-with-hole.geojson | 4 +- .../in/multi-linestrings-with-holes.geojson | 21 +- .../test/out/collection-linestring.geojson | 117 ++++---- .../test/out/geometry-linestring.geojson | 5 +- .../test/out/linestring-incomplete.geojson | 5 +- .../test/out/linestring.geojson | 4 +- .../out/multi-linestring-incomplete.geojson | 152 +++++----- .../out/multi-linestring-with-hole.geojson | 4 +- .../out/multi-linestrings-with-holes.geojson | 272 ++++++++---------- .../turf-linestring-to-polygon/test/types.ts | 14 +- 17 files changed, 389 insertions(+), 422 deletions(-) diff --git a/packages/turf-linestring-to-polygon/index.d.ts b/packages/turf-linestring-to-polygon/index.d.ts index 04f1c16626..4ca53a0c77 100644 --- a/packages/turf-linestring-to-polygon/index.d.ts +++ b/packages/turf-linestring-to-polygon/index.d.ts @@ -1,16 +1,24 @@ /// -import { - Polygon, - Polygons, - LineString, - LineStrings, - MultiLineString, - MultiLineStrings} from '@turf/helpers' +// Geometry Types +type MultiLineString = GeoJSON.MultiLineString +type LineString = GeoJSON.LineString +type Polygon = GeoJSON.Polygon +type MultiPolygon = GeoJSON.MultiPolygon +// Inputs +type Feature = GeoJSON.Feature | LineString | MultiLineString +type FeatureCollection = GeoJSON.FeatureCollection | GeoJSON.GeometryCollection + +/** + * Output type changes based on input type + * + * Feature => Polygon + * FeatureCollection => MultiPolygon + */ interface LineStringToPolygon { - (lines: LineString | MultiLineString): Polygon - (lines: LineStrings | MultiLineStrings): Polygons + (lines: Feature): GeoJSON.Feature + (lines: FeatureCollection): GeoJSON.Feature } declare const lineStringToPolygon: LineStringToPolygon; export = lineStringToPolygon; diff --git a/packages/turf-linestring-to-polygon/index.js b/packages/turf-linestring-to-polygon/index.js index 76b53f17a0..8ebeef97a3 100644 --- a/packages/turf-linestring-to-polygon/index.js +++ b/packages/turf-linestring-to-polygon/index.js @@ -1,15 +1,16 @@ var getCoords = require('@turf/invariant').getCoords; var helpers = require('@turf/helpers'); var polygon = helpers.polygon; -var featureCollection = helpers.featureCollection; +var multiPolygon = helpers.multiPolygon; /** * Converts (Multi)LineString(s) to Polygon(s). * * @name lineStringToPolygon * @param {FeatureCollection|Feature} lines Features to convert - * @param {boolean} [autoComplete=true] auto complete linestrings - * @returns {FeatureCollection|Feature} converted to Polygons + * @param {boolean} [autoComplete=true] auto complete linestrings (matches first & last coordinates) + * @param {Object} [properties] translates GeoJSON properties to Feature + * @returns {Feature} converted to Polygons * @example * var line = { * 'type': 'Feature', @@ -24,7 +25,7 @@ var featureCollection = helpers.featureCollection; * //addToMap * var addToMap = [polygon]; */ -module.exports = function (lines, autoComplete) { +module.exports = function (lines, autoComplete, properties) { // validation if (!lines) throw new Error('lines is required'); @@ -35,14 +36,14 @@ module.exports = function (lines, autoComplete) { switch (type) { case 'FeatureCollection': case 'GeometryCollection': - var results = []; + var coords = []; var features = (lines.features) ? lines.features : lines.geometries; features.forEach(function (line) { - results.push(lineStringToPolygon(line, autoComplete)); + coords.push(getCoords(lineStringToPolygon(line, autoComplete))); }); - return featureCollection(results); + return multiPolygon(coords, properties); } - return lineStringToPolygon(lines, autoComplete); + return lineStringToPolygon(lines, autoComplete, properties); }; /** @@ -51,12 +52,14 @@ module.exports = function (lines, autoComplete) { * @private * @param {Feature} line line * @param {boolean} [autoComplete=true] auto complete linestrings + * @param {Object} [properties] translates GeoJSON properties to Feature * @returns {Feature} line converted to Polygon */ -function lineStringToPolygon(line, autoComplete) { - var properties = line.properties; +function lineStringToPolygon(line, autoComplete, properties) { + properties = properties || line.properties || {}; var coords = getCoords(line); var type = geomType(line); + if (!coords.length) throw new Error('line must contain coordinates'); switch (type) { @@ -77,6 +80,12 @@ function geomType(feature) { return (feature.geometry) ? feature.geometry.type : feature.type; } +/** + * Auto Complete Coords - matches first & last coordinates + * + * @param {Array>} coords Coordinates + * @returns {Array>} auto completed coordinates + */ function autoCompleteCoords(coords) { var first = coords[0]; var x1 = first[0]; diff --git a/packages/turf-linestring-to-polygon/test.js b/packages/turf-linestring-to-polygon/test.js index 44bcaa4c12..e3f6d6ac6d 100644 --- a/packages/turf-linestring-to-polygon/test.js +++ b/packages/turf-linestring-to-polygon/test.js @@ -22,7 +22,9 @@ let fixtures = fs.readdirSync(directories.in).map(filename => { test('turf-linestring-to-polygon', t => { for (const {name, filename, geojson} of fixtures) { - const results = lineStringToPolygon(geojson); + let {autoComplete, properties} = geojson.properties || {}; + properties = properties || {stroke: '#F0F', 'stroke-width': 6}; + const results = lineStringToPolygon(geojson, autoComplete, properties); if (process.env.REGEN) write.sync(directories.out + filename, results); t.deepEqual(load.sync(directories.out + filename), results, name); @@ -30,6 +32,7 @@ test('turf-linestring-to-polygon', t => { // Handle Errors t.throws(() => lineStringToPolygon(point([10, 5])), 'throws - invalid geometry'); t.throws(() => lineStringToPolygon(lineString([])), 'throws - empty coordinates'); + t.throws(() => lineStringToPolygon(lineString([[10, 5], [20, 10], [30, 20]]), false), 'throws - autoComplete=false'); t.end(); }); diff --git a/packages/turf-linestring-to-polygon/test/in/collection-linestring.geojson b/packages/turf-linestring-to-polygon/test/in/collection-linestring.geojson index 4b4b5c9509..3a23ec6f02 100644 --- a/packages/turf-linestring-to-polygon/test/in/collection-linestring.geojson +++ b/packages/turf-linestring-to-polygon/test/in/collection-linestring.geojson @@ -4,8 +4,8 @@ { "type": "Feature", "properties": { - "name": "poly1", - "population": 400 + "stroke": "#F00", + "stroke-width": 6 }, "geometry": { "type": "LineString", @@ -36,8 +36,8 @@ { "type": "Feature", "properties": { - "name": "poly1", - "population": 400 + "stroke": "#00F", + "stroke-width": 6 }, "geometry": { "type": "LineString", diff --git a/packages/turf-linestring-to-polygon/test/in/linestring-incomplete.geojson b/packages/turf-linestring-to-polygon/test/in/linestring-incomplete.geojson index e862649925..2db492a70a 100644 --- a/packages/turf-linestring-to-polygon/test/in/linestring-incomplete.geojson +++ b/packages/turf-linestring-to-polygon/test/in/linestring-incomplete.geojson @@ -1,6 +1,9 @@ { "type": "Feature", - "properties": {}, + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, "geometry": { "type": "LineString", "coordinates": [ diff --git a/packages/turf-linestring-to-polygon/test/in/linestring.geojson b/packages/turf-linestring-to-polygon/test/in/linestring.geojson index 90b5a3d22f..b007bb5a68 100644 --- a/packages/turf-linestring-to-polygon/test/in/linestring.geojson +++ b/packages/turf-linestring-to-polygon/test/in/linestring.geojson @@ -2,8 +2,8 @@ { "type": "Feature", "properties": { - "name": "poly1", - "population": 400 + "stroke": "#F00", + "stroke-width": 6 }, "geometry": { "type": "LineString", diff --git a/packages/turf-linestring-to-polygon/test/in/multi-linestring-incomplete.geojson b/packages/turf-linestring-to-polygon/test/in/multi-linestring-incomplete.geojson index 285598c0c7..847c819352 100644 --- a/packages/turf-linestring-to-polygon/test/in/multi-linestring-incomplete.geojson +++ b/packages/turf-linestring-to-polygon/test/in/multi-linestring-incomplete.geojson @@ -1,74 +1,68 @@ { - "type": "FeatureCollection", - "features": [ - { - "type": "Feature", - "properties": { - "stroke": "#ff0000", - "stroke-width": 6, - "stroke-opacity": 1 - }, - "geometry": { - "type": "MultiLineString", - "coordinates": [ - [ - [ - 129.28710937499997, - -31.57853542647337 - ], - [ - 120.05859375, - -26.35249785815401 - ], - [ - 120.05859375, - -19.72534224805787 - ], - [ - 124.62890625, - -17.056784609942543 - ], - [ - 131.572265625, - -17.224758206624628 - ], - [ - 133.330078125, - -23.644524198573677 - ] - ], - [ - [ - 145.01953124999997, - -27.137368359795584 - ], - [ - 142.734375, - -29.535229562948444 - ], - [ - 139.306640625, - -28.690587654250685 - ], - [ - 137.8125, - -22.43134015636061 - ], - [ - 137.98828125, - -17.392579271057766 - ], - [ - 144.228515625, - -17.056784609942543 - ], - [ - 146.07421875, - -18.145851771694467 - ] - ] + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "MultiLineString", + "coordinates": [ + [ + [ + 129.28710937499997, + -31.57853542647337 + ], + [ + 120.05859375, + -26.35249785815401 + ], + [ + 120.05859375, + -19.72534224805787 + ], + [ + 124.62890625, + -17.056784609942543 + ], + [ + 131.572265625, + -17.224758206624628 + ], + [ + 133.330078125, + -23.644524198573677 ] - } - } - ] + ], + [ + [ + 145.01953124999997, + -27.137368359795584 + ], + [ + 142.734375, + -29.535229562948444 + ], + [ + 139.306640625, + -28.690587654250685 + ], + [ + 137.8125, + -22.43134015636061 + ], + [ + 137.98828125, + -17.392579271057766 + ], + [ + 144.228515625, + -17.056784609942543 + ], + [ + 146.07421875, + -18.145851771694467 + ] + ] + ] + } } \ No newline at end of file diff --git a/packages/turf-linestring-to-polygon/test/in/multi-linestring-with-hole.geojson b/packages/turf-linestring-to-polygon/test/in/multi-linestring-with-hole.geojson index d21fdb65a0..53dc4b052e 100644 --- a/packages/turf-linestring-to-polygon/test/in/multi-linestring-with-hole.geojson +++ b/packages/turf-linestring-to-polygon/test/in/multi-linestring-with-hole.geojson @@ -1,8 +1,8 @@ { "type": "Feature", "properties": { - "name": "poly1", - "population": 400 + "stroke": "#F00", + "stroke-width": 6 }, "geometry": { "type": "MultiLineString", diff --git a/packages/turf-linestring-to-polygon/test/in/multi-linestrings-with-holes.geojson b/packages/turf-linestring-to-polygon/test/in/multi-linestrings-with-holes.geojson index 1f2e2d615f..8841078359 100644 --- a/packages/turf-linestring-to-polygon/test/in/multi-linestrings-with-holes.geojson +++ b/packages/turf-linestring-to-polygon/test/in/multi-linestrings-with-holes.geojson @@ -4,11 +4,8 @@ { "type": "Feature", "properties": { - "stroke": "#0000ff", - "stroke-width": 6, - "stroke-opacity": 1, - "name": "poly1", - "population": 400 + "stroke": "#00F", + "stroke-width": 6 }, "geometry": { "type": "MultiLineString", @@ -63,11 +60,8 @@ { "type": "Feature", "properties": { - "stroke": "#ff8000", - "stroke-width": 6, - "stroke-opacity": 1, - "name": "poly1", - "population": 400 + "stroke": "#F80", + "stroke-width": 6 }, "geometry": { "type": "MultiLineString", @@ -122,11 +116,8 @@ { "type": "Feature", "properties": { - "stroke": "#008000", - "stroke-width": 6, - "stroke-opacity": 1, - "name": "poly1", - "population": 400 + "stroke": "#080", + "stroke-width": 6 }, "geometry": { "type": "LineString", diff --git a/packages/turf-linestring-to-polygon/test/out/collection-linestring.geojson b/packages/turf-linestring-to-polygon/test/out/collection-linestring.geojson index d127ea24b2..1ec3305893 100644 --- a/packages/turf-linestring-to-polygon/test/out/collection-linestring.geojson +++ b/packages/turf-linestring-to-polygon/test/out/collection-linestring.geojson @@ -1,73 +1,60 @@ { - "type": "FeatureCollection", - "features": [ - { - "type": "Feature", - "properties": { - "name": "poly1", - "population": 400 - }, - "geometry": { - "type": "Polygon", - "coordinates": [ + "type": "Feature", + "properties": { + "stroke": "#F0F", + "stroke-width": 6 + }, + "geometry": { + "type": "MultiPolygon", + "coordinates": [ + [ + [ [ - [ - 102, - 2 - ], - [ - 103, - 2 - ], - [ - 103, - 3 - ], - [ - 102, - 3 - ], - [ - 102, - 2 - ] + 102, + 2 + ], + [ + 103, + 2 + ], + [ + 103, + 3 + ], + [ + 102, + 3 + ], + [ + 102, + 2 ] ] - } - }, - { - "type": "Feature", - "properties": { - "name": "poly1", - "population": 400 - }, - "geometry": { - "type": "Polygon", - "coordinates": [ + ], + [ + [ + [ + 100, + 0 + ], + [ + 101, + 0 + ], + [ + 101, + 1 + ], + [ + 100, + 1 + ], [ - [ - 100, - 0 - ], - [ - 101, - 0 - ], - [ - 101, - 1 - ], - [ - 100, - 1 - ], - [ - 100, - 0 - ] + 100, + 0 ] ] - } - } - ] + ] + ] + } } diff --git a/packages/turf-linestring-to-polygon/test/out/geometry-linestring.geojson b/packages/turf-linestring-to-polygon/test/out/geometry-linestring.geojson index 7be1a77cbf..8eacb22d9e 100644 --- a/packages/turf-linestring-to-polygon/test/out/geometry-linestring.geojson +++ b/packages/turf-linestring-to-polygon/test/out/geometry-linestring.geojson @@ -1,6 +1,9 @@ { "type": "Feature", - "properties": {}, + "properties": { + "stroke": "#F0F", + "stroke-width": 6 + }, "geometry": { "type": "Polygon", "coordinates": [ diff --git a/packages/turf-linestring-to-polygon/test/out/linestring-incomplete.geojson b/packages/turf-linestring-to-polygon/test/out/linestring-incomplete.geojson index 34bba44b9e..cab40f9eaa 100644 --- a/packages/turf-linestring-to-polygon/test/out/linestring-incomplete.geojson +++ b/packages/turf-linestring-to-polygon/test/out/linestring-incomplete.geojson @@ -1,6 +1,9 @@ { "type": "Feature", - "properties": {}, + "properties": { + "stroke": "#F0F", + "stroke-width": 6 + }, "geometry": { "type": "Polygon", "coordinates": [ diff --git a/packages/turf-linestring-to-polygon/test/out/linestring.geojson b/packages/turf-linestring-to-polygon/test/out/linestring.geojson index 09670a8a7d..8eacb22d9e 100644 --- a/packages/turf-linestring-to-polygon/test/out/linestring.geojson +++ b/packages/turf-linestring-to-polygon/test/out/linestring.geojson @@ -1,8 +1,8 @@ { "type": "Feature", "properties": { - "name": "poly1", - "population": 400 + "stroke": "#F0F", + "stroke-width": 6 }, "geometry": { "type": "Polygon", diff --git a/packages/turf-linestring-to-polygon/test/out/multi-linestring-incomplete.geojson b/packages/turf-linestring-to-polygon/test/out/multi-linestring-incomplete.geojson index 1283678c9f..2fc4e7b7b4 100644 --- a/packages/turf-linestring-to-polygon/test/out/multi-linestring-incomplete.geojson +++ b/packages/turf-linestring-to-polygon/test/out/multi-linestring-incomplete.geojson @@ -1,82 +1,76 @@ { - "type": "FeatureCollection", - "features": [ - { - "type": "Feature", - "properties": { - "stroke": "#ff0000", - "stroke-width": 6, - "stroke-opacity": 1 - }, - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [ - 129.28710937499997, - -31.57853542647337 - ], - [ - 120.05859375, - -26.35249785815401 - ], - [ - 120.05859375, - -19.72534224805787 - ], - [ - 124.62890625, - -17.056784609942543 - ], - [ - 131.572265625, - -17.224758206624628 - ], - [ - 133.330078125, - -23.644524198573677 - ], - [ - 129.28710937499997, - -31.57853542647337 - ] - ], - [ - [ - 145.01953124999997, - -27.137368359795584 - ], - [ - 142.734375, - -29.535229562948444 - ], - [ - 139.306640625, - -28.690587654250685 - ], - [ - 137.8125, - -22.43134015636061 - ], - [ - 137.98828125, - -17.392579271057766 - ], - [ - 144.228515625, - -17.056784609942543 - ], - [ - 146.07421875, - -18.145851771694467 - ], - [ - 145.01953124999997, - -27.137368359795584 - ] - ] + "type": "Feature", + "properties": { + "stroke": "#F0F", + "stroke-width": 6 + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 129.28710937499997, + -31.57853542647337 + ], + [ + 120.05859375, + -26.35249785815401 + ], + [ + 120.05859375, + -19.72534224805787 + ], + [ + 124.62890625, + -17.056784609942543 + ], + [ + 131.572265625, + -17.224758206624628 + ], + [ + 133.330078125, + -23.644524198573677 + ], + [ + 129.28710937499997, + -31.57853542647337 ] - } - } - ] + ], + [ + [ + 145.01953124999997, + -27.137368359795584 + ], + [ + 142.734375, + -29.535229562948444 + ], + [ + 139.306640625, + -28.690587654250685 + ], + [ + 137.8125, + -22.43134015636061 + ], + [ + 137.98828125, + -17.392579271057766 + ], + [ + 144.228515625, + -17.056784609942543 + ], + [ + 146.07421875, + -18.145851771694467 + ], + [ + 145.01953124999997, + -27.137368359795584 + ] + ] + ] + } } diff --git a/packages/turf-linestring-to-polygon/test/out/multi-linestring-with-hole.geojson b/packages/turf-linestring-to-polygon/test/out/multi-linestring-with-hole.geojson index 308ee22477..9ffb8c0801 100644 --- a/packages/turf-linestring-to-polygon/test/out/multi-linestring-with-hole.geojson +++ b/packages/turf-linestring-to-polygon/test/out/multi-linestring-with-hole.geojson @@ -1,8 +1,8 @@ { "type": "Feature", "properties": { - "name": "poly1", - "population": 400 + "stroke": "#F0F", + "stroke-width": 6 }, "geometry": { "type": "Polygon", diff --git a/packages/turf-linestring-to-polygon/test/out/multi-linestrings-with-holes.geojson b/packages/turf-linestring-to-polygon/test/out/multi-linestrings-with-holes.geojson index f0a3193080..0fcc76bb65 100644 --- a/packages/turf-linestring-to-polygon/test/out/multi-linestrings-with-holes.geojson +++ b/packages/turf-linestring-to-polygon/test/out/multi-linestrings-with-holes.geojson @@ -1,160 +1,128 @@ { - "type": "FeatureCollection", - "features": [ - { - "type": "Feature", - "properties": { - "stroke": "#0000ff", - "stroke-width": 6, - "stroke-opacity": 1, - "name": "poly1", - "population": 400 - }, - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [ - 102, - 2 - ], - [ - 103, - 2 - ], - [ - 103, - 3 - ], - [ - 102, - 3 - ], - [ - 102, - 2 - ] - ], - [ - [ - 102.227783203125, - 2.191238104506552 - ], - [ - 102.227783203125, - 2.8223442468940902 - ], - [ - 102.843017578125, - 2.8223442468940902 - ], - [ - 102.843017578125, - 2.191238104506552 - ], - [ - 102.227783203125, - 2.191238104506552 - ] + "type": "Feature", + "properties": { + "stroke": "#F0F", + "stroke-width": 6 + }, + "geometry": { + "type": "MultiPolygon", + "coordinates": [ + [ + [ + [ + 102, + 2 + ], + [ + 103, + 2 + ], + [ + 103, + 3 + ], + [ + 102, + 3 + ], + [ + 102, + 2 + ] + ], + [ + [ + 102.227783203125, + 2.191238104506552 + ], + [ + 102.227783203125, + 2.8223442468940902 + ], + [ + 102.843017578125, + 2.8223442468940902 + ], + [ + 102.843017578125, + 2.191238104506552 + ], + [ + 102.227783203125, + 2.191238104506552 ] ] - } - }, - { - "type": "Feature", - "properties": { - "stroke": "#ff8000", - "stroke-width": 6, - "stroke-opacity": 1, - "name": "poly1", - "population": 400 - }, - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [ - 100, - 0 - ], - [ - 101, - 0 - ], - [ - 101, - 1 - ], - [ - 100, - 1 - ], - [ - 100, - 0 - ] - ], - [ - [ - 100.206298828125, - 0.2526847277643438 - ], - [ - 100.206298828125, - 0.7909904981540058 - ], - [ - 100.8050537109375, - 0.7909904981540058 - ], - [ - 100.8050537109375, - 0.2526847277643438 - ], - [ - 100.206298828125, - 0.2526847277643438 - ] + ], + [ + [ + [ + 100, + 0 + ], + [ + 101, + 0 + ], + [ + 101, + 1 + ], + [ + 100, + 1 + ], + [ + 100, + 0 + ] + ], + [ + [ + 100.206298828125, + 0.2526847277643438 + ], + [ + 100.206298828125, + 0.7909904981540058 + ], + [ + 100.8050537109375, + 0.7909904981540058 + ], + [ + 100.8050537109375, + 0.2526847277643438 + ], + [ + 100.206298828125, + 0.2526847277643438 ] ] - } - }, - { - "type": "Feature", - "properties": { - "stroke": "#008000", - "stroke-width": 6, - "stroke-opacity": 1, - "name": "poly1", - "population": 400 - }, - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [ - 101.700439453125, - 0.5273363048115169 - ], - [ - 102.645263671875, - 0.5273363048115169 - ], - [ - 102.645263671875, - 1.3511930983018892 - ], - [ - 101.700439453125, - 1.3511930983018892 - ], - [ - 101.700439453125, - 0.5273363048115169 - ] + ], + [ + [ + [ + 101.700439453125, + 0.5273363048115169 + ], + [ + 102.645263671875, + 0.5273363048115169 + ], + [ + 102.645263671875, + 1.3511930983018892 + ], + [ + 101.700439453125, + 1.3511930983018892 + ], + [ + 101.700439453125, + 0.5273363048115169 ] ] - } - } - ] + ] + ] + } } diff --git a/packages/turf-linestring-to-polygon/test/types.ts b/packages/turf-linestring-to-polygon/test/types.ts index ff8203fa75..1a4f5e64ad 100644 --- a/packages/turf-linestring-to-polygon/test/types.ts +++ b/packages/turf-linestring-to-polygon/test/types.ts @@ -1,8 +1,12 @@ -import {featureCollection, lineString} from '@turf/helpers' +import {featureCollection, lineString, multiLineString, Polygon, MultiPolygon} from '@turf/helpers' import * as polygonToLineString from '../' -const line1 = lineString([[125, -30], [145, -30], [145, -20], [125, -20], [125, -30]]); -const line2 = lineString([[125, -30], [145, -30], [145, -20], [125, -20], [125, -30]]); +// Fixtures +const coords = [[125, -30], [145, -30], [145, -20], [125, -20], [125, -30]]; +const line = lineString(coords); +const multiLine = multiLineString([coords, coords]); -const poly = polygonToLineString(line1); -const polys = polygonToLineString(featureCollection([line1, line2])); +// Assert results with types +const poly1: Polygon = polygonToLineString(line); +const poly2: Polygon = polygonToLineString(multiLine); +const multiPoly: MultiPolygon = polygonToLineString(featureCollection([line, multiLine])); From 212498894c00399f803119f6f27b759e33dcd18e Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 21 Apr 2017 19:34:48 -0400 Subject: [PATCH 5/5] Add `orderCoords` param to place outer ring in 1st position - Add Test fixture with outer ring in middle position (must be placed in first position) - Update Typescript definition - Update benchmark results (2-3x peformance loss, not significant enough to complain) CC: @stebogit --- packages/turf-linestring-to-polygon/bench.js | 7 +- .../turf-linestring-to-polygon/index.d.ts | 4 +- packages/turf-linestring-to-polygon/index.js | 52 ++++++++-- .../turf-linestring-to-polygon/package.json | 1 + packages/turf-linestring-to-polygon/test.js | 8 +- ...estring-outer-ring-middle-position.geojson | 91 ++++++++++++++++++ ...estring-outer-ring-middle-position.geojson | 94 +++++++++++++++++++ 7 files changed, 238 insertions(+), 19 deletions(-) create mode 100644 packages/turf-linestring-to-polygon/test/in/multi-linestring-outer-ring-middle-position.geojson create mode 100644 packages/turf-linestring-to-polygon/test/out/multi-linestring-outer-ring-middle-position.geojson diff --git a/packages/turf-linestring-to-polygon/bench.js b/packages/turf-linestring-to-polygon/bench.js index 0730fa931c..b6e7bd0d0a 100644 --- a/packages/turf-linestring-to-polygon/bench.js +++ b/packages/turf-linestring-to-polygon/bench.js @@ -21,9 +21,10 @@ let fixtures = fs.readdirSync(directory).map(filename => { * geometry-linestring x 6,574,088 ops/sec ±3.62% (83 runs sampled) * linestring-incomplete x 6,768,527 ops/sec ±3.60% (84 runs sampled) * linestring x 6,752,969 ops/sec ±1.94% (84 runs sampled) - * multi-linestring-incomplete x 3,263,278 ops/sec ±2.56% (88 runs sampled) - * multi-linestring-with-hole x 3,381,728 ops/sec ±1.75% (88 runs sampled) - * multi-linestrings-with-holes x 1,084,043 ops/sec ±2.28% (83 runs sampled) + * multi-linestring-incomplete x 808,779 ops/sec ±8.86% (80 runs sampled) + * multi-linestring-outer-ring-middle-position x 664,121 ops/sec ±1.52% (83 runs sampled) + * multi-linestring-with-hole x 1,018,657 ops/sec ±1.35% (86 runs sampled) + * multi-linestrings-with-holes x 421,758 ops/sec ±0.92% (88 runs sampled) */ const suite = new Benchmark.Suite('turf-linestring-to-polygon'); for (const {name, geojson} of fixtures) { diff --git a/packages/turf-linestring-to-polygon/index.d.ts b/packages/turf-linestring-to-polygon/index.d.ts index 4ca53a0c77..bf4a415f91 100644 --- a/packages/turf-linestring-to-polygon/index.d.ts +++ b/packages/turf-linestring-to-polygon/index.d.ts @@ -17,8 +17,8 @@ type FeatureCollection = GeoJSON.FeatureCollection * FeatureCollection => MultiPolygon */ interface LineStringToPolygon { - (lines: Feature): GeoJSON.Feature - (lines: FeatureCollection): GeoJSON.Feature + (lines: Feature, properties?: any, autoComplete?: boolean, orderCoords?: boolean): GeoJSON.Feature + (lines: FeatureCollection, properties?: any, autoComplete?: boolean, orderCoords?: boolean): GeoJSON.Feature } declare const lineStringToPolygon: LineStringToPolygon; export = lineStringToPolygon; diff --git a/packages/turf-linestring-to-polygon/index.js b/packages/turf-linestring-to-polygon/index.js index 8ebeef97a3..34d706b48a 100644 --- a/packages/turf-linestring-to-polygon/index.js +++ b/packages/turf-linestring-to-polygon/index.js @@ -1,15 +1,18 @@ +var bbox = require('@turf/bbox'); var getCoords = require('@turf/invariant').getCoords; var helpers = require('@turf/helpers'); var polygon = helpers.polygon; var multiPolygon = helpers.multiPolygon; +var lineString = helpers.lineString; /** * Converts (Multi)LineString(s) to Polygon(s). * * @name lineStringToPolygon * @param {FeatureCollection|Feature} lines Features to convert - * @param {boolean} [autoComplete=true] auto complete linestrings (matches first & last coordinates) * @param {Object} [properties] translates GeoJSON properties to Feature + * @param {boolean} [autoComplete=true] auto complete linestrings (matches first & last coordinates) + * @param {boolean} [orderCoords=true] sorts linestrings to place outer ring at the first position of the coordinates * @returns {Feature} converted to Polygons * @example * var line = { @@ -25,12 +28,13 @@ var multiPolygon = helpers.multiPolygon; * //addToMap * var addToMap = [polygon]; */ -module.exports = function (lines, autoComplete, properties) { +module.exports = function (lines, properties, autoComplete, orderCoords) { // validation if (!lines) throw new Error('lines is required'); // default params autoComplete = (autoComplete !== undefined) ? autoComplete : true; + orderCoords = (orderCoords !== undefined) ? orderCoords : true; var type = geomType(lines); switch (type) { @@ -39,11 +43,11 @@ module.exports = function (lines, autoComplete, properties) { var coords = []; var features = (lines.features) ? lines.features : lines.geometries; features.forEach(function (line) { - coords.push(getCoords(lineStringToPolygon(line, autoComplete))); + coords.push(getCoords(lineStringToPolygon(line, {}, autoComplete, orderCoords))); }); return multiPolygon(coords, properties); } - return lineStringToPolygon(lines, autoComplete, properties); + return lineStringToPolygon(lines, properties, autoComplete, orderCoords); }; /** @@ -51,11 +55,12 @@ module.exports = function (lines, autoComplete, properties) { * * @private * @param {Feature} line line - * @param {boolean} [autoComplete=true] auto complete linestrings * @param {Object} [properties] translates GeoJSON properties to Feature + * @param {boolean} [autoComplete=true] auto complete linestrings + * @param {boolean} [orderCoords=true] sorts linestrings to place outer ring at the first position of the coordinates * @returns {Feature} line converted to Polygon */ -function lineStringToPolygon(line, autoComplete, properties) { +function lineStringToPolygon(line, properties, autoComplete, orderCoords) { properties = properties || line.properties || {}; var coords = getCoords(line); var type = geomType(line); @@ -67,10 +72,24 @@ function lineStringToPolygon(line, autoComplete, properties) { if (autoComplete) coords = autoCompleteCoords(coords); return polygon([coords], properties); case 'MultiLineString': - if (autoComplete) coords.forEach(function (coord) { - coord = autoCompleteCoords(coord); + var multiCoords = []; + var largestArea = 0; + + coords.forEach(function (coord) { + if (autoComplete) coord = autoCompleteCoords(coord); + + // Largest LineString to be placed in the first position of the coordinates array + if (orderCoords) { + var area = calculateArea(bbox(lineString(coord))); + if (area > largestArea) { + multiCoords.unshift(coord); + largestArea = area; + } else multiCoords.push(coord); + } else { + multiCoords.push(coord); + } }); - return polygon(coords, properties); + return polygon(multiCoords, properties); default: throw new Error('geometry type ' + type + ' is not supported'); } @@ -98,3 +117,18 @@ function autoCompleteCoords(coords) { } return coords; } + +/** + * area - quick approximate area calculation (used to sort) + * + * @private + * @param {[number, number, number, number]} bbox BBox [west, south, east, north] + * @returns {number} very quick area calculation + */ +function calculateArea(bbox) { + var west = bbox[0]; + var south = bbox[1]; + var east = bbox[2]; + var north = bbox[3]; + return Math.abs(west - east) * Math.abs(south - north); +} diff --git a/packages/turf-linestring-to-polygon/package.json b/packages/turf-linestring-to-polygon/package.json index a4aacc6dc8..182fd7919c 100644 --- a/packages/turf-linestring-to-polygon/package.json +++ b/packages/turf-linestring-to-polygon/package.json @@ -36,6 +36,7 @@ "write-json-file": "^2.0.0" }, "dependencies": { + "@turf/bbox": "^4.1.0", "@turf/helpers": "^4.1.0", "@turf/invariant": "^4.1.0" } diff --git a/packages/turf-linestring-to-polygon/test.js b/packages/turf-linestring-to-polygon/test.js index e3f6d6ac6d..c6d1bc173f 100644 --- a/packages/turf-linestring-to-polygon/test.js +++ b/packages/turf-linestring-to-polygon/test.js @@ -22,9 +22,9 @@ let fixtures = fs.readdirSync(directories.in).map(filename => { test('turf-linestring-to-polygon', t => { for (const {name, filename, geojson} of fixtures) { - let {autoComplete, properties} = geojson.properties || {}; + let {autoComplete, properties, orderCoords} = geojson.properties || {}; properties = properties || {stroke: '#F0F', 'stroke-width': 6}; - const results = lineStringToPolygon(geojson, autoComplete, properties); + const results = lineStringToPolygon(geojson, properties, autoComplete, orderCoords); if (process.env.REGEN) write.sync(directories.out + filename, results); t.deepEqual(load.sync(directories.out + filename), results, name); @@ -32,8 +32,6 @@ test('turf-linestring-to-polygon', t => { // Handle Errors t.throws(() => lineStringToPolygon(point([10, 5])), 'throws - invalid geometry'); t.throws(() => lineStringToPolygon(lineString([])), 'throws - empty coordinates'); - t.throws(() => lineStringToPolygon(lineString([[10, 5], [20, 10], [30, 20]]), false), 'throws - autoComplete=false'); + t.throws(() => lineStringToPolygon(lineString([[10, 5], [20, 10], [30, 20]]), {}, false), 'throws - autoComplete=false'); t.end(); }); - - diff --git a/packages/turf-linestring-to-polygon/test/in/multi-linestring-outer-ring-middle-position.geojson b/packages/turf-linestring-to-polygon/test/in/multi-linestring-outer-ring-middle-position.geojson new file mode 100644 index 0000000000..58e1965f5f --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/in/multi-linestring-outer-ring-middle-position.geojson @@ -0,0 +1,91 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "MultiLineString", + "coordinates": [ + [ + [ + 33.398, + 46.867 + ], + [ + 34.057, + 46.867 + ], + [ + 34.057, + 47.197 + ], + [ + 33.398, + 47.197 + ], + [ + 33.398, + 46.867 + ] + ], + [ + [ + 33.42041015625, + 46.164614496897094 + ], + [ + 34.12353515625, + 46.58906908309182 + ], + [ + 34.73876953125, + 46.73233101286786 + ], + [ + 35.013427734375, + 47.34626718205302 + ], + [ + 34.07958984374999, + 47.73193447949174 + ], + [ + 32.947998046875, + 47.338822694822 + ], + [ + 32.354736328125, + 46.73986059969267 + ], + [ + 32.54150390625, + 46.240651955001695 + ], + [ + 33.42041015625, + 46.164614496897094 + ] + ], + [ + [ + 32.969, + 46.46 + ], + [ + 33.321, + 46.46 + ], + [ + 33.321, + 46.672 + ], + [ + 32.969, + 46.672 + ], + [ + 32.969, + 46.46 + ] + ] + ] + } +} \ No newline at end of file diff --git a/packages/turf-linestring-to-polygon/test/out/multi-linestring-outer-ring-middle-position.geojson b/packages/turf-linestring-to-polygon/test/out/multi-linestring-outer-ring-middle-position.geojson new file mode 100644 index 0000000000..d3bfa82f2d --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/out/multi-linestring-outer-ring-middle-position.geojson @@ -0,0 +1,94 @@ +{ + "type": "Feature", + "properties": { + "stroke": "#F0F", + "stroke-width": 6 + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 33.42041015625, + 46.164614496897094 + ], + [ + 34.12353515625, + 46.58906908309182 + ], + [ + 34.73876953125, + 46.73233101286786 + ], + [ + 35.013427734375, + 47.34626718205302 + ], + [ + 34.07958984374999, + 47.73193447949174 + ], + [ + 32.947998046875, + 47.338822694822 + ], + [ + 32.354736328125, + 46.73986059969267 + ], + [ + 32.54150390625, + 46.240651955001695 + ], + [ + 33.42041015625, + 46.164614496897094 + ] + ], + [ + [ + 33.398, + 46.867 + ], + [ + 34.057, + 46.867 + ], + [ + 34.057, + 47.197 + ], + [ + 33.398, + 47.197 + ], + [ + 33.398, + 46.867 + ] + ], + [ + [ + 32.969, + 46.46 + ], + [ + 33.321, + 46.46 + ], + [ + 33.321, + 46.672 + ], + [ + 32.969, + 46.672 + ], + [ + 32.969, + 46.46 + ] + ] + ] + } +}