-
Notifications
You must be signed in to change notification settings - Fork 944
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
Turf polygonize #767
Changes from 25 commits
626350d
25ab9ef
1fd0220
fc43147
067cb33
acabf67
fb6a2e5
b1593b0
365f6fc
20372ac
3c41abd
228f861
2e46b12
499ecee
3d6e2f6
6caa419
2c9eddc
af3c0be
ee31969
8769bda
92ffec8
3ef8f79
4557a0e
30166ce
2cf4a30
991bc80
96e37d5
35c16eb
3e5585e
cb6b30b
b75446e
cf6a7ad
fab043f
614723b
54f63c7
51b88c0
2298657
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
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://github.com/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 | ||
``` |
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. | ||
* | ||
* @param {FeatureCollection<LineString>} geoJson - Lines in order to polygonize | ||
* @returns {FeatureCollection<Polygon>} - Polygons created | ||
*/ | ||
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);
} |
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://github.com/Turfjs/turf.git" | ||
}, | ||
"keywords": [ | ||
"turf", | ||
"geojson", | ||
"gis", | ||
"polygonize" | ||
], | ||
"author": "Turf Authors", | ||
"contributors": [ | ||
"Nicolas Cisco <@nickcis>" | ||
], | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Turfjs/turf/issues" | ||
}, | ||
"homepage": "https://github.com/Turfjs/turf#readme", | ||
"devDependencies": { | ||
"@turf/helpers": "^4.3.0", | ||
"tape": "^4.6.3" | ||
}, | ||
"dependencies": { | ||
"polygonize": "^1.0.0" | ||
} | ||
} |
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') { | ||
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. Don't know the purpose of this? Since the files are being saved to directories.out + filename |
||
return path.join(directories[type], filename); | ||
} | ||
|
||
function readJsonFile(filename, type='in') { | ||
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. You can use 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)); | ||
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. Use write.sync(directories.out + filename, geojson) |
||
} | ||
|
||
fs.readdirSync(directories.in) | ||
.filter(filename => !filename.startsWith('.')) | ||
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. Which files are being filtered? 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. I work with vim, vim generates swap files in the file's folder ( 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. 😕 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}) => { | ||
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. I would recommend your testing template with one of the recent modules. https://github.com/Turfjs/turf/blob/master/packages/turf-transform-translate/test.js |
||
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); | ||
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. Writing GeoJSON should only be saved if 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. 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(); | ||
}); | ||
}); | ||
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. Add the following tests:
Examples here: https://github.com/Turfjs/turf/blob/master/packages/turf-transform-rotate/test.js 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. 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 ?) 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. 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 There's also MultiLineString that you could add as support. |
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