Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
} = {},
): Feature
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");
}
}
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);
}
// 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);
}
}
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)
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);
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);
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)
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");
}
}
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);