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 25 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.
46 changes: 46 additions & 0 deletions packages/turf-polygonize/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# @turf/polygonize

# index

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. LineStrings must only have two coordinate
points.

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)<[LineString](http://geojson.org/geojson-spec.html#linestring)>** Lines in order to polygonize

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
```
18 changes: 18 additions & 0 deletions packages/turf-polygonize/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var polygonize = require('polygonize');

/** 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. LineStrings must only have two coordinate
* points.
*
* 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

* @param {FeatureCollection<LineString>} geoJson - Lines in order to polygonize
* @returns {FeatureCollection<Polygon>} - Polygons created
*/
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);
}

38 changes: 38 additions & 0 deletions packages/turf-polygonize/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@turf/polygonize",
"version": "4.3.0",
"description": "turf polygonize module",
"main": "index.js",
"files": [
"index.js"
],
"scripts": {
"test": "tape test.js"
},
"repository": {
"type": "git",
"url": "git:/Turfjs/turf.git"
},
"keywords": [
"turf",
"geojson",
"gis",
"polygonize"
],
"author": "Turf Authors",
"contributors": [
"Nicolas Cisco <@nickcis>"
],
"license": "MIT",
"bugs": {
"url": "https:/Turfjs/turf/issues"
},
"homepage": "https:/Turfjs/turf#readme",
"devDependencies": {
"@turf/helpers": "^4.3.0",
"tape": "^4.6.3"
},
"dependencies": {
"polygonize": "^1.0.0"
}
}
51 changes: 51 additions & 0 deletions packages/turf-polygonize/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const test = require('tape'),
{ featureCollection, lineString, polygon } = require('@turf/helpers'),
polygonize = require('./'),
fs = require('fs'),
path = require('path');

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

function getFullPath(filename, type='in') {
Copy link
Member

Choose a reason for hiding this comment

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

Don't know the purpose of this? Since the files are being saved to test/out you can simply use:

directories.out + filename

return path.join(directories[type], filename);
}

function readJsonFile(filename, type='in') {
Copy link
Member

@DenisCarriere DenisCarriere May 28, 2017

Choose a reason for hiding this comment

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

You can use load-json-file which handles reading GeoJSON/JSON, most of the recent modules are using this.

load.sync(directories.in + filename)

try {
return JSON.parse(fs.readFileSync(getFullPath(filename, type)));
} catch(e) {
if (e.code !== 'ENOENT')
throw e;
return undefined;
}
}

function writeJsonFile(filename, data, type='out') {
fs.writeFileSync(getFullPath(filename, type), JSON.stringify(data));
Copy link
Member

@DenisCarriere DenisCarriere May 28, 2017

Choose a reason for hiding this comment

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

Use write-json-file for this behavior.

write.sync(directories.out + filename, geojson)

}

fs.readdirSync(directories.in)
.filter(filename => !filename.startsWith('.'))
Copy link
Member

Choose a reason for hiding this comment

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

Which files are being filtered?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I work with vim, vim generates swap files in the file's folder (.*.swp files). When editing tests you often forget to close the input file in order to eliminate the swap file.

Copy link
Member

Choose a reason for hiding this comment

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

😕 woot for vim

.map(filename => {
return {
filename,
name: path.parse(filename).name,
input: readJsonFile(filename, 'in'),
output: readJsonFile(filename, 'out'),
}
})
.forEach(({filename, name, input, output}) => {
Copy link
Member

Choose a reason for hiding this comment

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

test(`turf-polygonize :: ${name}`, t => {
const result = polygonize(input);
if (output) {
t.deepEqual(result, output);
} else {
t.skip(`${name} not found :: writing`);
writeJsonFile(filename, result);
Copy link
Member

Choose a reason for hiding this comment

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

Writing GeoJSON should only be saved if process.env.REGEN is true (or exists). Shouldn't be saved each time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The geoJsons are only written when no output test file exists.

I didn't know that turf has the convention of using that environmental variable. I'll change the test.

}
t.end();
});
});
Copy link
Member

Choose a reason for hiding this comment

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

Add the following tests:

  • Geometry Support
  • Input mutation

Examples here: https:/Turfjs/turf/blob/master/packages/turf-transform-rotate/test.js

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't understand what you are asking with geometry support. (I'm really new to geojson), should i test using a GeometryCollection as input, instead of always using FeatureCollection?.

(Is there a difference between Geometry(Collection) / Feature(Collection)?, Or is it just for naming / cohesion purposes ?)

Copy link
Member

Choose a reason for hiding this comment

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

A GeometryCollection is a geometry type of a Feature where a FeatureCollection is a list of Features.

The Geometry tests just make sure the module doesn't break when you input a Geometry<LineString> or a Feature<LineString>.

There's also MultiLineString that you could add as support.

Loading