How to use the @turf/helpers.multiPolygon function in @turf/helpers

To help you get started, we’ve selected a few @turf/helpers 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 Turfjs / turf / packages / turf-intersect / index.ts View on Github external
const subGeom = getGeom(polygon(coords));
        const subIntersection = intersect(subGeom, geom2);

        if (subIntersection) {
          const subIntGeom = getGeom(subIntersection);

          if (subIntGeom.type === "Polygon") { resultCoords.push(subIntGeom.coordinates);
          } else if (subIntGeom.type === "MultiPolygon") { resultCoords = resultCoords.concat(subIntGeom.coordinates);
          } else { throw new Error("intersection is invalid"); }
        }
      }

      // Make a polygon with the result
      if (resultCoords.length === 0) { return null; }
      if (resultCoords.length === 1) { return polygon(resultCoords[0], options.properties);
      } else { return multiPolygon(resultCoords, options.properties); }

    } else if (geom2.type === "MultiPolygon") {
      // geom1 is a polygon and geom2 a multiPolygon,
      // put the multiPolygon first and fallback to the previous case.
      return intersect(geom2, geom1);

    } else {
      // handle invalid geometry types
      throw new Error("poly1 and poly2 must be either polygons or multiPolygons");
    }
}
github Turfjs / turf / packages / turf-difference / index.js View on Github external
function difference(polygon1, polygon2) {
    var geom1 = getGeom(polygon1);
    var geom2 = getGeom(polygon2);
    var properties = polygon1.properties || {};

    // Issue #721 - JSTS/Martinez can't handle empty polygons
    geom1 = removeEmptyPolygon(geom1);
    geom2 = removeEmptyPolygon(geom2);
    if (!geom1) return null;
    if (!geom2) return feature(geom1, properties);

    var differenced = martinez.diff(geom1.coordinates, geom2.coordinates);
    if (differenced.length === 0) return null;
    if (differenced.length === 1) return polygon(differenced[0], properties);
    else return multiPolygon(differenced, properties);
}
github Turfjs / turf / packages / turf-line-to-polygon / index.ts View on Github external
// Optional parameters
    var properties = options.properties;
    var autoComplete = options.autoComplete;
    var orderCoords = options.orderCoords;

    // default params
    autoComplete = (autoComplete !== undefined) ? autoComplete : true;
    orderCoords = (orderCoords !== undefined) ? orderCoords : true;

    switch (lines.type) {
        case 'FeatureCollection':
            var coords = [];
            lines.features.forEach(function (line) {
                coords.push(getCoords(lineStringToPolygon(line, {}, autoComplete, orderCoords)));
            });
            return multiPolygon(coords, properties);
        default:
            return lineStringToPolygon(lines, properties, autoComplete, orderCoords);
    }
}
github Turfjs / turf / packages / turf-rewind / types.ts View on Github external
import { polygon, lineString, multiLineString, multiPolygon } from '@turf/helpers'
import rewind from './'

const coords = [[121, -29], [138, -29], [138, -18], [121, -18], [121, -29]]
const poly = polygon([coords])
const line = lineString(coords)
const multiPoly = multiPolygon([[coords], [coords]])
const multiLine = multiLineString([coords, coords])

rewind(line)
rewind(poly)
rewind(multiPoly)
rewind(multiLine)
rewind(poly, true)
rewind(poly, true, true)
github Turfjs / turf / packages / turf-polygon-to-line / types.ts View on Github external
import { polygon, multiPolygon } from '@turf/helpers';
import polygonToLine from './';

const poly = polygon([[[125, -30], [145, -30], [145, -20], [125, -20], [125, -30]]]);
const multiPoly = multiPolygon([
  [[[125, -30], [145, -30], [145, -20], [125, -20], [125, -30]]],
  [[[135, -40], [155, -40], [155, -30], [135, -30], [135, -40]]]
]);

const feature = polygonToLine(poly);
const collection = polygonToLine(multiPoly);
github Turfjs / turf / packages / turf-polygon-smooth / index.js View on Github external
if (i > 0) poly = polygon(outCoords).geometry;
                processPolygon(poly, tempOutput);
                outCoords = tempOutput.slice(0);
            }
            outPolys.push(polygon(outCoords, properties));
            break;
        case 'MultiPolygon':
            outCoords = [[[]]];
            for (var y = 0; y < iterations; y++) {
                tempOutput = [[[]]];
                poly = geom;
                if (y > 0) poly = multiPolygon(outCoords).geometry;
                processMultiPolygon(poly, tempOutput);
                outCoords = tempOutput.slice(0);
            }
            outPolys.push(multiPolygon(outCoords, properties));
            break;
        default:
            throw new Error('geometry is invalid, must be Polygon or MultiPolygon');
        }
    });
    return featureCollection(outPolys);
github Turfjs / turf / packages / turf-line-overlap / types.ts View on Github external
import {lineString, multiLineString, polygon, multiPolygon} from '@turf/helpers'
import lineOverlap from './'

const line = lineString([[0, 0], [10, 10]]);
const multiLine = multiLineString([[[0, 0], [10, 10]], [[30, 30], [50, 50]]]);
const poly = polygon([[[0, 0], [10, 10], [15, 15], [0, 0]]]);
const multiPoly = multiPolygon([
  [[[0, 0], [10, 10], [15, 15], [0, 0]]],
  [[[5, 5], [30, 30], [45, 45], [5, 5]]]
])

lineOverlap(line, poly)
lineOverlap(line, line)
lineOverlap(multiPoly, line)
lineOverlap(multiPoly, multiLine)
lineOverlap(multiPoly, multiLine, 5)
github Turfjs / turf / packages / turf-bbox-clip / index.ts View on Github external
let coords: any[] = geom.coordinates;

    switch (type) {
    case "LineString":
    case "MultiLineString":
        const lines: any[] = [];
        if (type === "LineString") { coords = [coords]; }
        coords.forEach((line) => {
            lineclip.polyline(line, bbox, lines);
        });
        if (lines.length === 1) { return lineString(lines[0], properties); }
        return multiLineString(lines, properties);
    case "Polygon":
        return polygon(clipPolygon(coords, bbox), properties);
    case "MultiPolygon":
        return multiPolygon(coords.map((poly) => {
            return clipPolygon(poly, bbox);
        }), properties);
    default:
        throw new Error("geometry " + type + " not supported");
    }
}
github Turfjs / turf / packages / turf-polygon-smooth / index.js View on Github external
outCoords = [[]];
            for (var i = 0; i < iterations; i++) {
                tempOutput = [[]];
                poly = geom;
                if (i > 0) poly = polygon(outCoords).geometry;
                processPolygon(poly, tempOutput);
                outCoords = tempOutput.slice(0);
            }
            outPolys.push(polygon(outCoords, properties));
            break;
        case 'MultiPolygon':
            outCoords = [[[]]];
            for (var y = 0; y < iterations; y++) {
                tempOutput = [[[]]];
                poly = geom;
                if (y > 0) poly = multiPolygon(outCoords).geometry;
                processMultiPolygon(poly, tempOutput);
                outCoords = tempOutput.slice(0);
            }
            outPolys.push(multiPolygon(outCoords, properties));
            break;
        default:
            throw new Error('geometry is invalid, must be Polygon or MultiPolygon');
        }
    });
    return featureCollection(outPolys);