UNPKG

@turf/line-slice

Version:

Useful for extracting only the part of a route between waypoints.

1 lines 4.81 kB
{"version":3,"sources":["/home/runner/work/turf/turf/packages/turf-line-slice/dist/cjs/index.cjs","../../index.ts"],"names":[],"mappings":"AAAA;ACAA,4CAAmC;AACnC,wCAAgD;AAChD,iEAAmC;AAgCnC,SAAS,SAAA,CACP,OAAA,EACA,MAAA,EACA,IAAA,EACqB;AAErB,EAAA,MAAM,OAAA,EAAS,kCAAA,IAAc,CAAA;AAC7B,EAAA,GAAA,CAAI,gCAAA,IAAY,EAAA,IAAM,YAAA;AACpB,IAAA,MAAM,IAAI,KAAA,CAAM,2BAA2B,CAAA;AAE7C,EAAA,MAAM,YAAA,EAAc,oDAAA,IAAmB,EAAM,OAAO,CAAA;AACpD,EAAA,MAAM,WAAA,EAAa,oDAAA,IAAmB,EAAM,MAAM,CAAA;AAIlD,EAAA,qBAAA,CAAsB,IAAA,EAAM,WAAW,CAAA;AACvC,EAAA,qBAAA,CAAsB,IAAA,EAAM,UAAU,CAAA;AAEtC,EAAA,MAAM,KAAA,EACJ,WAAA,CAAY,UAAA,CAAW,aAAA,GAAgB,UAAA,CAAW,UAAA,CAAW,aAAA,EACzD,CAAC,WAAA,EAAa,UAAU,EAAA,EACxB,CAAC,UAAA,EAAY,WAAW,CAAA;AAC9B,EAAA,MAAM,WAAA,EAAa,CAAC,IAAA,CAAK,CAAC,CAAA,CAAE,QAAA,CAAS,WAAW,CAAA;AAChD,EAAA,IAAA,CAAA,IACM,EAAA,EAAI,IAAA,CAAK,CAAC,CAAA,CAAE,UAAA,CAAW,aAAA,EAAe,CAAA,EAC1C,EAAA,EAAI,IAAA,CAAK,CAAC,CAAA,CAAE,UAAA,CAAW,aAAA,EAAe,CAAA,EACtC,CAAA,EAAA,EACA;AACA,IAAA,UAAA,CAAW,IAAA,CAAK,MAAA,CAAO,CAAC,CAAC,CAAA;AAAA,EAC3B;AACA,EAAA,UAAA,CAAW,IAAA,CAAK,IAAA,CAAK,CAAC,CAAA,CAAE,QAAA,CAAS,WAAW,CAAA;AAC5C,EAAA,OAAO,iCAAA,UAAW,EAAY,IAAA,CAAK,KAAA,IAAS,UAAA,EAAY,IAAA,CAAK,WAAA,EAAa,CAAC,CAAC,CAAA;AAC9E;AAEA,SAAS,qBAAA,CACP,IAAA,EACA,MAAA,EACA;AAMA,EAAA,IAAI,SAAA,EAAuB,IAAA,CAAK,KAAA,IAAS,UAAA,EAAY,IAAA,CAAK,SAAA,EAAW,IAAA;AAErE,EAAA,GAAA,CAAI,MAAA,CAAO,UAAA,CAAW,aAAA,GAAgB,QAAA,CAAS,WAAA,CAAY,OAAA,EAAS,CAAA,EAAG;AAGrE,IAAA,MAAA,CAAO,UAAA,CAAW,aAAA,EAAe,QAAA,CAAS,WAAA,CAAY,OAAA,EAAS,CAAA;AAAA,EACjE;AACF;AAGA,IAAO,cAAA,EAAQ,SAAA;AD5Df;AACE;AACA;AACF,+DAAC","file":"/home/runner/work/turf/turf/packages/turf-line-slice/dist/cjs/index.cjs","sourcesContent":[null,"import { getCoords, getType } from \"@turf/invariant\";\nimport { Coord, lineString as linestring } from \"@turf/helpers\";\nimport { nearestPointOnLine } from \"@turf/nearest-point-on-line\";\nimport type { Feature, LineString, Point } from \"geojson\";\n\n/**\n * Takes a {@link LineString|line}, a start {@link Point}, and a stop point\n * and returns a subsection of the line in-between those points.\n * The start & stop points don't need to fall exactly on the line.\n *\n * This can be useful for extracting only the part of a route between waypoints.\n *\n * @function\n * @param {Coord} startPt starting point\n * @param {Coord} stopPt stopping point\n * @param {Feature<LineString>|LineString} line line to slice\n * @returns {Feature<LineString>} sliced line\n * @example\n * var line = turf.lineString([\n * [-77.031669, 38.878605],\n * [-77.029609, 38.881946],\n * [-77.020339, 38.884084],\n * [-77.025661, 38.885821],\n * [-77.021884, 38.889563],\n * [-77.019824, 38.892368]\n * ]);\n * var start = turf.point([-77.029609, 38.881946]);\n * var stop = turf.point([-77.021884, 38.889563]);\n *\n * var sliced = turf.lineSlice(start, stop, line);\n *\n * //addToMap\n * var addToMap = [start, stop, line]\n */\nfunction lineSlice(\n startPt: Coord,\n stopPt: Coord,\n line: Feature<LineString> | LineString\n): Feature<LineString> {\n // Validation\n const coords = getCoords(line);\n if (getType(line) !== \"LineString\")\n throw new Error(\"line must be a LineString\");\n\n const startVertex = nearestPointOnLine(line, startPt);\n const stopVertex = nearestPointOnLine(line, stopPt);\n\n // Workaround until we can fix backwards incompatible nearestPointOnLine bug\n // #3016\n fixSegmentIndexBounds(line, startVertex);\n fixSegmentIndexBounds(line, stopVertex);\n\n const ends =\n startVertex.properties.segmentIndex <= stopVertex.properties.segmentIndex\n ? [startVertex, stopVertex]\n : [stopVertex, startVertex];\n const clipCoords = [ends[0].geometry.coordinates];\n for (\n let i = ends[0].properties.segmentIndex + 1;\n i < ends[1].properties.segmentIndex + 1;\n i++\n ) {\n clipCoords.push(coords[i]);\n }\n clipCoords.push(ends[1].geometry.coordinates);\n return linestring(clipCoords, line.type === \"Feature\" ? line.properties : {});\n}\n\nfunction fixSegmentIndexBounds(\n line: Feature<LineString> | LineString,\n vertex: Feature<Point, { segmentIndex: number }>\n) {\n // There is a bug in nearestPointOnLine where the returned segmentIndex can\n // refer to a non-existent segment (overflows). Until we can fix that bug\n // (see https://github.com/Turfjs/turf/issues/3016) we adjust the\n // segmentIndex here to make sure it's in range.\n\n let geometry: LineString = line.type === \"Feature\" ? line.geometry : line;\n\n if (vertex.properties.segmentIndex >= geometry.coordinates.length - 1) {\n // segmentIndex refers to a non-existent segment beyond the end of the\n // lineString. Override it.\n vertex.properties.segmentIndex = geometry.coordinates.length - 2;\n }\n}\n\nexport { lineSlice };\nexport default lineSlice;\n"]}