index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // index.ts
  2. import { featureCollection, lineString, multiLineString } from "@turf/helpers";
  3. import { getGeom } from "@turf/invariant";
  4. function polygonToLine(poly, options = {}) {
  5. const geom = getGeom(poly);
  6. if (!options.properties && poly.type === "Feature") {
  7. options.properties = poly.properties;
  8. }
  9. switch (geom.type) {
  10. case "Polygon":
  11. return singlePolygonToLine(geom, options);
  12. case "MultiPolygon":
  13. return multiPolygonToLine(geom, options);
  14. default:
  15. throw new Error("invalid poly");
  16. }
  17. }
  18. function singlePolygonToLine(poly, options = {}) {
  19. const geom = getGeom(poly);
  20. const coords = geom.coordinates;
  21. const properties = options.properties ? options.properties : poly.type === "Feature" ? poly.properties : {};
  22. return coordsToLine(coords, properties);
  23. }
  24. function multiPolygonToLine(multiPoly, options = {}) {
  25. const geom = getGeom(multiPoly);
  26. const coords = geom.coordinates;
  27. const properties = options.properties ? options.properties : multiPoly.type === "Feature" ? multiPoly.properties : {};
  28. const lines = [];
  29. coords.forEach((coord) => {
  30. lines.push(coordsToLine(coord, properties));
  31. });
  32. return featureCollection(lines);
  33. }
  34. function coordsToLine(coords, properties) {
  35. if (coords.length > 1) {
  36. return multiLineString(coords, properties);
  37. }
  38. return lineString(coords[0], properties);
  39. }
  40. var turf_polygon_to_line_default = polygonToLine;
  41. export {
  42. coordsToLine,
  43. turf_polygon_to_line_default as default,
  44. multiPolygonToLine,
  45. polygonToLine,
  46. singlePolygonToLine
  47. };
  48. //# sourceMappingURL=index.js.map