Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
}
function dissolve(geojson: FeatureCollection, options: {
mutate?: boolean,
} = {}): Feature | null {
// Optional parameters
options = options || {};
if (!isObject(options)) { throw new Error("options is invalid"); }
const mutate = options.mutate;
// 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 (mutate === false || mutate === undefined) { geojson = clone(geojson); }
// Assert homogenity
const type = getHomogenousType(geojson);
if (!type) { throw new Error("geojson must be homogenous"); }
// Data => Typescript hack
const data: any = geojson;
switch (type) {
case "LineString":
return lineDissolve(data, options);
case "Polygon":
return polygonDissolve(data, options);
default:
throw new Error(type + " is not supported");
}
function lineDissolve(
geojson: FeatureCollection,
options: {mutate?: boolean} = {},
): Feature | null {
// Optional parameters
options = options || {};
if (!isObject(options)) { throw new Error("options is invalid"); }
const mutate = options.mutate;
// 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
if (mutate === false || mutate === undefined) { geojson = clone(geojson); }
const result: any[] = [];
const lastLine = lineReduce(geojson, (previousLine: any, currentLine: any) => {
// Attempt to merge this LineString with the other LineStrings, updating
// the reference as it is merged with others and grows.
const merged = mergeLineStrings(previousLine, currentLine);
// Accumulate the merged LineString
if (merged) { return merged;
// Put the unmerged LineString back into the list
} else {
result.push(previousLine);
return currentLine;
}
});
// Append the last line
// calculate the distance from each input point to the grid points
featureEach(points, function (point) {
var gridPoint = (gridType === 'point') ? gridFeature : centroid(gridFeature);
var d = distance(gridPoint, point, units);
var zValue;
// property has priority for zValue, fallbacks to 3rd coordinate from geometry
if (property !== undefined) zValue = point.properties[property];
if (zValue === undefined) zValue = point.geometry.coordinates[2];
if (zValue === undefined) throw new Error('zValue is missing');
if (d === 0) zw = zValue;
var w = 1.0 / Math.pow(d, weight);
sw += w;
zw += w * zValue;
});
// write interpolated value for each grid point
var newFeature = clone(gridFeature);
newFeature.properties[property] = zw / sw;
results.push(newFeature);
});
return featureCollection(results);
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;
}
function dissolve(featureCollection, options) {
// Optional parameters
options = options || {};
if (!isObject(options)) throw new Error('options is invalid');
var propertyName = options.propertyName;
// Input validation
collectionOf(featureCollection, 'Polygon', 'dissolve');
// Main
var fc = clone(featureCollection);
var features = fc.features;
var originalIndexOfItemsRemoved = [];
features.forEach(function (f, i) {
f.properties.origIndexPosition = i;
});
var tree = rbush();
tree.load(fc);
for (var i in features) {
var polygon = features[i];
var featureChanged = false;
tree.search(polygon).features.forEach(function (potentialMatchingFeature) {
if (!isObject(options)) throw new Error('options is invalid');
var pivot = options.pivot;
var mutate = options.mutate;
// Input validation
if (!geojson) throw new Error('geojson is required');
if (angle === undefined || angle === null || isNaN(angle)) throw new Error('angle is required');
// Shortcut no-rotation
if (angle === 0) return geojson;
// Use centroid of GeoJSON if pivot is not provided
if (!pivot) pivot = centroid(geojson);
// Clone geojson to avoid side effects
if (mutate === false || mutate === undefined) geojson = clone(geojson);
// Rotate each coordinate
coordEach(geojson, function (pointCoords) {
var initialAngle = rhumbBearing(pivot, pointCoords);
var finalAngle = initialAngle + angle;
var distance = rhumbDistance(pivot, pointCoords);
var newCoords = getCoords(rhumbDestination(pivot, distance, finalAngle));
pointCoords[0] = newCoords[0];
pointCoords[1] = newCoords[1];
});
return geojson;
}
function nearestPoint(targetPoint: Coord, points: FeatureCollection): NearestPoint {
// Input validation
if (!targetPoint) throw new Error('targetPoint is required');
if (!points) throw new Error('points is required');
let nearest: NearestPoint;
let minDist: number = Infinity;
let bestFeatureIndex: number = 0;
featureEach(points, (pt, featureIndex) => {
const distanceToPoint = distance(targetPoint, pt);
if (distanceToPoint < minDist) {
bestFeatureIndex = featureIndex;
minDist = distanceToPoint;
}
});
nearest = clone(points.features[bestFeatureIndex]);
nearest.properties.featureIndex = bestFeatureIndex;
nearest.properties.distanceToPoint = minDist;
return nearest;
}
function convert(geojson: any, projection: string, options: {mutate?: boolean} = {}): any {
// Optional parameters
options = options || {};
var mutate = options.mutate;
// Validation
if (!geojson) throw new Error('geojson is required');
// Handle Position
if (Array.isArray(geojson) && isNumber(geojson[0])) geojson = (projection === 'mercator') ? convertToMercator(geojson) : convertToWgs84(geojson);
// Handle GeoJSON
else {
// Handle possible data mutation
if (mutate !== true) geojson = clone(geojson);
coordEach(geojson, function (coord) {
var newCoord = (projection === 'mercator') ? convertToMercator(coord) : convertToWgs84(coord);
coord[0] = newCoord[0];
coord[1] = newCoord[1];
});
}
return geojson;
}
function polygonDissolve(geojson, options) {
if (options === void 0) { options = {}; }
// Validation
if (invariant_1.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_1.default(geojson);
}
var geoms = [];
meta_1.flattenEach(geojson, function (feature) {
geoms.push(feature.geometry);
});
var topo = topojson_1.topology({ geoms: helpers_1.geometryCollection(geoms).geometry });
var merged = topojson_1.merge(topo, topo.objects.geoms.geometries);
return merged;
}
exports.default = polygonDissolve;