Skip to content

Instantly share code, notes, and snippets.

@nickoala
Last active March 11, 2023 18:47
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nickoala/14dcdea282534b862b5a438fb8a991ee to your computer and use it in GitHub Desktop.
Save nickoala/14dcdea282534b862b5a438fb8a991ee to your computer and use it in GitHub Desktop.
Log GPSD Outputs and Extract Lat/Lon

Log GPSD Outputs and Extract Lat/Lon

$ gpspipe -w             # log to stdout
$ gpspipe -w -o abc.log  # log to a file

To auto-log on startup, insert the following line to the file /etc/rc.local:

gpspipe -wdl -o /PATH/TO/DIRECTORY/`shuf -i 100000000-999999999 -n 1`.log

According to the manpage, the option -d causes it to run as a daemon, -l to sleep for ten seconds before attempting to connect to gpsd (useful when running as a daemon, giving gpsd time to start before attempting a connection).

shuf -i 100000000-999999999 -n 1 gives a random filename, so log files would not be accidentally overwritten. Using date/time as filename in this case is not ideal, because system time may not have been synced during such an early stage.

How to extract lat/lon from the log files?

We are after lines such as this:

{"class":"TPV","device":"/dev/ttyACM0","mode":2,"time":"2018-06-26T06:20:02.009Z","ept":0.003,"lat":10.23456789,"lon":100.23456789,"track":200.3456,"speed":0.235,"eps":11.22}

TPV is the keyword. But there are also lines such as this, without lat/lon, which we want to avoid:

{"class":"TPV","device":"/dev/ttyACM0","mode":1}

To get the first line with lat/lon:

$ grep TPV xxxxx.log | grep -m 1 lat

To get the last line with lat/lon:

$ grep TPV xxxxx.log | grep lat | tail -n 1

To count the number of lat/lon available:

$ grep TPV xxxxx.log | grep lat | wc -l

To extract all lat/lon, along with their times (you may have to install the command-line JSON parser beforehand: sudo apt-get install jq):

$ grep TPV xxxxx.log | grep lat | jq -r '"\(.lat), \(.lon), \(.time)"'

If GPS signal is very good, you may notice two lat/lon per second. In many cases, you may not even need one per second. For example, you can keep one for every two lines, or three lines:

$ grep TPV xxxxx.log | grep lat | sed -n '1~2p' | jq -r '"\(.lat), \(.lon), \(.time)"'
$ grep TPV xxxxx.log | grep lat | sed -n '1~3p' | jq -r '"\(.lat), \(.lon), \(.time)"'

Use sed -n '/pattern/,/pattern/p' to select a time range. For example, to extract only those between 14:30:00 and 15:00:00:

$ grep TPV xxxxx.log \
    | grep lat \
    | sed -n '/14:30:00/,/15:00:00/p' \
    | sed -n '1~2p' \
    | jq -r '"\(.lat), \(.lon), \(.time)"'

After you are happy with the extractions, you can plot them using the following websites:

https://www.darrinward.com/lat-long/

http://www.hamstermap.com

https://multiplottr.com

How big are the logs?

Assuming good GPS signals, one hour can produce 5 MB of logs. That is, 200 hours can produce 1 GB. Clean them periodically, or consider using logrotate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment