~mil/mepo

ref: e44f8f716f5a6fbfa9047e0813f13670d9b58bb0 mepo/scripts/mepo_ui_menu_route_graphhopper.sh -rwxr-xr-x 3.6 KiB
e44f8f71Miles Alan Properly highlight entire segment for active pin in ordered pin groups 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env sh
DISPLAYNAME="Route: via GraphHopper"

errexit() {
  echo "$1" >&2
  exit 1
}

nominatimquerytargetmenu() {

  COORDS=""

  while ! echo "$COORDS" | grep -Eq "[-0-9.]+[ ]+[-0-9.]+"; do
    QUERY="$(
        echo -e "centerpoint\ncursor\nOr enter nominatim query location" |
        PROMPT="$1" mepo_ui_helper_menu.sh |
        sed 's/ /%20/g'
    )"
    [ "$QUERY" = "centerpoint" ] && COORDS="$MEPO_CENTER_LAT $MEPO_CENTER_LON" && break
    [ "$QUERY" = "cursor" ] && COORDS="$MEPO_CURSOR_LAT $MEPO_CURSOR_LON" && break


    VIEWBOX="$MEPO_TL_LON,$MEPO_TL_LAT,$MEPO_BR_LON,$MEPO_BR_LAT"
    RESULT="$(
      curl "https://nominatim.openstreetmap.org/search?format=json&q=$QUERY&viewbox=$VIEWBOX&bounded=1&limit=50" |
        jq '.[] | "\(.display_name) \(.lat) \(.lon)" '
    )"

    if ! echo "$RESULT" | awk NF | grep -Eq .; then
      echo "No results - press any key!" | PROMPT="No results!" mepo_ui_helper_menu.sh  > /dev/null
      continue
    fi

    COORDS="$(
      echo "$RESULT" | 
      PROMPT="Pick result" mepo_ui_helper_menu.sh |
      grep -oE '[-0-9.]+' |
      tail -n2 |
      tr "\n" " "
    )"
  done

  echo "$COORDS"
}

curlgraphhopper() {
  MODE="$1"
  FROM="$2"
  DEST="$3"

  FROMLAT="$(echo $FROM | cut -d " " -f1)"
  FROMLON="$(echo $FROM | cut -d " " -f2)"
  DESTLAT="$(echo $DEST | cut -d " " -f1)"
  DESTLON="$(echo $DEST | cut -d " " -f2)"

  curl \
    -X POST \
    -H "Content-Type: application/json" \
    "https://graphhopper.com/api/1/route?key=$MEPO_GRAPHHOPPER_API_KEY" \
    -d '
      {
        "elevation": false,
        "points_encoded": false,
        "points": [
          ['$FROMLON','$FROMLAT'],
          ['$DESTLON','$DESTLAT']
        ],
        "vehicle": "'$MODE'"
      }
    '
}

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
}

main() {
  MEPO_GRAPHHOPPER_DEFAULT_API_KEY="49343b56-0779-47d1-8ca3-0ea706ddc9c7"
  [ -z "$MEPO_GRAPHHOPPER_API_KEY" ] && MEPO_GRAPHHOPPER_API_KEY="$MEPO_GRAPHHOPPER_DEFAULT_API_KEY"

  MODE="$(
    echo -e "car\nbike\nfoot" |
    PROMPT="Route mode" mepo_ui_helper_menu.sh
  )"
  echo "$MODE" | grep -qE "^(car|bike|foot)" || errexit "Unsupported route mode"

  LOCFROM="$(nominatimquerytargetmenu "Route from")"
  [ -z "$LOCFROM" ] && errexit "Failed to determine coordinates to route to"

  LOCTO="$(nominatimquerytargetmenu "Route to")"
  [ -z "$LOCTO" ] && errexit "Failed to determine coordinates to route to"

  printpins "$(curlgraphhopper "$MODE" "$LOCFROM" "$LOCTO")"
}

if [ -n "$1" ]; then "$@"; else main; fi