Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turf polygonize #767

Merged
merged 37 commits into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
626350d
First commit
NickCis May 22, 2017
25ab9ef
Implemented getEdgeRings
NickCis May 22, 2017
1fd0220
Added EdgeRing class. Improved documentation
NickCis May 23, 2017
fc43147
Implemented EdgeRing.isHole
NickCis May 23, 2017
067cb33
Added more comments
NickCis May 23, 2017
acabf67
Updating comments to JsDoc
NickCis May 23, 2017
fb6a2e5
Adding findEdgeRingContaining: to be finished
NickCis May 23, 2017
b1593b0
Finished algorthm, not working correctly
NickCis May 25, 2017
365f6fc
Works as expected :)
NickCis May 27, 2017
20372ac
Cleaned comments and added test
NickCis May 27, 2017
3c41abd
Move stuff to packages/turf-polygonize
NickCis May 27, 2017
228f861
Merge branch 'polygonize' into turf-polygonize
NickCis May 27, 2017
2e46b12
Refactor: Separate classes and tests in files
NickCis May 27, 2017
499ecee
Added Test. Updated documentation. Generated Readme
NickCis May 28, 2017
3d6e2f6
Fixed src/util.js lint
NickCis May 28, 2017
6caa419
Lint: fixed Node and Node.test
NickCis May 28, 2017
2c9eddc
Lint: fixed src/Graph.test.js
NickCis May 28, 2017
af3c0be
Lind: Fixed src/Graph.js
NickCis May 28, 2017
ee31969
Lint: Fixed src/EdgeRing
NickCis May 28, 2017
8769bda
Lint: fixed src/Edge
NickCis May 28, 2017
92ffec8
Lint: Fixed index.js
NickCis May 28, 2017
3ef8f79
Updated Readme
NickCis May 28, 2017
4557a0e
Moved polygonize to other package
NickCis May 28, 2017
30166ce
Updated Readme
NickCis May 28, 2017
2cf4a30
Added license and files in package.json
NickCis May 28, 2017
991bc80
Added homepage to package.json
NickCis May 28, 2017
96e37d5
Added @name to JSDoc of index
NickCis May 28, 2017
35c16eb
Fixing tests
NickCis May 28, 2017
3e5585e
Added mutation tests
May 30, 2017
cb6b30b
Add missing required files
DenisCarriere May 31, 2017
b75446e
End tests
DenisCarriere May 31, 2017
cf6a7ad
Added Support Feature/Geometry LineStrings
DenisCarriere May 31, 2017
fab043f
Update typescript definition
DenisCarriere May 31, 2017
614723b
Add MultiLineString Fixture
DenisCarriere May 31, 2017
54f63c7
Updated bench results
NickCis May 31, 2017
51b88c0
Upgraded poligonize version
NickCis May 31, 2017
2298657
Keep module as a function
DenisCarriere Jun 1, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
21 changes: 21 additions & 0 deletions packages/turf-polygonize/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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.
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add @name polygonize

* @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 = polygonize;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to keep the basic function syntax in case anyone wants to expand the TurfJS method they can easily do so.

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