Javascript implementation of GEOS's Polygonize function
npm install polygonize
Polygonizes a set of Geometrys which contain linework that represents the edges of a planar graph. It's basically an implementation of GEOS's Polygonizer.
Although, the algorithm is the same as GEOS, it isn't a literal transcription of the C++ source code. It was rewriten in order to get a more javascript like code.
``javascriptgeos::operation::polygonize::Polygonizer
/**
* Polygonizes {@link LineString|(Multi)LineString(s)} into {@link Polygons}.
*
* Implementation of GEOSPolygonize function ().`
*
* 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
* @returns {FeatureCollection
* @throws {Error} if geoJson is invalid.
*/
This example is the test found in test/in/complex.geojson.
`javascript
const polygonize = require('polygonize'),
fs = require('fs'),
input = JSON.parse(fs.readFileSync('./test/in/complex.geojson'));
console.log(JSON.stringify(polygonize(input)));
`
The input as GeoJson LineString:

Turned into polygons:

Polygonizes (Multi)LineString(s) into 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 \| Geometry \| Feature<(LineString \| MultiLineString)>) Lines in order to polygonize
- Throws Error if geoJson is invalid.
Returns FeatureCollection<Polygon> Polygons created
Install this module individually:
`sh``
$ npm install polygonize