Skip to content

Commit

Permalink
Turf polygonize (#767)
Browse files Browse the repository at this point in the history
* First commit

* Implemented getEdgeRings

* Added EdgeRing class. Improved documentation

* Implemented EdgeRing.isHole

* Added more comments

* Updating comments to JsDoc

* Adding findEdgeRingContaining: to be finished

* Finished algorthm, not working correctly

* Works as expected :)

* Cleaned comments and added test

* Move stuff to packages/turf-polygonize

* Refactor: Separate classes and tests in files

* Added Test. Updated documentation. Generated Readme

* Fixed src/util.js lint

* Lint: fixed Node and Node.test

* Lint: fixed src/Graph.test.js

* Lind: Fixed src/Graph.js

* Lint: Fixed src/EdgeRing

* Lint: fixed src/Edge

* Lint: Fixed index.js

* Updated Readme

* Moved polygonize to other package

* Updated Readme

* Added license and files in package.json

* Added homepage to package.json

* Added @name to JSDoc of index

* Fixing tests

* Added mutation tests

* Add missing required files
- Typescript definition
- Benchmark
- Yarn.lock
- Update test.js

* End tests

* Added Support Feature/Geometry LineStrings
- Colorized output
- Fixed fixtures

* Update typescript definition

* Add MultiLineString Fixture

* Updated bench results

* Upgraded poligonize version

* Keep module as a function
  • Loading branch information
NickCis authored and DenisCarriere committed Jun 1, 2017
1 parent 1cbe8fc commit 792f2f9
Show file tree
Hide file tree
Showing 22 changed files with 12,243 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/turf-polygonize/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2017 TurfJS

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
49 changes: 49 additions & 0 deletions packages/turf-polygonize/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# @turf/polygonize

# polygonize

