~mil/mepo

898c4e46569337fe45459266ffb60f324934b903 — Miles Alan 3 months ago e44f8f7
mepo_ui_menu_route_graphhopper.sh: Rewrite to use jq instead of mix of jq+sh

Script logic merging instructions/coordinates is alot more direct and readable
using just jq rather then shelling out for loop to merge in shellscript.
1 files changed, 14 insertions(+), 43 deletions(-)

M scripts/mepo_ui_menu_route_graphhopper.sh
M scripts/mepo_ui_menu_route_graphhopper.sh => scripts/mepo_ui_menu_route_graphhopper.sh +14 -43
@@ 73,55 73,26 @@ curlgraphhopper() {
printpins() {
  RESPONSE="$1"

  COORDS="$(
    echo "$RESPONSE" | 
      jq '.paths[0].points.coordinates | .[] | join(" ")' | 
      tr -d '"' |
      awk '{ print $2, $1 }'
  )"

  INSTRUCTIONS="$(
    echo "$RESPONSE" | 
      jq '
        .paths[0].instructions | 
        .[] | 
        "\(.interval[0]) \(.interval[1]) \(.text) (for \(.time)ms and \(.distance) meters)"
      ' |
      tr -d '"'
  )"

  echo "
    pin_groupprefset 1 ordered 1;
    pin_groupprefset 1 active 1;
    pin_purge;
  "


  echo "$INSTRUCTIONS" | while read -r LINE; do
    INSTRUCTIONSTART="$(echo "$LINE" | cut -d " " -f1 | xargs -ISTART echo START + 1 |  bc)"
    STRUCTURALSTART="$(echo "$LINE" | cut -d " " -f1 | xargs -IEND echo END + 1 |  bc)"
    STRUCTURALEND="$(echo "$LINE" | cut -d " " -f2)"

    # Instructive pin
    echo $(
      printf "pin_add 0 "
      echo "$COORDS" | sed -n ${INSTRUCTIONSTART}p;
      printf "[] [";
      echo "$LINE" | cut -d ' ' -f3-;
      printf "];"
    )

    # Structural pins
    I="$STRUCTURALSTART"
    while [ "$I" -lt "$STRUCTURALEND" ]; do
      echo $(
        printf "pin_add 1 "
        echo "$COORDS" | sed -n ${I}p;
        printf "[] [];"
      )
      I="$(echo $I + 1 | bc)"
    done
  done
  echo "$RESPONSE" |
    jq '
      . as $root | 
      [.paths[0].instructions | .[] | {"\(.interval[0])" : . }] | add as $instructions | 
      $root.paths[0].points.coordinates |
      to_entries |
      map({coords:.value, instruction:$instructions["\(.key)"]}) | 
      .[] | 
      if(.instruction != null) then 
        "pin_add 0 \(.coords[1]) \(.coords[0]) [] [\(.instruction.text)];"
        else
        "pin_add 1 \(.coords[1]) \(.coords[0]) [] [];"
      end
    ' | tr -d '"'
}

main() {