Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function unionPolygons(polygons) {
if (polygons.features.length <= 1) return polygons;
var tree = createIndex(polygons);
var results = [];
var removed = {};
flattenEach(polygons, function (currentFeature, currentIndex) {
// Exclude any removed features
if (removed[currentIndex]) return true;
// Don't search for itself
tree.remove({index: currentIndex}, filterByIndex);
removed[currentIndex] = true;
// Keep applying the union operation until no more overlapping features
while (true) {
var bbox = turfBBox(currentFeature);
var search = tree.search({
minX: bbox[0],
minY: bbox[1],
maxX: bbox[2],
maxY: bbox[3]
});
export default function polygonDissolve(
geojson: FeatureCollection,
options: {mutate?: boolean} = {},
): Feature | null {
// Validation
if (getType(geojson) !== "FeatureCollection") { throw new Error("geojson must be a FeatureCollection"); }
if (!geojson.features.length) { throw new Error("geojson is empty"); }
// Clone geojson to avoid side effects
// Topojson modifies in place, so we need to deep clone first
if (options.mutate === false || options.mutate === undefined) { geojson = clone(geojson); }
const geoms: any[] = [];
flattenEach(geojson, (feature) => {
geoms.push(feature.geometry);
});
const topo: any = topology({geoms: geometryCollection(geoms).geometry});
const merged: any = merge(topo, topo.objects.geoms.geometries);
return merged;
}
static fromGeoJson(geoJson) {
validateGeoJson(geoJson);
const graph = new Graph();
flattenEach(geoJson, feature => {
featureOf(feature, 'LineString', 'Graph::fromGeoJson');
// When a LineString if formed by many segments, split them
coordReduce(feature, (prev, cur) => {
if (prev) {
const start = graph.getNode(prev),
end = graph.getNode(cur);
graph.addEdge(start, end);
}
return cur;
});
});
return graph;
}
flattenEach(feature1, (flatten1) => {
flattenEach(feature2, (flatten2) => {
if (bool === true) { return true; }
bool = !booleanDisjoint(flatten1.geometry, flatten2.geometry);
});
});
return bool;
function removeEmptyPolygon(geom) {
switch (geom.type) {
case 'Polygon':
if (area(geom) > 1) return geom;
return null;
case 'MultiPolygon':
var coordinates = [];
flattenEach(geom, function (feature) {
if (area(feature) > 1) coordinates.push(feature.geometry.coordinates);
});
if (coordinates.length) return {type: 'MultiPolygon', coordinates: coordinates};
}
}
function buildMask(maskPolygon, polygonOuters, polygonInners) {
var coordinates = [];
coordinates.push(maskPolygon.geometry.coordinates[0]);
flattenEach(polygonOuters, function (feature) {
coordinates.push(feature.geometry.coordinates[0]);
});
flattenEach(polygonInners, function (feature) {
coordinates.push(feature.geometry.coordinates[0]);
});
return polygon(coordinates);
}
function buildMask(maskPolygon, polygonOuters, polygonInners) {
var coordinates = [];
coordinates.push(maskPolygon.geometry.coordinates[0]);
flattenEach(polygonOuters, function (feature) {
coordinates.push(feature.geometry.coordinates[0]);
});
flattenEach(polygonInners, function (feature) {
coordinates.push(feature.geometry.coordinates[0]);
});
return polygon(coordinates);
}
function booleanDisjoint(feature1: Feature | Geometry, feature2: Feature | Geometry): boolean {
let bool = true;
flattenEach(feature1, (flatten1) => {
flattenEach(feature2, (flatten2) => {
if (bool === false) { return false; }
bool = disjoint(flatten1.geometry, flatten2.geometry);
});
});
return bool;
}