-
Notifications
You must be signed in to change notification settings - Fork 937
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
Turf polygonize #767
Changes from 36 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
626350d
First commit
NickCis 25ab9ef
Implemented getEdgeRings
NickCis 1fd0220
Added EdgeRing class. Improved documentation
NickCis fc43147
Implemented EdgeRing.isHole
NickCis 067cb33
Added more comments
NickCis acabf67
Updating comments to JsDoc
NickCis fb6a2e5
Adding findEdgeRingContaining: to be finished
NickCis b1593b0
Finished algorthm, not working correctly
NickCis 365f6fc
Works as expected :)
NickCis 20372ac
Cleaned comments and added test
NickCis 3c41abd
Move stuff to packages/turf-polygonize
NickCis 228f861
Merge branch 'polygonize' into turf-polygonize
NickCis 2e46b12
Refactor: Separate classes and tests in files
NickCis 499ecee
Added Test. Updated documentation. Generated Readme
NickCis 3d6e2f6
Fixed src/util.js lint
NickCis 6caa419
Lint: fixed Node and Node.test
NickCis 2c9eddc
Lint: fixed src/Graph.test.js
NickCis af3c0be
Lind: Fixed src/Graph.js
NickCis ee31969
Lint: Fixed src/EdgeRing
NickCis 8769bda
Lint: fixed src/Edge
NickCis 92ffec8
Lint: Fixed index.js
NickCis 3ef8f79
Updated Readme
NickCis 4557a0e
Moved polygonize to other package
NickCis 30166ce
Updated Readme
NickCis 2cf4a30
Added license and files in package.json
NickCis 991bc80
Added homepage to package.json
NickCis 96e37d5
Added @name to JSDoc of index
NickCis 35c16eb
Fixing tests
NickCis 3e5585e
Added mutation tests
cb6b30b
Add missing required files
DenisCarriere b75446e
End tests
DenisCarriere cf6a7ad
Added Support Feature/Geometry LineStrings
DenisCarriere fab043f
Update typescript definition
DenisCarriere 614723b
Add MultiLineString Fixture
DenisCarriere 54f63c7
Updated bench results
NickCis 51b88c0
Upgraded poligonize version
NickCis 2298657
Keep module as a function
DenisCarriere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
* | ||
* @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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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);
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
@name polygonize