How to use @turf/polygonize - 2 common examples

To help you get started, we’ve selected a few @turf/polygonize examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github terrestris / ol-util / src / GeometryUtil / GeometryUtil.js View on Github external
geometries.forEach(geometry => {
      // Convert the polygon to turf.js/GeoJSON geometry while
      // reprojecting them to the internal turf.js projection 'EPSG:4326'.
      const turfPolygon = geoJsonFormat.writeGeometryObject(geometry);
      const turfPolygonCoordinates = turfPolygon.coordinates;
      // outer lines of the given polygon
      const outer = lineString(turfPolygonCoordinates[0]);
      // polygonized outer polygon
      const outerPolygon = lineToPolygon(outer);
      // holes in the polygon
      const inners = [];
      turfPolygonCoordinates.slice(1, turfPolygonCoordinates.length).forEach(function (coord) {
        inners.push(lineString(coord));
      });
      // Polygonize the holes in the polygon
      const innerPolygon = polygonize(featureCollection(inners));
      // make a lineString from the spliting line and the outer of the polygon
      let unionGeom = union(outer, turfLine);
      // Polygonize the combined lines.
      const polygonizedUnionGeom = polygonize(unionGeom);
      // Array of the split polygons within the geometry
      const splitedPolygons = [];
      // Iterate over each feature in the combined feature and remove sections that are outside the initial polygon and
      // remove the parts from the cutted polygons that are in polygon holes.
      featureEach(polygonizedUnionGeom, cuttedSection => {
        // checks to see if segment is in polygon
        const segmentInPolygon = intersect(cuttedSection, outerPolygon);
        if (segmentInPolygon && segmentInPolygon.geometry.type === 'Polygon') {
          let polygonWithoutHoles = [];
          if (innerPolygon.features.length > 0) {
            // iterates over all the holes and removes their intersection with the cutted polygon
            innerPolygon.features.forEach(holes => {
github terrestris / ol-util / src / GeometryUtil / GeometryUtil.js View on Github external
const turfPolygonCoordinates = turfPolygon.coordinates;
      // outer lines of the given polygon
      const outer = lineString(turfPolygonCoordinates[0]);
      // polygonized outer polygon
      const outerPolygon = lineToPolygon(outer);
      // holes in the polygon
      const inners = [];
      turfPolygonCoordinates.slice(1, turfPolygonCoordinates.length).forEach(function (coord) {
        inners.push(lineString(coord));
      });
      // Polygonize the holes in the polygon
      const innerPolygon = polygonize(featureCollection(inners));
      // make a lineString from the spliting line and the outer of the polygon
      let unionGeom = union(outer, turfLine);
      // Polygonize the combined lines.
      const polygonizedUnionGeom = polygonize(unionGeom);
      // Array of the split polygons within the geometry
      const splitedPolygons = [];
      // Iterate over each feature in the combined feature and remove sections that are outside the initial polygon and
      // remove the parts from the cutted polygons that are in polygon holes.
      featureEach(polygonizedUnionGeom, cuttedSection => {
        // checks to see if segment is in polygon
        const segmentInPolygon = intersect(cuttedSection, outerPolygon);
        if (segmentInPolygon && segmentInPolygon.geometry.type === 'Polygon') {
          let polygonWithoutHoles = [];
          if (innerPolygon.features.length > 0) {
            // iterates over all the holes and removes their intersection with the cutted polygon
            innerPolygon.features.forEach(holes => {
              const toCut = polygonWithoutHoles.length > 0 ? polygonWithoutHoles : [segmentInPolygon];
              toCut.forEach((tocutPart, i) => {
                let intersection = difference(tocutPart, holes);
                if (intersection && (intersection.geometry.type === 'Polygon' || intersection.geometry.type === 'MultiPolygon')) {

@turf/polygonize

turf polygonize module

MIT
Latest version published 4 months ago

Package Health Score

94 / 100
Full package analysis

Popular @turf/polygonize functions