index.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. // index.js
  2. import { feature, point, lineString, isObject } from "@turf/helpers";
  3. function coordEach(geojson, callback, excludeWrapCoord) {
  4. if (geojson === null) return;
  5. var j, k, l, geometry, stopG, coords, geometryMaybeCollection, wrapShrink = 0, coordIndex = 0, isGeometryCollection, type = geojson.type, isFeatureCollection = type === "FeatureCollection", isFeature = type === "Feature", stop = isFeatureCollection ? geojson.features.length : 1;
  6. for (var featureIndex = 0; featureIndex < stop; featureIndex++) {
  7. geometryMaybeCollection = isFeatureCollection ? geojson.features[featureIndex].geometry : isFeature ? geojson.geometry : geojson;
  8. isGeometryCollection = geometryMaybeCollection ? geometryMaybeCollection.type === "GeometryCollection" : false;
  9. stopG = isGeometryCollection ? geometryMaybeCollection.geometries.length : 1;
  10. for (var geomIndex = 0; geomIndex < stopG; geomIndex++) {
  11. var multiFeatureIndex = 0;
  12. var geometryIndex = 0;
  13. geometry = isGeometryCollection ? geometryMaybeCollection.geometries[geomIndex] : geometryMaybeCollection;
  14. if (geometry === null) continue;
  15. coords = geometry.coordinates;
  16. var geomType = geometry.type;
  17. wrapShrink = excludeWrapCoord && (geomType === "Polygon" || geomType === "MultiPolygon") ? 1 : 0;
  18. switch (geomType) {
  19. case null:
  20. break;
  21. case "Point":
  22. if (callback(
  23. coords,
  24. coordIndex,
  25. featureIndex,
  26. multiFeatureIndex,
  27. geometryIndex
  28. ) === false)
  29. return false;
  30. coordIndex++;
  31. multiFeatureIndex++;
  32. break;
  33. case "LineString":
  34. case "MultiPoint":
  35. for (j = 0; j < coords.length; j++) {
  36. if (callback(
  37. coords[j],
  38. coordIndex,
  39. featureIndex,
  40. multiFeatureIndex,
  41. geometryIndex
  42. ) === false)
  43. return false;
  44. coordIndex++;
  45. if (geomType === "MultiPoint") multiFeatureIndex++;
  46. }
  47. if (geomType === "LineString") multiFeatureIndex++;
  48. break;
  49. case "Polygon":
  50. case "MultiLineString":
  51. for (j = 0; j < coords.length; j++) {
  52. for (k = 0; k < coords[j].length - wrapShrink; k++) {
  53. if (callback(
  54. coords[j][k],
  55. coordIndex,
  56. featureIndex,
  57. multiFeatureIndex,
  58. geometryIndex
  59. ) === false)
  60. return false;
  61. coordIndex++;
  62. }
  63. if (geomType === "MultiLineString") multiFeatureIndex++;
  64. if (geomType === "Polygon") geometryIndex++;
  65. }
  66. if (geomType === "Polygon") multiFeatureIndex++;
  67. break;
  68. case "MultiPolygon":
  69. for (j = 0; j < coords.length; j++) {
  70. geometryIndex = 0;
  71. for (k = 0; k < coords[j].length; k++) {
  72. for (l = 0; l < coords[j][k].length - wrapShrink; l++) {
  73. if (callback(
  74. coords[j][k][l],
  75. coordIndex,
  76. featureIndex,
  77. multiFeatureIndex,
  78. geometryIndex
  79. ) === false)
  80. return false;
  81. coordIndex++;
  82. }
  83. geometryIndex++;
  84. }
  85. multiFeatureIndex++;
  86. }
  87. break;
  88. case "GeometryCollection":
  89. for (j = 0; j < geometry.geometries.length; j++)
  90. if (coordEach(geometry.geometries[j], callback, excludeWrapCoord) === false)
  91. return false;
  92. break;
  93. default:
  94. throw new Error("Unknown Geometry Type");
  95. }
  96. }
  97. }
  98. }
  99. function coordReduce(geojson, callback, initialValue, excludeWrapCoord) {
  100. var previousValue = initialValue;
  101. coordEach(
  102. geojson,
  103. function(currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) {
  104. if (coordIndex === 0 && initialValue === void 0)
  105. previousValue = currentCoord;
  106. else
  107. previousValue = callback(
  108. previousValue,
  109. currentCoord,
  110. coordIndex,
  111. featureIndex,
  112. multiFeatureIndex,
  113. geometryIndex
  114. );
  115. },
  116. excludeWrapCoord
  117. );
  118. return previousValue;
  119. }
  120. function propEach(geojson, callback) {
  121. var i;
  122. switch (geojson.type) {
  123. case "FeatureCollection":
  124. for (i = 0; i < geojson.features.length; i++) {
  125. if (callback(geojson.features[i].properties, i) === false) break;
  126. }
  127. break;
  128. case "Feature":
  129. callback(geojson.properties, 0);
  130. break;
  131. }
  132. }
  133. function propReduce(geojson, callback, initialValue) {
  134. var previousValue = initialValue;
  135. propEach(geojson, function(currentProperties, featureIndex) {
  136. if (featureIndex === 0 && initialValue === void 0)
  137. previousValue = currentProperties;
  138. else
  139. previousValue = callback(previousValue, currentProperties, featureIndex);
  140. });
  141. return previousValue;
  142. }
  143. function featureEach(geojson, callback) {
  144. if (geojson.type === "Feature") {
  145. callback(geojson, 0);
  146. } else if (geojson.type === "FeatureCollection") {
  147. for (var i = 0; i < geojson.features.length; i++) {
  148. if (callback(geojson.features[i], i) === false) break;
  149. }
  150. }
  151. }
  152. function featureReduce(geojson, callback, initialValue) {
  153. var previousValue = initialValue;
  154. featureEach(geojson, function(currentFeature, featureIndex) {
  155. if (featureIndex === 0 && initialValue === void 0)
  156. previousValue = currentFeature;
  157. else previousValue = callback(previousValue, currentFeature, featureIndex);
  158. });
  159. return previousValue;
  160. }
  161. function coordAll(geojson) {
  162. var coords = [];
  163. coordEach(geojson, function(coord) {
  164. coords.push(coord);
  165. });
  166. return coords;
  167. }
  168. function geomEach(geojson, callback) {
  169. var i, j, g, geometry, stopG, geometryMaybeCollection, isGeometryCollection, featureProperties, featureBBox, featureId, featureIndex = 0, isFeatureCollection = geojson.type === "FeatureCollection", isFeature = geojson.type === "Feature", stop = isFeatureCollection ? geojson.features.length : 1;
  170. for (i = 0; i < stop; i++) {
  171. geometryMaybeCollection = isFeatureCollection ? geojson.features[i].geometry : isFeature ? geojson.geometry : geojson;
  172. featureProperties = isFeatureCollection ? geojson.features[i].properties : isFeature ? geojson.properties : {};
  173. featureBBox = isFeatureCollection ? geojson.features[i].bbox : isFeature ? geojson.bbox : void 0;
  174. featureId = isFeatureCollection ? geojson.features[i].id : isFeature ? geojson.id : void 0;
  175. isGeometryCollection = geometryMaybeCollection ? geometryMaybeCollection.type === "GeometryCollection" : false;
  176. stopG = isGeometryCollection ? geometryMaybeCollection.geometries.length : 1;
  177. for (g = 0; g < stopG; g++) {
  178. geometry = isGeometryCollection ? geometryMaybeCollection.geometries[g] : geometryMaybeCollection;
  179. if (geometry === null) {
  180. if (callback(
  181. null,
  182. featureIndex,
  183. featureProperties,
  184. featureBBox,
  185. featureId
  186. ) === false)
  187. return false;
  188. continue;
  189. }
  190. switch (geometry.type) {
  191. case "Point":
  192. case "LineString":
  193. case "MultiPoint":
  194. case "Polygon":
  195. case "MultiLineString":
  196. case "MultiPolygon": {
  197. if (callback(
  198. geometry,
  199. featureIndex,
  200. featureProperties,
  201. featureBBox,
  202. featureId
  203. ) === false)
  204. return false;
  205. break;
  206. }
  207. case "GeometryCollection": {
  208. for (j = 0; j < geometry.geometries.length; j++) {
  209. if (callback(
  210. geometry.geometries[j],
  211. featureIndex,
  212. featureProperties,
  213. featureBBox,
  214. featureId
  215. ) === false)
  216. return false;
  217. }
  218. break;
  219. }
  220. default:
  221. throw new Error("Unknown Geometry Type");
  222. }
  223. }
  224. featureIndex++;
  225. }
  226. }
  227. function geomReduce(geojson, callback, initialValue) {
  228. var previousValue = initialValue;
  229. geomEach(
  230. geojson,
  231. function(currentGeometry, featureIndex, featureProperties, featureBBox, featureId) {
  232. if (featureIndex === 0 && initialValue === void 0)
  233. previousValue = currentGeometry;
  234. else
  235. previousValue = callback(
  236. previousValue,
  237. currentGeometry,
  238. featureIndex,
  239. featureProperties,
  240. featureBBox,
  241. featureId
  242. );
  243. }
  244. );
  245. return previousValue;
  246. }
  247. function flattenEach(geojson, callback) {
  248. geomEach(geojson, function(geometry, featureIndex, properties, bbox, id) {
  249. var type = geometry === null ? null : geometry.type;
  250. switch (type) {
  251. case null:
  252. case "Point":
  253. case "LineString":
  254. case "Polygon":
  255. if (callback(
  256. feature(geometry, properties, { bbox, id }),
  257. featureIndex,
  258. 0
  259. ) === false)
  260. return false;
  261. return;
  262. }
  263. var geomType;
  264. switch (type) {
  265. case "MultiPoint":
  266. geomType = "Point";
  267. break;
  268. case "MultiLineString":
  269. geomType = "LineString";
  270. break;
  271. case "MultiPolygon":
  272. geomType = "Polygon";
  273. break;
  274. }
  275. for (var multiFeatureIndex = 0; multiFeatureIndex < geometry.coordinates.length; multiFeatureIndex++) {
  276. var coordinate = geometry.coordinates[multiFeatureIndex];
  277. var geom = {
  278. type: geomType,
  279. coordinates: coordinate
  280. };
  281. if (callback(feature(geom, properties), featureIndex, multiFeatureIndex) === false)
  282. return false;
  283. }
  284. });
  285. }
  286. function flattenReduce(geojson, callback, initialValue) {
  287. var previousValue = initialValue;
  288. flattenEach(
  289. geojson,
  290. function(currentFeature, featureIndex, multiFeatureIndex) {
  291. if (featureIndex === 0 && multiFeatureIndex === 0 && initialValue === void 0)
  292. previousValue = currentFeature;
  293. else
  294. previousValue = callback(
  295. previousValue,
  296. currentFeature,
  297. featureIndex,
  298. multiFeatureIndex
  299. );
  300. }
  301. );
  302. return previousValue;
  303. }
  304. function segmentEach(geojson, callback) {
  305. flattenEach(geojson, function(feature2, featureIndex, multiFeatureIndex) {
  306. var segmentIndex = 0;
  307. if (!feature2.geometry) return;
  308. var type = feature2.geometry.type;
  309. if (type === "Point" || type === "MultiPoint") return;
  310. var previousCoords;
  311. var previousFeatureIndex = 0;
  312. var previousMultiIndex = 0;
  313. var prevGeomIndex = 0;
  314. if (coordEach(
  315. feature2,
  316. function(currentCoord, coordIndex, featureIndexCoord, multiPartIndexCoord, geometryIndex) {
  317. if (previousCoords === void 0 || featureIndex > previousFeatureIndex || multiPartIndexCoord > previousMultiIndex || geometryIndex > prevGeomIndex) {
  318. previousCoords = currentCoord;
  319. previousFeatureIndex = featureIndex;
  320. previousMultiIndex = multiPartIndexCoord;
  321. prevGeomIndex = geometryIndex;
  322. segmentIndex = 0;
  323. return;
  324. }
  325. var currentSegment = lineString(
  326. [previousCoords, currentCoord],
  327. feature2.properties
  328. );
  329. if (callback(
  330. currentSegment,
  331. featureIndex,
  332. multiFeatureIndex,
  333. geometryIndex,
  334. segmentIndex
  335. ) === false)
  336. return false;
  337. segmentIndex++;
  338. previousCoords = currentCoord;
  339. }
  340. ) === false)
  341. return false;
  342. });
  343. }
  344. function segmentReduce(geojson, callback, initialValue) {
  345. var previousValue = initialValue;
  346. var started = false;
  347. segmentEach(
  348. geojson,
  349. function(currentSegment, featureIndex, multiFeatureIndex, geometryIndex, segmentIndex) {
  350. if (started === false && initialValue === void 0)
  351. previousValue = currentSegment;
  352. else
  353. previousValue = callback(
  354. previousValue,
  355. currentSegment,
  356. featureIndex,
  357. multiFeatureIndex,
  358. geometryIndex,
  359. segmentIndex
  360. );
  361. started = true;
  362. }
  363. );
  364. return previousValue;
  365. }
  366. function lineEach(geojson, callback) {
  367. if (!geojson) throw new Error("geojson is required");
  368. flattenEach(geojson, function(feature2, featureIndex, multiFeatureIndex) {
  369. if (feature2.geometry === null) return;
  370. var type = feature2.geometry.type;
  371. var coords = feature2.geometry.coordinates;
  372. switch (type) {
  373. case "LineString":
  374. if (callback(feature2, featureIndex, multiFeatureIndex, 0, 0) === false)
  375. return false;
  376. break;
  377. case "Polygon":
  378. for (var geometryIndex = 0; geometryIndex < coords.length; geometryIndex++) {
  379. if (callback(
  380. lineString(coords[geometryIndex], feature2.properties),
  381. featureIndex,
  382. multiFeatureIndex,
  383. geometryIndex
  384. ) === false)
  385. return false;
  386. }
  387. break;
  388. }
  389. });
  390. }
  391. function lineReduce(geojson, callback, initialValue) {
  392. var previousValue = initialValue;
  393. lineEach(
  394. geojson,
  395. function(currentLine, featureIndex, multiFeatureIndex, geometryIndex) {
  396. if (featureIndex === 0 && initialValue === void 0)
  397. previousValue = currentLine;
  398. else
  399. previousValue = callback(
  400. previousValue,
  401. currentLine,
  402. featureIndex,
  403. multiFeatureIndex,
  404. geometryIndex
  405. );
  406. }
  407. );
  408. return previousValue;
  409. }
  410. function findSegment(geojson, options) {
  411. options = options || {};
  412. if (!isObject(options)) throw new Error("options is invalid");
  413. var featureIndex = options.featureIndex || 0;
  414. var multiFeatureIndex = options.multiFeatureIndex || 0;
  415. var geometryIndex = options.geometryIndex || 0;
  416. var segmentIndex = options.segmentIndex || 0;
  417. var properties = options.properties;
  418. var geometry;
  419. switch (geojson.type) {
  420. case "FeatureCollection":
  421. if (featureIndex < 0)
  422. featureIndex = geojson.features.length + featureIndex;
  423. properties = properties || geojson.features[featureIndex].properties;
  424. geometry = geojson.features[featureIndex].geometry;
  425. break;
  426. case "Feature":
  427. properties = properties || geojson.properties;
  428. geometry = geojson.geometry;
  429. break;
  430. case "Point":
  431. case "MultiPoint":
  432. return null;
  433. case "LineString":
  434. case "Polygon":
  435. case "MultiLineString":
  436. case "MultiPolygon":
  437. geometry = geojson;
  438. break;
  439. default:
  440. throw new Error("geojson is invalid");
  441. }
  442. if (geometry === null) return null;
  443. var coords = geometry.coordinates;
  444. switch (geometry.type) {
  445. case "Point":
  446. case "MultiPoint":
  447. return null;
  448. case "LineString":
  449. if (segmentIndex < 0) segmentIndex = coords.length + segmentIndex - 1;
  450. return lineString(
  451. [coords[segmentIndex], coords[segmentIndex + 1]],
  452. properties,
  453. options
  454. );
  455. case "Polygon":
  456. if (geometryIndex < 0) geometryIndex = coords.length + geometryIndex;
  457. if (segmentIndex < 0)
  458. segmentIndex = coords[geometryIndex].length + segmentIndex - 1;
  459. return lineString(
  460. [
  461. coords[geometryIndex][segmentIndex],
  462. coords[geometryIndex][segmentIndex + 1]
  463. ],
  464. properties,
  465. options
  466. );
  467. case "MultiLineString":
  468. if (multiFeatureIndex < 0)
  469. multiFeatureIndex = coords.length + multiFeatureIndex;
  470. if (segmentIndex < 0)
  471. segmentIndex = coords[multiFeatureIndex].length + segmentIndex - 1;
  472. return lineString(
  473. [
  474. coords[multiFeatureIndex][segmentIndex],
  475. coords[multiFeatureIndex][segmentIndex + 1]
  476. ],
  477. properties,
  478. options
  479. );
  480. case "MultiPolygon":
  481. if (multiFeatureIndex < 0)
  482. multiFeatureIndex = coords.length + multiFeatureIndex;
  483. if (geometryIndex < 0)
  484. geometryIndex = coords[multiFeatureIndex].length + geometryIndex;
  485. if (segmentIndex < 0)
  486. segmentIndex = coords[multiFeatureIndex][geometryIndex].length - segmentIndex - 1;
  487. return lineString(
  488. [
  489. coords[multiFeatureIndex][geometryIndex][segmentIndex],
  490. coords[multiFeatureIndex][geometryIndex][segmentIndex + 1]
  491. ],
  492. properties,
  493. options
  494. );
  495. }
  496. throw new Error("geojson is invalid");
  497. }
  498. function findPoint(geojson, options) {
  499. options = options || {};
  500. if (!isObject(options)) throw new Error("options is invalid");
  501. var featureIndex = options.featureIndex || 0;
  502. var multiFeatureIndex = options.multiFeatureIndex || 0;
  503. var geometryIndex = options.geometryIndex || 0;
  504. var coordIndex = options.coordIndex || 0;
  505. var properties = options.properties;
  506. var geometry;
  507. switch (geojson.type) {
  508. case "FeatureCollection":
  509. if (featureIndex < 0)
  510. featureIndex = geojson.features.length + featureIndex;
  511. properties = properties || geojson.features[featureIndex].properties;
  512. geometry = geojson.features[featureIndex].geometry;
  513. break;
  514. case "Feature":
  515. properties = properties || geojson.properties;
  516. geometry = geojson.geometry;
  517. break;
  518. case "Point":
  519. case "MultiPoint":
  520. return null;
  521. case "LineString":
  522. case "Polygon":
  523. case "MultiLineString":
  524. case "MultiPolygon":
  525. geometry = geojson;
  526. break;
  527. default:
  528. throw new Error("geojson is invalid");
  529. }
  530. if (geometry === null) return null;
  531. var coords = geometry.coordinates;
  532. switch (geometry.type) {
  533. case "Point":
  534. return point(coords, properties, options);
  535. case "MultiPoint":
  536. if (multiFeatureIndex < 0)
  537. multiFeatureIndex = coords.length + multiFeatureIndex;
  538. return point(coords[multiFeatureIndex], properties, options);
  539. case "LineString":
  540. if (coordIndex < 0) coordIndex = coords.length + coordIndex;
  541. return point(coords[coordIndex], properties, options);
  542. case "Polygon":
  543. if (geometryIndex < 0) geometryIndex = coords.length + geometryIndex;
  544. if (coordIndex < 0)
  545. coordIndex = coords[geometryIndex].length + coordIndex;
  546. return point(coords[geometryIndex][coordIndex], properties, options);
  547. case "MultiLineString":
  548. if (multiFeatureIndex < 0)
  549. multiFeatureIndex = coords.length + multiFeatureIndex;
  550. if (coordIndex < 0)
  551. coordIndex = coords[multiFeatureIndex].length + coordIndex;
  552. return point(coords[multiFeatureIndex][coordIndex], properties, options);
  553. case "MultiPolygon":
  554. if (multiFeatureIndex < 0)
  555. multiFeatureIndex = coords.length + multiFeatureIndex;
  556. if (geometryIndex < 0)
  557. geometryIndex = coords[multiFeatureIndex].length + geometryIndex;
  558. if (coordIndex < 0)
  559. coordIndex = coords[multiFeatureIndex][geometryIndex].length - coordIndex;
  560. return point(
  561. coords[multiFeatureIndex][geometryIndex][coordIndex],
  562. properties,
  563. options
  564. );
  565. }
  566. throw new Error("geojson is invalid");
  567. }
  568. export {
  569. coordAll,
  570. coordEach,
  571. coordReduce,
  572. featureEach,
  573. featureReduce,
  574. findPoint,
  575. findSegment,
  576. flattenEach,
  577. flattenReduce,
  578. geomEach,
  579. geomReduce,
  580. lineEach,
  581. lineReduce,
  582. propEach,
  583. propReduce,
  584. segmentEach,
  585. segmentReduce
  586. };
  587. //# sourceMappingURL=index.js.map