Polygonizes [(Multi)LineString(s)](http://geojson.org/geojson-spec.html#linestring) into [Polygons](Polygons).

Implementation of GEOSPolygonize function (`geos::operation::polygonize::Polygonizer`).

Polygonizes a set of lines that represents edges in a planar graph. Edges must be correctly
noded, i.e., they must only meet at their endpoints.

The implementation correctly handles:

- Dangles: edges which have one or both ends which are not incident on another edge endpoint.
- Cut Edges (bridges): edges that are connected at both ends but which do not form part of a polygon.

**Parameters**

- `geojson` **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<([LineString](http://geojson.org/geojson-spec.html#linestring) \| [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring))>)** Lines in order to polygonize


- Throws **[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)** if geoJson is invalid.

Returns **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<[Polygon](http://geojson.org/geojson-spec.html#polygon)>** Polygons created

<!-- This file is automatically generated. Please don't edit it directly:
if you find an error, edit the source file (likely index.js), and re-run
./scripts/generate-readmes in the turf project. -->

---

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:/Turfjs/turf) repository, where you can create
PRs and issues.

### Installation

Install this module individually:

```sh
$ npm install @turf/polygonize
```

Or install the Turf module that includes it as a function:

```sh
$ npm install @turf/turf
```
46 changes: 46 additions & 0 deletions packages/turf-polygonize/bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const fs = require('fs');
const path = require('path');
const load = require('load-json-file');
const Benchmark = require('benchmark');
const polygonize = require('./');

const directory = path.join(__dirname, 'test', 'in') + path.sep;
const fixtures = fs.readdirSync(directory).map(filename => {
return {
name: path.parse(filename).name,
geojson: load.sync(directory + filename)
};
});


/**
* Single Process Benchmark
*
* complex: 37.120ms
* cutedge: 0.858ms
* dangle: 0.289ms
* two-polygons: 0.784ms
*/
for (const {name, geojson} of fixtures) {
console.time(name);
polygonize(geojson);
console.timeEnd(name);
}

/**
* Benchmark Results
*
* complex x 54.67 ops/sec ±9.63% (47 runs sampled)
* cutedge x 5,413 ops/sec ±2.20% (84 runs sampled)
* dangle x 9,175 ops/sec ±4.44% (83 runs sampled)
* two-polygons x 16,323 ops/sec ±1.39% (91 runs sampled)
*/
const suite = new Benchmark.Suite('turf-transform-polygonize');
for (const {name, geojson} of fixtures) {
suite.add(name, () => polygonize(geojson));
}

suite
.on('cycle', e => console.log(String(e.target)))
.on('complete', () => {})
.run();
11 changes: 11 additions & 0 deletions packages/turf-polygonize/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference types="geojson" />

type Polygons = GeoJSON.FeatureCollection<GeoJSON.Polygon>;
type Geoms = GeoJSON.LineString | GeoJSON.MultiLineString;

/**
* http://turfjs.org/docs/#polygonize
*/
declare function polygonize<Geom extends Geoms>(geojson: GeoJSON.Feature<Geom> | GeoJSON.FeatureCollection<Geom> | Geom): Polygons;
declare namespace polygonize { }
export = polygonize;
23 changes: 23 additions & 0 deletions packages/turf-polygonize/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var polygonize = require('polygonize');

/**
* Polygonizes {@link LineString|(Multi)LineString(s)} into {@link Polygons}.
*
* Implementation of GEOSPolygonize function (`geos::operation::polygonize::Polygonizer`).
*
* Polygonizes a set of lines that represents edges in a planar graph. Edges must be correctly
* noded, i.e., they must only meet at their endpoints.
*
* The implementation correctly handles:
*
* - Dangles: edges which have one or both ends which are not incident on another edge endpoint.
* - Cut Edges (bridges): edges that are connected at both ends but which do not form part of a polygon.
*
* @name polygonize
* @param {FeatureCollection|Geometry|Feature<LineString|MultiLineString>} geojson Lines in order to polygonize
* @returns {FeatureCollection<Polygon>} Polygons created
* @throws {Error} if GeoJSON is invalid.
*/
module.exports = function (geojson) {
return polygonize(geojson);
};
46 changes: 46 additions & 0 deletions packages/turf-polygonize/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@turf/polygonize",
"version": "4.3.0",
"description": "turf polygonize 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:/Turfjs/turf.git"
},
"keywords": [
"turf",
"geojson",
"gis",
"polygonize"
],
"author": "Turf Authors",
"contributors": [
"Nicolas Cisco <@nickcis>",
"Denis Carriere <@DenisCarriere>"
],
"license": "MIT",
"bugs": {
"url": "https:/Turfjs/turf/issues"
},
"homepage": "https:/Turfjs/turf",
"devDependencies": {
"@turf/helpers": "^4.3.0",
"@turf/meta": "^4.3.0",
"benchmark": "^2.1.4",
"load-json-file": "^2.0.0",
"tape": "^4.6.3",
"write-json-file": "^2.2.0"
},
"dependencies": {
"polygonize": "^1.0.1"
}
}
70 changes: 70 additions & 0 deletions packages/turf-polygonize/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const fs = require('fs');
const test = require('tape');
const path = require('path');
const load = require('load-json-file');
const write = require('write-json-file');
const {featureEach} = require('@turf/meta');
const {featureCollection, lineString} = require('@turf/helpers');
const polygonize = require('./');

const directories = {
in: path.join(__dirname, 'test', 'in') + path.sep,
out: path.join(__dirname, 'test', 'out') + path.sep
};

const fixtures = fs.readdirSync(directories.in).map(filename => {
return {
filename,
name: path.parse(filename).name,
geojson: load.sync(directories.in + filename)
};
});

test('turf-polygonize', t => {
for (const {filename, name, geojson} of fixtures) {
const polygonized = polygonize(geojson);

const results = featureCollection([]);
featureEach(geojson, feature => results.features.push(colorize(feature)));
featureEach(polygonized, feature => results.features.push(colorize(feature, '#00F', 3)));

if (process.env.REGEN) write.sync(directories.out + filename, results);
t.deepEquals(results, load.sync(directories.out + filename), name);
}
t.end();
});

test('turf-polygonize -- Geometry Support', t => {
const line = lineString([[0, 0], [1, 1], [5, 2], [0, 0]]);

t.assert(polygonize(line.geometry), 'line geometry');
t.end();
});

test('turf-polygonize -- throws', t => {
// const line = lineString([[0, 0], [1, 1]]);

// t.throws(() => polygonize(line));
t.end();
});

test('turf-polygonize -- input mutation', t => {
const lines = featureCollection([
lineString([[0, 0], [1, 1]]),
lineString([[1, 1], [-1, -1]]),
lineString([[-1, -1], [0, 0]])
]);
const linesBefore = JSON.parse(JSON.stringify(lines));
polygonize(lines);

t.deepEquals(lines, linesBefore, 'input does not mutate');
t.end();
});

function colorize(feature, color = '#F00', width = 6) {
feature.properties['fill'] = color;
feature.properties['fill-opacity'] = 0.3;
feature.properties['stroke'] = color;
feature.properties['stroke-width'] = width;
return feature;
}
Loading

0 comments on commit 792f2f9

Please sign in to comment.