Source: densify.js

  1. import { geosloader } from "./helpers/geos.js";
  2. import { geojsonToGeosGeom, geosGeomToGeojson } from "geos-wasm/helpers";
  3. import { isemptygeom } from "./helpers/helpers";
  4. import { check } from "./helpers/check.js";
  5. /**
  6. * @function densify
  7. * @summary Densifies a geometry using a given distance tolerance
  8. * @description Based on `geos.GEOSDensify()`
  9. * @async
  10. * @param {object|array} data - A GeoJSON FeatureCollection, an array of features, an array of geometries, a single feature or a single geometry.
  11. * @param {object} options - Optional parameters
  12. * @param {number} [options.dist = 1] - The minimal distance between nodes
  13. * @returns {object|array} - A GeoJSON FeatureCollection, an array of features, an array of geometries, a single feature or a single geometry (it depends on what you've set as `data`).
  14. * @example
  15. * await geotoolbox.densify(*a geojson*, { dist:0.5 })
  16. */
  17. export async function densify(data, { dist = 1, mutate = false } = {}) {
  18. const geos = await geosloader();
  19. const handle = check(data, mutate);
  20. let x = handle.import(data);
  21. x.features.forEach((d) => {
  22. if (isemptygeom(d?.geometry)) {
  23. d.geometry = undefined;
  24. } else {
  25. const geosGeom = geojsonToGeosGeom(d, geos);
  26. const newGeom = geos.GEOSDensify(geosGeom, dist);
  27. const densiygeom = geosGeomToGeojson(newGeom, geos);
  28. d.geometry = densiygeom;
  29. geos.GEOSFree(geosGeom);
  30. geos.GEOSFree(newGeom);
  31. geos.GEOSFree(densiygeom);
  32. }
  33. });
  34. x.name = "densify";
  35. return handle.export(x);
  36. }