Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const simple_intersect = function (A_Vs2d, B_Vs2d) {
// find 2d intersection of two polygons each specified as list of 2d vertices.
try {
// martinez lib expects geojson multi-path polygon which uses repeated final vertices.
const A_Vs2d_ = A_Vs2d.concat([A_Vs2d[0]]);
const B_Vs2d_ = B_Vs2d.concat([B_Vs2d[0]]);
const result = martinez.intersection([A_Vs2d_], [B_Vs2d_]);
// assume simple intersection structure
// take 1st polygon, 1st path, drop last repeated vertex
if (result.length > 0 && result[0].length > 0) {
return result[0][0].slice(0, result[0][0].length);
} else {
return [];
}
} catch (e) { // catch rare errors
console.log(e);
return [];
}
}
Offset.prototype.offsetContour = function(curve, edges) {
var union, i, len;
if (isArray(curve[0]) && typeof curve[0][0] === 'number') {
// we have 1 less edge than vertices
for (i = 0, len = curve.length - 1; i < len; i++) {
var segment = this.ensureLastPoint(
this._offsetSegment(curve[i], curve[i + 1], edges[i], this._distance)
);
union = (i === 0) ?
[this.ensureLastPoint(segment)] :
martinez.union(union, this.ensureLastPoint(segment));
}
} else {
for (i = 0, len = edges.length; i < len; i++) {
union = (i === 0) ?
this.offsetContour(curve[i], edges[i]) :
martinez.union(union, this.offsetContour(curve[i], edges[i]));
}
}
return union;
};
var union, i, len;
if (isArray(curve[0]) && typeof curve[0][0] === 'number') {
// we have 1 less edge than vertices
for (i = 0, len = curve.length - 1; i < len; i++) {
var segment = this.ensureLastPoint(
this._offsetSegment(curve[i], curve[i + 1], edges[i], this._distance)
);
union = (i === 0) ?
[this.ensureLastPoint(segment)] :
martinez.union(union, this.ensureLastPoint(segment));
}
} else {
for (i = 0, len = edges.length; i < len; i++) {
union = (i === 0) ?
this.offsetContour(curve[i], edges[i]) :
martinez.union(union, this.offsetContour(curve[i], edges[i]));
}
}
return union;
};
Offset.prototype.offsetContour = function(curve, edges) {
var union, i, len;
if (isArray(curve[0]) && typeof curve[0][0] === 'number') {
// we have 1 less edge than vertices
for (i = 0, len = curve.length - 1; i < len; i++) {
var segment = this.ensureLastPoint(
this._offsetSegment(curve[i], curve[i + 1], edges[i], this._distance)
);
union = (i === 0) ?
[this.ensureLastPoint(segment)] :
martinez.union(union, this.ensureLastPoint(segment));
}
} else {
for (i = 0, len = edges.length; i < len; i++) {
union = (i === 0) ?
this.offsetContour(curve[i], edges[i]) :
martinez.union(union, this.offsetContour(curve[i], edges[i]));
}
}
return union;
};
var union, i, len;
if (isArray(curve[0]) && typeof curve[0][0] === 'number') {
// we have 1 less edge than vertices
for (i = 0, len = curve.length - 1; i < len; i++) {
var segment = this.ensureLastPoint(
this._offsetSegment(curve[i], curve[i + 1], edges[i], this._distance)
);
union = (i === 0) ?
[this.ensureLastPoint(segment)] :
martinez.union(union, this.ensureLastPoint(segment));
}
} else {
for (i = 0, len = edges.length; i < len; i++) {
union = (i === 0) ?
this.offsetContour(curve[i], edges[i]) :
martinez.union(union, this.offsetContour(curve[i], edges[i]));
}
}
return union;
};
Offset.prototype.margin = function(dist) {
this.distance(dist);
if (typeof this.vertices[0] === 'number') { // point
return this.offsetPoint(this._distance);
}
if (dist === 0) return this.vertices;
var union = this.offsetLines(this._distance);
//return union;
union = martinez.union(this.vertices, union);
return orientRings(union);
};
Offset.prototype.margin = function(dist) {
this.distance(dist);
if (typeof this.vertices[0] === 'number') { // point
return this.offsetPoint(this._distance);
}
if (dist === 0) return this.vertices;
var union = this.offsetLines(this._distance);
//return union;
union = martinez.union(this.vertices, union);
return orientRings(union);
};
}
// Generate some unique IDs and add some metadata
var featurehash = utilHashcode(stringify(feature));
var propertyhash = utilHashcode(stringify(feature.properties || {}));
feature.__layerID__ = layerID.replace(/[^_a-zA-Z0-9\-]/g, '_');
feature.__featurehash__ = featurehash;
feature.__propertyhash__ = propertyhash;
features.push(feature);
// Clipped Polygons at same zoom with identical properties can get merged
if (isClipped && geometry.type === 'MultiPolygon') {
var merged = mergeCache[propertyhash];
if (merged && merged.length) {
var other = merged[0];
var coords = martinez.union(
feature.geometry.coordinates, other.geometry.coordinates
);
if (!coords || !coords.length) {
continue; // something failed in martinez union
}
merged.push(feature);
for (var j = 0; j < merged.length; j++) { // all these features get...
merged[j].geometry.coordinates = coords; // same coords
merged[j].__featurehash__ = featurehash; // same hash, so deduplication works
}
} else {
mergeCache[propertyhash] = [feature];
}
}
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);
}
function intersect(poly1, poly2) {
var geom1 = getGeom(poly1);
var geom2 = getGeom(poly2);
var properties = poly1.properties || {};
// Return null if geometry is too narrow in coordinate precision
// fixes topology errors with JSTS
// https://github.com/Turfjs/turf/issues/463
// https://github.com/Turfjs/turf/pull/1004
// if (cleanCoords(truncate(geom2, {precision: 4})).coordinates[0].length < 4) return null;
// if (cleanCoords(truncate(geom1, {precision: 4})).coordinates[0].length < 4) return null;
var intersection = martinez.intersection(geom1.coordinates, geom2.coordinates);
if (intersection === null || intersection.length === 0) return null;
if (intersection.length === 1) return polygon(intersection[0], properties);
else return multiPolygon(intersection, properties);
}