index.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var helpers = require('@turf/helpers');
  4. /**
  5. * Callback for coordEach
  6. *
  7. * @callback coordEachCallback
  8. * @param {Array<number>} currentCoord The current coordinate being processed.
  9. * @param {number} coordIndex The current index of the coordinate being processed.
  10. * @param {number} featureIndex The current index of the Feature being processed.
  11. * @param {number} multiFeatureIndex The current index of the Multi-Feature being processed.
  12. * @param {number} geometryIndex The current index of the Geometry being processed.
  13. */
  14. /**
  15. * Iterate over coordinates in any GeoJSON object, similar to Array.forEach()
  16. *
  17. * @name coordEach
  18. * @param {FeatureCollection|Feature|Geometry} geojson any GeoJSON object
  19. * @param {Function} callback a method that takes (currentCoord, coordIndex, featureIndex, multiFeatureIndex)
  20. * @param {boolean} [excludeWrapCoord=false] whether or not to include the final coordinate of LinearRings that wraps the ring in its iteration.
  21. * @returns {void}
  22. * @example
  23. * var features = turf.featureCollection([
  24. * turf.point([26, 37], {"foo": "bar"}),
  25. * turf.point([36, 53], {"hello": "world"})
  26. * ]);
  27. *
  28. * turf.coordEach(features, function (currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) {
  29. * //=currentCoord
  30. * //=coordIndex
  31. * //=featureIndex
  32. * //=multiFeatureIndex
  33. * //=geometryIndex
  34. * });
  35. */
  36. function coordEach(geojson, callback, excludeWrapCoord) {
  37. // Handles null Geometry -- Skips this GeoJSON
  38. if (geojson === null) return;
  39. var j,
  40. k,
  41. l,
  42. geometry,
  43. stopG,
  44. coords,
  45. geometryMaybeCollection,
  46. wrapShrink = 0,
  47. coordIndex = 0,
  48. isGeometryCollection,
  49. type = geojson.type,
  50. isFeatureCollection = type === "FeatureCollection",
  51. isFeature = type === "Feature",
  52. stop = isFeatureCollection ? geojson.features.length : 1;
  53. // This logic may look a little weird. The reason why it is that way
  54. // is because it's trying to be fast. GeoJSON supports multiple kinds
  55. // of objects at its root: FeatureCollection, Features, Geometries.
  56. // This function has the responsibility of handling all of them, and that
  57. // means that some of the `for` loops you see below actually just don't apply
  58. // to certain inputs. For instance, if you give this just a
  59. // Point geometry, then both loops are short-circuited and all we do
  60. // is gradually rename the input until it's called 'geometry'.
  61. //
  62. // This also aims to allocate as few resources as possible: just a
  63. // few numbers and booleans, rather than any temporary arrays as would
  64. // be required with the normalization approach.
  65. for (var featureIndex = 0; featureIndex < stop; featureIndex++) {
  66. geometryMaybeCollection = isFeatureCollection
  67. ? geojson.features[featureIndex].geometry
  68. : isFeature
  69. ? geojson.geometry
  70. : geojson;
  71. isGeometryCollection = geometryMaybeCollection
  72. ? geometryMaybeCollection.type === "GeometryCollection"
  73. : false;
  74. stopG = isGeometryCollection
  75. ? geometryMaybeCollection.geometries.length
  76. : 1;
  77. for (var geomIndex = 0; geomIndex < stopG; geomIndex++) {
  78. var multiFeatureIndex = 0;
  79. var geometryIndex = 0;
  80. geometry = isGeometryCollection
  81. ? geometryMaybeCollection.geometries[geomIndex]
  82. : geometryMaybeCollection;
  83. // Handles null Geometry -- Skips this geometry
  84. if (geometry === null) continue;
  85. coords = geometry.coordinates;
  86. var geomType = geometry.type;
  87. wrapShrink =
  88. excludeWrapCoord &&
  89. (geomType === "Polygon" || geomType === "MultiPolygon")
  90. ? 1
  91. : 0;
  92. switch (geomType) {
  93. case null:
  94. break;
  95. case "Point":
  96. if (
  97. callback(
  98. coords,
  99. coordIndex,
  100. featureIndex,
  101. multiFeatureIndex,
  102. geometryIndex
  103. ) === false
  104. )
  105. return false;
  106. coordIndex++;
  107. multiFeatureIndex++;
  108. break;
  109. case "LineString":
  110. case "MultiPoint":
  111. for (j = 0; j < coords.length; j++) {
  112. if (
  113. callback(
  114. coords[j],
  115. coordIndex,
  116. featureIndex,
  117. multiFeatureIndex,
  118. geometryIndex
  119. ) === false
  120. )
  121. return false;
  122. coordIndex++;
  123. if (geomType === "MultiPoint") multiFeatureIndex++;
  124. }
  125. if (geomType === "LineString") multiFeatureIndex++;
  126. break;
  127. case "Polygon":
  128. case "MultiLineString":
  129. for (j = 0; j < coords.length; j++) {
  130. for (k = 0; k < coords[j].length - wrapShrink; k++) {
  131. if (
  132. callback(
  133. coords[j][k],
  134. coordIndex,
  135. featureIndex,
  136. multiFeatureIndex,
  137. geometryIndex
  138. ) === false
  139. )
  140. return false;
  141. coordIndex++;
  142. }
  143. if (geomType === "MultiLineString") multiFeatureIndex++;
  144. if (geomType === "Polygon") geometryIndex++;
  145. }
  146. if (geomType === "Polygon") multiFeatureIndex++;
  147. break;
  148. case "MultiPolygon":
  149. for (j = 0; j < coords.length; j++) {
  150. geometryIndex = 0;
  151. for (k = 0; k < coords[j].length; k++) {
  152. for (l = 0; l < coords[j][k].length - wrapShrink; l++) {
  153. if (
  154. callback(
  155. coords[j][k][l],
  156. coordIndex,
  157. featureIndex,
  158. multiFeatureIndex,
  159. geometryIndex
  160. ) === false
  161. )
  162. return false;
  163. coordIndex++;
  164. }
  165. geometryIndex++;
  166. }
  167. multiFeatureIndex++;
  168. }
  169. break;
  170. case "GeometryCollection":
  171. for (j = 0; j < geometry.geometries.length; j++)
  172. if (
  173. coordEach(geometry.geometries[j], callback, excludeWrapCoord) ===
  174. false
  175. )
  176. return false;
  177. break;
  178. default:
  179. throw new Error("Unknown Geometry Type");
  180. }
  181. }
  182. }
  183. }
  184. /**
  185. * Callback for coordReduce
  186. *
  187. * The first time the callback function is called, the values provided as arguments depend
  188. * on whether the reduce method has an initialValue argument.
  189. *
  190. * If an initialValue is provided to the reduce method:
  191. * - The previousValue argument is initialValue.
  192. * - The currentValue argument is the value of the first element present in the array.
  193. *
  194. * If an initialValue is not provided:
  195. * - The previousValue argument is the value of the first element present in the array.
  196. * - The currentValue argument is the value of the second element present in the array.
  197. *
  198. * @callback coordReduceCallback
  199. * @param {*} previousValue The accumulated value previously returned in the last invocation
  200. * of the callback, or initialValue, if supplied.
  201. * @param {Array<number>} currentCoord The current coordinate being processed.
  202. * @param {number} coordIndex The current index of the coordinate being processed.
  203. * Starts at index 0, if an initialValue is provided, and at index 1 otherwise.
  204. * @param {number} featureIndex The current index of the Feature being processed.
  205. * @param {number} multiFeatureIndex The current index of the Multi-Feature being processed.
  206. * @param {number} geometryIndex The current index of the Geometry being processed.
  207. */
  208. /**
  209. * Reduce coordinates in any GeoJSON object, similar to Array.reduce()
  210. *
  211. * @name coordReduce
  212. * @param {FeatureCollection|Geometry|Feature} geojson any GeoJSON object
  213. * @param {Function} callback a method that takes (previousValue, currentCoord, coordIndex)
  214. * @param {*} [initialValue] Value to use as the first argument to the first call of the callback.
  215. * @param {boolean} [excludeWrapCoord=false] whether or not to include the final coordinate of LinearRings that wraps the ring in its iteration.
  216. * @returns {*} The value that results from the reduction.
  217. * @example
  218. * var features = turf.featureCollection([
  219. * turf.point([26, 37], {"foo": "bar"}),
  220. * turf.point([36, 53], {"hello": "world"})
  221. * ]);
  222. *
  223. * turf.coordReduce(features, function (previousValue, currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) {
  224. * //=previousValue
  225. * //=currentCoord
  226. * //=coordIndex
  227. * //=featureIndex
  228. * //=multiFeatureIndex
  229. * //=geometryIndex
  230. * return currentCoord;
  231. * });
  232. */
  233. function coordReduce(geojson, callback, initialValue, excludeWrapCoord) {
  234. var previousValue = initialValue;
  235. coordEach(
  236. geojson,
  237. function (
  238. currentCoord,
  239. coordIndex,
  240. featureIndex,
  241. multiFeatureIndex,
  242. geometryIndex
  243. ) {
  244. if (coordIndex === 0 && initialValue === undefined)
  245. previousValue = currentCoord;
  246. else
  247. previousValue = callback(
  248. previousValue,
  249. currentCoord,
  250. coordIndex,
  251. featureIndex,
  252. multiFeatureIndex,
  253. geometryIndex
  254. );
  255. },
  256. excludeWrapCoord
  257. );
  258. return previousValue;
  259. }
  260. /**
  261. * Callback for propEach
  262. *
  263. * @callback propEachCallback
  264. * @param {Object} currentProperties The current Properties being processed.
  265. * @param {number} featureIndex The current index of the Feature being processed.
  266. */
  267. /**
  268. * Iterate over properties in any GeoJSON object, similar to Array.forEach()
  269. *
  270. * @name propEach
  271. * @param {FeatureCollection|Feature} geojson any GeoJSON object
  272. * @param {Function} callback a method that takes (currentProperties, featureIndex)
  273. * @returns {void}
  274. * @example
  275. * var features = turf.featureCollection([
  276. * turf.point([26, 37], {foo: 'bar'}),
  277. * turf.point([36, 53], {hello: 'world'})
  278. * ]);
  279. *
  280. * turf.propEach(features, function (currentProperties, featureIndex) {
  281. * //=currentProperties
  282. * //=featureIndex
  283. * });
  284. */
  285. function propEach(geojson, callback) {
  286. var i;
  287. switch (geojson.type) {
  288. case "FeatureCollection":
  289. for (i = 0; i < geojson.features.length; i++) {
  290. if (callback(geojson.features[i].properties, i) === false) break;
  291. }
  292. break;
  293. case "Feature":
  294. callback(geojson.properties, 0);
  295. break;
  296. }
  297. }
  298. /**
  299. * Callback for propReduce
  300. *
  301. * The first time the callback function is called, the values provided as arguments depend
  302. * on whether the reduce method has an initialValue argument.
  303. *
  304. * If an initialValue is provided to the reduce method:
  305. * - The previousValue argument is initialValue.
  306. * - The currentValue argument is the value of the first element present in the array.
  307. *
  308. * If an initialValue is not provided:
  309. * - The previousValue argument is the value of the first element present in the array.
  310. * - The currentValue argument is the value of the second element present in the array.
  311. *
  312. * @callback propReduceCallback
  313. * @param {*} previousValue The accumulated value previously returned in the last invocation
  314. * of the callback, or initialValue, if supplied.
  315. * @param {*} currentProperties The current Properties being processed.
  316. * @param {number} featureIndex The current index of the Feature being processed.
  317. */
  318. /**
  319. * Reduce properties in any GeoJSON object into a single value,
  320. * similar to how Array.reduce works. However, in this case we lazily run
  321. * the reduction, so an array of all properties is unnecessary.
  322. *
  323. * @name propReduce
  324. * @param {FeatureCollection|Feature} geojson any GeoJSON object
  325. * @param {Function} callback a method that takes (previousValue, currentProperties, featureIndex)
  326. * @param {*} [initialValue] Value to use as the first argument to the first call of the callback.
  327. * @returns {*} The value that results from the reduction.
  328. * @example
  329. * var features = turf.featureCollection([
  330. * turf.point([26, 37], {foo: 'bar'}),
  331. * turf.point([36, 53], {hello: 'world'})
  332. * ]);
  333. *
  334. * turf.propReduce(features, function (previousValue, currentProperties, featureIndex) {
  335. * //=previousValue
  336. * //=currentProperties
  337. * //=featureIndex
  338. * return currentProperties
  339. * });
  340. */
  341. function propReduce(geojson, callback, initialValue) {
  342. var previousValue = initialValue;
  343. propEach(geojson, function (currentProperties, featureIndex) {
  344. if (featureIndex === 0 && initialValue === undefined)
  345. previousValue = currentProperties;
  346. else
  347. previousValue = callback(previousValue, currentProperties, featureIndex);
  348. });
  349. return previousValue;
  350. }
  351. /**
  352. * Callback for featureEach
  353. *
  354. * @callback featureEachCallback
  355. * @param {Feature<any>} currentFeature The current Feature being processed.
  356. * @param {number} featureIndex The current index of the Feature being processed.
  357. */
  358. /**
  359. * Iterate over features in any GeoJSON object, similar to
  360. * Array.forEach.
  361. *
  362. * @name featureEach
  363. * @param {FeatureCollection|Feature|Geometry} geojson any GeoJSON object
  364. * @param {Function} callback a method that takes (currentFeature, featureIndex)
  365. * @returns {void}
  366. * @example
  367. * var features = turf.featureCollection([
  368. * turf.point([26, 37], {foo: 'bar'}),
  369. * turf.point([36, 53], {hello: 'world'})
  370. * ]);
  371. *
  372. * turf.featureEach(features, function (currentFeature, featureIndex) {
  373. * //=currentFeature
  374. * //=featureIndex
  375. * });
  376. */
  377. function featureEach(geojson, callback) {
  378. if (geojson.type === "Feature") {
  379. callback(geojson, 0);
  380. } else if (geojson.type === "FeatureCollection") {
  381. for (var i = 0; i < geojson.features.length; i++) {
  382. if (callback(geojson.features[i], i) === false) break;
  383. }
  384. }
  385. }
  386. /**
  387. * Callback for featureReduce
  388. *
  389. * The first time the callback function is called, the values provided as arguments depend
  390. * on whether the reduce method has an initialValue argument.
  391. *
  392. * If an initialValue is provided to the reduce method:
  393. * - The previousValue argument is initialValue.
  394. * - The currentValue argument is the value of the first element present in the array.
  395. *
  396. * If an initialValue is not provided:
  397. * - The previousValue argument is the value of the first element present in the array.
  398. * - The currentValue argument is the value of the second element present in the array.
  399. *
  400. * @callback featureReduceCallback
  401. * @param {*} previousValue The accumulated value previously returned in the last invocation
  402. * of the callback, or initialValue, if supplied.
  403. * @param {Feature} currentFeature The current Feature being processed.
  404. * @param {number} featureIndex The current index of the Feature being processed.
  405. */
  406. /**
  407. * Reduce features in any GeoJSON object, similar to Array.reduce().
  408. *
  409. * @name featureReduce
  410. * @param {FeatureCollection|Feature|Geometry} geojson any GeoJSON object
  411. * @param {Function} callback a method that takes (previousValue, currentFeature, featureIndex)
  412. * @param {*} [initialValue] Value to use as the first argument to the first call of the callback.
  413. * @returns {*} The value that results from the reduction.
  414. * @example
  415. * var features = turf.featureCollection([
  416. * turf.point([26, 37], {"foo": "bar"}),
  417. * turf.point([36, 53], {"hello": "world"})
  418. * ]);
  419. *
  420. * turf.featureReduce(features, function (previousValue, currentFeature, featureIndex) {
  421. * //=previousValue
  422. * //=currentFeature
  423. * //=featureIndex
  424. * return currentFeature
  425. * });
  426. */
  427. function featureReduce(geojson, callback, initialValue) {
  428. var previousValue = initialValue;
  429. featureEach(geojson, function (currentFeature, featureIndex) {
  430. if (featureIndex === 0 && initialValue === undefined)
  431. previousValue = currentFeature;
  432. else previousValue = callback(previousValue, currentFeature, featureIndex);
  433. });
  434. return previousValue;
  435. }
  436. /**
  437. * Get all coordinates from any GeoJSON object.
  438. *
  439. * @name coordAll
  440. * @param {FeatureCollection|Feature|Geometry} geojson any GeoJSON object
  441. * @returns {Array<Array<number>>} coordinate position array
  442. * @example
  443. * var features = turf.featureCollection([
  444. * turf.point([26, 37], {foo: 'bar'}),
  445. * turf.point([36, 53], {hello: 'world'})
  446. * ]);
  447. *
  448. * var coords = turf.coordAll(features);
  449. * //= [[26, 37], [36, 53]]
  450. */
  451. function coordAll(geojson) {
  452. var coords = [];
  453. coordEach(geojson, function (coord) {
  454. coords.push(coord);
  455. });
  456. return coords;
  457. }
  458. /**
  459. * Callback for geomEach
  460. *
  461. * @callback geomEachCallback
  462. * @param {Geometry} currentGeometry The current Geometry being processed.
  463. * @param {number} featureIndex The current index of the Feature being processed.
  464. * @param {Object} featureProperties The current Feature Properties being processed.
  465. * @param {Array<number>} featureBBox The current Feature BBox being processed.
  466. * @param {number|string} featureId The current Feature Id being processed.
  467. */
  468. /**
  469. * Iterate over each geometry in any GeoJSON object, similar to Array.forEach()
  470. *
  471. * @name geomEach
  472. * @param {FeatureCollection|Feature|Geometry} geojson any GeoJSON object
  473. * @param {Function} callback a method that takes (currentGeometry, featureIndex, featureProperties, featureBBox, featureId)
  474. * @returns {void}
  475. * @example
  476. * var features = turf.featureCollection([
  477. * turf.point([26, 37], {foo: 'bar'}),
  478. * turf.point([36, 53], {hello: 'world'})
  479. * ]);
  480. *
  481. * turf.geomEach(features, function (currentGeometry, featureIndex, featureProperties, featureBBox, featureId) {
  482. * //=currentGeometry
  483. * //=featureIndex
  484. * //=featureProperties
  485. * //=featureBBox
  486. * //=featureId
  487. * });
  488. */
  489. function geomEach(geojson, callback) {
  490. var i,
  491. j,
  492. g,
  493. geometry,
  494. stopG,
  495. geometryMaybeCollection,
  496. isGeometryCollection,
  497. featureProperties,
  498. featureBBox,
  499. featureId,
  500. featureIndex = 0,
  501. isFeatureCollection = geojson.type === "FeatureCollection",
  502. isFeature = geojson.type === "Feature",
  503. stop = isFeatureCollection ? geojson.features.length : 1;
  504. // This logic may look a little weird. The reason why it is that way
  505. // is because it's trying to be fast. GeoJSON supports multiple kinds
  506. // of objects at its root: FeatureCollection, Features, Geometries.
  507. // This function has the responsibility of handling all of them, and that
  508. // means that some of the `for` loops you see below actually just don't apply
  509. // to certain inputs. For instance, if you give this just a
  510. // Point geometry, then both loops are short-circuited and all we do
  511. // is gradually rename the input until it's called 'geometry'.
  512. //
  513. // This also aims to allocate as few resources as possible: just a
  514. // few numbers and booleans, rather than any temporary arrays as would
  515. // be required with the normalization approach.
  516. for (i = 0; i < stop; i++) {
  517. geometryMaybeCollection = isFeatureCollection
  518. ? geojson.features[i].geometry
  519. : isFeature
  520. ? geojson.geometry
  521. : geojson;
  522. featureProperties = isFeatureCollection
  523. ? geojson.features[i].properties
  524. : isFeature
  525. ? geojson.properties
  526. : {};
  527. featureBBox = isFeatureCollection
  528. ? geojson.features[i].bbox
  529. : isFeature
  530. ? geojson.bbox
  531. : undefined;
  532. featureId = isFeatureCollection
  533. ? geojson.features[i].id
  534. : isFeature
  535. ? geojson.id
  536. : undefined;
  537. isGeometryCollection = geometryMaybeCollection
  538. ? geometryMaybeCollection.type === "GeometryCollection"
  539. : false;
  540. stopG = isGeometryCollection
  541. ? geometryMaybeCollection.geometries.length
  542. : 1;
  543. for (g = 0; g < stopG; g++) {
  544. geometry = isGeometryCollection
  545. ? geometryMaybeCollection.geometries[g]
  546. : geometryMaybeCollection;
  547. // Handle null Geometry
  548. if (geometry === null) {
  549. if (
  550. callback(
  551. null,
  552. featureIndex,
  553. featureProperties,
  554. featureBBox,
  555. featureId
  556. ) === false
  557. )
  558. return false;
  559. continue;
  560. }
  561. switch (geometry.type) {
  562. case "Point":
  563. case "LineString":
  564. case "MultiPoint":
  565. case "Polygon":
  566. case "MultiLineString":
  567. case "MultiPolygon": {
  568. if (
  569. callback(
  570. geometry,
  571. featureIndex,
  572. featureProperties,
  573. featureBBox,
  574. featureId
  575. ) === false
  576. )
  577. return false;
  578. break;
  579. }
  580. case "GeometryCollection": {
  581. for (j = 0; j < geometry.geometries.length; j++) {
  582. if (
  583. callback(
  584. geometry.geometries[j],
  585. featureIndex,
  586. featureProperties,
  587. featureBBox,
  588. featureId
  589. ) === false
  590. )
  591. return false;
  592. }
  593. break;
  594. }
  595. default:
  596. throw new Error("Unknown Geometry Type");
  597. }
  598. }
  599. // Only increase `featureIndex` per each feature
  600. featureIndex++;
  601. }
  602. }
  603. /**
  604. * Callback for geomReduce
  605. *
  606. * The first time the callback function is called, the values provided as arguments depend
  607. * on whether the reduce method has an initialValue argument.
  608. *
  609. * If an initialValue is provided to the reduce method:
  610. * - The previousValue argument is initialValue.
  611. * - The currentValue argument is the value of the first element present in the array.
  612. *
  613. * If an initialValue is not provided:
  614. * - The previousValue argument is the value of the first element present in the array.
  615. * - The currentValue argument is the value of the second element present in the array.
  616. *
  617. * @callback geomReduceCallback
  618. * @param {*} previousValue The accumulated value previously returned in the last invocation
  619. * of the callback, or initialValue, if supplied.
  620. * @param {Geometry} currentGeometry The current Geometry being processed.
  621. * @param {number} featureIndex The current index of the Feature being processed.
  622. * @param {Object} featureProperties The current Feature Properties being processed.
  623. * @param {Array<number>} featureBBox The current Feature BBox being processed.
  624. * @param {number|string} featureId The current Feature Id being processed.
  625. */
  626. /**
  627. * Reduce geometry in any GeoJSON object, similar to Array.reduce().
  628. *
  629. * @name geomReduce
  630. * @param {FeatureCollection|Feature|Geometry} geojson any GeoJSON object
  631. * @param {Function} callback a method that takes (previousValue, currentGeometry, featureIndex, featureProperties, featureBBox, featureId)
  632. * @param {*} [initialValue] Value to use as the first argument to the first call of the callback.
  633. * @returns {*} The value that results from the reduction.
  634. * @example
  635. * var features = turf.featureCollection([
  636. * turf.point([26, 37], {foo: 'bar'}),
  637. * turf.point([36, 53], {hello: 'world'})
  638. * ]);
  639. *
  640. * turf.geomReduce(features, function (previousValue, currentGeometry, featureIndex, featureProperties, featureBBox, featureId) {
  641. * //=previousValue
  642. * //=currentGeometry
  643. * //=featureIndex
  644. * //=featureProperties
  645. * //=featureBBox
  646. * //=featureId
  647. * return currentGeometry
  648. * });
  649. */
  650. function geomReduce(geojson, callback, initialValue) {
  651. var previousValue = initialValue;
  652. geomEach(
  653. geojson,
  654. function (
  655. currentGeometry,
  656. featureIndex,
  657. featureProperties,
  658. featureBBox,
  659. featureId
  660. ) {
  661. if (featureIndex === 0 && initialValue === undefined)
  662. previousValue = currentGeometry;
  663. else
  664. previousValue = callback(
  665. previousValue,
  666. currentGeometry,
  667. featureIndex,
  668. featureProperties,
  669. featureBBox,
  670. featureId
  671. );
  672. }
  673. );
  674. return previousValue;
  675. }
  676. /**
  677. * Callback for flattenEach
  678. *
  679. * @callback flattenEachCallback
  680. * @param {Feature} currentFeature The current flattened feature being processed.
  681. * @param {number} featureIndex The current index of the Feature being processed.
  682. * @param {number} multiFeatureIndex The current index of the Multi-Feature being processed.
  683. */
  684. /**
  685. * Iterate over flattened features in any GeoJSON object, similar to
  686. * Array.forEach.
  687. *
  688. * @name flattenEach
  689. * @param {FeatureCollection|Feature|Geometry} geojson any GeoJSON object
  690. * @param {Function} callback a method that takes (currentFeature, featureIndex, multiFeatureIndex)
  691. * @example
  692. * var features = turf.featureCollection([
  693. * turf.point([26, 37], {foo: 'bar'}),
  694. * turf.multiPoint([[40, 30], [36, 53]], {hello: 'world'})
  695. * ]);
  696. *
  697. * turf.flattenEach(features, function (currentFeature, featureIndex, multiFeatureIndex) {
  698. * //=currentFeature
  699. * //=featureIndex
  700. * //=multiFeatureIndex
  701. * });
  702. */
  703. function flattenEach(geojson, callback) {
  704. geomEach(geojson, function (geometry, featureIndex, properties, bbox, id) {
  705. // Callback for single geometry
  706. var type = geometry === null ? null : geometry.type;
  707. switch (type) {
  708. case null:
  709. case "Point":
  710. case "LineString":
  711. case "Polygon":
  712. if (
  713. callback(
  714. helpers.feature(geometry, properties, { bbox: bbox, id: id }),
  715. featureIndex,
  716. 0
  717. ) === false
  718. )
  719. return false;
  720. return;
  721. }
  722. var geomType;
  723. // Callback for multi-geometry
  724. switch (type) {
  725. case "MultiPoint":
  726. geomType = "Point";
  727. break;
  728. case "MultiLineString":
  729. geomType = "LineString";
  730. break;
  731. case "MultiPolygon":
  732. geomType = "Polygon";
  733. break;
  734. }
  735. for (
  736. var multiFeatureIndex = 0;
  737. multiFeatureIndex < geometry.coordinates.length;
  738. multiFeatureIndex++
  739. ) {
  740. var coordinate = geometry.coordinates[multiFeatureIndex];
  741. var geom = {
  742. type: geomType,
  743. coordinates: coordinate,
  744. };
  745. if (
  746. callback(helpers.feature(geom, properties), featureIndex, multiFeatureIndex) ===
  747. false
  748. )
  749. return false;
  750. }
  751. });
  752. }
  753. /**
  754. * Callback for flattenReduce
  755. *
  756. * The first time the callback function is called, the values provided as arguments depend
  757. * on whether the reduce method has an initialValue argument.
  758. *
  759. * If an initialValue is provided to the reduce method:
  760. * - The previousValue argument is initialValue.
  761. * - The currentValue argument is the value of the first element present in the array.
  762. *
  763. * If an initialValue is not provided:
  764. * - The previousValue argument is the value of the first element present in the array.
  765. * - The currentValue argument is the value of the second element present in the array.
  766. *
  767. * @callback flattenReduceCallback
  768. * @param {*} previousValue The accumulated value previously returned in the last invocation
  769. * of the callback, or initialValue, if supplied.
  770. * @param {Feature} currentFeature The current Feature being processed.
  771. * @param {number} featureIndex The current index of the Feature being processed.
  772. * @param {number} multiFeatureIndex The current index of the Multi-Feature being processed.
  773. */
  774. /**
  775. * Reduce flattened features in any GeoJSON object, similar to Array.reduce().
  776. *
  777. * @name flattenReduce
  778. * @param {FeatureCollection|Feature|Geometry} geojson any GeoJSON object
  779. * @param {Function} callback a method that takes (previousValue, currentFeature, featureIndex, multiFeatureIndex)
  780. * @param {*} [initialValue] Value to use as the first argument to the first call of the callback.
  781. * @returns {*} The value that results from the reduction.
  782. * @example
  783. * var features = turf.featureCollection([
  784. * turf.point([26, 37], {foo: 'bar'}),
  785. * turf.multiPoint([[40, 30], [36, 53]], {hello: 'world'})
  786. * ]);
  787. *
  788. * turf.flattenReduce(features, function (previousValue, currentFeature, featureIndex, multiFeatureIndex) {
  789. * //=previousValue
  790. * //=currentFeature
  791. * //=featureIndex
  792. * //=multiFeatureIndex
  793. * return currentFeature
  794. * });
  795. */
  796. function flattenReduce(geojson, callback, initialValue) {
  797. var previousValue = initialValue;
  798. flattenEach(
  799. geojson,
  800. function (currentFeature, featureIndex, multiFeatureIndex) {
  801. if (
  802. featureIndex === 0 &&
  803. multiFeatureIndex === 0 &&
  804. initialValue === undefined
  805. )
  806. previousValue = currentFeature;
  807. else
  808. previousValue = callback(
  809. previousValue,
  810. currentFeature,
  811. featureIndex,
  812. multiFeatureIndex
  813. );
  814. }
  815. );
  816. return previousValue;
  817. }
  818. /**
  819. * Callback for segmentEach
  820. *
  821. * @callback segmentEachCallback
  822. * @param {Feature<LineString>} currentSegment The current Segment being processed.
  823. * @param {number} featureIndex The current index of the Feature being processed.
  824. * @param {number} multiFeatureIndex The current index of the Multi-Feature being processed.
  825. * @param {number} geometryIndex The current index of the Geometry being processed.
  826. * @param {number} segmentIndex The current index of the Segment being processed.
  827. * @returns {void}
  828. */
  829. /**
  830. * Iterate over 2-vertex line segment in any GeoJSON object, similar to Array.forEach()
  831. * (Multi)Point geometries do not contain segments therefore they are ignored during this operation.
  832. *
  833. * @param {FeatureCollection|Feature|Geometry} geojson any GeoJSON
  834. * @param {Function} callback a method that takes (currentSegment, featureIndex, multiFeatureIndex, geometryIndex, segmentIndex)
  835. * @returns {void}
  836. * @example
  837. * var polygon = turf.polygon([[[-50, 5], [-40, -10], [-50, -10], [-40, 5], [-50, 5]]]);
  838. *
  839. * // Iterate over GeoJSON by 2-vertex segments
  840. * turf.segmentEach(polygon, function (currentSegment, featureIndex, multiFeatureIndex, geometryIndex, segmentIndex) {
  841. * //=currentSegment
  842. * //=featureIndex
  843. * //=multiFeatureIndex
  844. * //=geometryIndex
  845. * //=segmentIndex
  846. * });
  847. *
  848. * // Calculate the total number of segments
  849. * var total = 0;
  850. * turf.segmentEach(polygon, function () {
  851. * total++;
  852. * });
  853. */
  854. function segmentEach(geojson, callback) {
  855. flattenEach(geojson, function (feature, featureIndex, multiFeatureIndex) {
  856. var segmentIndex = 0;
  857. // Exclude null Geometries
  858. if (!feature.geometry) return;
  859. // (Multi)Point geometries do not contain segments therefore they are ignored during this operation.
  860. var type = feature.geometry.type;
  861. if (type === "Point" || type === "MultiPoint") return;
  862. // Generate 2-vertex line segments
  863. var previousCoords;
  864. var previousFeatureIndex = 0;
  865. var previousMultiIndex = 0;
  866. var prevGeomIndex = 0;
  867. if (
  868. coordEach(
  869. feature,
  870. function (
  871. currentCoord,
  872. coordIndex,
  873. featureIndexCoord,
  874. multiPartIndexCoord,
  875. geometryIndex
  876. ) {
  877. // Simulating a meta.coordReduce() since `reduce` operations cannot be stopped by returning `false`
  878. if (
  879. previousCoords === undefined ||
  880. featureIndex > previousFeatureIndex ||
  881. multiPartIndexCoord > previousMultiIndex ||
  882. geometryIndex > prevGeomIndex
  883. ) {
  884. previousCoords = currentCoord;
  885. previousFeatureIndex = featureIndex;
  886. previousMultiIndex = multiPartIndexCoord;
  887. prevGeomIndex = geometryIndex;
  888. segmentIndex = 0;
  889. return;
  890. }
  891. var currentSegment = helpers.lineString(
  892. [previousCoords, currentCoord],
  893. feature.properties
  894. );
  895. if (
  896. callback(
  897. currentSegment,
  898. featureIndex,
  899. multiFeatureIndex,
  900. geometryIndex,
  901. segmentIndex
  902. ) === false
  903. )
  904. return false;
  905. segmentIndex++;
  906. previousCoords = currentCoord;
  907. }
  908. ) === false
  909. )
  910. return false;
  911. });
  912. }
  913. /**
  914. * Callback for segmentReduce
  915. *
  916. * The first time the callback function is called, the values provided as arguments depend
  917. * on whether the reduce method has an initialValue argument.
  918. *
  919. * If an initialValue is provided to the reduce method:
  920. * - The previousValue argument is initialValue.
  921. * - The currentValue argument is the value of the first element present in the array.
  922. *
  923. * If an initialValue is not provided:
  924. * - The previousValue argument is the value of the first element present in the array.
  925. * - The currentValue argument is the value of the second element present in the array.
  926. *
  927. * @callback segmentReduceCallback
  928. * @param {*} previousValue The accumulated value previously returned in the last invocation
  929. * of the callback, or initialValue, if supplied.
  930. * @param {Feature<LineString>} currentSegment The current Segment being processed.
  931. * @param {number} featureIndex The current index of the Feature being processed.
  932. * @param {number} multiFeatureIndex The current index of the Multi-Feature being processed.
  933. * @param {number} geometryIndex The current index of the Geometry being processed.
  934. * @param {number} segmentIndex The current index of the Segment being processed.
  935. */
  936. /**
  937. * Reduce 2-vertex line segment in any GeoJSON object, similar to Array.reduce()
  938. * (Multi)Point geometries do not contain segments therefore they are ignored during this operation.
  939. *
  940. * @param {FeatureCollection|Feature|Geometry} geojson any GeoJSON
  941. * @param {Function} callback a method that takes (previousValue, currentSegment, currentIndex)
  942. * @param {*} [initialValue] Value to use as the first argument to the first call of the callback.
  943. * @returns {void}
  944. * @example
  945. * var polygon = turf.polygon([[[-50, 5], [-40, -10], [-50, -10], [-40, 5], [-50, 5]]]);
  946. *
  947. * // Iterate over GeoJSON by 2-vertex segments
  948. * turf.segmentReduce(polygon, function (previousSegment, currentSegment, featureIndex, multiFeatureIndex, geometryIndex, segmentIndex) {
  949. * //= previousSegment
  950. * //= currentSegment
  951. * //= featureIndex
  952. * //= multiFeatureIndex
  953. * //= geometryIndex
  954. * //= segmentIndex
  955. * return currentSegment
  956. * });
  957. *
  958. * // Calculate the total number of segments
  959. * var initialValue = 0
  960. * var total = turf.segmentReduce(polygon, function (previousValue) {
  961. * previousValue++;
  962. * return previousValue;
  963. * }, initialValue);
  964. */
  965. function segmentReduce(geojson, callback, initialValue) {
  966. var previousValue = initialValue;
  967. var started = false;
  968. segmentEach(
  969. geojson,
  970. function (
  971. currentSegment,
  972. featureIndex,
  973. multiFeatureIndex,
  974. geometryIndex,
  975. segmentIndex
  976. ) {
  977. if (started === false && initialValue === undefined)
  978. previousValue = currentSegment;
  979. else
  980. previousValue = callback(
  981. previousValue,
  982. currentSegment,
  983. featureIndex,
  984. multiFeatureIndex,
  985. geometryIndex,
  986. segmentIndex
  987. );
  988. started = true;
  989. }
  990. );
  991. return previousValue;
  992. }
  993. /**
  994. * Callback for lineEach
  995. *
  996. * @callback lineEachCallback
  997. * @param {Feature<LineString>} currentLine The current LineString|LinearRing being processed
  998. * @param {number} featureIndex The current index of the Feature being processed
  999. * @param {number} multiFeatureIndex The current index of the Multi-Feature being processed
  1000. * @param {number} geometryIndex The current index of the Geometry being processed
  1001. */
  1002. /**
  1003. * Iterate over line or ring coordinates in LineString, Polygon, MultiLineString, MultiPolygon Features or Geometries,
  1004. * similar to Array.forEach.
  1005. *
  1006. * @name lineEach
  1007. * @param {Geometry|Feature<LineString|Polygon|MultiLineString|MultiPolygon>} geojson object
  1008. * @param {Function} callback a method that takes (currentLine, featureIndex, multiFeatureIndex, geometryIndex)
  1009. * @example
  1010. * var multiLine = turf.multiLineString([
  1011. * [[26, 37], [35, 45]],
  1012. * [[36, 53], [38, 50], [41, 55]]
  1013. * ]);
  1014. *
  1015. * turf.lineEach(multiLine, function (currentLine, featureIndex, multiFeatureIndex, geometryIndex) {
  1016. * //=currentLine
  1017. * //=featureIndex
  1018. * //=multiFeatureIndex
  1019. * //=geometryIndex
  1020. * });
  1021. */
  1022. function lineEach(geojson, callback) {
  1023. // validation
  1024. if (!geojson) throw new Error("geojson is required");
  1025. flattenEach(geojson, function (feature, featureIndex, multiFeatureIndex) {
  1026. if (feature.geometry === null) return;
  1027. var type = feature.geometry.type;
  1028. var coords = feature.geometry.coordinates;
  1029. switch (type) {
  1030. case "LineString":
  1031. if (callback(feature, featureIndex, multiFeatureIndex, 0, 0) === false)
  1032. return false;
  1033. break;
  1034. case "Polygon":
  1035. for (
  1036. var geometryIndex = 0;
  1037. geometryIndex < coords.length;
  1038. geometryIndex++
  1039. ) {
  1040. if (
  1041. callback(
  1042. helpers.lineString(coords[geometryIndex], feature.properties),
  1043. featureIndex,
  1044. multiFeatureIndex,
  1045. geometryIndex
  1046. ) === false
  1047. )
  1048. return false;
  1049. }
  1050. break;
  1051. }
  1052. });
  1053. }
  1054. /**
  1055. * Callback for lineReduce
  1056. *
  1057. * The first time the callback function is called, the values provided as arguments depend
  1058. * on whether the reduce method has an initialValue argument.
  1059. *
  1060. * If an initialValue is provided to the reduce method:
  1061. * - The previousValue argument is initialValue.
  1062. * - The currentValue argument is the value of the first element present in the array.
  1063. *
  1064. * If an initialValue is not provided:
  1065. * - The previousValue argument is the value of the first element present in the array.
  1066. * - The currentValue argument is the value of the second element present in the array.
  1067. *
  1068. * @callback lineReduceCallback
  1069. * @param {*} previousValue The accumulated value previously returned in the last invocation
  1070. * of the callback, or initialValue, if supplied.
  1071. * @param {Feature<LineString>} currentLine The current LineString|LinearRing being processed.
  1072. * @param {number} featureIndex The current index of the Feature being processed
  1073. * @param {number} multiFeatureIndex The current index of the Multi-Feature being processed
  1074. * @param {number} geometryIndex The current index of the Geometry being processed
  1075. */
  1076. /**
  1077. * Reduce features in any GeoJSON object, similar to Array.reduce().
  1078. *
  1079. * @name lineReduce
  1080. * @param {Geometry|Feature<LineString|Polygon|MultiLineString|MultiPolygon>} geojson object
  1081. * @param {Function} callback a method that takes (previousValue, currentLine, featureIndex, multiFeatureIndex, geometryIndex)
  1082. * @param {*} [initialValue] Value to use as the first argument to the first call of the callback.
  1083. * @returns {*} The value that results from the reduction.
  1084. * @example
  1085. * var multiPoly = turf.multiPolygon([
  1086. * turf.polygon([[[12,48],[2,41],[24,38],[12,48]], [[9,44],[13,41],[13,45],[9,44]]]),
  1087. * turf.polygon([[[5, 5], [0, 0], [2, 2], [4, 4], [5, 5]]])
  1088. * ]);
  1089. *
  1090. * turf.lineReduce(multiPoly, function (previousValue, currentLine, featureIndex, multiFeatureIndex, geometryIndex) {
  1091. * //=previousValue
  1092. * //=currentLine
  1093. * //=featureIndex
  1094. * //=multiFeatureIndex
  1095. * //=geometryIndex
  1096. * return currentLine
  1097. * });
  1098. */
  1099. function lineReduce(geojson, callback, initialValue) {
  1100. var previousValue = initialValue;
  1101. lineEach(
  1102. geojson,
  1103. function (currentLine, featureIndex, multiFeatureIndex, geometryIndex) {
  1104. if (featureIndex === 0 && initialValue === undefined)
  1105. previousValue = currentLine;
  1106. else
  1107. previousValue = callback(
  1108. previousValue,
  1109. currentLine,
  1110. featureIndex,
  1111. multiFeatureIndex,
  1112. geometryIndex
  1113. );
  1114. }
  1115. );
  1116. return previousValue;
  1117. }
  1118. /**
  1119. * Finds a particular 2-vertex LineString Segment from a GeoJSON using `@turf/meta` indexes.
  1120. *
  1121. * Negative indexes are permitted.
  1122. * Point & MultiPoint will always return null.
  1123. *
  1124. * @param {FeatureCollection|Feature|Geometry} geojson Any GeoJSON Feature or Geometry
  1125. * @param {Object} [options={}] Optional parameters
  1126. * @param {number} [options.featureIndex=0] Feature Index
  1127. * @param {number} [options.multiFeatureIndex=0] Multi-Feature Index
  1128. * @param {number} [options.geometryIndex=0] Geometry Index
  1129. * @param {number} [options.segmentIndex=0] Segment Index
  1130. * @param {Object} [options.properties={}] Translate Properties to output LineString
  1131. * @param {BBox} [options.bbox={}] Translate BBox to output LineString
  1132. * @param {number|string} [options.id={}] Translate Id to output LineString
  1133. * @returns {Feature<LineString>} 2-vertex GeoJSON Feature LineString
  1134. * @example
  1135. * var multiLine = turf.multiLineString([
  1136. * [[10, 10], [50, 30], [30, 40]],
  1137. * [[-10, -10], [-50, -30], [-30, -40]]
  1138. * ]);
  1139. *
  1140. * // First Segment (defaults are 0)
  1141. * turf.findSegment(multiLine);
  1142. * // => Feature<LineString<[[10, 10], [50, 30]]>>
  1143. *
  1144. * // First Segment of 2nd Multi Feature
  1145. * turf.findSegment(multiLine, {multiFeatureIndex: 1});
  1146. * // => Feature<LineString<[[-10, -10], [-50, -30]]>>
  1147. *
  1148. * // Last Segment of Last Multi Feature
  1149. * turf.findSegment(multiLine, {multiFeatureIndex: -1, segmentIndex: -1});
  1150. * // => Feature<LineString<[[-50, -30], [-30, -40]]>>
  1151. */
  1152. function findSegment(geojson, options) {
  1153. // Optional Parameters
  1154. options = options || {};
  1155. if (!helpers.isObject(options)) throw new Error("options is invalid");
  1156. var featureIndex = options.featureIndex || 0;
  1157. var multiFeatureIndex = options.multiFeatureIndex || 0;
  1158. var geometryIndex = options.geometryIndex || 0;
  1159. var segmentIndex = options.segmentIndex || 0;
  1160. // Find FeatureIndex
  1161. var properties = options.properties;
  1162. var geometry;
  1163. switch (geojson.type) {
  1164. case "FeatureCollection":
  1165. if (featureIndex < 0)
  1166. featureIndex = geojson.features.length + featureIndex;
  1167. properties = properties || geojson.features[featureIndex].properties;
  1168. geometry = geojson.features[featureIndex].geometry;
  1169. break;
  1170. case "Feature":
  1171. properties = properties || geojson.properties;
  1172. geometry = geojson.geometry;
  1173. break;
  1174. case "Point":
  1175. case "MultiPoint":
  1176. return null;
  1177. case "LineString":
  1178. case "Polygon":
  1179. case "MultiLineString":
  1180. case "MultiPolygon":
  1181. geometry = geojson;
  1182. break;
  1183. default:
  1184. throw new Error("geojson is invalid");
  1185. }
  1186. // Find SegmentIndex
  1187. if (geometry === null) return null;
  1188. var coords = geometry.coordinates;
  1189. switch (geometry.type) {
  1190. case "Point":
  1191. case "MultiPoint":
  1192. return null;
  1193. case "LineString":
  1194. if (segmentIndex < 0) segmentIndex = coords.length + segmentIndex - 1;
  1195. return helpers.lineString(
  1196. [coords[segmentIndex], coords[segmentIndex + 1]],
  1197. properties,
  1198. options
  1199. );
  1200. case "Polygon":
  1201. if (geometryIndex < 0) geometryIndex = coords.length + geometryIndex;
  1202. if (segmentIndex < 0)
  1203. segmentIndex = coords[geometryIndex].length + segmentIndex - 1;
  1204. return helpers.lineString(
  1205. [
  1206. coords[geometryIndex][segmentIndex],
  1207. coords[geometryIndex][segmentIndex + 1],
  1208. ],
  1209. properties,
  1210. options
  1211. );
  1212. case "MultiLineString":
  1213. if (multiFeatureIndex < 0)
  1214. multiFeatureIndex = coords.length + multiFeatureIndex;
  1215. if (segmentIndex < 0)
  1216. segmentIndex = coords[multiFeatureIndex].length + segmentIndex - 1;
  1217. return helpers.lineString(
  1218. [
  1219. coords[multiFeatureIndex][segmentIndex],
  1220. coords[multiFeatureIndex][segmentIndex + 1],
  1221. ],
  1222. properties,
  1223. options
  1224. );
  1225. case "MultiPolygon":
  1226. if (multiFeatureIndex < 0)
  1227. multiFeatureIndex = coords.length + multiFeatureIndex;
  1228. if (geometryIndex < 0)
  1229. geometryIndex = coords[multiFeatureIndex].length + geometryIndex;
  1230. if (segmentIndex < 0)
  1231. segmentIndex =
  1232. coords[multiFeatureIndex][geometryIndex].length - segmentIndex - 1;
  1233. return helpers.lineString(
  1234. [
  1235. coords[multiFeatureIndex][geometryIndex][segmentIndex],
  1236. coords[multiFeatureIndex][geometryIndex][segmentIndex + 1],
  1237. ],
  1238. properties,
  1239. options
  1240. );
  1241. }
  1242. throw new Error("geojson is invalid");
  1243. }
  1244. /**
  1245. * Finds a particular Point from a GeoJSON using `@turf/meta` indexes.
  1246. *
  1247. * Negative indexes are permitted.
  1248. *
  1249. * @param {FeatureCollection|Feature|Geometry} geojson Any GeoJSON Feature or Geometry
  1250. * @param {Object} [options={}] Optional parameters
  1251. * @param {number} [options.featureIndex=0] Feature Index
  1252. * @param {number} [options.multiFeatureIndex=0] Multi-Feature Index
  1253. * @param {number} [options.geometryIndex=0] Geometry Index
  1254. * @param {number} [options.coordIndex=0] Coord Index
  1255. * @param {Object} [options.properties={}] Translate Properties to output Point
  1256. * @param {BBox} [options.bbox={}] Translate BBox to output Point
  1257. * @param {number|string} [options.id={}] Translate Id to output Point
  1258. * @returns {Feature<Point>} 2-vertex GeoJSON Feature Point
  1259. * @example
  1260. * var multiLine = turf.multiLineString([
  1261. * [[10, 10], [50, 30], [30, 40]],
  1262. * [[-10, -10], [-50, -30], [-30, -40]]
  1263. * ]);
  1264. *
  1265. * // First Segment (defaults are 0)
  1266. * turf.findPoint(multiLine);
  1267. * // => Feature<Point<[10, 10]>>
  1268. *
  1269. * // First Segment of the 2nd Multi-Feature
  1270. * turf.findPoint(multiLine, {multiFeatureIndex: 1});
  1271. * // => Feature<Point<[-10, -10]>>
  1272. *
  1273. * // Last Segment of last Multi-Feature
  1274. * turf.findPoint(multiLine, {multiFeatureIndex: -1, coordIndex: -1});
  1275. * // => Feature<Point<[-30, -40]>>
  1276. */
  1277. function findPoint(geojson, options) {
  1278. // Optional Parameters
  1279. options = options || {};
  1280. if (!helpers.isObject(options)) throw new Error("options is invalid");
  1281. var featureIndex = options.featureIndex || 0;
  1282. var multiFeatureIndex = options.multiFeatureIndex || 0;
  1283. var geometryIndex = options.geometryIndex || 0;
  1284. var coordIndex = options.coordIndex || 0;
  1285. // Find FeatureIndex
  1286. var properties = options.properties;
  1287. var geometry;
  1288. switch (geojson.type) {
  1289. case "FeatureCollection":
  1290. if (featureIndex < 0)
  1291. featureIndex = geojson.features.length + featureIndex;
  1292. properties = properties || geojson.features[featureIndex].properties;
  1293. geometry = geojson.features[featureIndex].geometry;
  1294. break;
  1295. case "Feature":
  1296. properties = properties || geojson.properties;
  1297. geometry = geojson.geometry;
  1298. break;
  1299. case "Point":
  1300. case "MultiPoint":
  1301. return null;
  1302. case "LineString":
  1303. case "Polygon":
  1304. case "MultiLineString":
  1305. case "MultiPolygon":
  1306. geometry = geojson;
  1307. break;
  1308. default:
  1309. throw new Error("geojson is invalid");
  1310. }
  1311. // Find Coord Index
  1312. if (geometry === null) return null;
  1313. var coords = geometry.coordinates;
  1314. switch (geometry.type) {
  1315. case "Point":
  1316. return helpers.point(coords, properties, options);
  1317. case "MultiPoint":
  1318. if (multiFeatureIndex < 0)
  1319. multiFeatureIndex = coords.length + multiFeatureIndex;
  1320. return helpers.point(coords[multiFeatureIndex], properties, options);
  1321. case "LineString":
  1322. if (coordIndex < 0) coordIndex = coords.length + coordIndex;
  1323. return helpers.point(coords[coordIndex], properties, options);
  1324. case "Polygon":
  1325. if (geometryIndex < 0) geometryIndex = coords.length + geometryIndex;
  1326. if (coordIndex < 0)
  1327. coordIndex = coords[geometryIndex].length + coordIndex;
  1328. return helpers.point(coords[geometryIndex][coordIndex], properties, options);
  1329. case "MultiLineString":
  1330. if (multiFeatureIndex < 0)
  1331. multiFeatureIndex = coords.length + multiFeatureIndex;
  1332. if (coordIndex < 0)
  1333. coordIndex = coords[multiFeatureIndex].length + coordIndex;
  1334. return helpers.point(coords[multiFeatureIndex][coordIndex], properties, options);
  1335. case "MultiPolygon":
  1336. if (multiFeatureIndex < 0)
  1337. multiFeatureIndex = coords.length + multiFeatureIndex;
  1338. if (geometryIndex < 0)
  1339. geometryIndex = coords[multiFeatureIndex].length + geometryIndex;
  1340. if (coordIndex < 0)
  1341. coordIndex =
  1342. coords[multiFeatureIndex][geometryIndex].length - coordIndex;
  1343. return helpers.point(
  1344. coords[multiFeatureIndex][geometryIndex][coordIndex],
  1345. properties,
  1346. options
  1347. );
  1348. }
  1349. throw new Error("geojson is invalid");
  1350. }
  1351. exports.coordAll = coordAll;
  1352. exports.coordEach = coordEach;
  1353. exports.coordReduce = coordReduce;
  1354. exports.featureEach = featureEach;
  1355. exports.featureReduce = featureReduce;
  1356. exports.findPoint = findPoint;
  1357. exports.findSegment = findSegment;
  1358. exports.flattenEach = flattenEach;
  1359. exports.flattenReduce = flattenReduce;
  1360. exports.geomEach = geomEach;
  1361. exports.geomReduce = geomReduce;
  1362. exports.lineEach = lineEach;
  1363. exports.lineReduce = lineReduce;
  1364. exports.propEach = propEach;
  1365. exports.propReduce = propReduce;
  1366. exports.segmentEach = segmentEach;
  1367. exports.segmentReduce = segmentReduce;