index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // index.ts
  2. import { explode } from "@turf/explode";
  3. import { center as centroid } from "@turf/center";
  4. import { nearestPoint } from "@turf/nearest-point";
  5. import { booleanPointInPolygon } from "@turf/boolean-point-in-polygon";
  6. import { featureCollection, feature, point } from "@turf/helpers";
  7. function pointOnFeature(geojson) {
  8. const fc = normalize(geojson);
  9. const cent = centroid(fc);
  10. let onSurface = false;
  11. let i = 0;
  12. while (!onSurface && i < fc.features.length) {
  13. const geom = fc.features[i].geometry;
  14. let x, y, x1, y1, x2, y2;
  15. let onLine = false;
  16. if (geom.type === "Point") {
  17. if (cent.geometry.coordinates[0] === geom.coordinates[0] && cent.geometry.coordinates[1] === geom.coordinates[1]) {
  18. onSurface = true;
  19. }
  20. } else if (geom.type === "MultiPoint") {
  21. let onMultiPoint = false;
  22. let k = 0;
  23. while (!onMultiPoint && k < geom.coordinates.length) {
  24. if (cent.geometry.coordinates[0] === geom.coordinates[k][0] && cent.geometry.coordinates[1] === geom.coordinates[k][1]) {
  25. onSurface = true;
  26. onMultiPoint = true;
  27. }
  28. k++;
  29. }
  30. } else if (geom.type === "LineString") {
  31. let k = 0;
  32. while (!onLine && k < geom.coordinates.length - 1) {
  33. x = cent.geometry.coordinates[0];
  34. y = cent.geometry.coordinates[1];
  35. x1 = geom.coordinates[k][0];
  36. y1 = geom.coordinates[k][1];
  37. x2 = geom.coordinates[k + 1][0];
  38. y2 = geom.coordinates[k + 1][1];
  39. if (pointOnSegment(x, y, x1, y1, x2, y2)) {
  40. onLine = true;
  41. onSurface = true;
  42. }
  43. k++;
  44. }
  45. } else if (geom.type === "MultiLineString") {
  46. let j = 0;
  47. while (j < geom.coordinates.length) {
  48. onLine = false;
  49. let k = 0;
  50. const line = geom.coordinates[j];
  51. while (!onLine && k < line.length - 1) {
  52. x = cent.geometry.coordinates[0];
  53. y = cent.geometry.coordinates[1];
  54. x1 = line[k][0];
  55. y1 = line[k][1];
  56. x2 = line[k + 1][0];
  57. y2 = line[k + 1][1];
  58. if (pointOnSegment(x, y, x1, y1, x2, y2)) {
  59. onLine = true;
  60. onSurface = true;
  61. }
  62. k++;
  63. }
  64. j++;
  65. }
  66. } else if (geom.type === "Polygon" || geom.type === "MultiPolygon") {
  67. if (booleanPointInPolygon(cent, geom)) {
  68. onSurface = true;
  69. }
  70. }
  71. i++;
  72. }
  73. if (onSurface) {
  74. return cent;
  75. } else {
  76. const vertices = featureCollection([]);
  77. for (let f = 0; f < fc.features.length; f++) {
  78. vertices.features = vertices.features.concat(
  79. explode(fc.features[f]).features
  80. );
  81. }
  82. return point(nearestPoint(cent, vertices).geometry.coordinates);
  83. }
  84. }
  85. function normalize(geojson) {
  86. if (geojson.type !== "FeatureCollection") {
  87. if (geojson.type !== "Feature") {
  88. return featureCollection([feature(geojson)]);
  89. }
  90. return featureCollection([geojson]);
  91. }
  92. return geojson;
  93. }
  94. function pointOnSegment(x, y, x1, y1, x2, y2) {
  95. const ab = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
  96. const ap = Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
  97. const pb = Math.sqrt((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y));
  98. return ab === ap + pb;
  99. }
  100. var turf_point_on_feature_default = pointOnFeature;
  101. export {
  102. turf_point_on_feature_default as default,
  103. pointOnFeature
  104. };
  105. //# sourceMappingURL=index.js.map