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..924432c3a0 --- /dev/null +++ b/packages/turf-linestring-to-polygon/README.md @@ -0,0 +1,54 @@ +# @turf/linestring-to-polygon + +# lineStringToPolygon + +Converts (Multi)LineString(s) to Polygon(s). + +**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..b6e7bd0d0a --- /dev/null +++ b/packages/turf-linestring-to-polygon/bench.js @@ -0,0 +1,37 @@ +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 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) { + 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..bf4a415f91 --- /dev/null +++ b/packages/turf-linestring-to-polygon/index.d.ts @@ -0,0 +1,24 @@ +/// + +// 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: 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 new file mode 100644 index 0000000000..34d706b48a --- /dev/null +++ b/packages/turf-linestring-to-polygon/index.js @@ -0,0 +1,134 @@ +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 {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 = { + * '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, 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) { + case 'FeatureCollection': + case 'GeometryCollection': + var coords = []; + var features = (lines.features) ? lines.features : lines.geometries; + features.forEach(function (line) { + coords.push(getCoords(lineStringToPolygon(line, {}, autoComplete, orderCoords))); + }); + return multiPolygon(coords, properties); + } + return lineStringToPolygon(lines, properties, autoComplete, orderCoords); +}; + +/** + * LineString to Polygon + * + * @private + * @param {Feature} line line + * @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, properties, autoComplete, orderCoords) { + properties = 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': + 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(multiCoords, properties); + default: + throw new Error('geometry type ' + type + ' is not supported'); + } +} + +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]; + 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; +} + +/** + * 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 new file mode 100644 index 0000000000..182fd7919c --- /dev/null +++ b/packages/turf-linestring-to-polygon/package.json @@ -0,0 +1,43 @@ +{ + "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/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 new file mode 100644 index 0000000000..c6d1bc173f --- /dev/null +++ b/packages/turf-linestring-to-polygon/test.js @@ -0,0 +1,37 @@ +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) { + let {autoComplete, properties, orderCoords} = geojson.properties || {}; + properties = properties || {stroke: '#F0F', 'stroke-width': 6}; + 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); + } + // 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 new file mode 100644 index 0000000000..3a23ec6f02 --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/in/collection-linestring.geojson @@ -0,0 +1,69 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 102, + 2 + ], + [ + 103, + 2 + ], + [ + 103, + 3 + ], + [ + 102, + 3 + ], + [ + 102, + 2 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "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..2db492a70a --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/in/linestring-incomplete.geojson @@ -0,0 +1,44 @@ +{ + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "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..b007bb5a68 --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/in/linestring.geojson @@ -0,0 +1,33 @@ + +{ + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "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..847c819352 --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/in/multi-linestring-incomplete.geojson @@ -0,0 +1,68 @@ +{ + "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-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/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..53dc4b052e --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/in/multi-linestring-with-hole.geojson @@ -0,0 +1,56 @@ +{ + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "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..8841078359 --- /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": { + "stroke": "#00F", + "stroke-width": 6 + }, + "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": "#F80", + "stroke-width": 6 + }, + "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": "#080", + "stroke-width": 6 + }, + "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/collection-linestring.geojson b/packages/turf-linestring-to-polygon/test/out/collection-linestring.geojson new file mode 100644 index 0000000000..1ec3305893 --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/out/collection-linestring.geojson @@ -0,0 +1,60 @@ +{ + "type": "Feature", + "properties": { + "stroke": "#F0F", + "stroke-width": 6 + }, + "geometry": { + "type": "MultiPolygon", + "coordinates": [ + [ + [ + [ + 102, + 2 + ], + [ + 103, + 2 + ], + [ + 103, + 3 + ], + [ + 102, + 3 + ], + [ + 102, + 2 + ] + ] + ], + [ + [ + [ + 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..8eacb22d9e --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/out/geometry-linestring.geojson @@ -0,0 +1,34 @@ +{ + "type": "Feature", + "properties": { + "stroke": "#F0F", + "stroke-width": 6 + }, + "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..cab40f9eaa --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/out/linestring-incomplete.geojson @@ -0,0 +1,50 @@ +{ + "type": "Feature", + "properties": { + "stroke": "#F0F", + "stroke-width": 6 + }, + "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..8eacb22d9e --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/out/linestring.geojson @@ -0,0 +1,34 @@ +{ + "type": "Feature", + "properties": { + "stroke": "#F0F", + "stroke-width": 6 + }, + "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..2fc4e7b7b4 --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/out/multi-linestring-incomplete.geojson @@ -0,0 +1,76 @@ +{ + "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-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 + ] + ] + ] + } +} 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..9ffb8c0801 --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/out/multi-linestring-with-hole.geojson @@ -0,0 +1,56 @@ +{ + "type": "Feature", + "properties": { + "stroke": "#F0F", + "stroke-width": 6 + }, + "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..0fcc76bb65 --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/out/multi-linestrings-with-holes.geojson @@ -0,0 +1,128 @@ +{ + "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 + ] + ] + ], + [ + [ + [ + 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 + ] + ] + ], + [ + [ + [ + 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..1a4f5e64ad --- /dev/null +++ b/packages/turf-linestring-to-polygon/test/types.ts @@ -0,0 +1,12 @@ +import {featureCollection, lineString, multiLineString, Polygon, MultiPolygon} from '@turf/helpers' +import * as polygonToLineString from '../' + +// Fixtures +const coords = [[125, -30], [145, -30], [145, -20], [125, -20], [125, -30]]; +const line = lineString(coords); +const multiLine = multiLineString([coords, coords]); + +// Assert results with types +const poly1: Polygon = polygonToLineString(line); +const poly2: Polygon = polygonToLineString(multiLine); +const multiPoly: MultiPolygon = polygonToLineString(featureCollection([line, multiLine]));